Skip to content

Commit

Permalink
add delete all
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
  • Loading branch information
cpanato committed Apr 24, 2019
1 parent 540204c commit 189d19a
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions pkg/tool/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,21 @@ func (k Kubectl) DeleteNamespace(namespace string) {
fmt.Printf("Namespace '%s' did not terminate after %s.\n", namespace, timeoutSec)
}

if _, err := k.exec.RunProcessAndCaptureOutput("kubectl", "get", "namespace", namespace); err != nil {
fmt.Printf("Namespace '%s' terminated.\n", namespace)
return
}

fmt.Printf("Namespace '%s' did not terminate after %s.\n", namespace, timeoutSec)
if k.getNamespace(namespace) {
fmt.Printf("Namespace '%s' did not terminate after %s.\n", namespace, timeoutSec)

fmt.Println("Force-deleting pods...")
if err := k.exec.RunProcess("kubectl", "delete", "pods", "--namespace", namespace, "--all", "--force", "--grace-period=0"); err != nil {
fmt.Println("Error deleting pods:", err)
}
fmt.Println("Force-deleting everything...")
if err := k.exec.RunProcess("kubectl", "delete", "all", "--namespace", namespace, "--all", "--force", "--grace-period=0"); err != nil {
fmt.Printf("Error deleting everything in the namespace %v: %v", namespace, err)
}

fmt.Println("Force-deleting pvcs...")
if err := k.exec.RunProcess("kubectl", "delete", "pvc", "--namespace", namespace, "--all", "--force", "--grace-period=0"); err != nil {
fmt.Println("Error deleting pvc(s):", err)
}
// Give it some more time to be deleted by K8s
time.Sleep(5 * time.Second)

// Give it some more time to be deleted by K8s
time.Sleep(5 * time.Second)

if _, err := k.exec.RunProcessAndCaptureOutput("kubectl", "get", "namespace", namespace); err != nil {
fmt.Printf("Namespace '%s' terminated.\n", namespace)
} else {
if err := k.forceNamespaceDeletion(namespace); err != nil {
fmt.Println("Error force deleting namespace:", err)
if k.getNamespace(namespace) {
if err := k.forceNamespaceDeletion(namespace); err != nil {
fmt.Println("Error force deleting namespace:", err)
}
}
}
}
Expand Down Expand Up @@ -209,3 +199,12 @@ func (k Kubectl) GetInitContainers(namespace string, pod string) ([]string, erro
func (k Kubectl) GetContainers(namespace string, pod string) ([]string, error) {
return k.GetPods(pod, "--no-headers", "--namespace", namespace, "--output", "jsonpath={.spec.containers[*].name}")
}

func (k Kubectl) getNamespace(namespace string) bool {
if _, err := k.exec.RunProcessAndCaptureOutput("kubectl", "get", "namespace", namespace); err != nil {
fmt.Printf("Namespace '%s' terminated.\n", namespace)
return false
}

return true
}

0 comments on commit 189d19a

Please sign in to comment.