Skip to content

Commit e978ed7

Browse files
authored
fix(postgres): stop backup jobs from fighting over the WAL archive destination (#5221)
## What this PR does Part of #5202. A second PR will restore the last good destination when a job that changed it fails. The ObjectStore a Postgres Cluster archives its WAL to is shared by every backup of the application, yet every running BackupJob server-side applied its own strategy's destination onto it every five seconds with forced ownership. Two BackupJobs of one application with different BackupClasses flipped the destination back and forth until the healthy backup failed too, and a webhook or apiserver blip during that apply failed the BackupJob at once. - A BackupJob writes the destination and the Cluster plugin only before its `cnpg.io/Backup` exists. Afterwards it only reads the live Cluster, and a failed read is returned for a controller retry instead of failing the running backup. - Before writing, a BackupJob waits, with a `Ready=False` / `WaitingForOtherBackup` condition naming the other job, while another running BackupJob of the application has a `cnpg.io/Backup` in flight. The wait is not capped, for the same reason as in #5211. The queue reads the Cluster's own Backups from the apiserver; with leader election one reconcile runs at a time, so only a leader handover can still let two jobs through. An atomic per-application claim is #4559. - Before writing, a BackupJob also checks the apiserver for a restore of its application and holds while one runs, including a job released from the queue: a write landing during the restore would put the restored Cluster back on the source's WAL prefix. - Write errors that cannot succeed on retry (invalid, bad request, forbidden) still fail the job at once. Any other is retried with a `ClusterPluginApplyRetrying` condition for the backup deadline, counted from the first failure since the job last waited, and recorded in a new `DestinationApplied` condition. - The pass that creates the `cnpg.io/Backup` records on it the destination, endpoint, credentials Secret and serverName it wrote (`backups.cozystack.io/cnpg-written-destination`), and the artifact and its size probe are built from that record, so a strategy edited, or a Cluster re-rendered, while the backup runs does not redirect the artifact. A Backup without the record falls back to the strategy as before. - The plugin rollout deadline and the instance check count from the write that attached the plugin, not from the job's start. The apply that attaches it also stamps the time on the Cluster (`backups.cozystack.io/barman-plugin-attached-at`), so a lost BackupJob status write after the attach does not fall back to an older time, and a job that starts while another job's recent attach is still rolling waits for that rollout too. A stamp a few minutes ahead of the controller's clock is waited out; one further ahead is treated as corrupt. Tests drive the reconciler through a running backup that must not rewrite the destination, the queue and its release, a restore starting while a job waits, each class of write error, a recovered write, a failed Cluster read while the Backup runs, lost status writes around the attach, another job's rollout, clock drift on the attach stamp, the recorded destination and serverName, and the rollout deadline. Each guard was checked by mutation. ### Screenshots <!-- REQUIRED for UI changes: attach screenshots or screen recordings demonstrating the visual impact of your changes. PRs with UI changes without screenshots will not be merged. --> ### Downstream repositories <!-- Cozystack is upstream for repositories that are NOT kept in sync with it automatically, and nothing in CI compares the two sides, so a change here can break them silently. Which change forces what, and which file to touch, is the trigger map in docs/agents/contributing.md under "Downstream Repositories". Walk it before you tick anything. IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM TO FILL IN: 1. Walk the trigger map against the actual diff of this PR, file by file. Do not decide from the PR title or from your memory of what you changed. 2. For every repository you tick, open the follow-up PR there and link it on the same line. A ticked box with no link claims work that does not exist, which is worse than an empty box. Search that repository for an open PR or issue covering it first, and link that instead of filing a duplicate. 3. When the follow-up is out of scope, or needs a decision that is not yours to make, open an issue in that repository instead, link that, and say so in the PR body. Do not push a speculative PR into another repository just to fill a line here. 4. Do not tick "No downstream repository is affected" to make the checklist go away. If you are unsure, leave every box empty and say so in the PR body, so a human decides. --> - [x] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note <!-- Write a release note: - Explain what has changed internally and for users. - Start with the same `type(scope):` prefix as in the PR title - Follow the guidelines at https://github.com/kubernetes/community/blob/master/contributors/guide/release-notes.md. --> ```release-note fix(postgres): Postgres BackupJobs of the same application no longer overwrite each other's WAL archive destination: a job writes it only before its backup starts and waits while another backup or a restore of the application is in flight. A transient error while writing it is retried instead of failing the job at once, and the backup artifact names the destination and serverName the backup was actually written to. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Reliability** * CNPG backups wait while another backup or restore is using the shared destination, and retry temporary destination-write failures while observing the backup deadline. * Backup artifacts and size checks use the destination recorded when the backup was created, even if destination settings later change. * Backup processing handles plugin rollout timing and destination-hold release more consistently. * **Restore** * Restore operations account for shared WAL archive destination conflicts and guard against incompatible backup selections. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 2cf80c0 + 5c6fc10 commit e978ed7

5 files changed

Lines changed: 2960 additions & 91 deletions

File tree

‎internal/backupcontroller/backupjob_controller.go‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,14 +315,6 @@ func (r *BackupJobReconciler) handleProjectionError(ctx context.Context, j *back
315315
}
316316

317317
// markBackupJobFailed records a terminal Failed phase on the BackupJob.
318-
//
319-
// Coupling note: a failure that fires before reconcileCNPG's StartedAt block
320-
// has set backupJob.Status.StartedAt leaves StartedAt nil. The CNPG path's
321-
// cnpgDefaultBackupDeadline check only fires once StartedAt is set, so an
322-
// early-failure retry that reaches the StartedAt block restarts the deadline
323-
// budget from the retry's StartedAt - intentional, since retries are
324-
// user-driven and a fresh budget is what users expect after correcting the
325-
// cause of the previous failure.
326318
func (r *BackupJobReconciler) markBackupJobFailed(ctx context.Context, backupJob *backupsv1alpha1.BackupJob, message string) (ctrl.Result, error) {
327319
logger := getLogger(ctx)
328320
now := metav1.Now()

‎internal/backupcontroller/backupjob_restore_lock.go‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ const ConditionReasonRestoreInProgress = "RestoreInProgress"
3232
//
3333
// A backup must not start against an application a restore is rewriting. It
3434
// would capture a half-restored state at best, and on the CNPG path it does
35-
// worse: every BackupJob reconcile re-applies the Cluster's barman plugin, and
36-
// one landing between the restore re-rendering the Cluster with its new
37-
// serverName and the plugin being attached can put the restored cluster back
38-
// on the source's WAL prefix, where its recovery fails with "Expected empty
39-
// archive". Plan-created runs are the ones that hit this, since nothing else
40-
// looks at restores before creating a BackupJob.
41-
func activeRestoreTargeting(ctx context.Context, c client.Client, namespace string, app corev1.TypedLocalObjectReference) (string, error) {
35+
// worse: a BackupJob applies the Cluster's barman plugin before it creates
36+
// its Backup, and an apply landing between the restore re-rendering the
37+
// Cluster with its new serverName and the plugin being attached can put the
38+
// restored cluster back on the source's WAL prefix, where its recovery fails
39+
// with "Expected empty archive". Plan-created runs are the ones that hit
40+
// this, since nothing else looks at restores before creating a BackupJob.
41+
func activeRestoreTargeting(ctx context.Context, c client.Reader, namespace string, app corev1.TypedLocalObjectReference) (string, error) {
4242
app = NormalizeApplicationRef(app)
4343
list := &backupsv1alpha1.RestoreJobList{}
4444
if err := c.List(ctx, list, client.InNamespace(namespace)); err != nil {
@@ -80,7 +80,7 @@ func activeRestoreTargeting(ctx context.Context, c client.Client, namespace stri
8080
// reads options.targetNamespace, so the option is looked at only when the
8181
// Backup's strategy is Velero — the CNPG and Job-based drivers ignore it and
8282
// restore in place.
83-
func restoreJobTarget(ctx context.Context, c client.Client, rj *backupsv1alpha1.RestoreJob) (corev1.TypedLocalObjectReference, bool, error) {
83+
func restoreJobTarget(ctx context.Context, c client.Reader, rj *backupsv1alpha1.RestoreJob) (corev1.TypedLocalObjectReference, bool, error) {
8484
backup := &backupsv1alpha1.Backup{}
8585
if err := c.Get(ctx, client.ObjectKey{Namespace: rj.Namespace, Name: rj.Spec.BackupRef.Name}, backup); err != nil {
8686
if client.IgnoreNotFound(err) == nil {
@@ -130,13 +130,14 @@ func ptrString(s *string) string {
130130
return *s
131131
}
132132

133-
// holdForRestore keeps a BackupJob that has not started yet waiting while a
134-
// restore writes into its application. It stays non-terminal, so a scheduled
135-
// run still happens once the restore ends, only later than its slot.
133+
// holdForRestore keeps a BackupJob waiting while a restore writes into its
134+
// application: one that has not started yet, or a CNPG one about to write
135+
// its WAL archive destination. It stays non-terminal, so a scheduled run
136+
// still happens once the restore ends, only later than its slot.
136137
//
137138
// A failed status write is returned so the manager retries it with backoff.
138-
// The job is held either way, since nothing past the gate ran, but a hold
139-
// nobody can see reads as a run stuck for no reason.
139+
// The job is held either way, since it writes nothing past the hold, but a
140+
// hold nobody can see reads as a run stuck for no reason.
140141
func (r *BackupJobReconciler) holdForRestore(ctx context.Context, j *backupsv1alpha1.BackupJob, restore string) (ctrl.Result, error) {
141142
if j.Status.Phase == backupsv1alpha1.BackupJobPhaseEmpty {
142143
j.Status.Phase = backupsv1alpha1.BackupJobPhasePending

‎internal/backupcontroller/cnpg_artifact_size_test.go‎

Lines changed: 130 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func TestCreateCNPGBackupArtifact_SizeBytes(t *testing.T) {
284284
r := reconcilerWithLister(t, l)
285285
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", "creds"))
286286

287-
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
287+
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
288288
if err != nil {
289289
t.Fatalf("createCNPGBackupArtifact: %v", err)
290290
}
@@ -311,7 +311,7 @@ func TestCreateCNPGBackupArtifact_ExistingBackupSkipsProbe(t *testing.T) {
311311
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
312312
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
313313

314-
if _, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant")); err != nil {
314+
if _, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant")); err != nil {
315315
t.Fatalf("createCNPGBackupArtifact: %v", err)
316316
}
317317
if l.calls != 0 {
@@ -331,7 +331,7 @@ func TestCreateCNPGBackupArtifact_AlreadyExistsRefetchReadsPastTheCache(t *testi
331331
store := newCNPGStrategyTestClient(t, backup)
332332
r := &BackupJobReconciler{Client: staleCache(t, store), APIReader: notFoundOnce(t, store)}
333333
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://b/"}}
334-
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
334+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
335335
if err != nil || got == nil || got.Name != "bj-race" {
336336
t.Fatalf("createCNPGBackupArtifact = %v, %v; want the Backup that already exists", got, err)
337337
}
@@ -349,7 +349,7 @@ func TestCreateCNPGBackupArtifact_UnreadableExistingBackupIsRetried(t *testing.T
349349
store := newCNPGStrategyTestClient(t, backup)
350350
r := &BackupJobReconciler{Client: staleCache(t, store), APIReader: failingReads(t, store)}
351351
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://b/"}}
352-
_, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
352+
_, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
353353
if !errors.Is(err, errBackupNotReadBack) {
354354
t.Errorf("err = %v, want errBackupNotReadBack", err)
355355
}
@@ -409,7 +409,7 @@ func TestCreateCNPGBackupArtifact_FailedExistenceCheckSkipsProbe(t *testing.T) {
409409
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
410410
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
411411

412-
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
412+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
413413
if err != nil || got == nil {
414414
t.Fatalf("createCNPGBackupArtifact = %v, %v; a transient read must not fail the backup", got, err)
415415
}
@@ -451,3 +451,128 @@ func TestReconcileCNPG_RejectedCreateFailsTheJob(t *testing.T) {
451451
t.Errorf("phase = %q, want Failed for a rejected Create", persisted.Status.Phase)
452452
}
453453
}
454+
455+
// The probe measures the base backup where it was written: the destination
456+
// recorded on the cnpg.io/Backup when it was created, not the strategy as
457+
// it reads by the time the backup completes.
458+
func TestCreateCNPGBackupArtifact_SizeProbeReadsTheRecordedDestination(t *testing.T) {
459+
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
460+
j := &backupsv1alpha1.BackupJob{
461+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-moved"},
462+
Spec: backupsv1alpha1.BackupJobSpec{
463+
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
464+
},
465+
}
466+
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
467+
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
468+
oldCreds := "creds-before-edit"
469+
written := cnpgDestination{DestinationPath: "s3://old-bkt/before/edit/", EndpointURL: "https://old.example:9000", S3SecretRef: &oldCreds}
470+
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "before/edit/srv/base/20261021T100000/data.tar.gz", Size: 42}}}
471+
r := reconcilerWithLister(t, l)
472+
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", oldCreds))
473+
474+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
475+
if err != nil {
476+
t.Fatalf("createCNPGBackupArtifact: %v", err)
477+
}
478+
if l.bucket != "old-bkt" || l.prefix != "before/edit/srv/base/20261021T100000/" {
479+
t.Errorf("listed %s/%s, want the recorded destination old-bkt/before/edit/srv/base/20261021T100000/", l.bucket, l.prefix)
480+
}
481+
if l.conn.Endpoint != "old.example:9000" {
482+
t.Errorf("probed endpoint %q, want the recorded old.example:9000", l.conn.Endpoint)
483+
}
484+
if got.Status.Artifact == nil || got.Status.Artifact.SizeBytes != 42 {
485+
t.Errorf("Artifact = %#v, want SizeBytes 42", got.Status.Artifact)
486+
}
487+
}
488+
489+
// A backup recorded without a credentials Secret is not measured with the
490+
// Secret the strategy names now: that Secret belongs to another location.
491+
func TestCreateCNPGBackupArtifact_SizeProbeSkipsARecordWithoutCredentials(t *testing.T) {
492+
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
493+
j := &backupsv1alpha1.BackupJob{
494+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-nocreds"},
495+
Spec: backupsv1alpha1.BackupJobSpec{
496+
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
497+
},
498+
}
499+
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
500+
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
501+
written := cnpgDestination{DestinationPath: "s3://bkt/tenant/pg/", EndpointURL: "https://s3.example:8443"}
502+
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "x", Size: 1}}}
503+
r := reconcilerWithLister(t, l)
504+
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", "creds"))
505+
506+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
507+
if err != nil {
508+
t.Fatalf("createCNPGBackupArtifact: %v", err)
509+
}
510+
if l.calls != 0 {
511+
t.Errorf("S3 listed %d times with the strategy's Secret for a backup recorded without one", l.calls)
512+
}
513+
if got.Status.Artifact == nil || got.Status.Artifact.SizeBytes != 0 {
514+
t.Errorf("Artifact = %#v, want no size", got.Status.Artifact)
515+
}
516+
}
517+
518+
// probeRecordedBackup runs createCNPGBackupArtifact for a backup recorded as
519+
// written with credentials Secret recordedSecret, against rendered, and
520+
// returns the size it measured and the connection it measured through.
521+
func probeRecordedBackup(t *testing.T, rendered *strategyv1alpha1.CNPGTemplate, recordedSecret *corev1.Secret) (int64, s3Connection) {
522+
t.Helper()
523+
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
524+
j := &backupsv1alpha1.BackupJob{
525+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-probe"},
526+
Spec: backupsv1alpha1.BackupJobSpec{
527+
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
528+
},
529+
}
530+
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
531+
secretName := recordedSecret.Name
532+
written := cnpgDestination{DestinationPath: "s3://bkt/tenant/pg/", EndpointURL: "https://s3.example:8443", S3SecretRef: &secretName}
533+
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "tenant/pg/srv/base/20261021T100000/data.tar.gz", Size: 42}}}
534+
r := reconcilerWithLister(t, l)
535+
r.Client = newCNPGStrategyTestClient(t, recordedSecret)
536+
537+
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
538+
if err != nil {
539+
t.Fatalf("createCNPGBackupArtifact: %v", err)
540+
}
541+
if got.Status.Artifact == nil {
542+
t.Fatalf("no artifact")
543+
}
544+
return got.Status.Artifact.SizeBytes, l.conn
545+
}
546+
547+
// A strategy that has since dropped its credentials still measures a backup
548+
// recorded with a Secret, through that Secret and its default keys.
549+
func TestCreateCNPGBackupArtifact_SizeProbeOfARecordedSecretUnderAStrategyWithoutCredentials(t *testing.T) {
550+
store := newSizedBarmanObjectStore()
551+
store.S3Credentials = nil
552+
size, conn := probeRecordedBackup(t, &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *store}, s3CredsSecret("tenant", "creds-recorded"))
553+
if size != 42 {
554+
t.Errorf("SizeBytes = %d, want 42 measured through the recorded Secret", size)
555+
}
556+
if conn.AccessKey != "AKID" {
557+
t.Errorf("probed with access key %q, want the recorded Secret's AKID", conn.AccessKey)
558+
}
559+
}
560+
561+
// The record names the Secret, not its keys: the strategy's key names stay,
562+
// so a strategy with its own key names still reads the recorded Secret.
563+
func TestCreateCNPGBackupArtifact_SizeProbeKeepsTheStrategysSecretKeys(t *testing.T) {
564+
store := newSizedBarmanObjectStore()
565+
store.S3Credentials.AccessKeyIDKey = "id"
566+
store.S3Credentials.SecretAccessKeyKey = "key"
567+
secret := &corev1.Secret{
568+
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "creds-recorded"},
569+
Data: map[string][]byte{"id": []byte("CUSTOMID"), "key": []byte("custom-secret")},
570+
}
571+
size, conn := probeRecordedBackup(t, &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *store}, secret)
572+
if size != 42 {
573+
t.Errorf("SizeBytes = %d, want 42 measured with the strategy's key names", size)
574+
}
575+
if conn.AccessKey != "CUSTOMID" {
576+
t.Errorf("probed with access key %q, want CUSTOMID read under the strategy's key name", conn.AccessKey)
577+
}
578+
}

0 commit comments

Comments
 (0)