Skip to content

Commit eb8fbcf

Browse files
committed
fix: prevent job deletion until statefulset has been revived
1 parent d8fa120 commit eb8fbcf

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

internal/controller/job_controller.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"k8s.io/utils/ptr"
3434
ctrl "sigs.k8s.io/controller-runtime"
3535
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3637
klog "sigs.k8s.io/controller-runtime/pkg/log"
3738
)
3839

@@ -64,8 +65,8 @@ func (r *JobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
6465
return ctrl.Result{}, nil
6566
}
6667

67-
// The job has not completed, and so we have nothing to do
68-
if job.Status.CompletionTime.IsZero() {
68+
// The job has not completed, or StatefulSet has been updated, so we have nothing to do
69+
if job.Status.CompletionTime.IsZero() || !controllerutil.ContainsFinalizer(&job, finalizerName) {
6970
return ctrl.Result{}, nil
7071
}
7172

@@ -76,13 +77,9 @@ func (r *JobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
7677
return ctrl.Result{}, err
7778
}
7879

79-
// If the job status is `done`, we want to delete this job
80+
// If the job status is `done`, we have nothing to do
8081
if sfs.Annotations[iamProbeStatus] == iamProbeDone {
81-
err := client.IgnoreNotFound(r.Delete(ctx, &job))
82-
if err != nil {
83-
log.Error(err, "failed to queue job deletion")
84-
}
85-
return ctrl.Result{}, err
82+
return ctrl.Result{}, nil
8683
}
8784

8885
// Parse the IAM probe status to find the original replica count
@@ -153,9 +150,11 @@ func (r *JobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
153150
return ctrl.Result{}, err
154151
}
155152

156-
if err := r.Delete(ctx, &job); client.IgnoreNotFound(err) != nil {
157-
log.Error(err, "failed to queue job deletion")
158-
return ctrl.Result{}, err
153+
if controllerutil.RemoveFinalizer(&job, finalizerName) {
154+
if err := r.Update(ctx, &job); err != nil {
155+
log.Error(err, "failed to remove finalizer")
156+
return ctrl.Result{}, err
157+
}
159158
}
160159

161160
return ctrl.Result{}, nil

internal/controller/statefulset_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"k8s.io/utils/ptr"
2323
ctrl "sigs.k8s.io/controller-runtime"
2424
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2526
klog "sigs.k8s.io/controller-runtime/pkg/log"
2627
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
2728
)
@@ -214,6 +215,8 @@ func (m *StatefulsetMutator) launchIamProbe(ctx context.Context, sfs *appsv1.Sta
214215
},
215216
},
216217
}
218+
// Ensure Job isn't deleted before we've revived the StatefulSet
219+
controllerutil.AddFinalizer(probeJob, finalizerName)
217220

218221
return m.Client.Create(ctx, probeJob)
219222
}

0 commit comments

Comments
 (0)