Skip to content

Commit

Permalink
Remove force delete of pv and improve cleanup logic (#150)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Panato <[email protected]>
  • Loading branch information
cpanato authored and unguiculus committed Apr 24, 2019
1 parent d427587 commit 961a3ab
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions pkg/tool/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,26 @@ func NewKubectl(exec exec.ProcessExecutor) Kubectl {
// namespace and, eventually, the namespace itself are force-deleted.
func (k Kubectl) DeleteNamespace(namespace string) {
fmt.Printf("Deleting namespace '%s'...\n", namespace)
timeoutSec := "120s"
timeoutSec := "180s"
if err := k.exec.RunProcess("kubectl", "delete", "namespace", namespace, "--timeout", timeoutSec); err != nil {
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)

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)
}
if k.getNamespace(namespace) {
fmt.Printf("Namespace '%s' did not terminate after %s.\n", namespace, timeoutSec)

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)
}
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 pvs...")
if err := k.exec.RunProcess("kubectl", "delete", "pv", "--namespace", namespace, "--all", "--force", "--grace-period=0"); err != nil {
fmt.Println("Error deleting pv(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 @@ -214,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 961a3ab

Please sign in to comment.