diff --git a/api/v1/postgres_types.go b/api/v1/postgres_types.go index 212a1c8c..a479aafc 100644 --- a/api/v1/postgres_types.go +++ b/api/v1/postgres_types.go @@ -668,7 +668,7 @@ func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName { } } -func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32, dboIsSuperuser bool, enableTlsCert bool) (*unstructured.Unstructured, error) { +func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *corev1.ConfigMap, sc string, pgParamBlockList map[string]bool, rbs *BackupConfig, srcDB *Postgres, patroniTTL, patroniLoopWait, patroniRetryTimeout uint32, dboIsSuperuser bool, enableTlsCert bool, image string) (*unstructured.Unstructured, error) { if z == nil { z = &zalando.Postgresql{} } @@ -680,6 +680,9 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor // TODO once all the custom resources have that new label, move this part to p.ToZalandoPostgresqlMatchingLabels() z.Labels[PartitionIDLabelName] = p.Spec.PartitionID + if image != "" { + z.Spec.DockerImage = image + } z.Spec.NumberOfInstances = p.Spec.NumberOfInstances z.Spec.PostgresqlParam.PgVersion = p.Spec.Version diff --git a/api/v1/postgres_types_test.go b/api/v1/postgres_types_test.go index ba3a92b3..3e2b6a82 100644 --- a/api/v1/postgres_types_test.go +++ b/api/v1/postgres_types_test.go @@ -229,6 +229,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) pgParamBlockList map[string]bool rbs *BackupConfig srcDB *Postgres + image string want string wantErr bool }{ @@ -257,6 +258,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) Description: "description", }, }, + image: "image:tag", want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second... wantErr: false, }, @@ -283,6 +285,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) Description: "description", }, }, + image: "image:tag", want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second... wantErr: false, }, @@ -311,6 +314,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) Description: "description", }, }, + image: "image:tag", want: "invalid but whatever", wantErr: false, }, @@ -339,9 +343,39 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) Description: "description", }, }, + image: "image:tag", want: "oranges", wantErr: true, }, + { + name: "no image", + spec: PostgresSpec{ + Size: &Size{ + CPU: "1", + Memory: "4Gi", + SharedBuffer: "64Mi", + }, + PostgresRestore: &PostgresRestore{ + Timestamp: "apples", + }, + }, + c: nil, + sc: "fake-storage-class", + pgParamBlockList: map[string]bool{}, + rbs: &BackupConfig{}, + srcDB: &Postgres{ + ObjectMeta: v1.ObjectMeta{ + Name: uuid.NewString(), + }, + Spec: PostgresSpec{ + Tenant: "tenant", + Description: "description", + }, + }, + image: "", + want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second... + wantErr: false, + }, } for _, tt := range tests { tt := tt // pin! @@ -349,7 +383,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) p := &Postgres{ Spec: tt.spec, } - got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false, false) + got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false, false, "dockerImage") jsonZ, err := runtime.DefaultUnstructuredConverter.ToUnstructured(got) if err != nil { @@ -358,7 +392,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T) jsonSpec, _ := jsonZ["spec"].(map[string]interface{}) jsonClone, _ := jsonSpec["clone"].(map[string]interface{}) - if !tt.wantErr && tt.want != jsonClone["timestamp"] { + if !tt.wantErr && tt.want != jsonClone["timestamp"] && tt.image == jsonSpec["dockerImage"] { t.Errorf("Spec.Clone.Timestamp was %v, but expected %v", jsonClone["timestamp"], tt.want) } }) diff --git a/controllers/postgres_controller.go b/controllers/postgres_controller.go index d2893b9c..fef3a2b9 100644 --- a/controllers/postgres_controller.go +++ b/controllers/postgres_controller.go @@ -441,7 +441,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context return fmt.Errorf("failed to fetch zalando postgresql: %w", err) } - u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert) + u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage) if err != nil { return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err) } @@ -457,7 +457,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context // Update zalando postgresql mergeFrom := client.MergeFrom(rawZ.DeepCopy()) - u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert) + u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage) if err != nil { return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err) }