From c6793f6eaf04d6b095ef76916c09768477a4d24a Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Tue, 28 Apr 2020 23:51:23 +0300 Subject: [PATCH 1/8] Refactor create command - move GitRepository operations to create source git - update examples and e2e tests --- .github/workflows/e2e.yaml | 4 +- cmd/tk/create_source.go | 277 +--------------------------------- cmd/tk/create_source_git.go | 287 ++++++++++++++++++++++++++++++++++++ cmd/tk/main.go | 6 +- 4 files changed, 295 insertions(+), 279 deletions(-) create mode 100644 cmd/tk/create_source_git.go diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 622636da..0400754b 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -51,7 +51,9 @@ jobs: ./bin/tk install --manifests ./manifests/install/ - name: Run create source e2e tests run: | - ./bin/tk create source podinfo --git-url https://github.com/stefanprodan/podinfo-deploy --git-semver=">=0.0.1-rc.1 <0.1.0" + ./bin/tk create source git podinfo \ + --git-url https://github.com/stefanprodan/podinfo-deploy \ + --git-semver=">=0.0.1-rc.1 <0.1.0" - name: Debug failure if: failure() run: | diff --git a/cmd/tk/create_source.go b/cmd/tk/create_source.go index 344ba02b..9f4dc58d 100644 --- a/cmd/tk/create_source.go +++ b/cmd/tk/create_source.go @@ -1,287 +1,14 @@ package main import ( - "context" - "fmt" - "io/ioutil" - "net/url" - "os" - "strings" - "time" - - sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" - "github.com/manifoldco/promptui" "github.com/spf13/cobra" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/wait" - "sigs.k8s.io/controller-runtime/pkg/client" ) var createSourceCmd = &cobra.Command{ - Use: "source [name]", - Short: "Create or update a source resource", - Long: ` -The create source command generates a source.fluxcd.io resource and waits for it to sync. -For Git over SSH, host and SSH keys are automatically generated and stored in a Kubernetes secret. -For private Git repositories, the basic authentication credentials are stored in a Kubernetes secret.`, - Example: ` # Create a source from a public Git repository master branch - create source podinfo \ - --git-url=https://github.com/stefanprodan/podinfo \ - --git-branch=master - - # Create a source from a Git repository pinned to specific git tag - create source podinfo \ - --git-url=https://github.com/stefanprodan/podinfo \ - --git-tag="3.2.3" - - # Create a source from a public Git repository tag that matches a semver range - create source podinfo \ - --git-url=https://github.com/stefanprodan/podinfo \ - --git-semver=">=3.2.0 <3.3.0" - - # Create a source from a Git repository using SSH authentication - create source podinfo \ - --git-url=ssh://git@github.com/stefanprodan/podinfo \ - --git-branch=master - - # Create a source from a Git repository using basic authentication - create source podinfo \ - --git-url=https://github.com/stefanprodan/podinfo \ - --username=username \ - --password=password -`, - RunE: createSourceCmdRun, + Use: "source", + Short: "Create source commands", } -var ( - sourceGitURL string - sourceGitBranch string - sourceGitTag string - sourceGitSemver string - sourceUsername string - sourcePassword string -) - func init() { - createSourceCmd.Flags().StringVar(&sourceGitURL, "git-url", "", "git address, e.g. ssh://git@host/org/repository") - createSourceCmd.Flags().StringVar(&sourceGitBranch, "git-branch", "master", "git branch") - createSourceCmd.Flags().StringVar(&sourceGitTag, "git-tag", "", "git tag") - createSourceCmd.Flags().StringVar(&sourceGitSemver, "git-semver", "", "git tag semver range") - createSourceCmd.Flags().StringVarP(&sourceUsername, "username", "u", "", "basic authentication username") - createSourceCmd.Flags().StringVarP(&sourcePassword, "password", "p", "", "basic authentication password") - createCmd.AddCommand(createSourceCmd) } - -func createSourceCmdRun(cmd *cobra.Command, args []string) error { - if len(args) < 1 { - return fmt.Errorf("source name is required") - } - name := args[0] - - if sourceGitURL == "" { - return fmt.Errorf("git-url is required") - } - - tmpDir, err := ioutil.TempDir("", name) - if err != nil { - return err - } - defer os.RemoveAll(tmpDir) - - u, err := url.Parse(sourceGitURL) - if err != nil { - return fmt.Errorf("git URL parse failed: %w", err) - } - - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - - withAuth := false - if strings.HasPrefix(sourceGitURL, "ssh") { - if err := generateSSH(ctx, name, u.Host, tmpDir); err != nil { - return err - } - withAuth = true - } else if sourceUsername != "" && sourcePassword != "" { - if err := generateBasicAuth(ctx, name); err != nil { - return err - } - withAuth = true - } - - logAction("generating source %s in %s namespace", name, namespace) - - gitRepository := sourcev1.GitRepository{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, - }, - Spec: sourcev1.GitRepositorySpec{ - URL: sourceGitURL, - Interval: metav1.Duration{ - Duration: interval, - }, - Reference: &sourcev1.GitRepositoryRef{}, - }, - } - - if withAuth { - gitRepository.Spec.SecretRef = &corev1.LocalObjectReference{ - Name: name, - } - } - - if sourceGitSemver != "" { - gitRepository.Spec.Reference.SemVer = sourceGitSemver - } else if sourceGitTag != "" { - gitRepository.Spec.Reference.Tag = sourceGitTag - } else { - gitRepository.Spec.Reference.Branch = sourceGitBranch - } - - kubeClient, err := utils.kubeClient(kubeconfig) - if err != nil { - return err - } - - if err := upsertGitRepository(ctx, kubeClient, gitRepository); err != nil { - return err - } - - logAction("waiting for source sync") - if err := wait.PollImmediate(2*time.Second, timeout, - isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { - return err - } - - logSuccess("source %s is ready", name) - - namespacedName := types.NamespacedName{ - Namespace: namespace, - Name: name, - } - err = kubeClient.Get(ctx, namespacedName, &gitRepository) - if err != nil { - return fmt.Errorf("source sync failed: %w", err) - } - - if gitRepository.Status.Artifact != nil { - logSuccess("fetched revision %s", gitRepository.Status.Artifact.Revision) - } else { - return fmt.Errorf("source sync failed, artifact not found") - } - - return nil -} - -func generateBasicAuth(ctx context.Context, name string) error { - logAction("saving credentials") - credentials := fmt.Sprintf("--from-literal=username='%s' --from-literal=password='%s'", - sourceUsername, sourcePassword) - secret := fmt.Sprintf("kubectl -n %s create secret generic %s %s --dry-run=client -oyaml | kubectl apply -f-", - namespace, name, credentials) - if _, err := utils.execCommand(ctx, ModeOS, secret); err != nil { - return fmt.Errorf("kubectl create secret failed") - } - return nil -} - -func generateSSH(ctx context.Context, name, host, tmpDir string) error { - logAction("generating host key for %s", host) - - command := fmt.Sprintf("ssh-keyscan %s > %s/known_hosts", host, tmpDir) - if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { - return fmt.Errorf("ssh-keyscan failed") - } - - logAction("generating deploy key") - - command = fmt.Sprintf("ssh-keygen -b 2048 -t rsa -f %s/identity -q -N \"\"", tmpDir) - if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { - return fmt.Errorf("ssh-keygen failed") - } - - command = fmt.Sprintf("cat %s/identity.pub", tmpDir) - if deployKey, err := utils.execCommand(ctx, ModeCapture, command); err != nil { - return fmt.Errorf("unable to read identity.pub: %w", err) - } else { - fmt.Print(deployKey) - } - - prompt := promptui.Prompt{ - Label: "Have you added the deploy key to your repository", - IsConfirm: true, - } - if _, err := prompt.Run(); err != nil { - return fmt.Errorf("aborting") - } - - logAction("saving deploy key") - files := fmt.Sprintf("--from-file=%s/identity --from-file=%s/identity.pub --from-file=%s/known_hosts", - tmpDir, tmpDir, tmpDir) - secret := fmt.Sprintf("kubectl -n %s create secret generic %s %s --dry-run=client -oyaml | kubectl apply -f-", - namespace, name, files) - if _, err := utils.execCommand(ctx, ModeOS, secret); err != nil { - return fmt.Errorf("create secret failed") - } - return nil -} - -func upsertGitRepository(ctx context.Context, kubeClient client.Client, gitRepository sourcev1.GitRepository) error { - namespacedName := types.NamespacedName{ - Namespace: gitRepository.GetNamespace(), - Name: gitRepository.GetName(), - } - - var existing sourcev1.GitRepository - err := kubeClient.Get(ctx, namespacedName, &existing) - if err != nil { - if errors.IsNotFound(err) { - if err := kubeClient.Create(ctx, &gitRepository); err != nil { - return err - } else { - logSuccess("source created") - return nil - } - } - return err - } - - existing.Spec = gitRepository.Spec - if err := kubeClient.Update(ctx, &existing); err != nil { - return err - } - - logSuccess("source updated") - return nil -} - -func isGitRepositoryReady(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc { - return func() (bool, error) { - var gitRepository sourcev1.GitRepository - namespacedName := types.NamespacedName{ - Namespace: namespace, - Name: name, - } - - err := kubeClient.Get(ctx, namespacedName, &gitRepository) - if err != nil { - return false, err - } - - for _, condition := range gitRepository.Status.Conditions { - if condition.Type == sourcev1.ReadyCondition { - if condition.Status == corev1.ConditionTrue { - return true, nil - } else if condition.Status == corev1.ConditionFalse { - return false, fmt.Errorf(condition.Message) - } - } - } - return false, nil - } -} diff --git a/cmd/tk/create_source_git.go b/cmd/tk/create_source_git.go new file mode 100644 index 00000000..3dc1acb1 --- /dev/null +++ b/cmd/tk/create_source_git.go @@ -0,0 +1,287 @@ +package main + +import ( + "context" + "fmt" + "io/ioutil" + "net/url" + "os" + "strings" + "time" + + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" + "github.com/manifoldco/promptui" + "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/wait" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var createSourceGitCmd = &cobra.Command{ + Use: "git [name]", + Short: "Create or update a git source", + Long: ` +The create source command generates a GitRepository resource and waits for it to sync. +For Git over SSH, host and SSH keys are automatically generated and stored in a Kubernetes secret. +For private Git repositories, the basic authentication credentials are stored in a Kubernetes secret.`, + Example: ` # Create a source from a public Git repository master branch + create source git podinfo \ + --url=https://github.com/stefanprodan/podinfo \ + --branch=master + + # Create a source from a Git repository pinned to specific git tag + create source git podinfo \ + --url=https://github.com/stefanprodan/podinfo \ + --tag="3.2.3" + + # Create a source from a public Git repository tag that matches a semver range + create source git podinfo \ + --url=https://github.com/stefanprodan/podinfo \ + --tag-semver=">=3.2.0 <3.3.0" + + # Create a source from a Git repository using SSH authentication + create source git podinfo \ + --url=ssh://git@github.com/stefanprodan/podinfo \ + --branch=master + + # Create a source from a Git repository using basic authentication + create source git podinfo \ + --url=https://github.com/stefanprodan/podinfo \ + --username=username \ + --password=password +`, + RunE: createSourceGitCmdRun, +} + +var ( + sourceGitURL string + sourceGitBranch string + sourceGitTag string + sourceGitSemver string + sourceGitUsername string + sourceGitPassword string +) + +func init() { + createSourceGitCmd.Flags().StringVar(&sourceGitURL, "url", "", "git address, e.g. ssh://git@host/org/repository") + createSourceGitCmd.Flags().StringVar(&sourceGitBranch, "branch", "master", "git branch") + createSourceGitCmd.Flags().StringVar(&sourceGitTag, "tag", "", "git tag") + createSourceGitCmd.Flags().StringVar(&sourceGitSemver, "tag-semver", "", "git tag semver range") + createSourceGitCmd.Flags().StringVarP(&sourceGitUsername, "username", "u", "", "basic authentication username") + createSourceGitCmd.Flags().StringVarP(&sourceGitPassword, "password", "p", "", "basic authentication password") + + createSourceCmd.AddCommand(createSourceGitCmd) +} + +func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return fmt.Errorf("source name is required") + } + name := args[0] + + if sourceGitURL == "" { + return fmt.Errorf("git-url is required") + } + + tmpDir, err := ioutil.TempDir("", name) + if err != nil { + return err + } + defer os.RemoveAll(tmpDir) + + u, err := url.Parse(sourceGitURL) + if err != nil { + return fmt.Errorf("git URL parse failed: %w", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + withAuth := false + if strings.HasPrefix(sourceGitURL, "ssh") { + if err := generateSSH(ctx, name, u.Host, tmpDir); err != nil { + return err + } + withAuth = true + } else if sourceGitUsername != "" && sourceGitPassword != "" { + if err := generateBasicAuth(ctx, name); err != nil { + return err + } + withAuth = true + } + + logAction("generating git source %s in %s namespace", name, namespace) + + gitRepository := sourcev1.GitRepository{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: namespace, + }, + Spec: sourcev1.GitRepositorySpec{ + URL: sourceGitURL, + Interval: metav1.Duration{ + Duration: interval, + }, + Reference: &sourcev1.GitRepositoryRef{}, + }, + } + + if withAuth { + gitRepository.Spec.SecretRef = &corev1.LocalObjectReference{ + Name: name, + } + } + + if sourceGitSemver != "" { + gitRepository.Spec.Reference.SemVer = sourceGitSemver + } else if sourceGitTag != "" { + gitRepository.Spec.Reference.Tag = sourceGitTag + } else { + gitRepository.Spec.Reference.Branch = sourceGitBranch + } + + kubeClient, err := utils.kubeClient(kubeconfig) + if err != nil { + return err + } + + if err := upsertGitRepository(ctx, kubeClient, gitRepository); err != nil { + return err + } + + logAction("waiting for git sync") + if err := wait.PollImmediate(2*time.Second, timeout, + isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { + return err + } + + logSuccess("git sync completed") + + namespacedName := types.NamespacedName{ + Namespace: namespace, + Name: name, + } + err = kubeClient.Get(ctx, namespacedName, &gitRepository) + if err != nil { + return fmt.Errorf("git sync failed: %w", err) + } + + if gitRepository.Status.Artifact != nil { + logSuccess("fetched revision %s", gitRepository.Status.Artifact.Revision) + } else { + return fmt.Errorf("git sync failed, artifact not found") + } + + return nil +} + +func generateBasicAuth(ctx context.Context, name string) error { + logAction("saving credentials") + credentials := fmt.Sprintf("--from-literal=username='%s' --from-literal=password='%s'", + sourceGitUsername, sourceGitPassword) + secret := fmt.Sprintf("kubectl -n %s create secret generic %s %s --dry-run=client -oyaml | kubectl apply -f-", + namespace, name, credentials) + if _, err := utils.execCommand(ctx, ModeOS, secret); err != nil { + return fmt.Errorf("kubectl create secret failed") + } + return nil +} + +func generateSSH(ctx context.Context, name, host, tmpDir string) error { + logAction("generating host key for %s", host) + + command := fmt.Sprintf("ssh-keyscan %s > %s/known_hosts", host, tmpDir) + if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { + return fmt.Errorf("ssh-keyscan failed") + } + + logAction("generating deploy key") + + command = fmt.Sprintf("ssh-keygen -b 2048 -t rsa -f %s/identity -q -N \"\"", tmpDir) + if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { + return fmt.Errorf("ssh-keygen failed") + } + + command = fmt.Sprintf("cat %s/identity.pub", tmpDir) + if deployKey, err := utils.execCommand(ctx, ModeCapture, command); err != nil { + return fmt.Errorf("unable to read identity.pub: %w", err) + } else { + fmt.Print(deployKey) + } + + prompt := promptui.Prompt{ + Label: "Have you added the deploy key to your repository", + IsConfirm: true, + } + if _, err := prompt.Run(); err != nil { + return fmt.Errorf("aborting") + } + + logAction("saving deploy key") + files := fmt.Sprintf("--from-file=%s/identity --from-file=%s/identity.pub --from-file=%s/known_hosts", + tmpDir, tmpDir, tmpDir) + secret := fmt.Sprintf("kubectl -n %s create secret generic %s %s --dry-run=client -oyaml | kubectl apply -f-", + namespace, name, files) + if _, err := utils.execCommand(ctx, ModeOS, secret); err != nil { + return fmt.Errorf("create secret failed") + } + return nil +} + +func upsertGitRepository(ctx context.Context, kubeClient client.Client, gitRepository sourcev1.GitRepository) error { + namespacedName := types.NamespacedName{ + Namespace: gitRepository.GetNamespace(), + Name: gitRepository.GetName(), + } + + var existing sourcev1.GitRepository + err := kubeClient.Get(ctx, namespacedName, &existing) + if err != nil { + if errors.IsNotFound(err) { + if err := kubeClient.Create(ctx, &gitRepository); err != nil { + return err + } else { + logSuccess("source created") + return nil + } + } + return err + } + + existing.Spec = gitRepository.Spec + if err := kubeClient.Update(ctx, &existing); err != nil { + return err + } + + logSuccess("source updated") + return nil +} + +func isGitRepositoryReady(ctx context.Context, kubeClient client.Client, name, namespace string) wait.ConditionFunc { + return func() (bool, error) { + var gitRepository sourcev1.GitRepository + namespacedName := types.NamespacedName{ + Namespace: namespace, + Name: name, + } + + err := kubeClient.Get(ctx, namespacedName, &gitRepository) + if err != nil { + return false, err + } + + for _, condition := range gitRepository.Status.Conditions { + if condition.Type == sourcev1.ReadyCondition { + if condition.Status == corev1.ConditionTrue { + return true, nil + } else if condition.Status == corev1.ConditionFalse { + return false, fmt.Errorf(condition.Message) + } + } + } + return false, nil + } +} diff --git a/cmd/tk/main.go b/cmd/tk/main.go index 4cde0e67..17660b77 100644 --- a/cmd/tk/main.go +++ b/cmd/tk/main.go @@ -28,9 +28,9 @@ var rootCmd = &cobra.Command{ tk install --version=master # Create a source from a public Git repository - tk create source webapp \ - --git-url=https://github.com/stefanprodan/podinfo \ - --git-branch=master \ + tk create source git webapp \ + --url=https://github.com/stefanprodan/podinfo \ + --branch=master \ --interval=5m # Create a kustomization for deploying a series of microservices From 8d15970dbe91188a1018fe47db00a5c7fdebd409 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 08:25:12 +0300 Subject: [PATCH 2/8] Add wait log --- cmd/tk/create_kustomization.go | 2 +- cmd/tk/create_source_git.go | 2 +- cmd/tk/install.go | 4 ++-- cmd/tk/log.go | 19 ++++++++++++++++++ cmd/tk/main.go | 35 +++++++++++----------------------- 5 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 cmd/tk/log.go diff --git a/cmd/tk/create_kustomization.go b/cmd/tk/create_kustomization.go index 87011d1a..54e6c5b2 100644 --- a/cmd/tk/create_kustomization.go +++ b/cmd/tk/create_kustomization.go @@ -158,7 +158,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { return err } - logAction("waiting for kustomization sync") + logWaiting("waiting for kustomization sync") if err := wait.PollImmediate(2*time.Second, timeout, isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { return err diff --git a/cmd/tk/create_source_git.go b/cmd/tk/create_source_git.go index 3dc1acb1..245c4d99 100644 --- a/cmd/tk/create_source_git.go +++ b/cmd/tk/create_source_git.go @@ -152,7 +152,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { return err } - logAction("waiting for git sync") + logWaiting("waiting for git sync") if err := wait.PollImmediate(2*time.Second, timeout, isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { return err diff --git a/cmd/tk/install.go b/cmd/tk/install.go index 256e264b..04a093b2 100644 --- a/cmd/tk/install.go +++ b/cmd/tk/install.go @@ -88,7 +88,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { } logSuccess("build completed") - logAction("installing components in %s namespace", namespace) + logWaiting("installing components in %s namespace", namespace) applyOutput := ModeStderrOS if verbose { applyOutput = ModeOS @@ -110,7 +110,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { logSuccess("install completed") } - logAction("verifying installation") + logWaiting("verifying installation") for _, deployment := range components { command = fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s", namespace, deployment, timeout.String()) diff --git a/cmd/tk/log.go b/cmd/tk/log.go new file mode 100644 index 00000000..c06f3b59 --- /dev/null +++ b/cmd/tk/log.go @@ -0,0 +1,19 @@ +package main + +import "fmt" + +func logAction(format string, a ...interface{}) { + fmt.Println(`✚`, fmt.Sprintf(format, a...)) +} + +func logWaiting(format string, a ...interface{}) { + fmt.Println(`◎`, fmt.Sprintf(format, a...)) +} + +func logSuccess(format string, a ...interface{}) { + fmt.Println(`✔`, fmt.Sprintf(format, a...)) +} + +func logFailure(format string, a ...interface{}) { + fmt.Println(`✗`, fmt.Sprintf(format, a...)) +} diff --git a/cmd/tk/main.go b/cmd/tk/main.go index 17660b77..b113fee9 100644 --- a/cmd/tk/main.go +++ b/cmd/tk/main.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "log" "os" "path/filepath" @@ -78,23 +77,14 @@ func main() { } } -func homeDir() string { - if h := os.Getenv("HOME"); h != "" { - return h +func kubeconfigFlag() { + if home := homeDir(); home != "" { + rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"), + "path to the kubeconfig file") + } else { + rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "", + "absolute path to the kubeconfig file") } - return os.Getenv("USERPROFILE") // windows -} - -func logAction(format string, a ...interface{}) { - fmt.Println(`✚`, fmt.Sprintf(format, a...)) -} - -func logSuccess(format string, a ...interface{}) { - fmt.Println(`✔`, fmt.Sprintf(format, a...)) -} - -func logFailure(format string, a ...interface{}) { - fmt.Println(`✗`, fmt.Sprintf(format, a...)) } func generateDocs() { @@ -110,12 +100,9 @@ func generateDocs() { } } -func kubeconfigFlag() { - if home := homeDir(); home != "" { - rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", filepath.Join(home, ".kube", "config"), - "path to the kubeconfig file") - } else { - rootCmd.PersistentFlags().StringVarP(&kubeconfig, "kubeconfig", "", "", - "absolute path to the kubeconfig file") +func homeDir() string { + if h := os.Getenv("HOME"); h != "" { + return h } + return os.Getenv("USERPROFILE") // windows } From 21e0d38152af85a496dc354b9800026f91775a01 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 08:33:35 +0300 Subject: [PATCH 3/8] Implement sync command - sync git sources - sync kustomizations along with their sources --- cmd/tk/main.go | 7 ++- cmd/tk/sync.go | 14 +++++ cmd/tk/sync_kustomization.go | 107 +++++++++++++++++++++++++++++++++++ cmd/tk/sync_source.go | 14 +++++ cmd/tk/sync_source_git.go | 85 ++++++++++++++++++++++++++++ 5 files changed, 225 insertions(+), 2 deletions(-) create mode 100644 cmd/tk/sync.go create mode 100644 cmd/tk/sync_kustomization.go create mode 100644 cmd/tk/sync_source.go create mode 100644 cmd/tk/sync_source_git.go diff --git a/cmd/tk/main.go b/cmd/tk/main.go index b113fee9..6fbee802 100644 --- a/cmd/tk/main.go +++ b/cmd/tk/main.go @@ -19,7 +19,7 @@ var rootCmd = &cobra.Command{ SilenceUsage: true, SilenceErrors: true, Short: "Command line utility for assembling Kubernetes CD pipelines", - Long: `Command line utility for assembling Kubernetes CD pipelines.`, + Long: `Command line utility for assembling Kubernetes CD pipelines the GitOps way.`, Example: ` # Check prerequisites tk check --pre @@ -30,7 +30,7 @@ var rootCmd = &cobra.Command{ tk create source git webapp \ --url=https://github.com/stefanprodan/podinfo \ --branch=master \ - --interval=5m + --interval=3m # Create a kustomization for deploying a series of microservices tk create kustomization webapp \ @@ -43,6 +43,9 @@ var rootCmd = &cobra.Command{ --health-check="Deployment/backend.webapp" \ --health-check="Deployment/frontend.webapp" \ --health-check-timeout=2m + + # Trigger a git sync and apply changes if any + sync kustomization webapp --with-source `, } diff --git a/cmd/tk/sync.go b/cmd/tk/sync.go new file mode 100644 index 00000000..c51555aa --- /dev/null +++ b/cmd/tk/sync.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +var syncCmd = &cobra.Command{ + Use: "sync", + Short: "Synchronize commands", +} + +func init() { + rootCmd.AddCommand(syncCmd) +} diff --git a/cmd/tk/sync_kustomization.go b/cmd/tk/sync_kustomization.go new file mode 100644 index 00000000..951ceb81 --- /dev/null +++ b/cmd/tk/sync_kustomization.go @@ -0,0 +1,107 @@ +package main + +import ( + "context" + "fmt" + "time" + + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1alpha1" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/wait" +) + +var syncKsCmd = &cobra.Command{ + Use: "kustomization [name]", + Short: "Synchronize kustomization", + Long: ` +The sync kustomization command triggers a reconciliation of a Kustomization resource and waits for it to finish.`, + Example: ` # Trigger a kustomization apply outside of the reconciliation interval + sync kustomization podinfo + + # Trigger a git sync of the kustomization source and apply changes + sync kustomization podinfo --with-source +`, + RunE: syncKsCmdRun, +} + +var ( + syncKsWithSource bool +) + +func init() { + syncKsCmd.Flags().BoolVar(&syncKsWithSource, "with-source", false, "synchronize kustomization source") + + syncCmd.AddCommand(syncKsCmd) +} + +func syncKsCmdRun(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return fmt.Errorf("kustomization 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 kustomization kustomizev1.Kustomization + err = kubeClient.Get(ctx, namespacedName, &kustomization) + if err != nil { + return err + } + + if syncKsWithSource { + err := syncSourceGitCmdRun(nil, []string{kustomization.Spec.SourceRef.Name}) + if err != nil { + return err + } + } else { + logAction("annotating kustomization %s in %s namespace", name, namespace) + if kustomization.Annotations == nil { + kustomization.Annotations = map[string]string{ + kustomizev1.SyncAtAnnotation: time.Now().String(), + } + } else { + kustomization.Annotations[kustomizev1.SyncAtAnnotation] = time.Now().String() + } + if err := kubeClient.Update(ctx, &kustomization); err != nil { + return err + } + logSuccess("kustomization annotated") + } + + logWaiting("waiting for kustomization sync") + if err := wait.PollImmediate(2*time.Second, timeout, + isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { + return err + } + + logSuccess("kustomization sync completed") + + err = kubeClient.Get(ctx, namespacedName, &kustomization) + if err != nil { + return err + } + + err = kubeClient.Get(ctx, namespacedName, &kustomization) + if err != nil { + return err + } + + if kustomization.Status.LastAppliedRevision != "" { + logSuccess("applied revision %s", kustomization.Status.LastAppliedRevision) + } else { + return fmt.Errorf("kustomization sync failed") + } + return nil +} diff --git a/cmd/tk/sync_source.go b/cmd/tk/sync_source.go new file mode 100644 index 00000000..ce2ec70d --- /dev/null +++ b/cmd/tk/sync_source.go @@ -0,0 +1,14 @@ +package main + +import ( + "github.com/spf13/cobra" +) + +var syncSourceCmd = &cobra.Command{ + Use: "source", + Short: "Synchronize source commands", +} + +func init() { + syncCmd.AddCommand(syncSourceCmd) +} diff --git a/cmd/tk/sync_source_git.go b/cmd/tk/sync_source_git.go new file mode 100644 index 00000000..b714f2bb --- /dev/null +++ b/cmd/tk/sync_source_git.go @@ -0,0 +1,85 @@ +package main + +import ( + "context" + "fmt" + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/wait" + "time" +) + +var syncSourceGitCmd = &cobra.Command{ + Use: "git [name]", + Short: "Synchronize git source", + Long: ` +The sync source command triggers a reconciliation of a GitRepository resource and waits for it to finish.`, + Example: ` # Trigger a git pull for an existing source + sync source git podinfo +`, + RunE: syncSourceGitCmdRun, +} + +func init() { + syncSourceCmd.AddCommand(syncSourceGitCmd) +} + +func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return fmt.Errorf("source 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, + } + + logAction("annotating source %s in %s namespace", name, namespace) + var gitRepository sourcev1.GitRepository + err = kubeClient.Get(ctx, namespacedName, &gitRepository) + if err != nil { + return err + } + + if gitRepository.Annotations == nil { + gitRepository.Annotations = map[string]string{ + sourcev1.SyncAtAnnotation: time.Now().String(), + } + } else { + gitRepository.Annotations[sourcev1.SyncAtAnnotation] = time.Now().String() + } + if err := kubeClient.Update(ctx, &gitRepository); err != nil { + return err + } + logSuccess("source annotated") + + logWaiting("waiting for git sync") + if err := wait.PollImmediate(2*time.Second, timeout, + isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { + return err + } + + logSuccess("git sync completed") + + err = kubeClient.Get(ctx, namespacedName, &gitRepository) + if err != nil { + return err + } + + if gitRepository.Status.Artifact != nil { + logSuccess("fetched revision %s", gitRepository.Status.Artifact.Revision) + } else { + return fmt.Errorf("git sync failed, artifact not found") + } + return nil +} From aedff58f98b92c16805c47ef6f39421c62cbee76 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 08:59:03 +0300 Subject: [PATCH 4/8] ci: add kustomization e2e tests --- .github/workflows/e2e.yaml | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 0400754b..6161bd4e 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -37,23 +37,40 @@ jobs: fi - name: Build run: sudo go build -o ./bin/tk ./cmd/tk - - name: Run check e2e tests + - name: tk check --pre run: | - ./bin/tk check - - name: Run install version e2e tests + ./bin/tk check --pre + - name: tk install --version run: | ./bin/tk install --version=master --namespace=test --verbose - - name: Run uninstall e2e tests + - name: tk uninstall run: | ./bin/tk uninstall --namespace=test --crds --silent - - name: Run dev install e2e tests + - name: tk install --manifests run: | ./bin/tk install --manifests ./manifests/install/ - - name: Run create source e2e tests + - name: tk create source git run: | ./bin/tk create source git podinfo \ - --git-url https://github.com/stefanprodan/podinfo-deploy \ - --git-semver=">=0.0.1-rc.1 <0.1.0" + --url https://github.com/stefanprodan/podinfo \ + --tag-semver=">=3.2.3" + - name: tk create kustomization + run: | + ./bin/tk create kustomization podinfo \ + --source=podinfo \ + --path="./deploy/overlays/dev" \ + --prune="env=dev,instance=webapp" \ + --interval=5m \ + --validate=client \ + --health-check="Deployment/frontend.dev" \ + --health-check="Deployment/backend.dev" \ + --health-check-timeout=3m + - name: tk sync kustomization --with-source + run: | + ./bin/tk sync kustomization podinfo --with-source + - name: tk check + run: | + ./bin/tk check - name: Debug failure if: failure() run: | From 0f99f313a054fbe96f5977295a3530541dec2bc1 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 09:10:27 +0300 Subject: [PATCH 5/8] check: exit 1 on control plane failed checks --- cmd/tk/check.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/tk/check.go b/cmd/tk/check.go index 35b17485..a5826ce2 100644 --- a/cmd/tk/check.go +++ b/cmd/tk/check.go @@ -202,14 +202,16 @@ func componentsCheck() bool { ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel() + ok := true for _, deployment := range components { command := fmt.Sprintf("kubectl -n %s rollout status deployment %s --timeout=%s", namespace, deployment, timeout.String()) if output, err := utils.execCommand(ctx, ModeCapture, command); err != nil { logFailure("%s: %s", deployment, strings.TrimSuffix(output, "\n")) + ok = false } else { logSuccess("%s is healthy", deployment) } } - return true + return ok } From cfa72ddb0b83f133dd5532ee13e43724ea5c5aca Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 09:35:49 +0300 Subject: [PATCH 6/8] log: improve cmd logs --- cmd/tk/check.go | 1 + cmd/tk/create_kustomization.go | 3 ++- cmd/tk/create_source_git.go | 13 +++++++++---- cmd/tk/install.go | 6 +++--- cmd/tk/log.go | 4 ++++ cmd/tk/uninstall.go | 2 +- 6 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cmd/tk/check.go b/cmd/tk/check.go index a5826ce2..5e3a04c2 100644 --- a/cmd/tk/check.go +++ b/cmd/tk/check.go @@ -69,6 +69,7 @@ func runCheckCmd(cmd *cobra.Command, args []string) error { return nil } + logAction("checking controllers") if !componentsCheck() { checkFailed = true } diff --git a/cmd/tk/create_kustomization.go b/cmd/tk/create_kustomization.go index 54e6c5b2..b20c7487 100644 --- a/cmd/tk/create_kustomization.go +++ b/cmd/tk/create_kustomization.go @@ -95,7 +95,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { return err } - logAction("generating %s kustomization", name) + logGenerate("generating kustomization") emptyAPIGroup := "" kustomization := kustomizev1.Kustomization{ @@ -154,6 +154,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { } } + logAction("applying kustomization") if err := upsertKustomization(ctx, kubeClient, kustomization); err != nil { return err } diff --git a/cmd/tk/create_source_git.go b/cmd/tk/create_source_git.go index 245c4d99..6c0a409b 100644 --- a/cmd/tk/create_source_git.go +++ b/cmd/tk/create_source_git.go @@ -113,7 +113,11 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { withAuth = true } - logAction("generating git source %s in %s namespace", name, namespace) + if withAuth { + logSuccess("authentication configured") + } + + logGenerate("generating source") gitRepository := sourcev1.GitRepository{ ObjectMeta: metav1.ObjectMeta{ @@ -148,6 +152,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { return err } + logAction("applying source") if err := upsertGitRepository(ctx, kubeClient, gitRepository); err != nil { return err } @@ -191,14 +196,14 @@ func generateBasicAuth(ctx context.Context, name string) error { } func generateSSH(ctx context.Context, name, host, tmpDir string) error { - logAction("generating host key for %s", host) + logGenerate("generating host key for %s", host) command := fmt.Sprintf("ssh-keyscan %s > %s/known_hosts", host, tmpDir) if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { return fmt.Errorf("ssh-keyscan failed") } - logAction("generating deploy key") + logGenerate("generating deploy key") command = fmt.Sprintf("ssh-keygen -b 2048 -t rsa -f %s/identity -q -N \"\"", tmpDir) if _, err := utils.execCommand(ctx, ModeStderrOS, command); err != nil { @@ -220,7 +225,7 @@ func generateSSH(ctx context.Context, name, host, tmpDir string) error { return fmt.Errorf("aborting") } - logAction("saving deploy key") + logAction("saving keys") files := fmt.Sprintf("--from-file=%s/identity --from-file=%s/identity.pub --from-file=%s/known_hosts", tmpDir, tmpDir, tmpDir) secret := fmt.Sprintf("kubectl -n %s create secret generic %s %s --dry-run=client -oyaml | kubectl apply -f-", diff --git a/cmd/tk/install.go b/cmd/tk/install.go index 04a093b2..021ef03a 100644 --- a/cmd/tk/install.go +++ b/cmd/tk/install.go @@ -63,7 +63,7 @@ func installCmdRun(cmd *cobra.Command, args []string) error { } defer os.RemoveAll(tmpDir) - logAction("generating install manifests") + logGenerate("generating manifests") if kustomizePath == "" { err = genInstallManifests(installVersion, namespace, components, tmpDir) if err != nil { @@ -86,9 +86,9 @@ func installCmdRun(cmd *cobra.Command, args []string) error { fmt.Print(yaml) } } - logSuccess("build completed") + logSuccess("manifests build completed") - logWaiting("installing components in %s namespace", namespace) + logAction("installing components in %s namespace", namespace) applyOutput := ModeStderrOS if verbose { applyOutput = ModeOS diff --git a/cmd/tk/log.go b/cmd/tk/log.go index c06f3b59..b2be7feb 100644 --- a/cmd/tk/log.go +++ b/cmd/tk/log.go @@ -3,6 +3,10 @@ package main import "fmt" func logAction(format string, a ...interface{}) { + fmt.Println(`►`, fmt.Sprintf(format, a...)) +} + +func logGenerate(format string, a ...interface{}) { fmt.Println(`✚`, fmt.Sprintf(format, a...)) } diff --git a/cmd/tk/uninstall.go b/cmd/tk/uninstall.go index 12f0411d..85adaee6 100644 --- a/cmd/tk/uninstall.go +++ b/cmd/tk/uninstall.go @@ -17,7 +17,7 @@ cluster role bindings and CRDs.`, Example: ` # Dry-run uninstall of all components uninstall --dry-run --namespace=gitops-system - # Uninstall all components and custom resource definitions + # Uninstall all components and delete custom resource definitions uninstall --crds --namespace=gitops-system `, RunE: uninstallCmdRun, From 6d66f42382f3b36fc14d6ae6b980f0370cfe0f20 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 09:53:29 +0300 Subject: [PATCH 7/8] Add ks alias to kustomization commands --- cmd/tk/create_kustomization.go | 5 +++-- cmd/tk/sync_kustomization.go | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/cmd/tk/create_kustomization.go b/cmd/tk/create_kustomization.go index b20c7487..a15166da 100644 --- a/cmd/tk/create_kustomization.go +++ b/cmd/tk/create_kustomization.go @@ -18,8 +18,9 @@ import ( ) var createKsCmd = &cobra.Command{ - Use: "kustomization [name]", - Short: "Create or update a kustomization resource", + Use: "kustomization [name]", + Aliases: []string{"ks"}, + Short: "Create or update a kustomization resource", Long: ` The kustomization source command generates a kustomization.kustomize.fluxcd.io resource for a given GitRepository source. API spec: https://github.com/fluxcd/kustomize-controller/tree/master/docs/spec/v1alpha1`, diff --git a/cmd/tk/sync_kustomization.go b/cmd/tk/sync_kustomization.go index 951ceb81..20ab29b1 100644 --- a/cmd/tk/sync_kustomization.go +++ b/cmd/tk/sync_kustomization.go @@ -12,8 +12,9 @@ import ( ) var syncKsCmd = &cobra.Command{ - Use: "kustomization [name]", - Short: "Synchronize kustomization", + Use: "kustomization [name]", + Aliases: []string{"ks"}, + Short: "Synchronize kustomization", Long: ` The sync kustomization command triggers a reconciliation of a Kustomization resource and waits for it to finish.`, Example: ` # Trigger a kustomization apply outside of the reconciliation interval From 1e03155e8b89ce4c77bd1d7db04529e2ff760783 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 29 Apr 2020 10:48:12 +0300 Subject: [PATCH 8/8] Add poll interval global variable --- cmd/tk/create_kustomization.go | 2 +- cmd/tk/create_source_git.go | 12 +++++------- cmd/tk/main.go | 13 +++++++------ cmd/tk/sync_kustomization.go | 2 +- cmd/tk/sync_source_git.go | 2 +- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/cmd/tk/create_kustomization.go b/cmd/tk/create_kustomization.go index a15166da..34ecd526 100644 --- a/cmd/tk/create_kustomization.go +++ b/cmd/tk/create_kustomization.go @@ -161,7 +161,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { } logWaiting("waiting for kustomization sync") - if err := wait.PollImmediate(2*time.Second, timeout, + if err := wait.PollImmediate(pollInterval, timeout, isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { return err } diff --git a/cmd/tk/create_source_git.go b/cmd/tk/create_source_git.go index 6c0a409b..b5953b27 100644 --- a/cmd/tk/create_source_git.go +++ b/cmd/tk/create_source_git.go @@ -3,21 +3,19 @@ package main import ( "context" "fmt" - "io/ioutil" - "net/url" - "os" - "strings" - "time" - sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" "github.com/manifoldco/promptui" "github.com/spf13/cobra" + "io/ioutil" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" + "net/url" + "os" "sigs.k8s.io/controller-runtime/pkg/client" + "strings" ) var createSourceGitCmd = &cobra.Command{ @@ -158,7 +156,7 @@ func createSourceGitCmdRun(cmd *cobra.Command, args []string) error { } logWaiting("waiting for git sync") - if err := wait.PollImmediate(2*time.Second, timeout, + if err := wait.PollImmediate(pollInterval, timeout, isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { return err } diff --git a/cmd/tk/main.go b/cmd/tk/main.go index 6fbee802..afd97f65 100644 --- a/cmd/tk/main.go +++ b/cmd/tk/main.go @@ -50,12 +50,13 @@ var rootCmd = &cobra.Command{ } var ( - kubeconfig string - namespace string - timeout time.Duration - verbose bool - components []string - utils Utils + kubeconfig string + namespace string + timeout time.Duration + verbose bool + components []string + utils Utils + pollInterval = 2 * time.Second ) func init() { diff --git a/cmd/tk/sync_kustomization.go b/cmd/tk/sync_kustomization.go index 20ab29b1..e1def0fa 100644 --- a/cmd/tk/sync_kustomization.go +++ b/cmd/tk/sync_kustomization.go @@ -82,7 +82,7 @@ func syncKsCmdRun(cmd *cobra.Command, args []string) error { } logWaiting("waiting for kustomization sync") - if err := wait.PollImmediate(2*time.Second, timeout, + if err := wait.PollImmediate(pollInterval, timeout, isKustomizationReady(ctx, kubeClient, name, namespace)); err != nil { return err } diff --git a/cmd/tk/sync_source_git.go b/cmd/tk/sync_source_git.go index b714f2bb..d9c7ffa0 100644 --- a/cmd/tk/sync_source_git.go +++ b/cmd/tk/sync_source_git.go @@ -64,7 +64,7 @@ func syncSourceGitCmdRun(cmd *cobra.Command, args []string) error { logSuccess("source annotated") logWaiting("waiting for git sync") - if err := wait.PollImmediate(2*time.Second, timeout, + if err := wait.PollImmediate(pollInterval, timeout, isGitRepositoryReady(ctx, kubeClient, name, namespace)); err != nil { return err }