From e1b3c8fd9b4259f847b44b7cfd634c73ac7b3fc7 Mon Sep 17 00:00:00 2001 From: Cici Huang Date: Thu, 6 Feb 2025 17:39:52 -0800 Subject: [PATCH] Add e2e test --- test/e2e/apimachinery/namespace.go | 104 +++++++++++++++++++++++++++++ test/e2e/feature/feature.go | 4 ++ 2 files changed, 108 insertions(+) diff --git a/test/e2e/apimachinery/namespace.go b/test/e2e/apimachinery/namespace.go index 121f4be1cc3..f99922abee5 100644 --- a/test/e2e/apimachinery/namespace.go +++ b/test/e2e/apimachinery/namespace.go @@ -20,6 +20,7 @@ import ( "context" "encoding/json" "fmt" + "k8s.io/kubernetes/pkg/features" "strings" "sync" "time" @@ -475,3 +476,106 @@ 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.Failf("Pod %q in namespace %q does not have a metadata.deletionTimestamp set", pod.Name, nsName) + } + _, 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) + } + 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 + })) +} diff --git a/test/e2e/feature/feature.go b/test/e2e/feature/feature.go index 8c86e861a63..fcf689c4ea9 100644 --- a/test/e2e/feature/feature.go +++ b/test/e2e/feature/feature.go @@ -337,6 +337,10 @@ var ( // Tests aiming to verify oom_score functionality OOMScoreAdj = framework.WithFeature(framework.ValidFeatures.Add("OOMScoreAdj")) + // Owner: sig-api-machinery + // Marks tests that enforce ordered namespace deletion. + OrderedNamespaceDeletion = framework.WithFeature(framework.ValidFeatures.Add("OrderedNamespaceDeletion")) + // Owner: sig-node // Verify ProcMount feature. // Used in combination with user namespaces