mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(clickhouse): take no restore Job's result that this RestoreJob did not run
The restore path took any Job standing under a RestoreJob's Job name as its own, so a RestoreJob re-created under a used name met the completed Job of the earlier one before the garbage collector removed it and was marked Succeeded although nothing was restored for it. The restore path now refuses such a Job the way the backup path does: the Job of an earlier RestoreJob of the same name is waited out, with the Ready condition naming it, and any other Job holding the name fails the RestoreJob. Assisted-by: LLM Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
This commit is contained in:
1 parent
e199ef1d83
commit
084fed9e2f
2 files changed
+122
-11
No files matched your search
@@ -19,6 +19,7 @@ import (
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
@@ -520,7 +521,7 @@ func (r *BackupJobReconciler) ensureAltinityJob(
|
||||
existing := &batchv1.Job{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing)
|
||||
if err == nil {
|
||||
return existing, r.checkAltinityJobController(existing, owner)
|
||||
return existing, checkAltinityJobController(r.Scheme, existing, owner)
|
||||
}
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return nil, err
|
||||
@@ -539,26 +540,27 @@ func (r *BackupJobReconciler) ensureAltinityJob(
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return existing, r.checkAltinityJobController(existing, owner)
|
||||
return existing, checkAltinityJobController(r.Scheme, 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.
|
||||
// errAltinityPredecessorJob marks a Job left under the Job name by an earlier
|
||||
// BackupJob or RestoreJob of the same name. Its owner is gone, so the garbage
|
||||
// collector removes it, and the new owner 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 {
|
||||
// does not control. A BackupJob or RestoreJob re-created under a used name
|
||||
// meets the Job of the earlier one, and that Job's completion says nothing
|
||||
// about this run.
|
||||
func checkAltinityJobController(scheme *runtime.Scheme, job *batchv1.Job, owner client.Object) error {
|
||||
if metav1.IsControlledBy(job, owner) {
|
||||
return nil
|
||||
}
|
||||
gvk, err := apiutil.GVKForObject(owner, r.Scheme)
|
||||
gvk, err := apiutil.GVKForObject(owner, scheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -586,7 +588,7 @@ func (r *RestoreJobReconciler) ensureAltinityRestoreJob(
|
||||
existing := &batchv1.Job{}
|
||||
err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing)
|
||||
if err == nil {
|
||||
return existing, nil
|
||||
return existing, checkAltinityJobController(r.Scheme, existing, owner)
|
||||
}
|
||||
if !apierrors.IsNotFound(err) {
|
||||
return nil, err
|
||||
@@ -604,7 +606,7 @@ func (r *RestoreJobReconciler) ensureAltinityRestoreJob(
|
||||
if err := r.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, existing); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return existing, nil
|
||||
return existing, checkAltinityJobController(r.Scheme, existing, owner)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -920,6 +922,18 @@ func (r *RestoreJobReconciler) reconcileAltinityRestore(ctx context.Context, res
|
||||
},
|
||||
rendered,
|
||||
)
|
||||
if errors.Is(err, errAltinityPredecessorJob) {
|
||||
apimeta.SetStatusCondition(&restoreJob.Status.Conditions, metav1.Condition{
|
||||
Type: "Ready",
|
||||
Status: metav1.ConditionFalse,
|
||||
Reason: "PredecessorJobPending",
|
||||
Message: err.Error(),
|
||||
})
|
||||
if uerr := r.Status().Update(ctx, restoreJob); uerr != nil {
|
||||
return ctrl.Result{}, uerr
|
||||
}
|
||||
return ctrl.Result{RequeueAfter: altinityPollInterval}, nil
|
||||
}
|
||||
if err != nil {
|
||||
return r.markRestoreJobFailed(ctx, restoreJob, fmt.Sprintf("failed to ensure batch/v1.Job: %v", err))
|
||||
}
|
||||
|
||||
@@ -1809,3 +1809,100 @@ func TestReconcileAltinity_DoesNotReuseAJobItDoesNotControl(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestReconcileAltinityRestore_DoesNotReuseAJobItDoesNotControl is the restore
|
||||
// side of TestReconcileAltinity_DoesNotReuseAJobItDoesNotControl: taking
|
||||
// another Job's completion would report a restore that never ran.
|
||||
func TestReconcileAltinityRestore_DoesNotReuseAJobItDoesNotControl(t *testing.T) {
|
||||
restoreRef := func(name, uid string, gv schema.GroupVersion, kind string) []metav1.OwnerReference {
|
||||
owner := &backupsv1alpha1.RestoreJob{ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-test", UID: types.UID(uid)}}
|
||||
return []metav1.OwnerReference{*metav1.NewControllerRef(owner, gv.WithKind(kind))}
|
||||
}
|
||||
other := schema.GroupVersion{Group: "example.com", Version: "v1alpha1"}
|
||||
cases := []struct {
|
||||
name string
|
||||
owners []metav1.OwnerReference
|
||||
wantFailed bool
|
||||
}{
|
||||
{name: "an earlier RestoreJob of the same name controls it", owners: restoreRef("rj", "predecessor-uid", backupsv1alpha1.GroupVersion, "RestoreJob")},
|
||||
{name: "nothing controls it", wantFailed: true},
|
||||
{name: "a RestoreJob of another name controls it", wantFailed: true, owners: restoreRef("other-rj", "other-uid", backupsv1alpha1.GroupVersion, "RestoreJob")},
|
||||
{name: "a BackupJob controls it", wantFailed: true, owners: restoreRef("rj", "predecessor-uid", backupsv1alpha1.GroupVersion, "BackupJob")},
|
||||
{name: "a RestoreJob of another API group controls it", wantFailed: true, owners: restoreRef("rj", "predecessor-uid", other, "RestoreJob")},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
for _, raced := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s/raced=%t", tc.name, raced), func(t *testing.T) {
|
||||
targetApp := newAltinityClickHouseApp("ch-test", "tenant-test")
|
||||
strategy := newAltinityArchiveStrategy("clickhouse-strategy")
|
||||
now := metav1.Now()
|
||||
backup := &backupsv1alpha1.Backup{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "ch-backup", Namespace: "tenant-test"},
|
||||
Spec: backupsv1alpha1.BackupSpec{
|
||||
ApplicationRef: newAltinityClickHouseRef("ch-test"),
|
||||
StrategyRef: corev1.TypedLocalObjectReference{
|
||||
APIGroup: new(strategyv1alpha1.GroupVersion.Group),
|
||||
Kind: strategyv1alpha1.AltinityStrategyKind,
|
||||
Name: "clickhouse-strategy",
|
||||
},
|
||||
TakenAt: now,
|
||||
},
|
||||
}
|
||||
restoreJob := &backupsv1alpha1.RestoreJob{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "rj", Namespace: "tenant-test", UID: "this-uid"},
|
||||
Spec: backupsv1alpha1.RestoreJobSpec{BackupRef: corev1.LocalObjectReference{Name: backup.Name}},
|
||||
Status: backupsv1alpha1.RestoreJobStatus{StartedAt: &now, Phase: backupsv1alpha1.RestoreJobPhaseRunning},
|
||||
}
|
||||
job := &batchv1.Job{
|
||||
ObjectMeta: metav1.ObjectMeta{Name: jobNameForRestoreJob(restoreJob), Namespace: "tenant-test", OwnerReferences: tc.owners},
|
||||
Status: batchv1.JobStatus{Conditions: []batchv1.JobCondition{
|
||||
{Type: batchv1.JobComplete, Status: corev1.ConditionTrue},
|
||||
}},
|
||||
}
|
||||
builder := clientfake.NewClientBuilder().WithObjects(strategy, backup, restoreJob, 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...)
|
||||
},
|
||||
})
|
||||
}
|
||||
_, rr, _ := newAltinityTestEnv(t, targetApp, builder)
|
||||
ctx := context.Background()
|
||||
|
||||
res, err := rr.reconcileAltinityRestore(ctx, restoreJob, backup)
|
||||
if err != nil {
|
||||
t.Fatalf("reconcileAltinityRestore: %v", err)
|
||||
}
|
||||
|
||||
got := &backupsv1alpha1.RestoreJob{}
|
||||
if err := rr.Get(ctx, client.ObjectKeyFromObject(restoreJob), got); err != nil {
|
||||
t.Fatalf("get RestoreJob: %v", err)
|
||||
}
|
||||
if tc.wantFailed {
|
||||
if got.Status.Phase != backupsv1alpha1.RestoreJobPhaseFailed {
|
||||
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.RestoreJobPhaseRunning {
|
||||
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