fix(clickhouse): match a backup Job's archive by its whole name

The archive a backup Job carries was found as a substring of its Pod
spec. The name without the BackupJob's UID starts the name with it,
and a strategy may append to either, so a Job that wrote a longer
archive could get the shorter name recorded on its Backup, and a
restore of that Backup would read another archive.

Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
Alexey Artamonov committed 2026-11-09 17:53:43 +03:00
1 parent fc15ed7171
commit 2046649b4c
2 files changed
+33 -11

No files matched your search

@@ -2,11 +2,11 @@
package backupcontroller
import (
"bytes"
"context"
"encoding/json"
"fmt"
"maps"
"regexp"
"strings"
"time"
@@ -140,13 +140,16 @@ func altinityArchiveNameBeforeUIDs(j *backupsv1alpha1.BackupJob) string {
// the archive name. A Job created by a controller that predates archive
// names, or rendered from a strategy that names its own archives, wrote an
// archive under another name, and recording this one would point the Backup
// at an archive that does not exist.
// at an archive that does not exist. The name has to stand on its own: the
// name without the UID starts the name with it, and a strategy may append to
// either.
func altinityJobCarriesArchive(job *batchv1.Job, archive string) bool {
spec, err := json.Marshal(job.Spec.Template.Spec)
if err != nil {
return false
}
return bytes.Contains(spec, []byte(archive))
whole := regexp.MustCompile(`(^|[^A-Za-z0-9._-])` + regexp.QuoteMeta(archive) + `($|[^A-Za-z0-9._-])`)
return whole.Match(spec)
}
// renderAltinityTemplate runs the strategy's PodTemplateSpec through the
@@ -293,8 +296,6 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv
if j.Status.BackupRef != nil {
return ctrl.Result{}, nil
}
// The name without the UID is a prefix of the one with it, so it is
// tried only after the full name is not found.
if !altinityJobCarriesArchive(batchJob, archive) {
archive = altinityArchiveNameBeforeUIDs(j)
if !altinityJobCarriesArchive(batchJob, archive) {
@@ -835,6 +835,30 @@ func TestReconcileAltinity_RecordsTheArchiveTheJobWasGiven(t *testing.T) {
// re-rendered, so its archive carries the old name, and the Backup has to
// record that name rather than none.
func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T) {
const legacy = "clickhouse-ch-test-20260304T050607Z"
cases := []struct {
name string
archive string // archive the Job's Pod was rendered with
want string
}{
{name: "the name without the UID", archive: legacy, want: legacy},
{name: "a longer name that starts with it", archive: legacy + "-copy", want: ""},
{name: "a longer name that ends with it", archive: "copy-" + legacy, want: ""},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if got := recordAltinityArchiveOfJobRenderedWith(t, tc.archive); got != tc.want {
t.Errorf("Backup records archive %q, want %q", got, tc.want)
}
})
}
}
// recordAltinityArchiveOfJobRenderedWith completes a backup Job whose Pod
// carries the given archive in place of the one the controller rendered, and
// returns the archive the resulting Backup records.
func recordAltinityArchiveOfJobRenderedWith(t *testing.T, archive string) string {
t.Helper()
app := newAltinityClickHouseApp("ch-test", "tenant-test")
strategy := newAltinityArchiveStrategy("clickhouse-strategy")
startedAt := metav1.NewTime(time.Date(2026, 3, 4, 5, 6, 7, 0, time.UTC))
@@ -855,7 +879,6 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T)
}
r, _, _ := newAltinityTestEnv(t, app, clientfake.NewClientBuilder().WithObjects(backupJob, strategy))
ctx := context.Background()
const legacy = "clickhouse-ch-test-20260304T050607Z"
if _, err := r.reconcileAltinity(ctx, backupJob, resolved); err != nil {
t.Fatalf("reconcileAltinity() creating the Job: %v", err)
@@ -864,9 +887,9 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T)
if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: jobNameForBackupJob(backupJob)}, job); err != nil {
t.Fatalf("get backup Job: %v", err)
}
job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + legacy
job.Spec.Template.Spec.Containers[0].Args[1] = "--archive=" + archive
if err := r.Update(ctx, job); err != nil {
t.Fatalf("give the backup Job the name without the UID: %v", err)
t.Fatalf("render the backup Job with archive %q: %v", archive, err)
}
job.Status.Conditions = []batchv1.JobCondition{{Type: batchv1.JobComplete, Status: corev1.ConditionTrue}}
if err := r.Status().Update(ctx, job); err != nil {
@@ -883,9 +906,7 @@ func TestReconcileAltinity_RecordsTheArchiveOfAJobNamedBeforeUIDs(t *testing.T)
if err := r.Get(ctx, client.ObjectKey{Namespace: "tenant-test", Name: backupJob.Name}, created); err != nil {
t.Fatalf("get Backup: %v", err)
}
if got := created.Spec.DriverMetadata[altinityArchiveMetaKey]; got != legacy {
t.Errorf("Backup records archive %q, want %q", got, legacy)
}
return created.Spec.DriverMetadata[altinityArchiveMetaKey]
}
// TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven covers a Job whose