From 8e67cfd5c99a91963b0dab89fc039131a476b3b9 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Thu, 23 Jul 2020 08:41:27 +0300 Subject: [PATCH] Delete custom resources during uninstall Remove Kustomizations, GitRepositories and HelmRepositories before deleting the toolkit controllers and CRDs. --- cmd/tk/uninstall.go | 45 +++++++++++++++++++++------------------- docs/cmd/tk_uninstall.md | 12 +++++------ 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/cmd/tk/uninstall.go b/cmd/tk/uninstall.go index a9295119..5aca8a24 100644 --- a/cmd/tk/uninstall.go +++ b/cmd/tk/uninstall.go @@ -19,10 +19,12 @@ package main import ( "context" "fmt" - "time" "github.com/manifoldco/promptui" "github.com/spf13/cobra" + + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1" + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" ) var uninstallCmd = &cobra.Command{ @@ -33,24 +35,24 @@ var uninstallCmd = &cobra.Command{ tk uninstall --dry-run --namespace=gitops-system # Uninstall all components and delete custom resource definitions - tk uninstall --crds --namespace=gitops-system + tk uninstall --resources --crds --namespace=gitops-system `, RunE: uninstallCmdRun, } var ( - uninstallCRDs bool - uninstallKustomizations bool - uninstallDryRun bool - uninstallSilent bool + uninstallCRDs bool + uninstallResources bool + uninstallDryRun bool + uninstallSilent bool ) func init() { - uninstallCmd.Flags().BoolVarP(&uninstallKustomizations, "kustomizations", "", false, - "removes all Kustomizations previously installed") - uninstallCmd.Flags().BoolVarP(&uninstallCRDs, "crds", "", false, + uninstallCmd.Flags().BoolVar(&uninstallResources, "resources", false, + "removes custom resources such as Kustomizations, GitRepositories and HelmRepositories") + uninstallCmd.Flags().BoolVar(&uninstallCRDs, "crds", false, "removes all CRDs previously installed") - uninstallCmd.Flags().BoolVarP(&uninstallDryRun, "dry-run", "", false, + uninstallCmd.Flags().BoolVar(&uninstallDryRun, "dry-run", false, "only print the object that would be deleted") uninstallCmd.Flags().BoolVarP(&uninstallSilent, "silent", "s", false, "delete components without asking for confirmation") @@ -75,18 +77,19 @@ func uninstallCmdRun(cmd *cobra.Command, args []string) error { } } - if uninstallKustomizations { - logger.Actionf("uninstalling kustomizations") - command := fmt.Sprintf("kubectl -n %s delete kustomizations --all --timeout=%s %s", - namespace, timeout.String(), dryRun) - if _, err := utils.execCommand(ctx, ModeOS, command); err != nil { - return fmt.Errorf("uninstall failed") + if uninstallResources { + logger.Actionf("uninstalling custom resources") + for _, kind := range []string{ + kustomizev1.KustomizationKind, + sourcev1.GitRepositoryKind, + sourcev1.HelmRepositoryKind, + } { + command := fmt.Sprintf("kubectl -n %s delete %s --all --timeout=%s %s", + namespace, kind, timeout.String(), dryRun) + if _, err := utils.execCommand(ctx, ModeOS, command); err != nil { + return fmt.Errorf("uninstall failed") + } } - - // TODO: use the kustomizations snapshots to create a list of objects - // that are subject to deletion and wait for all of them to be terminated - logger.Waitingf("waiting on GC") - time.Sleep(30 * time.Second) } kinds := "namespace,clusterroles,clusterrolebindings" diff --git a/docs/cmd/tk_uninstall.md b/docs/cmd/tk_uninstall.md index c2643774..1ba46e3c 100644 --- a/docs/cmd/tk_uninstall.md +++ b/docs/cmd/tk_uninstall.md @@ -17,18 +17,18 @@ tk uninstall [flags] tk uninstall --dry-run --namespace=gitops-system # Uninstall all components and delete custom resource definitions - tk uninstall --crds --namespace=gitops-system + tk uninstall --resources --crds --namespace=gitops-system ``` ### Options ``` - --crds removes all CRDs previously installed - --dry-run only print the object that would be deleted - -h, --help help for uninstall - --kustomizations removes all Kustomizations previously installed - -s, --silent delete components without asking for confirmation + --crds removes all CRDs previously installed + --dry-run only print the object that would be deleted + -h, --help help for uninstall + --resources removes custom resources such as Kustomizations, GitRepositories and HelmRepositories + -s, --silent delete components without asking for confirmation ``` ### Options inherited from parent commands