Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix phase tests and use ensure container utilities #1250

Merged
merged 7 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apis/kafka/v1alpha1/connect_cluster_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (k *ConnectCluster) PodLabels(extraLabels ...map[string]string) map[string]
return k.offshootLabels(meta_util.OverwriteKeys(k.OffshootSelectors(), extraLabels...), k.Spec.PodTemplate.Labels)
}

func (k *ConnectCluster) StatefulSetName() string {
func (k *ConnectCluster) PetSetName() string {
return k.OffshootName()
}

Expand Down
2 changes: 1 addition & 1 deletion apis/kafka/v1alpha1/schemaregistry_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (k *SchemaRegistry) PodLabels(extraLabels ...map[string]string) map[string]
return k.offshootLabels(meta_util.OverwriteKeys(k.OffshootSelectors(), extraLabels...), k.Spec.PodTemplate.Labels)
}

func (k *SchemaRegistry) StatefulSetName() string {
func (k *SchemaRegistry) PetSetName() string {
return k.OffshootName()
}

Expand Down
37 changes: 0 additions & 37 deletions apis/kubedb/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
core_util "kmodules.xyz/client-go/core/v1"
ofstv1 "kmodules.xyz/offshoot-api/api/v1"
ofstv2 "kmodules.xyz/offshoot-api/api/v2"
petsetutil "kubeops.dev/petset/client/clientset/versioned/typed/apps/v1"
pslister "kubeops.dev/petset/client/listers/apps/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -124,41 +122,6 @@ func GetDatabasePodsByPetSetLister(db metav1.Object, psLister pslister.PetSetLis
return dbPods, nil
}

// EnsureContainerExists ensures that given container either exits by default else
// it creates the container and insert it to the podtemplate
func EnsureContainerExists(podTemplate *ofstv2.PodTemplateSpec, containerName string) *core.Container {
container := core_util.GetContainerByName(podTemplate.Spec.Containers, containerName)
if container == nil {
container = &core.Container{
Name: containerName,
}
}
podTemplate.Spec.Containers = core_util.UpsertContainer(podTemplate.Spec.Containers, *container)
for i := range podTemplate.Spec.Containers {
if podTemplate.Spec.Containers[i].Name == containerName {
container = &podTemplate.Spec.Containers[i]
}
}
return container
}

// it creates the container and insert it to the podtemplate
func EnsureInitContainerExists(podTemplate *ofstv2.PodTemplateSpec, containerName string) *core.Container {
container := core_util.GetContainerByName(podTemplate.Spec.InitContainers, containerName)
if container == nil {
container = &core.Container{
Name: containerName,
}
}
podTemplate.Spec.InitContainers = core_util.UpsertContainer(podTemplate.Spec.InitContainers, *container)
for i := range podTemplate.Spec.InitContainers {
if podTemplate.Spec.InitContainers[i].Name == containerName {
container = &podTemplate.Spec.InitContainers[i]
}
}
return container
}

// Upsert elements to string slice
func upsertStringSlice(inSlice []string, values ...string) []string {
upsert := func(m string) {
Expand Down
7 changes: 4 additions & 3 deletions apis/kubedb/v1/mongodb_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ofstv2 "kmodules.xyz/offshoot-api/api/v2"
ofst_util "kmodules.xyz/offshoot-api/util"
pslister "kubeops.dev/petset/client/listers/apps/v1"
)

Expand Down Expand Up @@ -712,14 +713,14 @@ func (m *MongoDB) setPodTemplateDefaultValues(podTemplate *ofstv2.PodTemplateSpe
defaultResource = kubedb.DefaultResourcesCPUIntensive
}

container := EnsureInitContainerExists(podTemplate, kubedb.MongoDBInitInstallContainerName)
container := ofst_util.EnsureInitContainerExists(podTemplate, kubedb.MongoDBInitInstallContainerName)
m.setContainerDefaultValues(container, mgVersion, defaultResource, isArbiter...)

container = EnsureContainerExists(podTemplate, kubedb.MongoDBContainerName)
container = ofst_util.EnsureContainerExists(podTemplate, kubedb.MongoDBContainerName)
m.setContainerDefaultValues(container, mgVersion, defaultResource, isArbiter...)

if moodDetectorNeeded {
container = EnsureContainerExists(podTemplate, kubedb.ReplicationModeDetectorContainerName)
container = ofst_util.EnsureContainerExists(podTemplate, kubedb.ReplicationModeDetectorContainerName)
m.setContainerDefaultValues(container, mgVersion, defaultResource, isArbiter...)
}
}
Expand Down
3 changes: 2 additions & 1 deletion apis/kubedb/v1/pgbouncer_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ofstv2 "kmodules.xyz/offshoot-api/api/v2"
ofst_util "kmodules.xyz/offshoot-api/util"
pslister "kubeops.dev/petset/client/listers/apps/v1"
)

