mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-21 12:13:55 +09:00
fix(postgres): build a backup's artifact from the destination it was written to
The WAL archive destination is written by the pass that creates the cnpg.io/Backup and not again while it runs, yet the artifact took the destination, the endpoint and the credentials Secret from the strategy rendered when the Backup completed. A strategy or BackupClass edited while the backup ran gave an artifact that pointed somewhere nothing had been written, and a restore from it found no data. The pass that writes the destination now records it on the cnpg.io/Backup it creates, and the artifact is built from that record. The record lives on the Backup rather than on the BackupJob because a tenant can edit its BackupJobs but has no access to postgresql.cnpg.io objects: a record a tenant could rewrite would point the artifact, and a restore from it, which takes its S3 keys from the strategy, at an endpoint of the tenant's choosing. The artifact size probe measures the same place: the recorded location and credentials Secret, with the Secret keys and the endpoint CA, which the record does not carry, from the strategy. Should those no longer fit after an edit, the probe reports no size instead of measuring somewhere else. A record that cannot be read, or that names no destination path, creates no artifact and fails the job, since the strategy may no longer match the write. A Backup without a record was created by a controller that kept none, and its artifact is built from the strategy as before. Assisted-by: LLM Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
1 parent
258c5359b6
commit
a6af14ce74
3 files changed
+323
-16
No files matched your search
@@ -284,7 +284,7 @@ func TestCreateCNPGBackupArtifact_SizeBytes(t *testing.T) {
|
||||
r := reconcilerWithLister(t, l)
|
||||
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", "creds"))
|
||||
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
|
||||
if err != nil {
|
||||
t.Fatalf("createCNPGBackupArtifact: %v", err)
|
||||
}
|
||||
@@ -311,7 +311,7 @@ func TestCreateCNPGBackupArtifact_ExistingBackupSkipsProbe(t *testing.T) {
|
||||
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
|
||||
|
||||
if _, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant")); err != nil {
|
||||
if _, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant")); err != nil {
|
||||
t.Fatalf("createCNPGBackupArtifact: %v", err)
|
||||
}
|
||||
if l.calls != 0 {
|
||||
@@ -331,7 +331,7 @@ func TestCreateCNPGBackupArtifact_AlreadyExistsRefetchReadsPastTheCache(t *testi
|
||||
store := newCNPGStrategyTestClient(t, backup)
|
||||
r := &BackupJobReconciler{Client: staleCache(t, store), APIReader: notFoundOnce(t, store)}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://b/"}}
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
|
||||
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"))
|
||||
if err != nil || got == nil || got.Name != "bj-race" {
|
||||
t.Fatalf("createCNPGBackupArtifact = %v, %v; want the Backup that already exists", got, err)
|
||||
}
|
||||
@@ -349,7 +349,7 @@ func TestCreateCNPGBackupArtifact_UnreadableExistingBackupIsRetried(t *testing.T
|
||||
store := newCNPGStrategyTestClient(t, backup)
|
||||
r := &BackupJobReconciler{Client: staleCache(t, store), APIReader: failingReads(t, store)}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://b/"}}
|
||||
_, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}}, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
|
||||
_, 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"))
|
||||
if !errors.Is(err, errBackupNotReadBack) {
|
||||
t.Errorf("err = %v, want errBackupNotReadBack", err)
|
||||
}
|
||||
@@ -409,7 +409,7 @@ func TestCreateCNPGBackupArtifact_FailedExistenceCheckSkipsProbe(t *testing.T) {
|
||||
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
|
||||
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", rendered, newPostgresApp("pg", "tenant"))
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
|
||||
if err != nil || got == nil {
|
||||
t.Fatalf("createCNPGBackupArtifact = %v, %v; a transient read must not fail the backup", got, err)
|
||||
}
|
||||
@@ -451,3 +451,66 @@ func TestReconcileCNPG_RejectedCreateFailsTheJob(t *testing.T) {
|
||||
t.Errorf("phase = %q, want Failed for a rejected Create", persisted.Status.Phase)
|
||||
}
|
||||
}
|
||||
|
||||
// The probe measures the base backup where it was written: the destination
|
||||
// recorded on the cnpg.io/Backup when it was created, not the strategy as
|
||||
// it reads by the time the backup completes.
|
||||
func TestCreateCNPGBackupArtifact_SizeProbeReadsTheRecordedDestination(t *testing.T) {
|
||||
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
|
||||
j := &backupsv1alpha1.BackupJob{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-moved"},
|
||||
Spec: backupsv1alpha1.BackupJobSpec{
|
||||
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
|
||||
},
|
||||
}
|
||||
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
|
||||
oldCreds := "creds-before-edit"
|
||||
written := cnpgDestination{DestinationPath: "s3://old-bkt/before/edit/", EndpointURL: "https://old.example:9000", S3SecretRef: &oldCreds}
|
||||
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "before/edit/srv/base/20261021T100000/data.tar.gz", Size: 42}}}
|
||||
r := reconcilerWithLister(t, l)
|
||||
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", oldCreds))
|
||||
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
|
||||
if err != nil {
|
||||
t.Fatalf("createCNPGBackupArtifact: %v", err)
|
||||
}
|
||||
if l.bucket != "old-bkt" || l.prefix != "before/edit/srv/base/20261021T100000/" {
|
||||
t.Errorf("listed %s/%s, want the recorded destination old-bkt/before/edit/srv/base/20261021T100000/", l.bucket, l.prefix)
|
||||
}
|
||||
if l.conn.Endpoint != "old.example:9000" {
|
||||
t.Errorf("probed endpoint %q, want the recorded old.example:9000", l.conn.Endpoint)
|
||||
}
|
||||
if got.Status.Artifact == nil || got.Status.Artifact.SizeBytes != 42 {
|
||||
t.Errorf("Artifact = %#v, want SizeBytes 42", got.Status.Artifact)
|
||||
}
|
||||
}
|
||||
|
||||
// A backup recorded without a credentials Secret is not measured with the
|
||||
// Secret the strategy names now: that Secret belongs to another location.
|
||||
func TestCreateCNPGBackupArtifact_SizeProbeSkipsARecordWithoutCredentials(t *testing.T) {
|
||||
apiGroup := backupsv1alpha1.DefaultApplicationAPIGroup
|
||||
j := &backupsv1alpha1.BackupJob{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant", Name: "bj-nocreds"},
|
||||
Spec: backupsv1alpha1.BackupJobSpec{
|
||||
ApplicationRef: corev1.TypedLocalObjectReference{APIGroup: &apiGroup, Kind: postgresAppKind, Name: "pg"},
|
||||
},
|
||||
}
|
||||
cnpgBk := &cnpgtypes.Backup{ObjectMeta: metav1.ObjectMeta{Name: "cnpg-bk"}, Status: cnpgtypes.BackupStatus{BackupID: "20261021T100000"}}
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{BarmanObjectStore: *newSizedBarmanObjectStore()}
|
||||
written := cnpgDestination{DestinationPath: "s3://bkt/tenant/pg/", EndpointURL: "https://s3.example:8443"}
|
||||
l := &fakeS3Lister{objects: []s3ObjectInfo{{Key: "x", Size: 1}}}
|
||||
r := reconcilerWithLister(t, l)
|
||||
r.Client = newCNPGStrategyTestClient(t, s3CredsSecret("tenant", "creds"))
|
||||
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, &ResolvedBackupConfig{}, cnpgBk, "postgres-pg", "srv", "postgresql", written, rendered, newPostgresApp("pg", "tenant"))
|
||||
if err != nil {
|
||||
t.Fatalf("createCNPGBackupArtifact: %v", err)
|
||||
}
|
||||
if l.calls != 0 {
|
||||
t.Errorf("S3 listed %d times with the strategy's Secret for a backup recorded without one", l.calls)
|
||||
}
|
||||
if got.Status.Artifact == nil || got.Status.Artifact.SizeBytes != 0 {
|
||||
t.Errorf("Artifact = %#v, want no size", got.Status.Artifact)
|
||||
}
|
||||
}
|
||||
@@ -219,6 +219,11 @@ const (
|
||||
// instance pods still have to postdate the rollout's own start.
|
||||
backupCondDestinationWriteHeld = "DestinationWriteHeld"
|
||||
|
||||
// Annotation on a cnpg.io/Backup carrying, as JSON, the WAL archive
|
||||
// destination written by the pass that created it: the destination
|
||||
// path, the endpoint URL and the name of the credentials Secret.
|
||||
cnpgWrittenDestinationAnnotation = "backups.cozystack.io/cnpg-written-destination"
|
||||
|
||||
cnpgPodRoleLabel = "cnpg.io/podRole"
|
||||
cnpgPodRoleInstance = "instance"
|
||||
)
|
||||
@@ -526,7 +531,7 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
|
||||
"cluster", clusterName, "live", attachment.flavor, "application", specFlavor)
|
||||
}
|
||||
|
||||
cnpgBackup, err := r.ensureCNPGBackup(ctx, j, clusterName)
|
||||
cnpgBackup, err := r.ensureCNPGBackup(ctx, j, clusterName, cnpgDestinationOf(rendered))
|
||||
if err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to ensure cnpg.io/Backup: %v", err))
|
||||
}
|
||||
@@ -546,7 +551,11 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
|
||||
if j.Status.BackupRef != nil {
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
artifact, err := r.createCNPGBackupArtifact(ctx, j, resolved, cnpgBackup, clusterName, serverName, attachment.flavor, rendered, app)
|
||||
destination, err := cnpgArtifactDestination(cnpgBackup, rendered)
|
||||
if err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("refusing to create the Backup artifact: %v", err))
|
||||
}
|
||||
artifact, err := r.createCNPGBackupArtifact(ctx, j, resolved, cnpgBackup, clusterName, serverName, attachment.flavor, destination, rendered, app)
|
||||
if err != nil {
|
||||
return r.failBackupArtifact(ctx, j, err)
|
||||
}
|
||||
@@ -869,7 +878,12 @@ func currentBarmanServerName(c *cnpgtypes.Cluster) string {
|
||||
// ensureCNPGBackup creates a one-shot postgresql.cnpg.io/Backup CR labelled
|
||||
// with the BackupJob, or returns the existing one if a previous reconcile
|
||||
// already created it. Idempotency relies on the OwningJob labels.
|
||||
func (r *BackupJobReconciler) ensureCNPGBackup(ctx context.Context, j *backupsv1alpha1.BackupJob, clusterName string) (*cnpgtypes.Backup, error) {
|
||||
//
|
||||
// written is the destination this pass wrote to the ObjectStore. A pass
|
||||
// reaches the create only after that write: a job without a Backup writes
|
||||
// the destination on every pass, and a pass that wrote nothing, or wrote it
|
||||
// and waits for the plugin rollout, returns before this point.
|
||||
func (r *BackupJobReconciler) ensureCNPGBackup(ctx context.Context, j *backupsv1alpha1.BackupJob, clusterName string, written cnpgDestination) (*cnpgtypes.Backup, error) {
|
||||
existing, err := r.findCNPGBackupForJob(ctx, j)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -877,6 +891,10 @@ func (r *BackupJobReconciler) ensureCNPGBackup(ctx context.Context, j *backupsv1
|
||||
if existing != nil {
|
||||
return existing, nil
|
||||
}
|
||||
record, err := json.Marshal(written)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("encode the WAL archive destination: %w", err)
|
||||
}
|
||||
|
||||
obj := &cnpgtypes.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
@@ -887,6 +905,7 @@ func (r *BackupJobReconciler) ensureCNPGBackup(ctx context.Context, j *backupsv1
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: j.Namespace,
|
||||
cnpgClusterLabel: clusterName,
|
||||
},
|
||||
Annotations: map[string]string{cnpgWrittenDestinationAnnotation: string(record)},
|
||||
},
|
||||
Spec: cnpgtypes.BackupSpec{
|
||||
Method: cnpgtypes.BackupMethodPlugin,
|
||||
@@ -984,6 +1003,67 @@ func (r *BackupJobReconciler) waitCNPGBackupJob(ctx context.Context, j *backupsv
|
||||
return ctrl.Result{RequeueAfter: cnpgPollInterval}, nil
|
||||
}
|
||||
|
||||
// cnpgDestination is the WAL archive destination recorded in
|
||||
// cnpgWrittenDestinationAnnotation. S3SecretRef is nil when the strategy
|
||||
// names no credentials Secret.
|
||||
type cnpgDestination struct {
|
||||
DestinationPath string `json:"destinationPath"`
|
||||
EndpointURL string `json:"endpointURL,omitempty"`
|
||||
S3SecretRef *string `json:"s3SecretRef,omitempty"`
|
||||
}
|
||||
|
||||
func cnpgDestinationOf(t *strategyv1alpha1.CNPGTemplate) cnpgDestination {
|
||||
d := cnpgDestination{
|
||||
DestinationPath: t.BarmanObjectStore.DestinationPath,
|
||||
EndpointURL: t.BarmanObjectStore.EndpointURL,
|
||||
}
|
||||
if t.BarmanObjectStore.S3Credentials != nil {
|
||||
name := t.BarmanObjectStore.S3Credentials.SecretRef.Name
|
||||
d.S3SecretRef = &name
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// cnpgArtifactDestination returns the destination the artifact of b names:
|
||||
// the one recorded when b was created. A Backup without the record was
|
||||
// created by a controller that kept none, and falls back to the strategy
|
||||
// rendered now. A record that names no destination is an error: the
|
||||
// strategy may have changed since the write, so it cannot stand in.
|
||||
func cnpgArtifactDestination(b *cnpgtypes.Backup, rendered *strategyv1alpha1.CNPGTemplate) (cnpgDestination, error) {
|
||||
raw, ok := b.Annotations[cnpgWrittenDestinationAnnotation]
|
||||
if !ok {
|
||||
return cnpgDestinationOf(rendered), nil
|
||||
}
|
||||
var d cnpgDestination
|
||||
if err := json.Unmarshal([]byte(raw), &d); err != nil {
|
||||
return cnpgDestination{}, fmt.Errorf("read the %s annotation of cnpg.io/Backup %s/%s: %w", cnpgWrittenDestinationAnnotation, b.Namespace, b.Name, err)
|
||||
}
|
||||
if d.DestinationPath == "" {
|
||||
return cnpgDestination{}, fmt.Errorf("the %s annotation of cnpg.io/Backup %s/%s names no destination path", cnpgWrittenDestinationAnnotation, b.Namespace, b.Name)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// cnpgProbeStore is the object store the size probe reads: the location
|
||||
// and the credentials Secret recorded for the backup, with the Secret keys
|
||||
// and the endpoint CA the record does not carry taken from the strategy.
|
||||
// After a strategy edit those may not fit the recorded location; the probe
|
||||
// then reports no size rather than measuring somewhere else.
|
||||
func cnpgProbeStore(rendered *strategyv1alpha1.BarmanObjectStoreTemplate, destination cnpgDestination) *strategyv1alpha1.BarmanObjectStoreTemplate {
|
||||
store := rendered.DeepCopy()
|
||||
store.DestinationPath = destination.DestinationPath
|
||||
store.EndpointURL = destination.EndpointURL
|
||||
if destination.S3SecretRef == nil {
|
||||
store.S3Credentials = nil
|
||||
} else {
|
||||
if store.S3Credentials == nil {
|
||||
store.S3Credentials = &strategyv1alpha1.S3CredentialsTemplate{}
|
||||
}
|
||||
store.S3Credentials.SecretRef.Name = *destination.S3SecretRef
|
||||
}
|
||||
return store
|
||||
}
|
||||
|
||||
// createCNPGBackupArtifact materialises a Cozystack Backup resource carrying
|
||||
// the metadata callers need to drive a future restore. The source app's
|
||||
// spec.databases / spec.users are snapshotted into Status.UnderlyingResources
|
||||
@@ -995,6 +1075,7 @@ func (r *BackupJobReconciler) createCNPGBackupArtifact(
|
||||
resolved *ResolvedBackupConfig,
|
||||
cnpgBackup *cnpgtypes.Backup,
|
||||
clusterName, serverName, flavor string,
|
||||
destination cnpgDestination,
|
||||
rendered *strategyv1alpha1.CNPGTemplate,
|
||||
sourceApp *postgresapp.Postgres,
|
||||
) (*backupsv1alpha1.Backup, error) {
|
||||
@@ -1010,12 +1091,12 @@ func (r *BackupJobReconciler) createCNPGBackupArtifact(
|
||||
driverMD := map[string]string{
|
||||
cnpgBackupNameKey: cnpgBackup.Name,
|
||||
cnpgServerNameKey: serverName,
|
||||
cnpgDestinationPathKey: rendered.BarmanObjectStore.DestinationPath,
|
||||
cnpgEndpointURLKey: rendered.BarmanObjectStore.EndpointURL,
|
||||
cnpgDestinationPathKey: destination.DestinationPath,
|
||||
cnpgEndpointURLKey: destination.EndpointURL,
|
||||
cnpgClusterNameKey: clusterName,
|
||||
}
|
||||
if rendered.BarmanObjectStore.S3Credentials != nil {
|
||||
driverMD[cnpgS3SecretRefKey] = rendered.BarmanObjectStore.S3Credentials.SecretRef.Name
|
||||
if destination.S3SecretRef != nil {
|
||||
driverMD[cnpgS3SecretRefKey] = *destination.S3SecretRef
|
||||
}
|
||||
// Copied rather than read back at restore time: barman retention deletes
|
||||
// the cnpg.io/Backup while the artifact can still be restored from.
|
||||
@@ -1048,7 +1129,7 @@ func (r *BackupJobReconciler) createCNPGBackupArtifact(
|
||||
},
|
||||
}
|
||||
if probe {
|
||||
backup.Status.Artifact.SizeBytes = r.cnpgArtifactSizeBytes(ctx, j.Namespace, &rendered.BarmanObjectStore, serverName, cnpgBackup.Status.BackupID)
|
||||
backup.Status.Artifact.SizeBytes = r.cnpgArtifactSizeBytes(ctx, j.Namespace, cnpgProbeStore(&rendered.BarmanObjectStore, destination), serverName, cnpgBackup.Status.BackupID)
|
||||
}
|
||||
if j.Spec.PlanRef != nil {
|
||||
backup.Spec.PlanRef = j.Spec.PlanRef
|
||||
|
||||
@@ -1075,7 +1075,7 @@ func TestCreateCNPGBackupArtifact_AlreadyExistsReturnsExisting(t *testing.T) {
|
||||
}
|
||||
|
||||
sourceApp := newPostgresApp("pg", "tenant")
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "postgres-pg", "postgresql", rendered, sourceApp)
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "postgres-pg", "postgresql", cnpgDestinationOf(rendered), rendered, sourceApp)
|
||||
if err != nil {
|
||||
t.Fatalf("expected AlreadyExists to be swallowed, got error %v", err)
|
||||
}
|
||||
@@ -2207,7 +2207,7 @@ func TestEnsureCNPGBackup_UsesPluginMethod(t *testing.T) {
|
||||
c := newCNPGStrategyTestClient(t, j)
|
||||
r := &BackupJobReconciler{Client: c}
|
||||
|
||||
bk, err := r.ensureCNPGBackup(context.Background(), j, "postgres-app")
|
||||
bk, err := r.ensureCNPGBackup(context.Background(), j, "postgres-app", cnpgDestination{DestinationPath: "s3://bucket/"})
|
||||
if err != nil {
|
||||
t.Fatalf("ensureCNPGBackup: %v", err)
|
||||
}
|
||||
@@ -5311,7 +5311,7 @@ func TestCreateCNPGBackupArtifact_RecordsMajorVersion(t *testing.T) {
|
||||
rendered := &strategyv1alpha1.CNPGTemplate{
|
||||
BarmanObjectStore: strategyv1alpha1.BarmanObjectStoreTemplate{DestinationPath: "s3://b/"},
|
||||
}
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "postgres-pg", "postgresql", rendered, newPostgresApp("pg", "tenant"))
|
||||
got, err := r.createCNPGBackupArtifact(context.Background(), j, resolved, cnpgBk, "postgres-pg", "postgres-pg", "postgresql", cnpgDestinationOf(rendered), rendered, newPostgresApp("pg", "tenant"))
|
||||
if err != nil {
|
||||
t.Fatalf("createCNPGBackupArtifact: %v", err)
|
||||
}
|
||||
@@ -5492,3 +5492,166 @@ func TestReconcileCNPGRestore_RefusesCrossMajorRestore(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (f *cnpgRolloutFixture) setStrategyDestination(destination, endpoint, secret string) {
|
||||
f.t.Helper()
|
||||
strategy := &strategyv1alpha1.CNPG{}
|
||||
if err := f.c.Get(context.Background(), client.ObjectKey{Name: "cnpg-strategy"}, strategy); err != nil {
|
||||
f.t.Fatalf("get strategy: %v", err)
|
||||
}
|
||||
strategy.Spec.Template.BarmanObjectStore.DestinationPath = destination
|
||||
strategy.Spec.Template.BarmanObjectStore.EndpointURL = endpoint
|
||||
strategy.Spec.Template.BarmanObjectStore.S3Credentials = &strategyv1alpha1.S3CredentialsTemplate{SecretRef: corev1.LocalObjectReference{Name: secret}}
|
||||
if err := f.c.Update(context.Background(), strategy); err != nil {
|
||||
f.t.Fatalf("update strategy: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// completeOwnBackup applies edit, when given, to the job's cnpg.io/Backup and
|
||||
// marks it completed.
|
||||
func (f *cnpgRolloutFixture) completeOwnBackup(edit func(b *cnpgtypes.Backup)) {
|
||||
f.t.Helper()
|
||||
backups := &cnpgtypes.BackupList{}
|
||||
if err := f.c.List(context.Background(), backups, client.InNamespace(rolloutNS)); err != nil {
|
||||
f.t.Fatalf("list cnpg.io/Backups: %v", err)
|
||||
}
|
||||
for i := range backups.Items {
|
||||
b := &backups.Items[i]
|
||||
if b.Labels[backupsv1alpha1.OwningJobNameLabel] != "bj" {
|
||||
continue
|
||||
}
|
||||
if edit != nil {
|
||||
edit(b)
|
||||
}
|
||||
b.Status.Phase = cnpgBackupPhaseComplete
|
||||
if err := f.c.Update(context.Background(), b); err != nil {
|
||||
f.t.Fatalf("complete cnpg.io/Backup: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
f.t.Fatalf("the job has no cnpg.io/Backup")
|
||||
}
|
||||
|
||||
func (f *cnpgRolloutFixture) artifact() *backupsv1alpha1.Backup {
|
||||
f.t.Helper()
|
||||
artifact := &backupsv1alpha1.Backup{}
|
||||
if err := f.c.Get(context.Background(), client.ObjectKey{Namespace: rolloutNS, Name: "bj"}, artifact); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
f.t.Fatalf("get Backup artifact: %v", err)
|
||||
}
|
||||
return artifact
|
||||
}
|
||||
|
||||
// The destination is written by the pass that creates the cnpg.io/Backup and
|
||||
// not again while it runs, so the artifact has to name what that pass wrote:
|
||||
// a strategy edited while the backup runs does not reach the backup, and must
|
||||
// not reach its artifact either. The BackupJob, which a tenant can edit,
|
||||
// carries no part of it.
|
||||
func TestReconcileCNPG_ArtifactNamesTheDestinationItsBackupWasCreatedWith(t *testing.T) {
|
||||
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://other/")
|
||||
f.setStrategyDestination("s3://bucket/", "https://s3.one", "creds-one")
|
||||
|
||||
j, backups := f.pass()
|
||||
if len(backups) != 1 {
|
||||
t.Fatalf("got %d cnpg.io/Backups after the first pass, want 1; Ready = %+v", len(backups), apimeta.FindStatusCondition(j.Status.Conditions, "Ready"))
|
||||
}
|
||||
if got := f.liveDestination(); got != "s3://bucket/" {
|
||||
t.Fatalf("destination = %q, want s3://bucket/", got)
|
||||
}
|
||||
want := `{"destinationPath":"s3://bucket/","endpointURL":"https://s3.one","s3SecretRef":"creds-one"}`
|
||||
if got := backups[0].Annotations[cnpgWrittenDestinationAnnotation]; got != want {
|
||||
t.Fatalf("%s on the created cnpg.io/Backup = %q, want %q", cnpgWrittenDestinationAnnotation, got, want)
|
||||
}
|
||||
|
||||
f.setStrategyDestination("s3://edited/", "https://s3.two", "creds-two")
|
||||
j.Annotations = map[string]string{cnpgWrittenDestinationAnnotation: `{"destinationPath":"s3://tenant/","endpointURL":"https://tenant","s3SecretRef":"creds-one"}`}
|
||||
if err := f.c.Update(context.Background(), j); err != nil {
|
||||
t.Fatalf("annotate BackupJob: %v", err)
|
||||
}
|
||||
f.completeOwnBackup(nil)
|
||||
j, _ = f.pass()
|
||||
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
|
||||
t.Fatalf("phase = %q (%s), want Succeeded", j.Status.Phase, j.Status.Message)
|
||||
}
|
||||
artifact := f.artifact()
|
||||
if artifact == nil {
|
||||
t.Fatalf("no Backup artifact")
|
||||
}
|
||||
md := artifact.Spec.DriverMetadata
|
||||
if md[cnpgDestinationPathKey] != "s3://bucket/" || md[cnpgEndpointURLKey] != "https://s3.one" || md[cnpgS3SecretRefKey] != "creds-one" {
|
||||
t.Fatalf("artifact driver metadata = %v, want the destination written with the Backup (s3://bucket/, https://s3.one, creds-one)", md)
|
||||
}
|
||||
}
|
||||
|
||||
// A strategy without credentials records none, and the artifact names none.
|
||||
func TestReconcileCNPG_ArtifactOfARecordWithoutCredentials(t *testing.T) {
|
||||
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://bucket/")
|
||||
if _, backups := f.pass(); len(backups) != 1 || backups[0].Annotations[cnpgWrittenDestinationAnnotation] != `{"destinationPath":"s3://bucket/"}` {
|
||||
t.Fatalf("cnpg.io/Backups after the first pass = %+v, want one recording only s3://bucket/", backups)
|
||||
}
|
||||
f.setStrategyDestination("s3://edited/", "https://s3.two", "creds-two")
|
||||
f.completeOwnBackup(nil)
|
||||
j, _ := f.pass()
|
||||
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
|
||||
t.Fatalf("phase = %q (%s), want Succeeded", j.Status.Phase, j.Status.Message)
|
||||
}
|
||||
md := f.artifact().Spec.DriverMetadata
|
||||
if _, ok := md[cnpgS3SecretRefKey]; ok || md[cnpgDestinationPathKey] != "s3://bucket/" || md[cnpgEndpointURLKey] != "" {
|
||||
t.Fatalf("artifact driver metadata = %v, want s3://bucket/ with no endpoint and no credentials", md)
|
||||
}
|
||||
}
|
||||
|
||||
// A record that names no usable destination cannot say where the backup is,
|
||||
// so no artifact is created rather than one pointing at the strategy as it
|
||||
// reads now.
|
||||
func TestReconcileCNPG_ArtifactRefusedForAnUnusableDestinationRecord(t *testing.T) {
|
||||
for name, value := range map[string]string{
|
||||
"empty": "",
|
||||
"unreadable": "{not json",
|
||||
"not an object": `"s3://bucket/"`,
|
||||
"null": "null",
|
||||
"no fields": "{}",
|
||||
"empty destination": `{"destinationPath":"","endpointURL":"https://s3.one"}`,
|
||||
// Decoding sets the destination path and skips the mistyped
|
||||
// endpoint, which would leave the artifact on the default one.
|
||||
"mistyped endpoint": `{"destinationPath":"s3://bucket/","endpointURL":7}`,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://other/")
|
||||
if _, backups := f.pass(); len(backups) != 1 {
|
||||
t.Fatalf("got %d cnpg.io/Backups after the first pass, want 1", len(backups))
|
||||
}
|
||||
f.completeOwnBackup(func(b *cnpgtypes.Backup) {
|
||||
if b.Annotations == nil {
|
||||
b.Annotations = map[string]string{}
|
||||
}
|
||||
b.Annotations[cnpgWrittenDestinationAnnotation] = value
|
||||
})
|
||||
j, _ := f.pass()
|
||||
if f.artifact() != nil {
|
||||
t.Fatalf("an artifact was created from the record %q", value)
|
||||
}
|
||||
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed || !strings.Contains(j.Status.Message, cnpgWrittenDestinationAnnotation) {
|
||||
t.Fatalf("phase = %q (%s), want Failed naming %s", j.Status.Phase, j.Status.Message, cnpgWrittenDestinationAnnotation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A cnpg.io/Backup created by a controller that recorded nothing keeps
|
||||
// getting its artifact from the strategy.
|
||||
func TestReconcileCNPG_ArtifactOfABackupWithoutARecord(t *testing.T) {
|
||||
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://bucket/")
|
||||
f.seedCNPGBackup("bj-legacy", "bj", rolloutCluster, cnpgBackupPhaseComplete)
|
||||
|
||||
j, _ := f.pass()
|
||||
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
|
||||
t.Fatalf("phase = %q (%s), want Succeeded", j.Status.Phase, j.Status.Message)
|
||||
}
|
||||
artifact := f.artifact()
|
||||
if artifact == nil || artifact.Spec.DriverMetadata[cnpgDestinationPathKey] != "s3://bucket/" {
|
||||
t.Fatalf("artifact = %+v, want one built from the strategy", artifact)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user