From a87b03cbcee4e5bcbef35333df45688e90ceadef Mon Sep 17 00:00:00 2001 From: Chris Bandy Date: Fri, 7 Mar 2025 12:05:46 -0600 Subject: [PATCH] Use int32 for PostgresVersion in Go structs This aligns the PostgresCluster and PGUpgrade structs. Tidy up some Job YAML, too. --- ...ator.crunchydata.com_postgresclusters.yaml | 4 ++ internal/collector/postgres.go | 2 +- .../pgupgrade/pgupgrade_controller.go | 2 +- .../controller/postgrescluster/volumes.go | 64 +++++++++---------- .../postgrescluster/volumes_test.go | 54 ++++++++++------ internal/pgbackrest/config.go | 7 +- internal/postgres/config_test.go | 2 +- internal/postgres/versions.go | 4 +- .../v1/postgrescluster_types.go | 4 +- .../v1beta1/postgrescluster_types.go | 4 +- 10 files changed, 80 insertions(+), 67 deletions(-) diff --git a/config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml b/config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml index 8556b11d24..ce139e4c70 100644 --- a/config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml +++ b/config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml @@ -13035,6 +13035,7 @@ spec: postgresVersion: description: The major version of PostgreSQL installed in the PostgreSQL image + format: int32 maximum: 17 minimum: 11 type: integer @@ -18812,6 +18813,7 @@ spec: description: |- Stores the current PostgreSQL major version following a successful major PostgreSQL upgrade. + format: int32 type: integer proxy: description: Current state of the PostgreSQL proxy. @@ -31867,6 +31869,7 @@ spec: postgresVersion: description: The major version of PostgreSQL installed in the PostgreSQL image + format: int32 maximum: 17 minimum: 11 type: integer @@ -37612,6 +37615,7 @@ spec: description: |- Stores the current PostgreSQL major version following a successful major PostgreSQL upgrade. + format: int32 type: integer proxy: description: Current state of the PostgreSQL proxy. diff --git a/internal/collector/postgres.go b/internal/collector/postgres.go index ca627a8fda..a279be33ca 100644 --- a/internal/collector/postgres.go +++ b/internal/collector/postgres.go @@ -92,7 +92,7 @@ func NewConfigForPostgresPod(ctx context.Context, var postgresLogsTransforms json.RawMessage // postgresCSVNames returns the names of fields in the CSV logs for version. -func postgresCSVNames(version int) string { +func postgresCSVNames(version int32) string { // JSON is the preferred format, so use those names. // https://www.postgresql.org/docs/current/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-JSONLOG diff --git a/internal/controller/pgupgrade/pgupgrade_controller.go b/internal/controller/pgupgrade/pgupgrade_controller.go index 653ea9e55e..f5a3fc598d 100644 --- a/internal/controller/pgupgrade/pgupgrade_controller.go +++ b/internal/controller/pgupgrade/pgupgrade_controller.go @@ -440,7 +440,7 @@ func (r *PGUpgradeReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( // Set the cluster status when we know the upgrade has completed successfully. // This will serve to help the user see that the upgrade has completed if they // are only watching the PostgresCluster - patch.Status.PostgresVersion = int(upgrade.Spec.ToPostgresVersion) + patch.Status.PostgresVersion = upgrade.Spec.ToPostgresVersion // Set the pgBackRest status for bootstrapping patch.Status.PGBackRest.Repos = []v1beta1.RepoStatus{} diff --git a/internal/controller/postgrescluster/volumes.go b/internal/controller/postgrescluster/volumes.go index 93c8ded149..919633377f 100644 --- a/internal/controller/postgrescluster/volumes.go +++ b/internal/controller/postgrescluster/volumes.go @@ -7,7 +7,6 @@ package postgrescluster import ( "context" "fmt" - "strconv" "github.com/pkg/errors" batchv1 "k8s.io/api/batch/v1" @@ -476,21 +475,20 @@ func (r *Reconciler) reconcileMovePGDataDir(ctx context.Context, // `patroni.dynamic.json` holds the previous state of the DCS. Since we are // migrating the volumes, we want to clear out any obsolete configuration info. script := fmt.Sprintf(`echo "Preparing cluster %s volumes for PGO v5.x" - echo "pgdata_pvc=%s" - echo "Current PG data directory volume contents:" - ls -lh "/pgdata" - echo "Now updating PG data directory..." - [ -d "/pgdata/%s" ] && mv "/pgdata/%s" "/pgdata/pg%s_bootstrap" - rm -f "/pgdata/pg%s/patroni.dynamic.json" - echo "Updated PG data directory contents:" - ls -lh "/pgdata" - echo "PG Data directory preparation complete" - `, cluster.Name, +echo "pgdata_pvc=%s" +echo "Current PG data directory volume contents:" +ls -lh "/pgdata" +echo "Now updating PG data directory..." +[ -d "/pgdata/%s" ] && mv "/pgdata/%s" "/pgdata/pg%d_bootstrap" +rm -f "/pgdata/pg%d/patroni.dynamic.json" +echo "Updated PG data directory contents:" +ls -lh "/pgdata" +echo "PG Data directory preparation complete"`, cluster.Name, cluster.Spec.DataSource.Volumes.PGDataVolume.PVCName, cluster.Spec.DataSource.Volumes.PGDataVolume.Directory, cluster.Spec.DataSource.Volumes.PGDataVolume.Directory, - strconv.Itoa(cluster.Spec.PostgresVersion), - strconv.Itoa(cluster.Spec.PostgresVersion)) + cluster.Spec.PostgresVersion, + cluster.Spec.PostgresVersion) container := corev1.Container{ Command: []string{"bash", "-ceu", script}, @@ -596,15 +594,14 @@ func (r *Reconciler) reconcileMoveWALDir(ctx context.Context, moveDirJob.Labels = labels script := fmt.Sprintf(`echo "Preparing cluster %s volumes for PGO v5.x" - echo "pg_wal_pvc=%s" - echo "Current PG WAL directory volume contents:" - ls -lh "/pgwal" - echo "Now updating PG WAL directory..." - [ -d "/pgwal/%s" ] && mv "/pgwal/%s" "/pgwal/%s-wal" - echo "Updated PG WAL directory contents:" - ls -lh "/pgwal" - echo "PG WAL directory preparation complete" - `, cluster.Name, +echo "pg_wal_pvc=%s" +echo "Current PG WAL directory volume contents:" +ls -lh "/pgwal" +echo "Now updating PG WAL directory..." +[ -d "/pgwal/%s" ] && mv "/pgwal/%s" "/pgwal/%s-wal" +echo "Updated PG WAL directory contents:" +ls -lh "/pgwal" +echo "PG WAL directory preparation complete"`, cluster.Name, cluster.Spec.DataSource.Volumes.PGWALVolume.PVCName, cluster.Spec.DataSource.Volumes.PGWALVolume.Directory, cluster.Spec.DataSource.Volumes.PGWALVolume.Directory, @@ -714,18 +711,17 @@ func (r *Reconciler) reconcileMoveRepoDir(ctx context.Context, moveDirJob.Labels = labels script := fmt.Sprintf(`echo "Preparing cluster %s pgBackRest repo volume for PGO v5.x" - echo "repo_pvc=%s" - echo "pgbackrest directory:" - ls -lh /pgbackrest - echo "Current pgBackRest repo directory volume contents:" - ls -lh "/pgbackrest/%s" - echo "Now updating repo directory..." - [ -d "/pgbackrest/%s" ] && mv -t "/pgbackrest/" "/pgbackrest/%s/archive" - [ -d "/pgbackrest/%s" ] && mv -t "/pgbackrest/" "/pgbackrest/%s/backup" - echo "Updated /pgbackrest directory contents:" - ls -lh "/pgbackrest" - echo "Repo directory preparation complete" - `, cluster.Name, +echo "repo_pvc=%s" +echo "pgbackrest directory:" +ls -lh /pgbackrest +echo "Current pgBackRest repo directory volume contents:" +ls -lh "/pgbackrest/%s" +echo "Now updating repo directory..." +[ -d "/pgbackrest/%s" ] && mv -t "/pgbackrest/" "/pgbackrest/%s/archive" +[ -d "/pgbackrest/%s" ] && mv -t "/pgbackrest/" "/pgbackrest/%s/backup" +echo "Updated /pgbackrest directory contents:" +ls -lh "/pgbackrest" +echo "Repo directory preparation complete"`, cluster.Name, cluster.Spec.DataSource.Volumes.PGBackRestVolume.PVCName, cluster.Spec.DataSource.Volumes.PGBackRestVolume.Directory, cluster.Spec.DataSource.Volumes.PGBackRestVolume.Directory, diff --git a/internal/controller/postgrescluster/volumes_test.go b/internal/controller/postgrescluster/volumes_test.go index 2f6db31981..4404164610 100644 --- a/internal/controller/postgrescluster/volumes_test.go +++ b/internal/controller/postgrescluster/volumes_test.go @@ -754,12 +754,17 @@ containers: - command: - bash - -ceu - - "echo \"Preparing cluster testcluster volumes for PGO v5.x\"\n echo \"pgdata_pvc=testpgdata\"\n - \ echo \"Current PG data directory volume contents:\" \n ls -lh \"/pgdata\"\n - \ echo \"Now updating PG data directory...\"\n [ -d \"/pgdata/testpgdatadir\" - ] && mv \"/pgdata/testpgdatadir\" \"/pgdata/pg13_bootstrap\"\n rm -f \"/pgdata/pg13/patroni.dynamic.json\"\n - \ echo \"Updated PG data directory contents:\" \n ls -lh \"/pgdata\"\n echo - \"PG Data directory preparation complete\"\n " + - |- + echo "Preparing cluster testcluster volumes for PGO v5.x" + echo "pgdata_pvc=testpgdata" + echo "Current PG data directory volume contents:" + ls -lh "/pgdata" + echo "Now updating PG data directory..." + [ -d "/pgdata/testpgdatadir" ] && mv "/pgdata/testpgdatadir" "/pgdata/pg13_bootstrap" + rm -f "/pgdata/pg13/patroni.dynamic.json" + echo "Updated PG data directory contents:" + ls -lh "/pgdata" + echo "PG Data directory preparation complete" image: example.com/crunchy-postgres-ha:test imagePullPolicy: Always name: pgdata-move-job @@ -814,12 +819,16 @@ containers: - command: - bash - -ceu - - "echo \"Preparing cluster testcluster volumes for PGO v5.x\"\n echo \"pg_wal_pvc=testwal\"\n - \ echo \"Current PG WAL directory volume contents:\"\n ls -lh \"/pgwal\"\n - \ echo \"Now updating PG WAL directory...\"\n [ -d \"/pgwal/testwaldir\" - ] && mv \"/pgwal/testwaldir\" \"/pgwal/testcluster-wal\"\n echo \"Updated PG - WAL directory contents:\"\n ls -lh \"/pgwal\"\n echo \"PG WAL directory - preparation complete\"\n " + - |- + echo "Preparing cluster testcluster volumes for PGO v5.x" + echo "pg_wal_pvc=testwal" + echo "Current PG WAL directory volume contents:" + ls -lh "/pgwal" + echo "Now updating PG WAL directory..." + [ -d "/pgwal/testwaldir" ] && mv "/pgwal/testwaldir" "/pgwal/testcluster-wal" + echo "Updated PG WAL directory contents:" + ls -lh "/pgwal" + echo "PG WAL directory preparation complete" image: example.com/crunchy-postgres-ha:test imagePullPolicy: Always name: pgwal-move-job @@ -874,14 +883,19 @@ containers: - command: - bash - -ceu - - "echo \"Preparing cluster testcluster pgBackRest repo volume for PGO v5.x\"\n - \ echo \"repo_pvc=testrepo\"\n echo \"pgbackrest directory:\"\n ls -lh - /pgbackrest\n echo \"Current pgBackRest repo directory volume contents:\" \n - \ ls -lh \"/pgbackrest/testrepodir\"\n echo \"Now updating repo directory...\"\n - \ [ -d \"/pgbackrest/testrepodir\" ] && mv -t \"/pgbackrest/\" \"/pgbackrest/testrepodir/archive\"\n - \ [ -d \"/pgbackrest/testrepodir\" ] && mv -t \"/pgbackrest/\" \"/pgbackrest/testrepodir/backup\"\n - \ echo \"Updated /pgbackrest directory contents:\"\n ls -lh \"/pgbackrest\"\n - \ echo \"Repo directory preparation complete\"\n " + - |- + echo "Preparing cluster testcluster pgBackRest repo volume for PGO v5.x" + echo "repo_pvc=testrepo" + echo "pgbackrest directory:" + ls -lh /pgbackrest + echo "Current pgBackRest repo directory volume contents:" + ls -lh "/pgbackrest/testrepodir" + echo "Now updating repo directory..." + [ -d "/pgbackrest/testrepodir" ] && mv -t "/pgbackrest/" "/pgbackrest/testrepodir/archive" + [ -d "/pgbackrest/testrepodir" ] && mv -t "/pgbackrest/" "/pgbackrest/testrepodir/backup" + echo "Updated /pgbackrest directory contents:" + ls -lh "/pgbackrest" + echo "Repo directory preparation complete" image: example.com/crunchy-pgbackrest:test imagePullPolicy: Always name: repo-move-job diff --git a/internal/pgbackrest/config.go b/internal/pgbackrest/config.go index f1d1fc30f8..8083540072 100644 --- a/internal/pgbackrest/config.go +++ b/internal/pgbackrest/config.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "path" - "strconv" "strings" "time" @@ -109,7 +108,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet populatePGInstanceConfigurationMap( serviceName, serviceNamespace, repoHostName, pgdataDir, config.FetchKeyCommand(&postgresCluster.Spec), - strconv.Itoa(postgresCluster.Spec.PostgresVersion), + fmt.Sprint(postgresCluster.Spec.PostgresVersion), pgPort, postgresCluster.Spec.Backups.PGBackRest.Repos, postgresCluster.Spec.Backups.PGBackRest.Global, util.GetPGBackRestLogPathForInstance(postgresCluster), @@ -130,7 +129,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet populateRepoHostConfigurationMap( serviceName, serviceNamespace, pgdataDir, config.FetchKeyCommand(&postgresCluster.Spec), - strconv.Itoa(postgresCluster.Spec.PostgresVersion), + fmt.Sprint(postgresCluster.Spec.PostgresVersion), pgPort, instanceNames, postgresCluster.Spec.Backups.PGBackRest.Repos, postgresCluster.Spec.Backups.PGBackRest.Global, @@ -161,7 +160,7 @@ func CreatePGBackRestConfigMapIntent(ctx context.Context, postgresCluster *v1bet populateCloudRepoConfigurationMap( serviceName, serviceNamespace, pgdataDir, config.FetchKeyCommand(&postgresCluster.Spec), - strconv.Itoa(postgresCluster.Spec.PostgresVersion), + fmt.Sprint(postgresCluster.Spec.PostgresVersion), cloudLogPath, pgPort, instanceNames, postgresCluster.Spec.Backups.PGBackRest.Repos, postgresCluster.Spec.Backups.PGBackRest.Global, diff --git a/internal/postgres/config_test.go b/internal/postgres/config_test.go index 53bcfc1bf1..ec6a374e22 100644 --- a/internal/postgres/config_test.go +++ b/internal/postgres/config_test.go @@ -42,7 +42,7 @@ func TestDataDirectory(t *testing.T) { func TestDataStorage(t *testing.T) { cluster := new(v1beta1.PostgresCluster) - cluster.Spec.PostgresVersion = rand.IntN(20) + cluster.Spec.PostgresVersion = rand.Int32N(20) assert.Equal(t, DataStorage(cluster), "/pgdata") } diff --git a/internal/postgres/versions.go b/internal/postgres/versions.go index 17d067966d..bf700d9729 100644 --- a/internal/postgres/versions.go +++ b/internal/postgres/versions.go @@ -20,7 +20,7 @@ var finalReleaseDates = map[int]time.Time{ // ReleaseIsFinal returns whether or not t is definitively past the final // scheduled release of a Postgres version. -func ReleaseIsFinal(majorVersion int, t time.Time) bool { - known, ok := finalReleaseDates[majorVersion] +func ReleaseIsFinal[N ~int | ~int32](majorVersion N, t time.Time) bool { + known, ok := finalReleaseDates[int(majorVersion)] return ok && t.After(known) } diff --git a/pkg/apis/postgres-operator.crunchydata.com/v1/postgrescluster_types.go b/pkg/apis/postgres-operator.crunchydata.com/v1/postgrescluster_types.go index 36177d1e44..8ae03bf25e 100644 --- a/pkg/apis/postgres-operator.crunchydata.com/v1/postgrescluster_types.go +++ b/pkg/apis/postgres-operator.crunchydata.com/v1/postgrescluster_types.go @@ -144,7 +144,7 @@ type PostgresClusterSpec struct { // +kubebuilder:validation:Minimum=11 // +kubebuilder:validation:Maximum=17 // +operator-sdk:csv:customresourcedefinitions:type=spec,order=1 - PostgresVersion int `json:"postgresVersion"` + PostgresVersion int32 `json:"postgresVersion"` // The PostGIS extension version installed in the PostgreSQL image. // When image is not set, indicates a PostGIS enabled image will be used. @@ -391,7 +391,7 @@ type PostgresClusterStatus struct { // Stores the current PostgreSQL major version following a successful // major PostgreSQL upgrade. // +optional - PostgresVersion int `json:"postgresVersion"` + PostgresVersion int32 `json:"postgresVersion"` // Current state of the PostgreSQL proxy. // +optional diff --git a/pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go b/pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go index 60a65d3231..88c16b9aff 100644 --- a/pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go +++ b/pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go @@ -129,7 +129,7 @@ type PostgresClusterSpec struct { // +kubebuilder:validation:Minimum=11 // +kubebuilder:validation:Maximum=17 // +operator-sdk:csv:customresourcedefinitions:type=spec,order=1 - PostgresVersion int `json:"postgresVersion"` + PostgresVersion int32 `json:"postgresVersion"` // The PostGIS extension version installed in the PostgreSQL image. // When image is not set, indicates a PostGIS enabled image will be used. @@ -375,7 +375,7 @@ type PostgresClusterStatus struct { // Stores the current PostgreSQL major version following a successful // major PostgreSQL upgrade. // +optional - PostgresVersion int `json:"postgresVersion"` + PostgresVersion int32 `json:"postgresVersion"` // Current state of the PostgreSQL proxy. // +optional