Merge pull request #130509 from cici37/automated-cherry-pick-of-#130035-upstream-release-1.31

Automated cherry pick of #130035: [KEP-5080]Ordered Namespace Deletion
This commit is contained in:
Kubernetes Prow Robot
2025-03-07 22:45:44 -08:00
committed by GitHub
4 changed files with 175 additions and 0 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"k8s.io/kubernetes/pkg/features"
"strings"
"sync"
"time"
@@ -475,3 +476,124 @@ func unstructuredToNamespace(obj *unstructured.Unstructured) (*v1.Namespace, err
return ns, err
}
var _ = SIGDescribe("OrderedNamespaceDeletion", func() {
f := framework.NewDefaultFramework("namespacedeletion")
f.NamespacePodSecurityLevel = admissionapi.LevelBaseline
f.It("namespace deletion should delete pod first", feature.OrderedNamespaceDeletion, framework.WithFeatureGate(features.OrderedNamespaceDeletion), func(ctx context.Context) {
ensurePodsAreRemovedFirstInOrderedNamespaceDeletion(ctx, f)
})
})
func ensurePodsAreRemovedFirstInOrderedNamespaceDeletion(ctx context.Context, f *framework.Framework) {
ginkgo.By("Creating a test namespace")
namespaceName := "nsdeletetest"
namespace, err := f.CreateNamespace(ctx, namespaceName, nil)
framework.ExpectNoError(err, "failed to create namespace: %s", namespaceName)
nsName := namespace.Name
ginkgo.By("Waiting for a default service account to be provisioned in namespace")
err = framework.WaitForDefaultServiceAccountInNamespace(ctx, f.ClientSet, nsName)
framework.ExpectNoError(err, "failure while waiting for a default service account to be provisioned in namespace: %s", nsName)
ginkgo.By("Creating a pod with finalizer in the namespace")
podName := "test-pod"
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Finalizers: []string{
"e2e.example.com/finalizer",
},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "nginx",
Image: imageutils.GetPauseImageName(),
},
},
},
}
pod, err = f.ClientSet.CoreV1().Pods(nsName).Create(ctx, pod, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create pod %s in namespace: %s", podName, nsName)
ginkgo.By("Waiting for the pod to have running status")
framework.ExpectNoError(e2epod.WaitForPodRunningInNamespace(ctx, f.ClientSet, pod))
configMapName := "test-configmap"
ginkgo.By(fmt.Sprintf("Creating a configmap %q in namespace %q", configMapName, nsName))
configMap := &v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: nsName,
},
Data: map[string]string{
"key": "value",
},
}
_, err = f.ClientSet.CoreV1().ConfigMaps(nsName).Create(ctx, configMap, metav1.CreateOptions{})
framework.ExpectNoError(err, "failed to create configmap %q in namespace %q", configMapName, nsName)
ginkgo.By("Deleting the namespace")
err = f.ClientSet.CoreV1().Namespaces().Delete(ctx, nsName, metav1.DeleteOptions{})
framework.ExpectNoError(err, "failed to delete namespace: %s", nsName)
// wait 10 seconds to allow the namespace controller to process
time.Sleep(10 * time.Second)
ginkgo.By("the pod should be deleted before processing deletion for other resources")
framework.ExpectNoError(wait.PollUntilContextTimeout(ctx, 2*time.Second, 60*time.Second, true,
func(ctx context.Context) (bool, error) {
_, err = f.ClientSet.CoreV1().ConfigMaps(nsName).Get(ctx, configMapName, metav1.GetOptions{})
framework.ExpectNoError(err, "configmap %q should still exist in namespace %q", configMapName, nsName)
// the pod should exist and has a deletionTimestamp set
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.Logf("Pod %q in namespace %q does not yet have a metadata.deletionTimestamp set, retrying...", pod.Name, nsName)
return false, nil
}
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
}))
ginkgo.By(fmt.Sprintf("Removing finalizer from pod %q in namespace %q", podName, nsName))
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
pod, err = f.ClientSet.CoreV1().Pods(nsName).Get(ctx, podName, metav1.GetOptions{})
framework.ExpectNoError(err, "failed to get pod %q in namespace %q", pod.Name, nsName)
pod.Finalizers = []string{}
_, err = f.ClientSet.CoreV1().Pods(nsName).Update(ctx, pod, metav1.UpdateOptions{})
return err
})
framework.ExpectNoError(err, "failed to update pod %q and remove finalizer in namespace %q", podName, nsName)
ginkgo.By("Waiting for the namespace to be removed.")
maxWaitSeconds := int64(60) + *pod.Spec.TerminationGracePeriodSeconds
framework.ExpectNoError(wait.PollUntilContextTimeout(ctx, 1*time.Second, time.Duration(maxWaitSeconds)*time.Second, true,
func(ctx context.Context) (bool, error) {
_, err = f.ClientSet.CoreV1().Namespaces().Get(ctx, namespace.Name, metav1.GetOptions{})
if err != nil && apierrors.IsNotFound(err) {
return true, nil
}
return false, nil
}))
}

View File

@@ -251,6 +251,10 @@ var (
// TODO: document the feature (owning SIG, when to use this feature for a test)
NodeOutOfServiceVolumeDetach = framework.WithFeature(framework.ValidFeatures.Add("NodeOutOfServiceVolumeDetach"))
// Owner: sig-api-machinery
// Marks tests that enforce ordered namespace deletion.
OrderedNamespaceDeletion = framework.WithFeature(framework.ValidFeatures.Add("OrderedNamespaceDeletion"))
// Owner: sig-network
// Marks a single test that tests cluster DNS performance with many services.
PerformanceDNS = framework.WithFeature(framework.ValidFeatures.Add("PerformanceDNS"))