Skip to content

Commit fc15ed7

Browse files
committed
fix(clickhouse): keep the archive name of a backup running across the upgrade
A backup Job created before archive names carried the BackupJob's UID writes its archive under the name without it, and the Job is not re-rendered when the controller is upgraded. Matching only the new name left that Backup with no archive recorded, so its restore fell back to guessing by timestamp. The name without the UID is matched second, because it is a prefix of the one with it. Assisted-by: LLM Signed-off-by: Alexey Artamonov <[email protected]>
1 parent 0f336e9 commit fc15ed7

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎internal/backupcontroller/altinitystrategy_controller.go‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,14 @@ func backupParameters(b *backupsv1alpha1.Backup) map[string]string {
126126
// chronologically, and the restore of a Backup that records no archive, which
127127
// accepts only clickhouse-<release>-<timestamp>, never picks one of these.
128128
func altinityArchiveName(j *backupsv1alpha1.BackupJob) string {
129-
return fmt.Sprintf("clickhouse-%s-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout), j.UID)
129+
return altinityArchiveNameBeforeUIDs(j) + "-" + string(j.UID)
130+
}
131+
132+
// altinityArchiveNameBeforeUIDs is the name a controller that did not append
133+
// the UID gave the same BackupJob, which a Job it created before an upgrade
134+
// still writes.
135+
func altinityArchiveNameBeforeUIDs(j *backupsv1alpha1.BackupJob) string {
136+
return fmt.Sprintf("clickhouse-%s-%s", j.Spec.ApplicationRef.Name, j.Status.StartedAt.UTC().Format(altinityArchiveTimeLayout))
130137
}
131138

132139
// altinityJobCarriesArchive reports whether the Job's Pod was rendered with
@@ -286,8 +293,13 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv
286293
if j.Status.BackupRef != nil {
287294
return ctrl.Result{}, nil
288295
}
296+
// The name without the UID is a prefix of the one with it, so it is
297+
// tried only after the full name is not found.
289298
if !altinityJobCarriesArchive(batchJob, archive) {
290-
archive = ""
299+
archive = altinityArchiveNameBeforeUIDs(j)
300+
if !altinityJobCarriesArchive(batchJob, archive) {
301+
archive = ""
302+
}
291303
}
292304
artifact, err := r.createAltinityBackupArtifact(ctx, j, resolved, archive)
293305
if err != nil {

‎internal/backupcontroller/altinitystrategy_controller_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,65 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) {
829829
}
830830
}
831831

832+
// TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs covers a backup
833+
// Job created by a controller that named archives without the BackupJob's UID
834+
// and still running when the controller is upgraded: the Job is not
835+
// re-rendered, so its archive carries the old name, and the Backup has to
836+
// record that name rather than none.
837+
func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) {
838+
app := newAltinityClickHouseApp("ch-test", "tenant-test")
839+
strategy := newAltinityArchiveStrategy("clickhouse-strategy")
840+
startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC))
841+
backupJob := &backupsv1alpha1.BackupJob{
842+
ObjectMeta: metav1.ObjectMeta{Name: "test-bj", Namespace: "tenant-test", UID: "test-bj-uid"},
843+
Spec: backupsv1alpha1.BackupJobSpec{
844+
ApplicationRef: newAltinityClickHouseRef("ch-test"),
845+
BackupClassName: "clickhouse-backup",
846+
},
847+
Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt, Phase: backupsv1alpha1.BackupJobPhaseRunning},
848+
}
849+
resolved := &ResolvedBackupConfig{
850+
StrategyRef: corev1.TypedLocalObjectReference{
851+
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
852+
Kind: strategyv1alpha1.AltinityStrategyKind,
853+
Name: "clickhouse-strategy",
854+
},
855+
}
856+
r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy))
857+
ctx := context.Background()
858+
const legacy = "clickhouse-ch-test-20260304T050607Z"
859+
860+
if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil {
861+
t.Fatalf("reconcileAltinity() creating the Job: %v", err)
862+
}
863+
job := &batchv1.Job{}
864+
if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: jobNameForBackupJob(backupJob)}, job); err != nil {
865+
t.Fatalf("get backup Job: %v", err)
866+
}
867+
job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + legacy
868+
if err := r.Update(ctx, job); err != nil {
869+
t.Fatalf("give the backup Job the name without the UID: %v", err)
870+
}
871+
job.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobComplete, Status: corev1.ConditionTrue}}
872+
if err := r.Status().Update(ctx, job); err != nil {
873+
t.Fatalf("mark backup Job complete: %v", err)
874+
}
875+
if err := r.Get(ctx, client.ObjectKeyFromObject(backupJob), backupJob); err != nil {
876+
t.Fatalf("refresh BackupJob: %v", err)
877+
}
878+
if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil {
879+
t.Fatalf("reconcileAltinity() on the completed Job: %v", err)
880+
}
881+
882+
created := &backupsv1alpha1.Backup{}
883+
if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: backupJob.Name}, created); err != nil {
884+
t.Fatalf("get Backup: %v", err)
885+
}
886+
if got := created.Spec.DriverMetadata[altinityArchiveMetaKey]; got != legacy {
887+
t.Errorf("Backup records archive %q, want %q", got, legacy)
888+
}
889+
}
890+
832891
// TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven covers a Job whose
833892
// Pod never carried the driver's archive name: one created before the
834893
// upgrade that introduced the names, or rendered from a strategy that names

0 commit comments

Comments
 (0)