Skip to content

Commit

Permalink
Shrink the initialize package by using generics
Browse files Browse the repository at this point in the history
  • Loading branch information
cbandy committed Oct 1, 2024
1 parent 66174ec commit cd966cd
Show file tree
Hide file tree
Showing 24 changed files with 121 additions and 209 deletions.
11 changes: 5 additions & 6 deletions internal/bridge/crunchybridgecluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/crunchydata/postgres-operator/internal/bridge"
"github.com/crunchydata/postgres-operator/internal/initialize"
"github.com/crunchydata/postgres-operator/internal/naming"
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
Expand All @@ -34,11 +33,11 @@ func (r *CrunchyBridgeClusterReconciler) generatePostgresRoleSecret(
Name: secretName,
}}
intent.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
initialize.StringMap(&intent.StringData)

intent.StringData["name"] = clusterRole.Name
intent.StringData["password"] = clusterRole.Password
intent.StringData["uri"] = clusterRole.URI
intent.StringData = map[string]string{
"name": clusterRole.Name,
"password": clusterRole.Password,
"uri": clusterRole.URI,
}

intent.Annotations = cluster.Spec.Metadata.GetAnnotationsOrNil()
intent.Labels = naming.Merge(
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/pgupgrade/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func (r *PGUpgradeReconciler) generateUpgradeJob(

// The following will set these fields to null if not set in the spec
job.Spec.Template.Spec.Affinity = upgrade.Spec.Affinity
job.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(
upgrade.Spec.PriorityClassName)
job.Spec.Template.Spec.PriorityClassName =
initialize.FromPointer(upgrade.Spec.PriorityClassName)
job.Spec.Template.Spec.Tolerations = upgrade.Spec.Tolerations

r.setControllerReference(upgrade, job)
Expand Down Expand Up @@ -292,8 +292,8 @@ func (r *PGUpgradeReconciler) generateRemoveDataJob(

// The following will set these fields to null if not set in the spec
job.Spec.Template.Spec.Affinity = upgrade.Spec.Affinity
job.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(
upgrade.Spec.PriorityClassName)
job.Spec.Template.Spec.PriorityClassName =
initialize.FromPointer(upgrade.Spec.PriorityClassName)
job.Spec.Template.Spec.Tolerations = upgrade.Spec.Tolerations

r.setControllerReference(upgrade, job)
Expand Down
8 changes: 2 additions & 6 deletions internal/controller/postgrescluster/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1298,15 +1298,11 @@ func generateInstanceStatefulSetIntent(_ context.Context,
sts.Spec.Template.Spec.Affinity = spec.Affinity
sts.Spec.Template.Spec.Tolerations = spec.Tolerations
sts.Spec.Template.Spec.TopologySpreadConstraints = spec.TopologySpreadConstraints
if spec.PriorityClassName != nil {
sts.Spec.Template.Spec.PriorityClassName = *spec.PriorityClassName
}
sts.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(spec.PriorityClassName)

// if default pod scheduling is not explicitly disabled, add the default
// pod topology spread constraints
if cluster.Spec.DisableDefaultPodScheduling == nil ||
(cluster.Spec.DisableDefaultPodScheduling != nil &&
!*cluster.Spec.DisableDefaultPodScheduling) {
if !initialize.FromPointer(cluster.Spec.DisableDefaultPodScheduling) {
sts.Spec.Template.Spec.TopologySpreadConstraints = append(
sts.Spec.Template.Spec.TopologySpreadConstraints,
defaultTopologySpreadConstraints(
Expand Down
20 changes: 10 additions & 10 deletions internal/controller/postgrescluster/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,7 +1972,7 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
spec := &cluster.Spec.InstanceSets[0]
spec.MinAvailable = initialize.IntOrStringInt32(0)
spec.MinAvailable = initialize.Pointer(intstr.FromInt32(0))
assert.NilError(t, r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, spec))
assert.Assert(t, !foundPDB(cluster, spec))
})
Expand All @@ -1981,7 +1981,7 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
spec := &cluster.Spec.InstanceSets[0]
spec.MinAvailable = initialize.IntOrStringInt32(1)
spec.MinAvailable = initialize.Pointer(intstr.FromInt32(1))

assert.NilError(t, r.Client.Create(ctx, cluster))
t.Cleanup(func() { assert.Check(t, r.Client.Delete(ctx, cluster)) })
Expand All @@ -1990,7 +1990,7 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
assert.Assert(t, foundPDB(cluster, spec))

t.Run("deleted", func(t *testing.T) {
spec.MinAvailable = initialize.IntOrStringInt32(0)
spec.MinAvailable = initialize.Pointer(intstr.FromInt32(0))
err := r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, spec)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand All @@ -2008,7 +2008,7 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
spec := &cluster.Spec.InstanceSets[0]
spec.MinAvailable = initialize.IntOrStringString("50%")
spec.MinAvailable = initialize.Pointer(intstr.FromString("50%"))

assert.NilError(t, r.Client.Create(ctx, cluster))
t.Cleanup(func() { assert.Check(t, r.Client.Delete(ctx, cluster)) })
Expand All @@ -2017,7 +2017,7 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
assert.Assert(t, foundPDB(cluster, spec))

t.Run("deleted", func(t *testing.T) {
spec.MinAvailable = initialize.IntOrStringString("0%")
spec.MinAvailable = initialize.Pointer(intstr.FromString("0%"))
err := r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, spec)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand All @@ -2031,13 +2031,13 @@ func TestReconcileInstanceSetPodDisruptionBudget(t *testing.T) {
})

t.Run("delete with 00%", func(t *testing.T) {
spec.MinAvailable = initialize.IntOrStringString("50%")
spec.MinAvailable = initialize.Pointer(intstr.FromString("50%"))

assert.NilError(t, r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, spec))
assert.Assert(t, foundPDB(cluster, spec))

t.Run("deleted", func(t *testing.T) {
spec.MinAvailable = initialize.IntOrStringString("00%")
spec.MinAvailable = initialize.Pointer(intstr.FromString("00%"))
err := r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, spec)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand Down Expand Up @@ -2110,13 +2110,13 @@ func TestCleanupDisruptionBudgets(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
spec := &cluster.Spec.InstanceSets[0]
spec.MinAvailable = initialize.IntOrStringInt32(1)
spec.MinAvailable = initialize.Pointer(intstr.FromInt32(1))

assert.NilError(t, r.Client.Create(ctx, cluster))
t.Cleanup(func() { assert.Check(t, r.Client.Delete(ctx, cluster)) })

expectedPDB := generatePDB(t, cluster, spec,
initialize.IntOrStringInt32(1))
initialize.Pointer(intstr.FromInt32(1)))
assert.NilError(t, createPDB(expectedPDB))

t.Run("no instances were removed", func(t *testing.T) {
Expand All @@ -2129,7 +2129,7 @@ func TestCleanupDisruptionBudgets(t *testing.T) {
leftoverPDB := generatePDB(t, cluster, &v1beta1.PostgresInstanceSetSpec{
Name: "old-instance",
Replicas: initialize.Int32(1),
}, initialize.IntOrStringInt32(1))
}, initialize.Pointer(intstr.FromInt32(1)))
assert.NilError(t, createPDB(leftoverPDB))

assert.Assert(t, foundPDB(expectedPDB))
Expand Down
9 changes: 3 additions & 6 deletions internal/controller/postgrescluster/pgadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (r *Reconciler) generatePGAdminService(
// requires updates to the pgAdmin service configuration.
servicePort := corev1.ServicePort{
Name: naming.PortPGAdmin,
Port: *initialize.Int32(5050),
Port: 5050,
Protocol: corev1.ProtocolTCP,
TargetPort: intstr.FromString(naming.PortPGAdmin),
}
Expand Down Expand Up @@ -294,11 +294,8 @@ func (r *Reconciler) reconcilePGAdminStatefulSet(
// Use scheduling constraints from the cluster spec.
sts.Spec.Template.Spec.Affinity = cluster.Spec.UserInterface.PGAdmin.Affinity
sts.Spec.Template.Spec.Tolerations = cluster.Spec.UserInterface.PGAdmin.Tolerations

if cluster.Spec.UserInterface.PGAdmin.PriorityClassName != nil {
sts.Spec.Template.Spec.PriorityClassName = *cluster.Spec.UserInterface.PGAdmin.PriorityClassName
}

sts.Spec.Template.Spec.PriorityClassName =
initialize.FromPointer(cluster.Spec.UserInterface.PGAdmin.PriorityClassName)
sts.Spec.Template.Spec.TopologySpreadConstraints =
cluster.Spec.UserInterface.PGAdmin.TopologySpreadConstraints

Expand Down
18 changes: 5 additions & 13 deletions internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,16 +610,12 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster
repo.Spec.Template.Spec.Affinity = repoHost.Affinity
repo.Spec.Template.Spec.Tolerations = repoHost.Tolerations
repo.Spec.Template.Spec.TopologySpreadConstraints = repoHost.TopologySpreadConstraints
if repoHost.PriorityClassName != nil {
repo.Spec.Template.Spec.PriorityClassName = *repoHost.PriorityClassName
}
repo.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(repoHost.PriorityClassName)
}

// if default pod scheduling is not explicitly disabled, add the default
// pod topology spread constraints
if postgresCluster.Spec.DisableDefaultPodScheduling == nil ||
(postgresCluster.Spec.DisableDefaultPodScheduling != nil &&
!*postgresCluster.Spec.DisableDefaultPodScheduling) {
if !initialize.FromPointer(postgresCluster.Spec.DisableDefaultPodScheduling) {
repo.Spec.Template.Spec.TopologySpreadConstraints = append(
repo.Spec.Template.Spec.TopologySpreadConstraints,
defaultTopologySpreadConstraints(
Expand Down Expand Up @@ -821,12 +817,10 @@ func generateBackupJobSpecIntent(postgresCluster *v1beta1.PostgresCluster,

// set the priority class name, tolerations, and affinity, if they exist
if postgresCluster.Spec.Backups.PGBackRest.Jobs != nil {
if postgresCluster.Spec.Backups.PGBackRest.Jobs.PriorityClassName != nil {
jobSpec.Template.Spec.PriorityClassName =
*postgresCluster.Spec.Backups.PGBackRest.Jobs.PriorityClassName
}
jobSpec.Template.Spec.Tolerations = postgresCluster.Spec.Backups.PGBackRest.Jobs.Tolerations
jobSpec.Template.Spec.Affinity = postgresCluster.Spec.Backups.PGBackRest.Jobs.Affinity
jobSpec.Template.Spec.PriorityClassName =
initialize.FromPointer(postgresCluster.Spec.Backups.PGBackRest.Jobs.PriorityClassName)
}

// Set the image pull secrets, if any exist.
Expand Down Expand Up @@ -1318,9 +1312,7 @@ func (r *Reconciler) generateRestoreJobIntent(cluster *v1beta1.PostgresCluster,
job.Spec.Template.Spec.SecurityContext = postgres.PodSecurityContext(cluster)

// set the priority class name, if it exists
if dataSource.PriorityClassName != nil {
job.Spec.Template.Spec.PriorityClassName = *dataSource.PriorityClassName
}
job.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(dataSource.PriorityClassName)

job.SetGroupVersionKind(batchv1.SchemeGroupVersion.WithKind("Job"))
if err := errors.WithStack(r.setControllerReference(cluster, job)); err != nil {
Expand Down
13 changes: 4 additions & 9 deletions internal/controller/postgrescluster/pgbouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,25 +395,20 @@ func (r *Reconciler) generatePGBouncerDeployment(
// - https://docs.k8s.io/concepts/workloads/controllers/deployment/#rolling-update-deployment
deploy.Spec.Strategy.Type = appsv1.RollingUpdateDeploymentStrategyType
deploy.Spec.Strategy.RollingUpdate = &appsv1.RollingUpdateDeployment{
MaxUnavailable: intstr.ValueOrDefault(nil, intstr.FromInt(0)),
MaxUnavailable: initialize.Pointer(intstr.FromInt32(0)),
}

// Use scheduling constraints from the cluster spec.
deploy.Spec.Template.Spec.Affinity = cluster.Spec.Proxy.PGBouncer.Affinity
deploy.Spec.Template.Spec.Tolerations = cluster.Spec.Proxy.PGBouncer.Tolerations

if cluster.Spec.Proxy.PGBouncer.PriorityClassName != nil {
deploy.Spec.Template.Spec.PriorityClassName = *cluster.Spec.Proxy.PGBouncer.PriorityClassName
}

deploy.Spec.Template.Spec.PriorityClassName =
initialize.FromPointer(cluster.Spec.Proxy.PGBouncer.PriorityClassName)
deploy.Spec.Template.Spec.TopologySpreadConstraints =
cluster.Spec.Proxy.PGBouncer.TopologySpreadConstraints

// if default pod scheduling is not explicitly disabled, add the default
// pod topology spread constraints
if cluster.Spec.DisableDefaultPodScheduling == nil ||
(cluster.Spec.DisableDefaultPodScheduling != nil &&
!*cluster.Spec.DisableDefaultPodScheduling) {
if !initialize.FromPointer(cluster.Spec.DisableDefaultPodScheduling) {
deploy.Spec.Template.Spec.TopologySpreadConstraints = append(
deploy.Spec.Template.Spec.TopologySpreadConstraints,
defaultTopologySpreadConstraints(*deploy.Spec.Selector)...)
Expand Down
15 changes: 8 additions & 7 deletions internal/controller/postgrescluster/pgbouncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
policyv1 "k8s.io/api/policy/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand Down Expand Up @@ -551,7 +552,7 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
cluster.Spec.Proxy.PGBouncer.Replicas = initialize.Int32(1)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringInt32(0)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromInt32(0))
assert.NilError(t, r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster))
assert.Assert(t, !foundPDB(cluster))
})
Expand All @@ -560,7 +561,7 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
cluster.Spec.Proxy.PGBouncer.Replicas = initialize.Int32(1)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringInt32(1)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromInt32(1))

assert.NilError(t, r.Client.Create(ctx, cluster))
t.Cleanup(func() { assert.Check(t, r.Client.Delete(ctx, cluster)) })
Expand All @@ -569,7 +570,7 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
assert.Assert(t, foundPDB(cluster))

t.Run("deleted", func(t *testing.T) {
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringInt32(0)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromInt32(0))
err := r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand All @@ -587,7 +588,7 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
cluster := testCluster()
cluster.Namespace = ns.Name
cluster.Spec.Proxy.PGBouncer.Replicas = initialize.Int32(1)
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringString("50%")
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromString("50%"))

assert.NilError(t, r.Client.Create(ctx, cluster))
t.Cleanup(func() { assert.Check(t, r.Client.Delete(ctx, cluster)) })
Expand All @@ -596,7 +597,7 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
assert.Assert(t, foundPDB(cluster))

t.Run("deleted", func(t *testing.T) {
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringString("0%")
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromString("0%"))
err := r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand All @@ -610,13 +611,13 @@ func TestReconcilePGBouncerDisruptionBudget(t *testing.T) {
})

t.Run("delete with 00%", func(t *testing.T) {
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringString("50%")
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromString("50%"))

assert.NilError(t, r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster))
assert.Assert(t, foundPDB(cluster))

t.Run("deleted", func(t *testing.T) {
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.IntOrStringString("00%")
cluster.Spec.Proxy.PGBouncer.MinAvailable = initialize.Pointer(intstr.FromString("00%"))
err := r.reconcilePGBouncerPodDisruptionBudget(ctx, cluster)
if apierrors.IsConflict(err) {
// When running in an existing environment another controller will sometimes update
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ func getMinAvailable(
}

// If more than one replica is not defined, we will default to '0'
return initialize.IntOrStringInt32(expect)
return initialize.Pointer(intstr.FromInt32(expect))
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
"anno-key": "anno-value",
},
}
minAvailable = initialize.IntOrStringInt32(1)
minAvailable = initialize.Pointer(intstr.FromInt32(1))
selector := metav1.LabelSelector{
MatchLabels: map[string]string{
"key": "value",
Expand Down Expand Up @@ -78,19 +78,19 @@ func TestGeneratePodDisruptionBudget(t *testing.T) {
func TestGetMinAvailable(t *testing.T) {
t.Run("minAvailable provided", func(t *testing.T) {
// minAvailable is defined so use that value
ma := initialize.IntOrStringInt32(0)
ma := initialize.Pointer(intstr.FromInt32(0))
expect := getMinAvailable(ma, 1)
assert.Equal(t, *expect, intstr.FromInt(0))

ma = initialize.IntOrStringInt32(1)
ma = initialize.Pointer(intstr.FromInt32(1))
expect = getMinAvailable(ma, 2)
assert.Equal(t, *expect, intstr.FromInt(1))

ma = initialize.IntOrStringString("50%")
ma = initialize.Pointer(intstr.FromString("50%"))
expect = getMinAvailable(ma, 3)
assert.Equal(t, *expect, intstr.FromString("50%"))

ma = initialize.IntOrStringString("200%")
ma = initialize.Pointer(intstr.FromString("200%"))
expect = getMinAvailable(ma, 2147483647)
assert.Equal(t, *expect, intstr.FromString("200%"))
})
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *Reconciler) generatePostgresUserSecret(
username := string(spec.Name)
intent := &corev1.Secret{ObjectMeta: naming.PostgresUserSecret(cluster, username)}
intent.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
initialize.ByteMap(&intent.Data)
initialize.Map(&intent.Data)

// Populate the Secret with libpq keywords for connecting through
// the primary Service.
Expand Down
Loading

0 comments on commit cd966cd

Please sign in to comment.