From b605f9dbd590b74e7d81d5c26859c20ed0005a29 Mon Sep 17 00:00:00 2001 From: danfengl Date: Mon, 11 Mar 2024 03:45:24 +0000 Subject: [PATCH] Delete ns using kubectl Signed-off-by: danfengl --- test/e2e/basic/nodeport.go | 2 +- test/util/k8s/common.go | 6 ++++++ test/util/k8s/namespace.go | 43 +++++++++++++++++++++++--------------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/test/e2e/basic/nodeport.go b/test/e2e/basic/nodeport.go index 811e2fca4..bba687bbd 100644 --- a/test/e2e/basic/nodeport.go +++ b/test/e2e/basic/nodeport.go @@ -46,7 +46,7 @@ func (n *NodePort) Init() error { n.NSIncluded = &[]string{} for nsNum := 0; nsNum < n.NamespacesTotal; nsNum++ { createNSName := fmt.Sprintf("%s-%00000d", n.CaseBaseName, nsNum) - n.namespaceToCollision = append(n.namespaceToCollision, createNSName+"tmp") + n.namespaceToCollision = append(n.namespaceToCollision, createNSName+"-tmp") *n.NSIncluded = append(*n.NSIncluded, createNSName) } diff --git a/test/util/k8s/common.go b/test/util/k8s/common.go index 728704ef6..097e3be2f 100644 --- a/test/util/k8s/common.go +++ b/test/util/k8s/common.go @@ -190,6 +190,12 @@ func KubectlGetNS(ctx context.Context, name string) ([]string, error) { } cmds = append(cmds, cmd) + cmd = &common.OsCommandLine{ + Cmd: "awk", + Args: []string{"{print $1}"}, + } + cmds = append(cmds, cmd) + return common.GetListByCmdPipes(ctx, cmds) } diff --git a/test/util/k8s/namespace.go b/test/util/k8s/namespace.go index 3790b8a96..e747a4d15 100644 --- a/test/util/k8s/namespace.go +++ b/test/util/k8s/namespace.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "os/exec" + "slices" "strings" "time" @@ -31,6 +32,7 @@ import ( waitutil "k8s.io/apimachinery/pkg/util/wait" "github.com/vmware-tanzu/velero/pkg/builder" + veleroexec "github.com/vmware-tanzu/velero/pkg/util/exec" ) func CreateNamespace(ctx context.Context, client TestClient, namespace string) error { @@ -79,37 +81,44 @@ func GetNamespace(ctx context.Context, client TestClient, namespace string) (*co return client.ClientGo.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) } +func KubectlDeleteNamespace(ctx context.Context, namespace string) error { + args := []string{"delete", "namespace", namespace} + fmt.Println(args) + + cmd := exec.CommandContext(ctx, "kubectl", args...) + fmt.Println(cmd) + stdout, stderr, err := veleroexec.RunCommand(cmd) + fmt.Printf("Output: %v\n", stdout) + if strings.Contains(stderr, "NotFound") { + err = nil + } + return err +} + func DeleteNamespace(ctx context.Context, client TestClient, namespace string, wait bool) error { tenMinuteTimeout, ctxCancel := context.WithTimeout(context.Background(), time.Minute*10) defer ctxCancel() - var zero int64 = 0 - policy := metav1.DeletePropagationForeground - return waitutil.PollImmediateInfinite(5*time.Second, func() (bool, error) { // Retry namespace deletion, see issue: https://github.com/kubernetes/kubernetes/issues/60807 - if err := client.ClientGo.CoreV1().Namespaces().Delete(context.TODO(), namespace, metav1.DeleteOptions{ - GracePeriodSeconds: &zero, - PropagationPolicy: &policy, - }); err != nil { - if apierrors.IsNotFound(err) { - return true, nil - } else { - fmt.Printf("Delete namespace %s err: %v", namespace, err) - return false, errors.Wrap(err, fmt.Sprintf("failed to delete the namespace %q", namespace)) - } + if err := KubectlDeleteNamespace(tenMinuteTimeout, namespace); err != nil { + fmt.Printf("Delete namespace %s err: %v", namespace, err) + return false, errors.Wrap(err, fmt.Sprintf("failed to delete the namespace %q", namespace)) } if !wait { return true, nil } - ns, err := KubectlGetNS(tenMinuteTimeout, namespace) + nsList, err := KubectlGetNS(tenMinuteTimeout, namespace) + fmt.Println("kubectl get ns output:") + fmt.Println(nsList) if err != nil { - if len(ns) == 0 { - return true, nil - } fmt.Printf("Get namespace %s err: %v", namespace, err) return false, err + } else { + if !slices.Contains(nsList, namespace) { + return true, nil + } } fmt.Printf("namespace %q is still being deleted...\n", namespace) logrus.Debugf("namespace %q is still being deleted...", namespace)