From 6d590fe4d2f7bc55cc19043fa21342a3c0cd86e7 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 23:13:56 +0300 Subject: [PATCH] Implement delete for git sources - add delete source git command - add delete source git e2e test --- .github/workflows/e2e.yaml | 3 ++ cmd/tk/delete_source.go | 14 ++++++++ cmd/tk/delete_source_git.go | 68 +++++++++++++++++++++++++++++++++++++ cmd/tk/main.go | 29 +++++++++++++--- 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 cmd/tk/delete_source.go create mode 100644 cmd/tk/delete_source_git.go diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 99fd2160..004f731f 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -83,6 +83,9 @@ jobs: - name: tk delete kustomization run: | ./bin/tk delete kustomization podinfo --silent + - name: tk delete source git + run: | + ./bin/tk delete source git podinfo --silent - name: tk check run: | ./bin/tk check diff --git a/cmd/tk/delete_source.go b/cmd/tk/delete_source.go new file mode 100644 index 00000000..31e95266 --- /dev/null +++ b/cmd/tk/delete_source.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +var deleteSourceCmd = &cobra.Command{ + Use: "source", + Short: "Delete sources commands", +} + +func init() { + deleteCmd.AddCommand(deleteSourceCmd) +} diff --git a/cmd/tk/delete_source_git.go b/cmd/tk/delete_source_git.go new file mode 100644 index 00000000..ea9fb5a1 --- /dev/null +++ b/cmd/tk/delete_source_git.go @@ -0,0 +1,68 @@ +package main + +import ( + "context" + "fmt" + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" + + "github.com/manifoldco/promptui" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/types" +) + +var deleteSourceGitCmd = &cobra.Command{ + Use: "git [name]", + Short: "Delete git source", + RunE: deleteSourceGitCmdRun, +} + +func init() { + deleteSourceCmd.AddCommand(deleteSourceGitCmd) +} + +func deleteSourceGitCmdRun(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return fmt.Errorf("git name is required") + } + name := args[0] + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + kubeClient, err := utils.kubeClient(kubeconfig) + if err != nil { + return err + } + + namespacedName := types.NamespacedName{ + Namespace: namespace, + Name: name, + } + + var git sourcev1.GitRepository + err = kubeClient.Get(ctx, namespacedName, &git) + if err != nil { + return err + } + + if !deleteSilent { + prompt := promptui.Prompt{ + Label: fmt.Sprintf( + "Are you sure you want to delete the %s source from the %s namespace", name, namespace, + ), + IsConfirm: true, + } + if _, err := prompt.Run(); err != nil { + return fmt.Errorf("aborting") + } + } + + logAction("deleting source %s in %s namespace", name, namespace) + err = kubeClient.Delete(ctx, &git) + if err != nil { + return err + } + logSuccess("source deleted") + + return nil +} diff --git a/cmd/tk/main.go b/cmd/tk/main.go index afd97f65..5ea1f48b 100644 --- a/cmd/tk/main.go +++ b/cmd/tk/main.go @@ -27,14 +27,20 @@ var rootCmd = &cobra.Command{ tk install --version=master # Create a source from a public Git repository - tk create source git webapp \ + tk create source git webapp-latest \ --url=https://github.com/stefanprodan/podinfo \ --branch=master \ --interval=3m + # List git sources and their status + tk get sources git + + # Trigger a git sync + tk sync source git webapp-latest + # Create a kustomization for deploying a series of microservices - tk create kustomization webapp \ - --source=webapp \ + tk create kustomization webapp-dev \ + --source=webapp-latest \ --path="./deploy/webapp/" \ --prune="instance=webapp" \ --generate=true \ @@ -45,7 +51,22 @@ var rootCmd = &cobra.Command{ --health-check-timeout=2m # Trigger a git sync and apply changes if any - sync kustomization webapp --with-source + tk sync kustomization webapp-dev --with-source + + # Suspend a kustomization reconciliation + tk suspend kustomization webapp-dev + + # Resume a kustomization reconciliation + tk resume kustomization webapp-dev + + # Delete a kustomization + tk delete kustomization webapp-dev + + # Delete a git source + tk delete source git webapp-latest + + # Uninstall the toolkit and delete CRDs + tk uninstall --crds `, }