From ae9dcc0a75a2669194e812ace83f7b68adcf7dc2 Mon Sep 17 00:00:00 2001 From: Cici Huang Date: Thu, 6 Mar 2025 08:10:46 -0800 Subject: [PATCH] Update status before returning err --- .../deletion/namespaced_resources_deleter.go | 5 +++++ test/e2e/apimachinery/namespace.go | 22 +++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/controller/namespace/deletion/namespaced_resources_deleter.go b/pkg/controller/namespace/deletion/namespaced_resources_deleter.go index 81f0d9b13c3..7197adbacec 100644 --- a/pkg/controller/namespace/deletion/namespaced_resources_deleter.go +++ b/pkg/controller/namespace/deletion/namespaced_resources_deleter.go @@ -553,6 +553,11 @@ func (d *namespacedResourcesDeleter) deleteAllContent(ctx context.Context, ns *v // Check if any pods remain before proceeding to delete other resources if numRemainingTotals.gvrToNumRemaining[podsGVR] > 0 { logger.V(5).Info("Namespace controller - pods still remain, delaying deletion of other resources", "namespace", namespace) + if hasChanged := conditionUpdater.Update(ns); hasChanged { + if _, err = d.nsClient.UpdateStatus(ctx, ns, metav1.UpdateOptions{}); err != nil { + utilruntime.HandleError(fmt.Errorf("couldn't update status condition for namespace %q: %w", namespace, err)) + } + } return estimate, utilerrors.NewAggregate(errs) } } diff --git a/test/e2e/apimachinery/namespace.go b/test/e2e/apimachinery/namespace.go index f99922abee5..7c3910af1f0 100644 --- a/test/e2e/apimachinery/namespace.go +++ b/test/e2e/apimachinery/namespace.go @@ -549,12 +549,30 @@ func ensurePodsAreRemovedFirstInOrderedNamespaceDeletion(ctx context.Context, f pod, err = f.ClientSet.CoreV1().Pods(nsName).Get(ctx, pod.Name, metav1.GetOptions{}) framework.ExpectNoError(err, "failed to get pod %q in namespace %q", pod.Name, nsName) if pod.DeletionTimestamp == nil { - framework.Failf("Pod %q in namespace %q does not have a metadata.deletionTimestamp set", pod.Name, nsName) + framework.Logf("Pod %q in namespace %q does not yet have a metadata.deletionTimestamp set, retrying...", pod.Name, nsName) + return false, nil } - _, err = f.ClientSet.CoreV1().Namespaces().Get(ctx, nsName, metav1.GetOptions{}) + ns, err := f.ClientSet.CoreV1().Namespaces().Get(ctx, nsName, metav1.GetOptions{}) if err != nil && apierrors.IsNotFound(err) { return false, fmt.Errorf("namespace %s was deleted unexpectedly", nsName) } + ginkgo.By("Read namespace status") + nsResource := v1.SchemeGroupVersion.WithResource("namespaces") + unstruct, err := f.DynamicClient.Resource(nsResource).Get(ctx, ns.Name, metav1.GetOptions{}, "status") + framework.ExpectNoError(err, "failed to fetch NamespaceStatus %s", ns) + nsStatus, err := unstructuredToNamespace(unstruct) + framework.ExpectNoError(err, "Getting the status of the namespace %s", ns) + gomega.Expect(nsStatus.Status.Phase).To(gomega.Equal(v1.NamespaceTerminating), "The phase returned was %v", nsStatus.Status.Phase) + hasContextFailure := false + for _, cond := range nsStatus.Status.Conditions { + if cond.Type == v1.NamespaceDeletionContentFailure { + hasContextFailure = true + } + } + if !hasContextFailure { + framework.Logf("Namespace %q does not yet have a NamespaceDeletionContentFailure condition, retrying...", nsName) + return false, nil + } return true, nil }))