Skip to content

Commit ed2c0d7

Browse files
authored
fix: remove finalizer when validation label is removed from pod (#179)
* fix: remove finalizer when validation label is removed from pod When a user removes the validation.ml.sigstore.dev/ml label from a running pod, the PodReconciler now removes the finalizer before cleaning up tracking. Previously the finalizer was left behind, preventing the pod from being garbage-collected until an explicit deletion event. Signed-off-by: Sachin Sampras M <sampras343@gmail.com> * ci: retrigger checks Signed-off-by: Sachin Sampras M <sampras343@gmail.com> --------- Signed-off-by: Sachin Sampras M <sampras343@gmail.com>
1 parent 533e84d commit ed2c0d7

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎internal/controller/pod_controller.go‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ func (r *PodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result
7474

7575
modelValidationName, ok := pod.Labels[constants.ModelValidationLabel]
7676
if !ok || modelValidationName == "" {
77-
// Try to remove the pod in case it was previously tracked but label was removed
77+
if controllerutil.ContainsFinalizer(pod, constants.ModelValidationFinalizer) {
78+
controllerutil.RemoveFinalizer(pod, constants.ModelValidationFinalizer)
79+
if err := r.Update(ctx, pod); err != nil {
80+
logger.Error(err, "Failed to remove finalizer from unlabeled pod", "pod", req.NamespacedName)
81+
return reconcile.Result{}, err
82+
}
83+
}
7884
if err := r.Tracker.RemovePodByName(ctx, req.NamespacedName); err != nil {
7985
logger.Error(err, "Failed to remove pod without label from tracking", "pod", req.NamespacedName)
8086
return reconcile.Result{}, err

‎internal/controller/pod_controller_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,38 @@ var _ = Describe("PodReconciler", func() {
5959
Expect(removeByNameCalls[0].Namespace).To(Equal("default"))
6060
})
6161

62+
It("should remove finalizer when label is removed from a previously injected pod", func() {
63+
pod := testutil.CreateTestPod(testutil.TestPodOptions{
64+
Name: "unlabeled-pod",
65+
Namespace: "default",
66+
Finalizers: []string{constants.ModelValidationFinalizer},
67+
})
68+
69+
fakeClient := testutil.SetupFakeClientWithObjects(pod)
70+
71+
reconciler = &PodReconciler{
72+
Client: fakeClient,
73+
Scheme: runtime.NewScheme(),
74+
Tracker: mockTracker,
75+
}
76+
77+
req := testutil.CreateReconcileRequest(pod.Namespace, pod.Name)
78+
79+
result, err := reconciler.Reconcile(ctx, req)
80+
Expect(err).NotTo(HaveOccurred())
81+
Expect(result).To(Equal(ctrl.Result{}))
82+
83+
updatedPod := &corev1.Pod{}
84+
err = fakeClient.Get(ctx, req.NamespacedName, updatedPod)
85+
Expect(err).NotTo(HaveOccurred())
86+
Expect(updatedPod.Finalizers).NotTo(ContainElement(constants.ModelValidationFinalizer),
87+
"Finalizer must be removed when label is absent")
88+
89+
removeByNameCalls := mockTracker.GetRemovePodByNameCalls()
90+
Expect(removeByNameCalls).To(HaveLen(1))
91+
Expect(removeByNameCalls[0].Name).To(Equal("unlabeled-pod"))
92+
})
93+
6294
It("should process pods with finalizer but not being deleted", func() {
6395
pod := testutil.CreateTestPod(testutil.TestPodOptions{
6496
Name: "test-pod",

0 commit comments

Comments
 (0)