← Back to Pull requests

Fix leftover importer pod blocking PVC deletion after a failed cleanup #4293

Open
pujitha24 opened this pull request with 45 lines diff across 2 files
View on GitHub
pujitha24 pujitha24 commented on Sep 23, 2026 16:01 UTC
Author

<!-- Thanks for sending a pull request! Here are some tips for you:
1. Follow the instructions for writing a release note from k8s: https://git.k8s.io/community/contributors/guide/release-notes.md
-->

What this PR does / why we need it:

If the importer pod delete in updatePvcFromPod fails after a successful import (e.g. a transient API server error), the error is returned and the reconcile is requeued. On that requeue, shouldReconcilePVC returns false because the PVC's phase is already Succeeded, so Reconcile returns early and never retries the pod delete. The "PVC being terminated, delete pods" branch in reconcilePvc sits behind that same early-return check, so it is unreachable too — even after the PVC is deleted later, CDI never tries to remove the leftover pod. The pod keeps a controller owner reference (blockOwnerDeletion: true) to the PVC, and Kubernetes' pvc-protection controller treats any pod naming the PVC as a user regardless of phase, so the PVC's pvc-protection finalizer is never cleared and the PVC hangs in Terminating indefinitely.

This PR makes Reconcile skip the early return for PVCs that CDI actually manages (has the import AnnEndpoint/AnnSource annotations) while they are being deleted, so reconcilePvc's existing terminating-PVC branch can run and clean up a leftover importer pod even when the PVC itself was already marked complete. The gate is intentionally scoped to CDI import PVCs only (same annotation check shouldReconcilePVC already uses), so unrelated terminating PVCs that this controller merely watches are unaffected and don't pick up any extra reconcile activity.

Per review, the import controller's PVC watch now also has a predicate that only admits PVCs with AnnEndpoint/AnnSource, so unrelated PVCs are dropped before they reach the queue. Reconcile already returned early for them, so this doesn't change behavior. The check in Reconcile is kept because the Pod owner watch can still enqueue non-import PVCs. The same change for the upload and clone controllers is left for a follow-up.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

This addresses the second of the two approaches suggested in the issue: run the "PVC being terminated, delete pods" branch even when the PVC is already complete, instead of retrying the initial post-import delete. It's the smaller, more surgical change of the two options.

