diff --git a/internal/bridge/crunchybridgecluster/postgres.go b/internal/bridge/crunchybridgecluster/postgres.go index c0dc1b255..024631de6 100644 --- a/internal/bridge/crunchybridgecluster/postgres.go +++ b/internal/bridge/crunchybridgecluster/postgres.go @@ -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" ) @@ -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( diff --git a/internal/controller/pgupgrade/jobs.go b/internal/controller/pgupgrade/jobs.go index eeafb05d5..a1722dfc1 100644 --- a/internal/controller/pgupgrade/jobs.go +++ b/internal/controller/pgupgrade/jobs.go @@ -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) @@ -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) diff --git a/internal/controller/postgrescluster/instance.go b/internal/controller/postgrescluster/instance.go index df71596ea..66321cc73 100644 --- a/internal/controller/postgrescluster/instance.go +++ b/internal/controller/postgrescluster/instance.go @@ -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( diff --git a/internal/controller/postgrescluster/instance_test.go b/internal/controller/postgrescluster/instance_test.go index b1e993f2f..f7f59f50a 100644 --- a/internal/controller/postgrescluster/instance_test.go +++ b/internal/controller/postgrescluster/instance_test.go @@ -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)) }) @@ -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)) }) @@ -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 @@ -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)) }) @@ -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 @@ -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 @@ -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) { @@ -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)) diff --git a/internal/controller/postgrescluster/pgadmin.go b/internal/controller/postgrescluster/pgadmin.go index 7e3494f76..c0a936ba1 100644 --- a/internal/controller/postgrescluster/pgadmin.go +++ b/internal/controller/postgrescluster/pgadmin.go @@ -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), } @@ -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 diff --git a/internal/controller/postgrescluster/pgbackrest.go b/internal/controller/postgrescluster/pgbackrest.go index 670ece55b..395e3c73f 100644 --- a/internal/controller/postgrescluster/pgbackrest.go +++ b/internal/controller/postgrescluster/pgbackrest.go @@ -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( @@ -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. @@ -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 { diff --git a/internal/controller/postgrescluster/pgbouncer.go b/internal/controller/postgrescluster/pgbouncer.go index 235d910eb..76207fac0 100644 --- a/internal/controller/postgrescluster/pgbouncer.go +++ b/internal/controller/postgrescluster/pgbouncer.go @@ -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)...) diff --git a/internal/controller/postgrescluster/pgbouncer_test.go b/internal/controller/postgrescluster/pgbouncer_test.go index 5ad7956ca..9bbced524 100644 --- a/internal/controller/postgrescluster/pgbouncer_test.go +++ b/internal/controller/postgrescluster/pgbouncer_test.go @@ -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" @@ -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)) }) @@ -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)) }) @@ -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 @@ -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)) }) @@ -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 @@ -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 diff --git a/internal/controller/postgrescluster/pod_disruption_budget.go b/internal/controller/postgrescluster/pod_disruption_budget.go index f9b568934..4bff4a974 100644 --- a/internal/controller/postgrescluster/pod_disruption_budget.go +++ b/internal/controller/postgrescluster/pod_disruption_budget.go @@ -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)) } diff --git a/internal/controller/postgrescluster/pod_disruption_budget_test.go b/internal/controller/postgrescluster/pod_disruption_budget_test.go index 9ab119cd6..55e2bb63c 100644 --- a/internal/controller/postgrescluster/pod_disruption_budget_test.go +++ b/internal/controller/postgrescluster/pod_disruption_budget_test.go @@ -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", @@ -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%")) }) diff --git a/internal/controller/postgrescluster/postgres.go b/internal/controller/postgrescluster/postgres.go index 2816624ac..312079d82 100644 --- a/internal/controller/postgrescluster/postgres.go +++ b/internal/controller/postgrescluster/postgres.go @@ -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. diff --git a/internal/controller/postgrescluster/volumes.go b/internal/controller/postgrescluster/volumes.go index e22f49d5b..e40710d4f 100644 --- a/internal/controller/postgrescluster/volumes.go +++ b/internal/controller/postgrescluster/volumes.go @@ -499,10 +499,9 @@ func (r *Reconciler) reconcileMovePGDataDir(ctx context.Context, }, } // set the priority class name, if it exists - if len(cluster.Spec.InstanceSets) > 0 && - cluster.Spec.InstanceSets[0].PriorityClassName != nil { + if len(cluster.Spec.InstanceSets) > 0 { jobSpec.Template.Spec.PriorityClassName = - *cluster.Spec.InstanceSets[0].PriorityClassName + initialize.FromPointer(cluster.Spec.InstanceSets[0].PriorityClassName) } moveDirJob.Spec = *jobSpec @@ -617,10 +616,9 @@ func (r *Reconciler) reconcileMoveWALDir(ctx context.Context, }, } // set the priority class name, if it exists - if len(cluster.Spec.InstanceSets) > 0 && - cluster.Spec.InstanceSets[0].PriorityClassName != nil { + if len(cluster.Spec.InstanceSets) > 0 { jobSpec.Template.Spec.PriorityClassName = - *cluster.Spec.InstanceSets[0].PriorityClassName + initialize.FromPointer(cluster.Spec.InstanceSets[0].PriorityClassName) } moveDirJob.Spec = *jobSpec @@ -740,9 +738,7 @@ func (r *Reconciler) reconcileMoveRepoDir(ctx context.Context, } // set the priority class name, if it exists if repoHost := cluster.Spec.Backups.PGBackRest.RepoHost; repoHost != nil { - if repoHost.PriorityClassName != nil { - jobSpec.Template.Spec.PriorityClassName = *repoHost.PriorityClassName - } + jobSpec.Template.Spec.PriorityClassName = initialize.FromPointer(repoHost.PriorityClassName) } moveDirJob.Spec = *jobSpec diff --git a/internal/controller/standalone_pgadmin/configmap.go b/internal/controller/standalone_pgadmin/configmap.go index 2ce9a271d..d1ec39bf1 100644 --- a/internal/controller/standalone_pgadmin/configmap.go +++ b/internal/controller/standalone_pgadmin/configmap.go @@ -53,7 +53,7 @@ func configmap(pgadmin *v1beta1.PGAdmin, naming.StandalonePGAdminLabels(pgadmin.Name)) // TODO(tjmoore4): Populate configuration details. - initialize.StringMap(&configmap.Data) + initialize.Map(&configmap.Data) configSettings, err := generateConfig(pgadmin) if err == nil { configmap.Data[settingsConfigMapKey] = configSettings diff --git a/internal/controller/standalone_pgadmin/pod.go b/internal/controller/standalone_pgadmin/pod.go index c7ebe5a00..8212c63c1 100644 --- a/internal/controller/standalone_pgadmin/pod.go +++ b/internal/controller/standalone_pgadmin/pod.go @@ -10,6 +10,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/util/intstr" "github.com/crunchydata/postgres-operator/internal/config" "github.com/crunchydata/postgres-operator/internal/initialize" @@ -159,7 +160,7 @@ func pod( readinessProbe := &corev1.Probe{ ProbeHandler: corev1.ProbeHandler{ HTTPGet: &corev1.HTTPGetAction{ - Port: *initialize.IntOrStringInt32(pgAdminPort), + Port: intstr.FromInt32(pgAdminPort), Path: "/login", Scheme: corev1.URISchemeHTTP, }, diff --git a/internal/controller/standalone_pgadmin/statefulset.go b/internal/controller/standalone_pgadmin/statefulset.go index 31b59684e..e086e333f 100644 --- a/internal/controller/standalone_pgadmin/statefulset.go +++ b/internal/controller/standalone_pgadmin/statefulset.go @@ -94,10 +94,7 @@ func statefulset( // Use scheduling constraints from the cluster spec. sts.Spec.Template.Spec.Affinity = pgadmin.Spec.Affinity sts.Spec.Template.Spec.Tolerations = pgadmin.Spec.Tolerations - - if pgadmin.Spec.PriorityClassName != nil { - sts.Spec.Template.Spec.PriorityClassName = *pgadmin.Spec.PriorityClassName - } + sts.Spec.Template.Spec.PriorityClassName = initialize.FromPointer(pgadmin.Spec.PriorityClassName) // Restart containers any time they stop, die, are killed, etc. // - https://docs.k8s.io/concepts/workloads/pods/pod-lifecycle/#restart-policy diff --git a/internal/initialize/intstr.go b/internal/initialize/intstr.go deleted file mode 100644 index 01e66401c..000000000 --- a/internal/initialize/intstr.go +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. -// -// SPDX-License-Identifier: Apache-2.0 - -package initialize - -import ( - "k8s.io/apimachinery/pkg/util/intstr" -) - -// IntOrStringInt32 returns an *intstr.IntOrString containing i. -func IntOrStringInt32(i int32) *intstr.IntOrString { - return IntOrString(intstr.FromInt(int(i))) -} - -// IntOrStringString returns an *intstr.IntOrString containing s. -func IntOrStringString(s string) *intstr.IntOrString { - return IntOrString(intstr.FromString(s)) -} - -// IntOrString returns a pointer to the provided IntOrString -func IntOrString(ios intstr.IntOrString) *intstr.IntOrString { - return &ios -} diff --git a/internal/initialize/intstr_test.go b/internal/initialize/intstr_test.go deleted file mode 100644 index ec6cc4bd9..000000000 --- a/internal/initialize/intstr_test.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2021 - 2024 Crunchy Data Solutions, Inc. -// -// SPDX-License-Identifier: Apache-2.0 - -package initialize_test - -import ( - "testing" - - "gotest.tools/v3/assert" - "k8s.io/apimachinery/pkg/util/intstr" - - "github.com/crunchydata/postgres-operator/internal/initialize" -) - -func TestIntOrStringInt32(t *testing.T) { - // Same content as the upstream constructor. - upstream := intstr.FromInt(42) - n := initialize.IntOrStringInt32(42) - - assert.DeepEqual(t, &upstream, n) -} - -func TestIntOrStringString(t *testing.T) { - upstream := intstr.FromString("50%") - s := initialize.IntOrStringString("50%") - - assert.DeepEqual(t, &upstream, s) -} -func TestIntOrString(t *testing.T) { - upstream := intstr.FromInt(0) - - ios := initialize.IntOrString(intstr.FromInt(0)) - assert.DeepEqual(t, *ios, upstream) -} diff --git a/internal/initialize/primitives.go b/internal/initialize/primitives.go index 5fa02f5ce..9bc264f88 100644 --- a/internal/initialize/primitives.go +++ b/internal/initialize/primitives.go @@ -7,13 +7,6 @@ package initialize // Bool returns a pointer to v. func Bool(v bool) *bool { return &v } -// ByteMap initializes m when it points to nil. -func ByteMap(m *map[string][]byte) { - if m != nil && *m == nil { - *m = make(map[string][]byte) - } -} - // FromPointer returns the value that p points to. // When p is nil, it returns the zero value of T. func FromPointer[T any](p *T) T { @@ -30,15 +23,17 @@ func Int32(v int32) *int32 { return &v } // Int64 returns a pointer to v. func Int64(v int64) *int64 { return &v } +// Map initializes m when it points to nil. +func Map[M ~map[K]V, K comparable, V any](m *M) { + // See https://pkg.go.dev/maps for similar type constraints. + + if m != nil && *m == nil { + *m = make(M) + } +} + // Pointer returns a pointer to v. func Pointer[T any](v T) *T { return &v } // String returns a pointer to v. func String(v string) *string { return &v } - -// StringMap initializes m when it points to nil. -func StringMap(m *map[string]string) { - if m != nil && *m == nil { - *m = make(map[string]string) - } -} diff --git a/internal/initialize/primitives_test.go b/internal/initialize/primitives_test.go index 6ca062d32..e39898b4f 100644 --- a/internal/initialize/primitives_test.go +++ b/internal/initialize/primitives_test.go @@ -24,27 +24,6 @@ func TestBool(t *testing.T) { } } -func TestByteMap(t *testing.T) { - // Ignores nil pointer. - initialize.ByteMap(nil) - - var m map[string][]byte - - // Starts nil. - assert.Assert(t, m == nil) - - // Gets initialized. - initialize.ByteMap(&m) - assert.DeepEqual(t, m, map[string][]byte{}) - - // Now writable. - m["x"] = []byte("y") - - // Doesn't overwrite. - initialize.ByteMap(&m) - assert.DeepEqual(t, m, map[string][]byte{"x": []byte("y")}) -} - func TestFromPointer(t *testing.T) { t.Run("bool", func(t *testing.T) { assert.Equal(t, initialize.FromPointer((*bool)(nil)), false) @@ -107,6 +86,50 @@ func TestInt64(t *testing.T) { } } +func TestMap(t *testing.T) { + t.Run("map[string][]byte", func(t *testing.T) { + // Ignores nil pointer. + initialize.Map((*map[string][]byte)(nil)) + + var m map[string][]byte + + // Starts nil. + assert.Assert(t, m == nil) + + // Gets initialized. + initialize.Map(&m) + assert.DeepEqual(t, m, map[string][]byte{}) + + // Now writable. + m["x"] = []byte("y") + + // Doesn't overwrite. + initialize.Map(&m) + assert.DeepEqual(t, m, map[string][]byte{"x": []byte("y")}) + }) + + t.Run("map[string]string", func(t *testing.T) { + // Ignores nil pointer. + initialize.Map((*map[string]string)(nil)) + + var m map[string]string + + // Starts nil. + assert.Assert(t, m == nil) + + // Gets initialized. + initialize.Map(&m) + assert.DeepEqual(t, m, map[string]string{}) + + // Now writable. + m["x"] = "y" + + // Doesn't overwrite. + initialize.Map(&m) + assert.DeepEqual(t, m, map[string]string{"x": "y"}) + }) +} + func TestPointer(t *testing.T) { t.Run("bool", func(t *testing.T) { n := initialize.Pointer(false) @@ -178,24 +201,3 @@ func TestString(t *testing.T) { assert.Equal(t, *n, "sup") } } - -func TestStringMap(t *testing.T) { - // Ignores nil pointer. - initialize.StringMap(nil) - - var m map[string]string - - // Starts nil. - assert.Assert(t, m == nil) - - // Gets initialized. - initialize.StringMap(&m) - assert.DeepEqual(t, m, map[string]string{}) - - // Now writable. - m["x"] = "y" - - // Doesn't overwrite. - initialize.StringMap(&m) - assert.DeepEqual(t, m, map[string]string{"x": "y"}) -} diff --git a/internal/patroni/reconcile.go b/internal/patroni/reconcile.go index 26f0014cb..4fbb08b67 100644 --- a/internal/patroni/reconcile.go +++ b/internal/patroni/reconcile.go @@ -35,7 +35,7 @@ func ClusterConfigMap(ctx context.Context, ) error { var err error - initialize.StringMap(&outClusterConfigMap.Data) + initialize.Map(&outClusterConfigMap.Data) outClusterConfigMap.Data[configMapFileKey], err = clusterYAML(inCluster, inHBAs, inParameters) @@ -51,7 +51,7 @@ func InstanceConfigMap(ctx context.Context, ) error { var err error - initialize.StringMap(&outInstanceConfigMap.Data) + initialize.Map(&outInstanceConfigMap.Data) command := pgbackrest.ReplicaCreateCommand(inCluster, inInstanceSpec) @@ -66,7 +66,7 @@ func InstanceCertificates(ctx context.Context, inRoot pki.Certificate, inDNS pki.Certificate, inDNSKey pki.PrivateKey, outInstanceCertificates *corev1.Secret, ) error { - initialize.ByteMap(&outInstanceCertificates.Data) + initialize.Map(&outInstanceCertificates.Data) var err error outInstanceCertificates.Data[certAuthorityFileKey], err = certFile(inRoot) diff --git a/internal/pgadmin/reconcile.go b/internal/pgadmin/reconcile.go index 69a319a26..af62c482f 100644 --- a/internal/pgadmin/reconcile.go +++ b/internal/pgadmin/reconcile.go @@ -133,7 +133,7 @@ func ConfigMap( return nil } - initialize.StringMap(&outConfigMap.Data) + initialize.Map(&outConfigMap.Data) // To avoid spurious reconciles, the following value must not change when // the spec does not change. [json.Encoder] and [json.Marshal] do this by diff --git a/internal/pgbackrest/config.go b/internal/pgbackrest/config.go index 09c56c027..15e0a26ea 100644 --- a/internal/pgbackrest/config.go +++ b/internal/pgbackrest/config.go @@ -88,7 +88,7 @@ func CreatePGBackRestConfigMapIntent(postgresCluster *v1beta1.PostgresCluster, } // create an empty map for the config data - initialize.StringMap(&cm.Data) + initialize.Map(&cm.Data) pgdataDir := postgres.DataDirectory(postgresCluster) // Port will always be populated, since the API will set a default of 5432 if not provided diff --git a/internal/pgbackrest/reconcile.go b/internal/pgbackrest/reconcile.go index 89af42001..d22bccc3c 100644 --- a/internal/pgbackrest/reconcile.go +++ b/internal/pgbackrest/reconcile.go @@ -406,7 +406,7 @@ func InstanceCertificates(ctx context.Context, ) error { var err error - initialize.ByteMap(&outInstanceCertificates.Data) + initialize.Map(&outInstanceCertificates.Data) if err == nil { outInstanceCertificates.Data[certInstanceSecretKey], err = certFile(inDNS) @@ -473,7 +473,7 @@ func RestoreConfig( sourceConfigMap, targetConfigMap *corev1.ConfigMap, sourceSecret, targetSecret *corev1.Secret, ) { - initialize.StringMap(&targetConfigMap.Data) + initialize.Map(&targetConfigMap.Data) // Use the repository definitions from the source cluster. // @@ -485,7 +485,7 @@ func RestoreConfig( targetConfigMap.Data[CMInstanceKey] = sourceConfigMap.Data[CMInstanceKey] if sourceSecret != nil && targetSecret != nil { - initialize.ByteMap(&targetSecret.Data) + initialize.Map(&targetSecret.Data) // - https://golang.org/issue/45038 bytesClone := func(b []byte) []byte { return append([]byte(nil), b...) } @@ -509,7 +509,7 @@ func Secret(ctx context.Context, // Save the CA and generate a TLS client certificate for the entire cluster. if inRepoHost != nil { - initialize.ByteMap(&outSecret.Data) + initialize.Map(&outSecret.Data) // The server verifies its "tls-server-auth" option contains the common // name (CN) of the certificate presented by a client. The entire diff --git a/internal/pgbouncer/reconcile.go b/internal/pgbouncer/reconcile.go index e9233406f..999d6524a 100644 --- a/internal/pgbouncer/reconcile.go +++ b/internal/pgbouncer/reconcile.go @@ -30,7 +30,7 @@ func ConfigMap( return } - initialize.StringMap(&outConfigMap.Data) + initialize.Map(&outConfigMap.Data) outConfigMap.Data[emptyConfigMapKey] = "" outConfigMap.Data[iniFileConfigMapKey] = clusterINI(inCluster) @@ -50,7 +50,7 @@ func Secret(ctx context.Context, } var err error - initialize.ByteMap(&outSecret.Data) + initialize.Map(&outSecret.Data) // Use the existing password and verifier. Generate both when either is missing. // NOTE(cbandy): We don't have a function to compare a plaintext password