Skip to content

Set dockerImage in CR #603

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

Merged
merged 1 commit into from
Jun 13, 2025
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
5 changes: 4 additions & 1 deletion api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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

Expand Down
38 changes: 36 additions & 2 deletions api/v1/postgres_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
pgParamBlockList map[string]bool
rbs *BackupConfig
srcDB *Postgres
image string
want string
wantErr bool
}{
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down Expand Up @@ -311,6 +314,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
Description: "description",
},
},
image: "image:tag",
want: "invalid but whatever",
wantErr: false,
},
Expand Down Expand Up @@ -339,17 +343,47 @@ 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!
t.Run(tt.name, func(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 {
Expand All @@ -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)
}
})
Expand Down
4 changes: 2 additions & 2 deletions controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down