From ff71af0673708383eb339c5abb4f1af513b2ae4a Mon Sep 17 00:00:00 2001 From: Lukasz Zajaczkowski Date: Thu, 12 Sep 2024 14:48:56 +0200 Subject: [PATCH] delete plural-runtime namespace before executing terraform in plural down --- pkg/up/deploy.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/pkg/up/deploy.go b/pkg/up/deploy.go index 319906e2..7f85ba3c 100644 --- a/pkg/up/deploy.go +++ b/pkg/up/deploy.go @@ -1,12 +1,18 @@ package up import ( + "context" "encoding/json" "fmt" "os" "os/exec" "time" + "github.com/pluralsh/plural-cli/pkg/kubernetes" + "github.com/samber/lo" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "github.com/pluralsh/plural-cli/pkg/utils" ) @@ -84,12 +90,42 @@ func (ctx *Context) Deploy(commit func() error) error { } func (ctx *Context) Destroy() error { + if err := ctx.DestroyNamespace("plural-runtime"); err != nil { + return err + } return runAll([]terraformCmd{ {dir: "./terraform/mgmt", cmd: "init", args: []string{"-upgrade"}}, {dir: "./terraform/mgmt", cmd: "destroy", args: []string{"-auto-approve"}, retries: 2}, }) } +func (ctx *Context) DestroyNamespace(name string) error { + utils.Highlight("\nCleaning up namespace %s...\n", name) + // ensure current kubeconfig is correct before destroying stuff + if err := ctx.Provider.KubeConfig(); err != nil { + return err + } + kube, err := kubernetes.Kubernetes() + if err != nil { + utils.Error("Could not set up k8s client due to %s\n", err) + return err + } + c := context.Background() + namespace, err := kube.GetClient().CoreV1().Namespaces().Get(c, name, metav1.GetOptions{}) + if err != nil && !errors.IsNotFound(err) { + return err + } + + if namespace != nil { + return kube.GetClient().CoreV1().Namespaces().Delete(c, name, metav1.DeleteOptions{ + GracePeriodSeconds: lo.ToPtr(int64(0)), + }) + } + + return nil + +} + func runAll(cmds []terraformCmd) error { for _, cmd := range cmds { if err := cmd.run(); err != nil {