Delete ns using kubectl

Signed-off-by: danfengl <danfengl@vmware.com>
This commit is contained in:
danfengl
2024-03-11 03:45:24 +00:00
parent 3c704ba1b1
commit b605f9dbd5
3 changed files with 33 additions and 18 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)