Expand Down Expand Up @@ -237,7 +238,7 @@ func (p *PgBouncer) setPgBouncerContainerDefaults(podTemplate *ofstv2.PodTemplat
if podTemplate == nil {
return
}
container := EnsureContainerExists(podTemplate, kubedb.PgBouncerContainerName)
container := ofst_util.EnsureContainerExists(podTemplate, kubedb.PgBouncerContainerName)
p.setContainerDefaultResources(container, *kubedb.DefaultResources.DeepCopy())
}

Expand Down
7 changes: 4 additions & 3 deletions apis/kubedb/v1/postgres_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
appcat "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
mona "kmodules.xyz/monitoring-agent-api/api/v1"
ofstv2 "kmodules.xyz/offshoot-api/api/v2"
ofst_util "kmodules.xyz/offshoot-api/util"
pslister "kubeops.dev/petset/client/listers/apps/v1"
)

Expand Down Expand Up @@ -332,7 +333,7 @@ func (p *Postgres) setInitContainerDefaults(podTemplate *ofstv2.PodTemplateSpec,
if podTemplate == nil {
return
}
container := EnsureInitContainerExists(podTemplate, kubedb.PostgresInitContainerName)
container := ofst_util.EnsureInitContainerExists(podTemplate, kubedb.PostgresInitContainerName)
p.setContainerDefaultSecurityContext(container, pgVersion)
p.setContainerDefaultResources(container, *kubedb.DefaultInitContainerResource.DeepCopy())
}
Expand All @@ -341,7 +342,7 @@ func (p *Postgres) setPostgresContainerDefaults(podTemplate *ofstv2.PodTemplateS
if podTemplate == nil {
return
}
container := EnsureContainerExists(podTemplate, kubedb.PostgresContainerName)
container := ofst_util.EnsureContainerExists(podTemplate, kubedb.PostgresContainerName)
p.setContainerDefaultSecurityContext(container, pgVersion)
p.setContainerDefaultResources(container, *kubedb.DefaultResources.DeepCopy())
}
Expand All @@ -350,7 +351,7 @@ func (p *Postgres) setCoordinatorContainerDefaults(podTemplate *ofstv2.PodTempla
if podTemplate == nil {
return
}
container := EnsureContainerExists(podTemplate, kubedb.PostgresCoordinatorContainerName)
container := ofst_util.EnsureContainerExists(podTemplate, kubedb.PostgresCoordinatorContainerName)
p.setContainerDefaultSecurityContext(container, pgVersion)
p.setContainerDefaultResources(container, *kubedb.CoordinatorDefaultResources.DeepCopy())
}
Expand Down
15 changes: 15 additions & 0 deletions apis/kubedb/v1alpha2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,21 @@ func Convert_v1_PostgresSpec_To_v1alpha2_PostgresSpec(in *v1.PostgresSpec, out *
return nil
}

func Convert_v1alpha2_MySQLRouterSpec_To_v1_MySQLRouterSpec(in *MySQLRouterSpec, out *v1.MySQLRouterSpec, s conversion.Scope) error {
out.Replicas = (*int32)(unsafe.Pointer(in.Replicas))
if in.PodTemplate != nil {
in, out := &in.PodTemplate, &out.PodTemplate
*out = new(ofstv2.PodTemplateSpec)
if err := Convert_v1_PodTemplateSpec_To_v2_PodTemplateSpec(*in, *out, s); err != nil {
return err
}
(*out).Spec.Containers[0].Name = kubedb.MySQLRouterContainerName
} else {
out.PodTemplate = nil
}
return nil
}

