mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(clickhouse): take no backup Job's result that this BackupJob did not run
A Job standing under a BackupJob's Job name was taken as its own. A BackupJob re-created under a used name, as a Plan does when the BackupJob of a schedule slot is deleted within the slot's starting deadline, met the completed Job of the earlier one before the garbage collector removed it, was marked Succeeded without uploading anything, and recorded a Backup for an archive nobody uploaded for it. A Job controlled by an earlier BackupJob of the same name is now waited out, with the Ready condition naming it; any other Job holding the name fails the BackupJob, since nothing will remove it. Assisted-by: LLM Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
1 parent
564bdd3a79
commit
e199ef1d83
3 files changed
+144
-8
No files matched your search
@@ -66,7 +66,7 @@ func runningAltinityBackupJob() *backupsv1alpha1.BackupJob {
|
||||
// BackupJob's archive name.
|
||||
func completedAltinityK8sJob(j *backupsv1alpha1.BackupJob) *batchv1.Job {
|
||||
return &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: jobNameForBackupJob(j), Namespace: j.Namespace},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: jobNameForBackupJob(j), Namespace: j.Namespace, OwnerReferences: altinityControllerRef(j)},
|
||||
Spec: batchv1.JobSpec{Template: corev1.PodTemplateSpec{Spec: corev1.PodSpec{
|
||||
Containers: []corev1.Container{{Name: "c", Args: []string{"--archive=" + altinityArchiveName(j)}}},
|
||||
}}},
|
||||
|
||||
@@ -4,6 +4,7 @@ package backupcontroller
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"regexp"
|
||||
@@ -22,6 +23,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
|
||||
@@ -429,6 +431,18 @@ func (r *BackupJobReconciler) reconcileAltinity(ctx context.Context, j *backupsv
|
||||
jobAnnotations,
|
||||
rendered,
|
||||
)
|
||||
if errors.Is(err, errAltinityPredecessorJob) {
|
||||
apimeta.SetStatusCondition(&j.Status.Conditions, metav1.Condition{
|
||||
Type: "Ready",
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: "PredecessorJobPending",
|
||||
Message: err.Error(),
|
||||
})
|
||||
if uerr := r.Status().Update(ctx, j); uerr != nil {
|
||||
return ctrl.Result{}, uerr
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: altinityPollInterval}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return r.markBackupJobFailed(ctx, j, fmt.Sprintf("failed to ensure batch/v1.Job: %v", err))
|
||||
}
|
||||
@@ -506,7 +520,7 @@ func (r *BackupJobReconciler) ensureAltinityJob(
|
||||
existing := &batchv1.Job{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing)
|
||||
if err == nil {
|
||||
return existing, nil
|
||||
return existing, r.checkAltinityJobController(existing, owner)
|
||||
}
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return nil, err
|
||||
@@ -525,13 +539,37 @@ func (r *BackupJobReconciler) ensureAltinityJob(
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return existing, nil
|
||||
return existing, r.checkAltinityJobController(existing, owner)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return desired, nil
|
||||
}
|
||||
|
||||
// errAltinityPredecessorJob marks a backup Job left under the Job name by an
|
||||
// earlier BackupJob of the same name. Its owner is gone, so the garbage
|
||||
// collector removes it, and the BackupJob waits for that instead of failing.
|
||||
var errAltinityPredecessorJob = errors.New("waiting for the garbage collector to remove a Job of an earlier owner of the same name")
|
||||
|
||||
// checkAltinityJobController refuses a Job under owner's Job name that owner
|
||||
// does not control. A BackupJob re-created under a used name meets the Job of
|
||||
// the earlier one, and that Job's completion says nothing about this run.
|
||||
func (r *BackupJobReconciler) checkAltinityJobController(job *batchv1.Job, owner client.Object) error {
|
||||
if metav1.IsControlledBy(job, owner) {
|
||||
return nil
|
||||
}
|
||||
gvk, err := apiutil.GVKForObject(owner, r.Scheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ref := metav1.GetControllerOf(job); ref != nil && ref.Kind == gvk.Kind && ref.Name == owner.GetName() {
|
||||
if gv, perr := schema.ParseGroupVersion(ref.APIVersion); perr == nil && gv.Group == gvk.Group {
|
||||
return fmt.Errorf("%w: Job %s", errAltinityPredecessorJob, job.Name)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Job %s, which this %s did not create, holds its Job name", job.Name, gvk.Kind)
|
||||
}
|
||||
|
||||
// ensureAltinityRestoreJob mirrors ensureAltinityJob from the RestoreJob
|
||||
// reconciler. The Job lives in the RestoreJob's namespace - both
|
||||
// BackupRef and TargetApplicationRef on RestoreJob are
|
||||
|
||||
@@ -4,6 +4,7 @@ package backupcontroller
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -44,6 +45,12 @@ func newAltinityClickHouseApp(name, namespace string) *unstructured.Unstructured
|
||||
return u
|
||||
}
|
||||
|
||||
// altinityControllerRef is the controllerRef ensureAltinityJob gives the
|
||||
// backup Job it creates for j.
|
||||
func altinityControllerRef(j *backupsv1alpha1.BackupJob) []metav1.OwnerReference {
|
||||
return []metav1.OwnerReference{*metav1.NewControllerRef(j, backupsv1alpha1.GroupVersion.WithKind("BackupJob"))}
|
||||
}
|
||||
|
||||
// newAltinityTestEnv builds the scheme, dynamic client, REST mapper, and
|
||||
// reconciler harness used across Altinity strategy unit tests.
|
||||
func newAltinityTestEnv(t *testing.T, app *unstructured.Unstructured, builder *clientfake.ClientBuilder) (*BackupJobReconciler, *RestoreJobReconciler, *runtime.Scheme) {
|
||||
@@ -213,8 +220,9 @@ func TestReconcileAltinity_CompletesAndCreatesBackup(t *testing.T) {
|
||||
}
|
||||
completedK8sJob := &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: jobNameForBackupJob(backupJob),
|
||||
Namespace: backupJob.Namespace,
|
||||
Name: jobNameForBackupJob(backupJob),
|
||||
Namespace: backupJob.Namespace,
|
||||
OwnerReferences: altinityControllerRef(backupJob),
|
||||
Labels: map[string]string{
|
||||
backupsv1alpha1.OwningJobNameLabel: backupJob.Name,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: backupJob.Namespace,
|
||||
@@ -294,8 +302,9 @@ func TestReconcileAltinity_FailsOnJobFailed(t *testing.T) {
|
||||
}
|
||||
failedK8sJob := &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: jobNameForBackupJob(backupJob),
|
||||
Namespace: backupJob.Namespace,
|
||||
Name: jobNameForBackupJob(backupJob),
|
||||
Namespace: backupJob.Namespace,
|
||||
OwnerReferences: altinityControllerRef(backupJob),
|
||||
Labels: map[string]string{
|
||||
backupsv1alpha1.OwningJobNameLabel: backupJob.Name,
|
||||
backupsv1alpha1.OwningJobNamespaceLabel: backupJob.Namespace,
|
||||
@@ -963,7 +972,7 @@ func TestReconcileAltinity_RecordsNoArchiveTheJobWasNotGiven(t *testing.T) {
|
||||
Status: backupsv1alpha1.BackupJobStatus{StartedAt: &startedAt, Phase: backupsv1alpha1.BackupJobPhaseRunning},
|
||||
}
|
||||
completedK8sJob := &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: jobNameForBackupJob(backupJob), Namespace: backupJob.Namespace},
|
||||
ObjectMeta: metav1.ObjectMeta{Name: jobNameForBackupJob(backupJob), Namespace: backupJob.Namespace, OwnerReferences: altinityControllerRef(backupJob)},
|
||||
Spec: batchv1.JobSpec{
|
||||
Template: corev1.PodTemplateSpec{Spec: corev1.PodSpec{
|
||||
RestartPolicy: corev1.RestartPolicyNever,
|
||||
@@ -1711,3 +1720,92 @@ func TestReconcileAltinity_CredentialsWaitKeepsTheArchiveName(t *testing.T) {
|
||||
t.Errorf("Backup takenAt %s is earlier than its archive's stamp", stamp)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReconcileAltinity_DoesNotReuseAJobItDoesNotControl covers a backup Job
|
||||
// already standing under the BackupJob's Job name that this BackupJob did not
|
||||
// create. Taking its completion for this run's would report a backup that
|
||||
// never ran and record an archive nobody uploaded for it.
|
||||
func TestReconcileAltinity_DoesNotReuseAJobItDoesNotControl(t *testing.T) {
|
||||
predecessor := runningAltinityBackupJob()
|
||||
predecessor.UID = "predecessor-uid"
|
||||
cases := []struct {
|
||||
name string
|
||||
owners []metav1.OwnerReference
|
||||
wantFailed bool
|
||||
}{
|
||||
{name: "an earlier BackupJob of the same name controls it", owners: altinityControllerRef(predecessor)},
|
||||
{name: "nothing controls it", wantFailed: true},
|
||||
{name: "a BackupJob of another name controls it", wantFailed: true, owners: altinityControllerRef(&backupsv1alpha1.BackupJob{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "other-bj", Namespace: "tenant-test", UID: "other-uid"},
|
||||
})},
|
||||
{name: "a RestoreJob controls it", wantFailed: true, owners: []metav1.OwnerReference{
|
||||
*metav1.NewControllerRef(predecessor, backupsv1alpha1.GroupVersion.WithKind("RestoreJob")),
|
||||
}},
|
||||
{name: "a BackupJob of another API group controls it", wantFailed: true, owners: []metav1.OwnerReference{
|
||||
*metav1.NewControllerRef(predecessor, schema.GroupVersion{Group: "example.com", Version: "v1alpha1"}.WithKind("BackupJob")),
|
||||
}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
// raced: the cache does not show the Job yet, so the driver meets it
|
||||
// only when its own create is refused.
|
||||
for _, raced := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/raced=%t", tc.name, raced), func(t *testing.T) {
|
||||
app := newAltinityClickHouseApp("ch-test", "tenant-test")
|
||||
strategy := newAltinityArchiveStrategy("clickhouse-strategy")
|
||||
bj := runningAltinityBackupJob()
|
||||
bj.UID = "this-uid"
|
||||
job := completedAltinityK8sJob(bj)
|
||||
job.OwnerReferences = tc.owners
|
||||
builder := clientfake.NewClientBuilder().WithObjects(bj, strategy, job)
|
||||
if raced {
|
||||
hidden := false
|
||||
builder = builder.WithInterceptorFuncs(interceptor.Funcs{
|
||||
Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
||||
if _, isJob := obj.(*batchv1.Job); isJob && !hidden {
|
||||
hidden = true
|
||||
return apierrors.NewNotFound(batchv1.Resource("jobs"), key.Name)
|
||||
}
|
||||
return c.Get(ctx, key, obj, opts...)
|
||||
},
|
||||
})
|
||||
}
|
||||
r, _, _ := newAltinityTestEnv(t, app, builder)
|
||||
ctx := context.Background()
|
||||
|
||||
res, err := r.reconcileAltinity(ctx, bj, altinityResolved(strategy.Name))
|
||||
if err != nil {
|
||||
t.Fatalf("reconcileAltinity: %v", err)
|
||||
}
|
||||
|
||||
got := &backupsv1alpha1.BackupJob{}
|
||||
if err := r.Get(ctx, client.ObjectKeyFromObject(bj), got); err != nil {
|
||||
t.Fatalf("get BackupJob: %v", err)
|
||||
}
|
||||
if got.Status.BackupRef != nil {
|
||||
t.Errorf("BackupJob points at Backup %q made from a Job it did not run", got.Status.BackupRef.Name)
|
||||
}
|
||||
if err := r.Get(ctx, client.ObjectKey{Namespace: bj.Namespace, Name: bj.Name}, &backupsv1alpha1.Backup{}); err == nil {
|
||||
t.Error("a Backup was created from a Job this BackupJob did not run")
|
||||
}
|
||||
if tc.wantFailed {
|
||||
if got.Status.Phase != backupsv1alpha1.BackupJobPhaseFailed {
|
||||
t.Fatalf("phase = %q, want Failed", got.Status.Phase)
|
||||
}
|
||||
if !strings.Contains(got.Status.Message, job.Name) {
|
||||
t.Errorf("message %q does not name Job %s", got.Status.Message, job.Name)
|
||||
}
|
||||
return
|
||||
}
|
||||
if got.Status.Phase != backupsv1alpha1.BackupJobPhaseRunning {
|
||||
t.Errorf("phase = %q, want Running while the earlier Job awaits garbage collection", got.Status.Phase)
|
||||
}
|
||||
if res.RequeueAfter <= 0 {
|
||||
t.Errorf("no requeue while the earlier Job awaits garbage collection: %+v", res)
|
||||
}
|
||||
if c := meta.FindStatusCondition(got.Status.Conditions, "Ready"); c == nil || !strings.Contains(c.Message, job.Name) {
|
||||
t.Errorf("Ready condition %+v does not say which Job it waits for", c)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user