Validation performed locally (no live cluster available in this environment):

  • go build ./pkg/controller/... and go vet ./pkg/controller/... — clean.

  • go test ./pkg/controller/ -run TestController — full existing suite (375 specs after the new tests) passes.

  • Added a regression test, "Should delete a leftover importer pod when a completed PVC is being terminated", that reproduces the bug: fails against the unpatched code (importer pod is never deleted), passes with the fix.

  • Added a second test, "Should not requeue an unrelated terminating PVC that CDI never imported into", verifying the gate stays scoped to CDI-managed PVCs and doesn't add reconcile work for unrelated terminating PVCs (they can still reach Reconcile through the Pod owner watch).

  • gofmt -l on the changed files: clean.

  • Could not run golangci-lint (the repo's pinned v1.64.8) end to end: it fails with widespread undefined: Expect/It/... typecheck errors across many pre-existing files in pkg/controller/ unrelated to this change, reproducing identically against the unmodified tree — this looks like a local go.work vendor sync issue in this environment (go build/go test print benign "ignoring package ... missing from vendor/modules.txt" warnings but still succeed), not something introduced by this diff.

  • This repo's CI is Prow-gated; real CI on a fork PR needs a maintainer to add ok-to-test.
  • Release note:
    <!-- Write your release note:
    1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
    2. If no release note is required, just write "NONE".
    -->
    ``release-note
    Fixed a bug where a leftover importer pod could keep a PVC stuck in
    Terminating forever if the pod's delete call failed once right after a successful import.
    ``

    Fixes #4292

    Diff: 45 lines (+43 / -2) across 2 files Reviewer: akalenyu Review duration: 101567m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.34) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.34 · 45 lines diff across 2 files
    0.34
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 5 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    87%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.229
    Real PR Conversation Timeline
    14 items
    kubevirt-prow[bot] kubevirt-prow[bot] commented on Sep 23, 2026 16:02 UTC
    Bot

    Hi @pujitha24. Thanks for your PR.

    PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

    I understand the commands that are listed here.

    <details>

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
    </details>

    kubevirt-prow[bot] kubevirt-prow[bot] commented on Sep 23, 2026 16:02 UTC
    Bot

    [APPROVALNOTIFIER] This PR is NOT APPROVED

    This pull-request has been approved by:
    Once this PR has been reviewed and has the lgtm label, please assign akalenyu for approval. For more information see the Code Review Process.

    The full list of commands accepted by this bot can be found here.

    <details open>
    Needs approval from an approver in each of these files:

  • OWNERS
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":["akalenyu"]} -->

    akalenyu akalenyu commented on code on Sep 24, 2026 08:50 UTC
    Code Review
    pkg/controller/import-controller_test.go:1
    @@ -222,6 +222,30 @@ var _ = Describe("ImportConfig Controller reconcile loop", func() {
     		Expect(reflect.DeepEqual(orgPvc, resPvc)).To(BeTrue())
     	})
     
    +	It("Should delete a leftover importer pod when a completed PVC is being terminated", func() {
    +		now := metav1.Now()
    +		pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodSucceeded)}, nil)
    +		pvc.DeletionTimestamp = &now
    +		pvc.Finalizers = []string{"kubernetes.io/pvc-protection"}
    +		pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
    +		reconciler = createImportReconciler(pvc, pod)
    +		_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
    +		Expect(err).ToNot(HaveOccurred())
    +		err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: "default"}, &corev1.Pod{})
    +		Expect(errors.IsNotFound(err)).To(BeTrue())
    +	})
    +
    +	It("Should not requeue an unrelated terminating PVC that CDI never imported into", func() {

    should we handle this on the predicate level? how come these make it into the queue at all?

    akalenyu akalenyu commented on Sep 24, 2026 08:51 UTC
    Reviewer

    thank you!

    akalenyu akalenyu commented on Sep 24, 2026 08:51 UTC
    Contributor

    /test all

    coveralls coveralls commented on Sep 24, 2026 09:05 UTC
    Contributor

    ![Coverage Status](https://coveralls.io/builds/81939478)

    coverage: 49.862% (+0.05%) from 49.812% — pujitha24:auto/issue-4292 into kubevirt:main

    pujitha24 pujitha24 commented on Sep 27, 2026 03:33 UTC
    Author

    They get in because the PVC watch in addImportControllerWatches is a plain EnqueueRequestForObject with no predicate, so every PVC event in the cluster gets queued and Reconcile does the filtering itself via shouldReconcilePVC. That test just pins down that my deletion-timestamp bypass doesn't widen that for PVCs without AnnEndpoint/AnnSource.

    A predicate on that watch that drops PVCs without those two annotations does seem cleaner to me. It would change what the import controller enqueues in general though, so do you want it in this PR, or as a separate follow-up?

    akalenyu akalenyu commented on Sep 27, 2026 10:51 UTC
    Contributor

    They get in because the PVC watch in addImportControllerWatches is a plain EnqueueRequestForObject with no predicate, so every PVC event in the cluster gets queued and Reconcile does the filtering itself via shouldReconcilePVC. That test just pins down that my deletion-timestamp bypass doesn't widen that for PVCs without AnnEndpoint/AnnSource.

    > A predicate on that watch that drops PVCs without those two annotations does seem cleaner to me. It would change what the import controller enqueues in general though, so do you want it in this PR, or as a separate follow-up?

    If it's otherwise equivalent (short circuited at predicate level instead of reconcile loop), we can include in this PR

    pujitha24 pujitha24 commented on Sep 27, 2026 10:55 UTC
    Author

    They make it in because the PVC watch in addImportControllerWatches uses a plain EnqueueRequestForObject with no predicate, so every PVC in the cluster gets enqueued and Reconcile does the filtering itself via shouldReconcilePVC. That's how it works on main too, not something this PR adds. This test is only there to check that the new terminating-PVC case doesn't pick up unannotated PVCs. I agree a predicate that only lets through PVCs with AnnEndpoint/AnnSource would be cleaner. The upload and clone controllers have the same unfiltered PVC watch, and a predicate changes what reaches the queue for every event, not just the deletion path, so should I add it in this PR or send it as a separate follow-up?

    akalenyu akalenyu commented on Sep 27, 2026 10:59 UTC
    Contributor

    They make it in because the PVC watch in addImportControllerWatches uses a plain EnqueueRequestForObject with no predicate, so every PVC in the cluster gets enqueued and Reconcile does the filtering itself via shouldReconcilePVC. That's how it works on main too, not something this PR adds. This test is only there to check that the new terminating-PVC case doesn't pick up unannotated PVCs. I agree a predicate that only lets through PVCs with AnnEndpoint/AnnSource would be cleaner. The upload and clone controllers have the same unfiltered PVC watch, and a predicate changes what reaches the queue for every event, not just the deletion path, so should I add it in this PR or send it as a separate follow-up?

    Other ones can be in follow up

    akalenyu akalenyu commented on code on Sep 28, 2026 09:02 UTC
    Code Review
    pkg/controller/import-controller_test.go:1
    @@ -222,6 +222,30 @@ var _ = Describe("ImportConfig Controller reconcile loop", func() {
     		Expect(reflect.DeepEqual(orgPvc, resPvc)).To(BeTrue())
     	})
     
    +	It("Should delete a leftover importer pod when a completed PVC is being terminated", func() {
    +		now := metav1.Now()
    +		pvc := cc.CreatePvc("testPvc1", "default", map[string]string{cc.AnnEndpoint: testEndPoint, cc.AnnPodPhase: string(corev1.PodSucceeded)}, nil)
    +		pvc.DeletionTimestamp = &now
    +		pvc.Finalizers = []string{"kubernetes.io/pvc-protection"}
    +		pod := cc.CreateImporterTestPod(pvc, "testPvc1", nil)
    +		reconciler = createImportReconciler(pvc, pod)
    +		_, err := reconciler.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "testPvc1", Namespace: "default"}})
    +		Expect(err).ToNot(HaveOccurred())
    +		err = reconciler.client.Get(context.TODO(), types.NamespacedName{Name: pod.Name, Namespace: "default"}, &corev1.Pod{})
    +		Expect(errors.IsNotFound(err)).To(BeTrue())
    +	})
    +
    +	It("Should not requeue an unrelated terminating PVC that CDI never imported into", func() {

    should we keep this test case in that case?

    pujitha24 pujitha24 commented on Sep 28, 2026 09:06 UTC
    Author

    I'd lean towards keeping it. The predicate only covers the PVC watch, but the Pod owner watch still enqueues any PVC that controls a pod. The upload server pod is created with MakePVCOwnerReference, so a terminating upload PVC still lands in import Reconcile. I tried dropping && isImportPVC(pvc) from the gate locally and this test fails: the unannotated PVC gets requeued every 2s. That said, if you'd rather drop it, or reword it so it's clearly about the owner-watch path, I'm happy to do either.

    pujitha24 pujitha24 commented on Oct 08, 2026 15:17 UTC
    Author

    /assign @akalenyu

    The PVC watch predicate is in and the checks are green, so this should be ready for another look when you have time.

    Reviewers
    Assignees
    pujitha24 pujitha24
    Labels
    risk: low confidence: 88.0% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 34.4% (LOW) · Higher = larger blast radius
    34.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.0% · Lower = rushed or shallow review
    88.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.1% · Safe to merge
    4.1%