func Convert_v1alpha2_MySQLSpec_To_v1_MySQLSpec(in *MySQLSpec, out *v1.MySQLSpec, s conversion.Scope) error {
if err := Convert_v1alpha2_AutoOpsSpec_To_v1_AutoOpsSpec(&in.AutoOps, &out.AutoOps, s); err != nil {
return err
Expand Down
15 changes: 5 additions & 10 deletions apis/kubedb/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ require (
kmodules.xyz/custom-resources v0.30.0
kmodules.xyz/monitoring-agent-api v0.29.0
kmodules.xyz/objectstore-api v0.29.1
kmodules.xyz/offshoot-api v0.29.4
kmodules.xyz/offshoot-api v0.29.5-0.20240705043904-bb118b293ce9
Neaj-Morshad-101 marked this conversation as resolved.
Show resolved Hide resolved
kmodules.xyz/resource-metadata v0.18.10
kmodules.xyz/webhook-runtime v0.29.1
kubeops.dev/petset v0.0.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/objectstore-api v0.29.1 h1:uUsjf8KU0w4LYowSEOnl0AbHT3hsHIu1wNLHqGe1o6s=
kmodules.xyz/objectstore-api v0.29.1/go.mod h1:xG+5awH1SXYKxwN/+k1FEQvzixd5tgNqEN/1LEiB2FE=
kmodules.xyz/offshoot-api v0.29.4 h1:WQV2BIUIoVKKiqZNmZ4gAy367jEdwBhEl3dFCLZM1qA=
kmodules.xyz/offshoot-api v0.29.4/go.mod h1:e+NQ0s4gW/YTPWBWEfdISZcmk+tlTq8IjvP5SLdqvko=
kmodules.xyz/offshoot-api v0.29.5-0.20240705043904-bb118b293ce9 h1:untyw2D2PwFO6HpTCE2SU3ji48ZbU/u/I8ApTDRxumM=
kmodules.xyz/offshoot-api v0.29.5-0.20240705043904-bb118b293ce9/go.mod h1:e+NQ0s4gW/YTPWBWEfdISZcmk+tlTq8IjvP5SLdqvko=
kmodules.xyz/prober v0.29.0 h1:Ex7m4F9rH7uWNNJlLgP63ROOM+nUATJkC2L5OQ7nwMg=
kmodules.xyz/prober v0.29.0/go.mod h1:UtK+HKyI1lFLEKX+HFLyOCVju6TO93zv3kwGpzqmKOo=
kmodules.xyz/resource-metadata v0.18.10 h1:B0DAMsJJBuSOGCKzptZvB8AWcbxzDdUm+3CIE7g9ftE=
Expand Down
25 changes: 13 additions & 12 deletions pkg/phase/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
esapi "kubedb.dev/apimachinery/apis/elasticsearch/v1alpha1"
"kubedb.dev/apimachinery/apis/kubedb"
dbapi "kubedb.dev/apimachinery/apis/kubedb/v1"
olddbapi "kubedb.dev/apimachinery/apis/kubedb/v1alpha2"

kmapi "kmodules.xyz/client-go/api/v1"
cutil "kmodules.xyz/client-go/conditions"
Expand Down Expand Up @@ -55,7 +56,7 @@ func DashboardPhaseFromCondition(conditions []kmapi.Condition) esapi.DashboardPh
return esapi.DashboardPhaseNotReady
}

func PhaseFromCondition(conditions []kmapi.Condition) dbapi.DatabasePhase {
func PhaseFromCondition(conditions []kmapi.Condition) olddbapi.DatabasePhase {
// Generally, the conditions should maintain the following chronological order
// For normal restore process:
// ProvisioningStarted --> ReplicaReady --> AcceptingConnection --> DataRestoreStarted --> DataRestored --> Ready --> Provisioned
Expand All @@ -71,26 +72,26 @@ func PhaseFromCondition(conditions []kmapi.Condition) dbapi.DatabasePhase {
// 6. Paused
// 7. HealthCheckPaused

var phase dbapi.DatabasePhase
var phase olddbapi.DatabasePhase

// ================================= Handling "HealthCheckPaused" condition ==========================
// If the condition is present and its "true", then the phase should be "Unknown".
// Skip if the database isn't provisioned yet.
if cutil.IsConditionTrue(conditions, kubedb.DatabaseHealthCheckPaused) {
return dbapi.DatabasePhaseUnknown
return olddbapi.DatabasePhaseUnknown
}

// ================================== Handling "ProvisioningStarted" condition ========================
// If the condition is present and its "true", then the phase should be "Provisioning".
if cutil.IsConditionTrue(conditions, kubedb.DatabaseProvisioningStarted) {
phase = dbapi.DatabasePhaseProvisioning
phase = olddbapi.DatabasePhaseProvisioning
}

// ================================== Handling "Halted" condition =======================================
// The "Halted" condition has higher priority, that's why it is placed at the top.
// If the condition is present and its "true", then the phase should be "Halted".
if cutil.IsConditionTrue(conditions, kubedb.DatabaseHalted) {
return dbapi.DatabasePhaseHalted
return olddbapi.DatabasePhaseHalted
}

// =================================== Handling "DataRestoreStarted" and "DataRestored" conditions ==================================================
Expand All @@ -106,38 +107,38 @@ func PhaseFromCondition(conditions []kmapi.Condition) dbapi.DatabasePhase {
// - Just return "Restoring" in future.
if cutil.HasCondition(conditions, kubedb.DatabaseDataRestored) {
if cutil.IsConditionFalse(conditions, kubedb.DatabaseDataRestored) {
return dbapi.DatabasePhaseNotReady
return olddbapi.DatabasePhaseNotReady
}
} else {
return dbapi.DatabasePhaseDataRestoring
return olddbapi.DatabasePhaseDataRestoring
}
}
if cutil.IsConditionFalse(conditions, kubedb.DatabaseDataRestored) {
return dbapi.DatabasePhaseNotReady
return olddbapi.DatabasePhaseNotReady
}

// ================================= Handling "AcceptingConnection" condition ==========================
// If the condition is present and its "false", then the phase should be "NotReady".
// Skip if the database isn't provisioned yet.
if cutil.IsConditionFalse(conditions, kubedb.DatabaseAcceptingConnection) && cutil.IsConditionTrue(conditions, kubedb.DatabaseProvisioned) {
return dbapi.DatabasePhaseNotReady
return olddbapi.DatabasePhaseNotReady
}

// ================================= Handling "ReplicaReady" condition ==========================
// If the condition is present and its "false", then the phase should be "Critical".
// Skip if the database isn't provisioned yet.
if cutil.IsConditionFalse(conditions, kubedb.DatabaseReplicaReady) && cutil.IsConditionTrue(conditions, kubedb.DatabaseProvisioned) {
return dbapi.DatabasePhaseCritical
return olddbapi.DatabasePhaseCritical
}

// ================================= Handling "Ready" condition ==========================
// Skip if the database isn't provisioned yet.
if cutil.IsConditionFalse(conditions, kubedb.DatabaseReady) && cutil.IsConditionTrue(conditions, kubedb.DatabaseProvisioned) {
return dbapi.DatabasePhaseCritical
return olddbapi.DatabasePhaseCritical
}
// Ready, if the database is provisioned and readinessProbe passed.
if cutil.IsConditionTrue(conditions, kubedb.DatabaseReady) && cutil.IsConditionTrue(conditions, kubedb.DatabaseProvisioned) {
return dbapi.DatabasePhaseReady
return olddbapi.DatabasePhaseReady
}

// ================================= Handling "Provisioned" and "Paused" conditions ==========================
Expand Down
34 changes: 34 additions & 0 deletions vendor/kmodules.xyz/offshoot-api/util/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package util

import (
core_util "kmodules.xyz/client-go/core/v1"
ofstv2 "kmodules.xyz/offshoot-api/api/v2"

core "k8s.io/api/core/v1"
)

// EnsureContainerExists ensures that given container either exits by default or
// it will create the container, then insert it to the podTemplate and return a pointer of that container
func EnsureContainerExists(podTemplate *ofstv2.PodTemplateSpec, containerName string) *core.Container {
container := core_util.GetContainerByName(podTemplate.Spec.Containers, containerName)
if container == nil {
container = &core.Container{
Name: containerName,
}
}
podTemplate.Spec.Containers = core_util.UpsertContainer(podTemplate.Spec.Containers, *container)
return core_util.GetContainerByName(podTemplate.Spec.Containers, containerName)
}

// EnsureInitContainerExists ensures that given initContainer either exits by default or
// it will create the initContainer, then insert it to the podTemplate and return a pointer of that initContainer
func EnsureInitContainerExists(podTemplate *ofstv2.PodTemplateSpec, containerName string) *core.Container {
container := core_util.GetContainerByName(podTemplate.Spec.InitContainers, containerName)
if container == nil {
container = &core.Container{
Name: containerName,
}
}
podTemplate.Spec.InitContainers = core_util.UpsertContainer(podTemplate.Spec.InitContainers, *container)
return core_util.GetContainerByName(podTemplate.Spec.InitContainers, containerName)
}
Loading
Loading