Skip to content

Commit 200779f

Browse files
committed
fix: handle pod termination during reconcile
1 parent 88509ac commit 200779f

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

controllers/secretproviderclasspodstatus_controller.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,22 @@ func (r *SecretProviderClassPodStatusReconciler) Reconcile(req ctrl.Request) (ct
209209
return ctrl.Result{}, nil
210210
}
211211

212+
// Obtain the full pod metadata. An object reference is needed for sending
213+
// events and the UID is helpful for validating the SPCPS TargetPath.
214+
pod := &v1.Pod{}
215+
if err := r.reader.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName}, pod); err != nil {
216+
klog.ErrorS(err, "failed to get pod", "pod", klog.ObjectRef{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName})
217+
if apierrors.IsNotFound(err) {
218+
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
219+
}
220+
return ctrl.Result{}, err
221+
}
222+
// pod is being terminated so don't reconcile
223+
if !pod.GetDeletionTimestamp().IsZero() {
224+
klog.InfoS("pod is being terminated, skipping reconcile", "pod", klog.KObj(pod))
225+
return ctrl.Result{}, nil
226+
}
227+
212228
spcName := spcPodStatus.Status.SecretProviderClassName
213229
spc := &v1alpha1.SecretProviderClass{}
214230
if err := r.reader.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: spcName}, spc); err != nil {
@@ -224,17 +240,6 @@ func (r *SecretProviderClassPodStatusReconciler) Reconcile(req ctrl.Request) (ct
224240
return ctrl.Result{}, nil
225241
}
226242

227-
// Obtain the full pod metadata. An object reference is needed for sending
228-
// events and the UID is helpful for validating the SPCPS TargetPath.
229-
pod := &v1.Pod{}
230-
if err := r.reader.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName}, pod); err != nil {
231-
klog.ErrorS(err, "failed to get pod", "pod", klog.ObjectRef{Namespace: req.Namespace, Name: spcPodStatus.Status.PodName})
232-
if apierrors.IsNotFound(err) {
233-
return ctrl.Result{RequeueAfter: 5 * time.Second}, nil
234-
}
235-
return ctrl.Result{}, err
236-
}
237-
238243
// determine which pod volume this is associated with
239244
podVol := k8sutil.SPCVolume(pod, spc.Name)
240245
if podVol == nil {

pkg/rotation/reconciler.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
172172
r.reporter.reportRotationDuration(time.Since(begin).Seconds())
173173
}()
174174

175+
// get pod from informer cache
176+
podName, podNamespace := spcps.Status.PodName, spcps.Namespace
177+
pod, err := r.store.GetPod(podName, podNamespace)
178+
if err != nil {
179+
errorReason = internalerrors.PodNotFound
180+
return fmt.Errorf("failed to get pod %s/%s, err: %+v", podNamespace, podName, err)
181+
}
182+
// if the pod is being terminated, then skip rotation
183+
// the spcps will be gc when the pod is deleted and will not show up in the next rotation cycle
184+
if !pod.GetDeletionTimestamp().IsZero() {
185+
klog.InfoS("pod is being terminated, skipping rotation", "pod", klog.KObj(pod))
186+
return nil
187+
}
188+
175189
spcName, spcNamespace := spcps.Status.SecretProviderClassName, spcps.Namespace
176190

177191
// get the secret provider class the pod status is referencing from informer cache
@@ -180,13 +194,6 @@ func (r *Reconciler) reconcile(ctx context.Context, spcps *v1alpha1.SecretProvid
180194
errorReason = internalerrors.SecretProviderClassNotFound
181195
return fmt.Errorf("failed to get secret provider class %s/%s, err: %+v", spcNamespace, spcName, err)
182196
}
183-
// get pod from informer cache
184-
podName, podNamespace := spcps.Status.PodName, spcps.Namespace
185-
pod, err := r.store.GetPod(podName, podNamespace)
186-
if err != nil {
187-
errorReason = internalerrors.PodNotFound
188-
return fmt.Errorf("failed to get pod %s/%s, err: %+v", podNamespace, podName, err)
189-
}
190197

191198
// determine which pod volume this is associated with
192199
podVol := k8sutil.SPCVolume(pod, spc.Name)

pkg/rotation/reconciler_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,17 @@ func TestReconcileNoError(t *testing.T) {
596596
// 2 normal events - one for successfully updating the mounted contents and
597597
// second for successfully rotating the K8s secret
598598
g.Expect(len(fakeRecorder.Events)).To(BeNumerically("==", 2))
599+
600+
// test with pod being terminated
601+
podToAdd.DeletionTimestamp = &metav1.Time{Time: time.Now()}
602+
kubeClient = fake.NewSimpleClientset(podToAdd, secretToAdd)
603+
testReconciler, err = newTestReconciler(scheme, kubeClient, crdClient, ctrlClient, 60*time.Second, socketPath)
604+
g.Expect(err).NotTo(HaveOccurred())
605+
err = testReconciler.store.Run(wait.NeverStop)
606+
g.Expect(err).NotTo(HaveOccurred())
607+
608+
err = testReconciler.reconcile(context.TODO(), secretProviderClassPodStatusToProcess)
609+
g.Expect(err).NotTo(HaveOccurred())
599610
}
600611

601612
func TestPatchSecret(t *testing.T) {

0 commit comments

Comments
 (0)