Skip to content

Commit b3a5305

Browse files
committed
Set dockerImage in CR (to prevent rolling pods when only updating the opreator)
1 parent 6e5cca4 commit b3a5305

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

api/v1/postgres_types.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func (p *Postgres) ToPeripheralResourceLookupKey() types.NamespacedName {
668668
}
669669
}
670670

671-
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) {
671+
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) {
672672
if z == nil {
673673
z = &zalando.Postgresql{}
674674
}
@@ -680,6 +680,9 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
680680
// TODO once all the custom resources have that new label, move this part to p.ToZalandoPostgresqlMatchingLabels()
681681
z.Labels[PartitionIDLabelName] = p.Spec.PartitionID
682682

683+
if image != "" {
684+
z.Spec.DockerImage = image
685+
}
683686
z.Spec.NumberOfInstances = p.Spec.NumberOfInstances
684687
z.Spec.PostgresqlParam.PgVersion = p.Spec.Version
685688

api/v1/postgres_types_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
229229
pgParamBlockList map[string]bool
230230
rbs *BackupConfig
231231
srcDB *Postgres
232+
image string
232233
want string
233234
wantErr bool
234235
}{
@@ -257,6 +258,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
257258
Description: "description",
258259
},
259260
},
261+
image: "image:tag",
260262
want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second...
261263
wantErr: false,
262264
},
@@ -283,6 +285,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
283285
Description: "description",
284286
},
285287
},
288+
image: "image:tag",
286289
want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second...
287290
wantErr: false,
288291
},
@@ -311,6 +314,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
311314
Description: "description",
312315
},
313316
},
317+
image: "image:tag",
314318
want: "invalid but whatever",
315319
wantErr: false,
316320
},
@@ -339,17 +343,47 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
339343
Description: "description",
340344
},
341345
},
346+
image: "image:tag",
342347
want: "oranges",
343348
wantErr: true,
344349
},
350+
{
351+
name: "no image",
352+
spec: PostgresSpec{
353+
Size: &Size{
354+
CPU: "1",
355+
Memory: "4Gi",
356+
SharedBuffer: "64Mi",
357+
},
358+
PostgresRestore: &PostgresRestore{
359+
Timestamp: "apples",
360+
},
361+
},
362+
c: nil,
363+
sc: "fake-storage-class",
364+
pgParamBlockList: map[string]bool{},
365+
rbs: &BackupConfig{},
366+
srcDB: &Postgres{
367+
ObjectMeta: v1.ObjectMeta{
368+
Name: uuid.NewString(),
369+
},
370+
Spec: PostgresSpec{
371+
Tenant: "tenant",
372+
Description: "description",
373+
},
374+
},
375+
image: "",
376+
want: time.Now().Format(zalando_timestamp_format), // I know this is not perfect, let's just hope we always finish within the same second...
377+
wantErr: false,
378+
},
345379
}
346380
for _, tt := range tests {
347381
tt := tt // pin!
348382
t.Run(tt.name, func(t *testing.T) {
349383
p := &Postgres{
350384
Spec: tt.spec,
351385
}
352-
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false, false)
386+
got, _ := p.ToUnstructuredZalandoPostgresql(nil, tt.c, tt.sc, tt.pgParamBlockList, tt.rbs, tt.srcDB, 130, 10, 60, false, false, "dockerImage")
353387

354388
jsonZ, err := runtime.DefaultUnstructuredConverter.ToUnstructured(got)
355389
if err != nil {
@@ -358,7 +392,7 @@ func TestPostgresRestoreTimestamp_ToUnstructuredZalandoPostgresql(t *testing.T)
358392
jsonSpec, _ := jsonZ["spec"].(map[string]interface{})
359393
jsonClone, _ := jsonSpec["clone"].(map[string]interface{})
360394

361-
if !tt.wantErr && tt.want != jsonClone["timestamp"] {
395+
if !tt.wantErr && tt.want != jsonClone["timestamp"] && tt.image == jsonSpec["dockerImage"] {
362396
t.Errorf("Spec.Clone.Timestamp was %v, but expected %v", jsonClone["timestamp"], tt.want)
363397
}
364398
})

controllers/postgres_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
441441
return fmt.Errorf("failed to fetch zalando postgresql: %w", err)
442442
}
443443

444-
u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert)
444+
u, err := instance.ToUnstructuredZalandoPostgresql(nil, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage)
445445
if err != nil {
446446
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
447447
}
@@ -457,7 +457,7 @@ func (r *PostgresReconciler) createOrUpdateZalandoPostgresql(ctx context.Context
457457
// Update zalando postgresql
458458
mergeFrom := client.MergeFrom(rawZ.DeepCopy())
459459

460-
u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert)
460+
u, err := instance.ToUnstructuredZalandoPostgresql(rawZ, sidecarsCM, r.StorageClass, r.PgParamBlockList, restoreBackupConfig, restoreSourceInstance, patroniTTL, patroniLoopWait, patroniRetryTimeout, r.EnableSuperUserForDBO, r.EnableCustomTLSCert, r.PostgresImage)
461461
if err != nil {
462462
return fmt.Errorf("failed to convert to unstructured zalando postgresql: %w", err)
463463
}

0 commit comments

Comments
 (0)