fix(postgres): record the serverName a backup was archived under

The artifact took its destination from the record written when the
cnpg.io/Backup was created, but its serverName, the archive prefix
under that destination, from the live Cluster when the backup
completed. A restore that re-renders the Cluster in between gives it
another serverName, and the artifact then pairs the original
destination with a prefix the backup was never written to, so a later
restore searches the wrong archive. The record now carries the
serverName too; a record written before it did still falls back to the
live Cluster.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov committed 2026-11-10 00:08:41 +03:00
1 parent b4551eb379
commit 9a8025ca14
2 files changed
+57 -5

No files matched your search

@@ -223,7 +223,8 @@ const (
// 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.
// path, the endpoint URL, the name of the credentials Secret and the
// serverName the Cluster archived under.
cnpgWrittenDestinationAnnotation = "backups.cozystack.io/cnpg-written-destination"
// Annotation on a cnpg.io/Cluster carrying, as RFC 3339, when a backup
@@ -557,7 +558,9 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
"cluster", clusterName, "live", attachment.flavor, "application", specFlavor)
}
cnpgBackup, err := r.ensureCNPGBackup(ctx, j, clusterName, existingBackup, cnpgDestinationOf(rendered))
written := cnpgDestinationOf(rendered)
written.ServerName = serverName
cnpgBackup, err := r.ensureCNPGBackup(ctx, j, clusterName, existingBackup, written)
if err != nil {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to ensure cnpg.io/Backup: %v", err))
}
@@ -581,6 +584,11 @@ func (r *BackupJobReconciler) reconcileCNPG(ctx context.Context, j *backupsv1alp
if err != nil {
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("refusing to create the Backup artifact: %v", err))
}
// A restore can re-render the Cluster under another serverName
// between the backup and this pass.
if destination.ServerName != "" {
serverName = destination.ServerName
}
artifact, err := r.createCNPGBackupArtifact(ctx, j, resolved, cnpgBackup, clusterName, serverName, attachment.flavor, destination, rendered, app)
if err != nil {
return r.failBackupArtifact(ctx, j, err)
@@ -1056,11 +1064,13 @@ func (r *BackupJobReconciler) waitCNPGBackupJob(ctx context.Context, j *backupsv
// cnpgDestination is the WAL archive destination recorded in
// cnpgWrittenDestinationAnnotation. S3SecretRef is nil when the strategy
// names no credentials Secret.
// names no credentials Secret. ServerName is empty in a record written
// before it carried one.
type cnpgDestination struct {
DestinationPath string `json:"destinationPath"`
EndpointURL string `json:"endpointURL,omitempty"`
S3SecretRef *string `json:"s3SecretRef,omitempty"`
ServerName string `json:"serverName,omitempty"`
}
func cnpgDestinationOf(t *strategyv1alpha1.CNPGTemplate) cnpgDestination {
@@ -5777,7 +5777,7 @@ func TestReconcileCNPG_ArtifactNamesTheDestinationItsBackupWasCreatedWith(t *tes
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"}`
want := `{"destinationPath":"s3://bucket/","endpointURL":"https://s3.one","s3SecretRef":"creds-one","serverName":"` + rolloutCluster + `"}`
if got := backups[0].Annotations[cnpgWrittenDestinationAnnotation]; got != want {
t.Fatalf("%s on the created cnpg.io/Backup = %q, want %q", cnpgWrittenDestinationAnnotation, got, want)
}
@@ -5802,10 +5802,52 @@ func TestReconcileCNPG_ArtifactNamesTheDestinationItsBackupWasCreatedWith(t *tes
}
}
// The serverName is the archive prefix under the destination, so the record
// carries it too: a restore that re-renders the Cluster between the backup
// and its artifact gives the live Cluster another one. A record written
// before it carried a serverName falls back to the live Cluster.
func TestReconcileCNPG_ArtifactNamesTheServerNameItsBackupWasCreatedWith(t *testing.T) {
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://bucket/")
_, backups := f.pass()
want := `{"destinationPath":"s3://bucket/","serverName":"` + rolloutCluster + `"}`
if len(backups) != 1 || backups[0].Annotations[cnpgWrittenDestinationAnnotation] != want {
t.Fatalf("cnpg.io/Backups after the first pass = %+v, want one recording %s", backups, want)
}
f.observeCNPG(newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, "restored-server")))
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 got := artifact.Spec.DriverMetadata[cnpgServerNameKey]; got != rolloutCluster {
t.Errorf("artifact serverName = %q, want %q recorded with the Backup", got, rolloutCluster)
}
if !strings.HasPrefix(artifact.Status.Artifact.URI, "cnpg://"+rolloutCluster+"/") {
t.Errorf("artifact URI = %q, want it under %s", artifact.Status.Artifact.URI, rolloutCluster)
}
}
func TestReconcileCNPG_ArtifactOfARecordWithoutServerName(t *testing.T) {
f := newAttachedObjectStoreFixture(t, time.Now(), "s3://bucket/")
f.pass()
f.observeCNPG(newRolloutCluster(cnpgClusterHealthyPhase, buildBarmanPlugin(rolloutCluster, "live-server")))
f.completeOwnBackup(func(b *cnpgtypes.Backup) {
b.Annotations[cnpgWrittenDestinationAnnotation] = `{"destinationPath":"s3://bucket/"}`
})
j, _ := f.pass()
if j.Status.Phase != backupsv1alpha1.BackupJobPhaseSucceeded {
t.Fatalf("phase = %q (%s), want Succeeded", j.Status.Phase, j.Status.Message)
}
if got := f.artifact().Spec.DriverMetadata[cnpgServerNameKey]; got != "live-server" {
t.Errorf("artifact serverName = %q, want the live Cluster's live-server", got)
}
}
// 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/"}` {
if _, backups := f.pass(); len(backups) != 1 || backups[0].Annotations[cnpgWrittenDestinationAnnotation] != `{"destinationPath":"s3://bucket/","serverName":"`+rolloutCluster+`"}` {
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")