From 0646538ceff26fc913a9ab00fdc15bffdb35f779 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Thu, 7 Jan 2021 10:13:59 +0100 Subject: [PATCH 1/3] Checks if bootstrap path differs Signed-off-by: Somtochi Onyekwere --- cmd/flux/bootstrap.go | 17 +++++++++++++++++ cmd/flux/bootstrap_github.go | 22 ++++++++++++++-------- cmd/flux/bootstrap_gitlab.go | 22 ++++++++++++++-------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/cmd/flux/bootstrap.go b/cmd/flux/bootstrap.go index 7f92ca51..fd53ddc0 100644 --- a/cmd/flux/bootstrap.go +++ b/cmd/flux/bootstrap.go @@ -266,3 +266,20 @@ func generateDeployKey(ctx context.Context, kubeClient client.Client, url *url.U return string(pair.PublicKey), nil } + +func checkIfBootstrapPathDiffers(ctx context.Context, kubeClient client.Client, namespace string, path string) bool { + namespacedName := types.NamespacedName{ + Name: namespace, + Namespace: namespace, + } + var fluxSystemKustomization kustomizev1.Kustomization + err := kubeClient.Get(ctx, namespacedName, &fluxSystemKustomization) + if err != nil { + return false + } + if fluxSystemKustomization.Spec.Path == path { + return false + } + + return true +} diff --git a/cmd/flux/bootstrap_github.go b/cmd/flux/bootstrap_github.go index b9cf8450..2a12991d 100644 --- a/cmd/flux/bootstrap_github.go +++ b/cmd/flux/bootstrap_github.go @@ -115,6 +115,20 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { return err } + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + kubeClient, err := utils.KubeClient(kubeconfig, kubecontext) + if err != nil { + return err + } + + bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(ghPath.String())) + + if bootstrapPathDiffers { + return fmt.Errorf("cluster already bootstrapped to a different path") + } + repository, err := git.NewRepository(ghRepository, ghOwner, ghHostname, ghToken, "flux", ghOwner+"@users.noreply.github.com") if err != nil { return err @@ -135,9 +149,6 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { } defer os.RemoveAll(tmpDir) - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - if ghDelete { if err := provider.DeleteRepository(ctx, repository); err != nil { return err @@ -198,11 +209,6 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { logger.Successf("components are up to date") } - kubeClient, err := utils.KubeClient(kubeconfig, kubecontext) - if err != nil { - return err - } - // determine if repo synchronization is working isInstall := shouldInstallManifests(ctx, kubeClient, namespace) diff --git a/cmd/flux/bootstrap_gitlab.go b/cmd/flux/bootstrap_gitlab.go index e8b9556c..63677466 100644 --- a/cmd/flux/bootstrap_gitlab.go +++ b/cmd/flux/bootstrap_gitlab.go @@ -115,6 +115,20 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error { return err } + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + kubeClient, err := utils.KubeClient(kubeconfig, kubecontext) + if err != nil { + return err + } + + bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(glPath.String())) + + if bootstrapPathDiffers { + return fmt.Errorf("cluster already bootstrapped to a different path") + } + repository, err := git.NewRepository(glRepository, glOwner, glHostname, glToken, "flux", glOwner+"@users.noreply.gitlab.com") if err != nil { return err @@ -129,20 +143,12 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error { IsPersonal: glPersonal, } - kubeClient, err := utils.KubeClient(kubeconfig, kubecontext) - if err != nil { - return err - } - tmpDir, err := ioutil.TempDir("", namespace) if err != nil { return err } defer os.RemoveAll(tmpDir) - ctx, cancel := context.WithTimeout(context.Background(), timeout) - defer cancel() - // create GitLab project if doesn't exists logger.Actionf("connecting to %s", glHostname) changed, err := provider.CreateRepository(ctx, repository) From b8d4af5538447e42b116f6c104c449985acc88b7 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Thu, 7 Jan 2021 10:30:17 +0100 Subject: [PATCH 2/3] Inform user of path being used Signed-off-by: Somtochi Onyekwere --- cmd/flux/bootstrap.go | 8 ++++---- cmd/flux/bootstrap_github.go | 4 ++-- cmd/flux/bootstrap_gitlab.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cmd/flux/bootstrap.go b/cmd/flux/bootstrap.go index fd53ddc0..ddfc2ff2 100644 --- a/cmd/flux/bootstrap.go +++ b/cmd/flux/bootstrap.go @@ -267,7 +267,7 @@ func generateDeployKey(ctx context.Context, kubeClient client.Client, url *url.U return string(pair.PublicKey), nil } -func checkIfBootstrapPathDiffers(ctx context.Context, kubeClient client.Client, namespace string, path string) bool { +func checkIfBootstrapPathDiffers(ctx context.Context, kubeClient client.Client, namespace string, path string) (string, bool) { namespacedName := types.NamespacedName{ Name: namespace, Namespace: namespace, @@ -275,11 +275,11 @@ func checkIfBootstrapPathDiffers(ctx context.Context, kubeClient client.Client, var fluxSystemKustomization kustomizev1.Kustomization err := kubeClient.Get(ctx, namespacedName, &fluxSystemKustomization) if err != nil { - return false + return "", false } if fluxSystemKustomization.Spec.Path == path { - return false + return "", false } - return true + return fluxSystemKustomization.Spec.Path, true } diff --git a/cmd/flux/bootstrap_github.go b/cmd/flux/bootstrap_github.go index 2a12991d..5c4158c2 100644 --- a/cmd/flux/bootstrap_github.go +++ b/cmd/flux/bootstrap_github.go @@ -123,10 +123,10 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { return err } - bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(ghPath.String())) + usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(ghPath.String())) if bootstrapPathDiffers { - return fmt.Errorf("cluster already bootstrapped to a different path") + return fmt.Errorf("cluster already bootstrapped to a %v path", usedPath) } repository, err := git.NewRepository(ghRepository, ghOwner, ghHostname, ghToken, "flux", ghOwner+"@users.noreply.github.com") diff --git a/cmd/flux/bootstrap_gitlab.go b/cmd/flux/bootstrap_gitlab.go index 63677466..9bffa71a 100644 --- a/cmd/flux/bootstrap_gitlab.go +++ b/cmd/flux/bootstrap_gitlab.go @@ -123,10 +123,10 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error { return err } - bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(glPath.String())) + usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(glPath.String())) if bootstrapPathDiffers { - return fmt.Errorf("cluster already bootstrapped to a different path") + return fmt.Errorf("cluster already bootstrapped to a %v path", usedPath) } repository, err := git.NewRepository(glRepository, glOwner, glHostname, glToken, "flux", glOwner+"@users.noreply.gitlab.com") From 3a4a2002d428d92b95336cf672709ae7be3a1c4f Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Thu, 7 Jan 2021 10:44:40 +0100 Subject: [PATCH 3/3] Corrects typo Signed-off-by: Somtochi Onyekwere --- cmd/flux/bootstrap_github.go | 2 +- cmd/flux/bootstrap_gitlab.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/flux/bootstrap_github.go b/cmd/flux/bootstrap_github.go index 5c4158c2..a0e808d9 100644 --- a/cmd/flux/bootstrap_github.go +++ b/cmd/flux/bootstrap_github.go @@ -126,7 +126,7 @@ func bootstrapGitHubCmdRun(cmd *cobra.Command, args []string) error { usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(ghPath.String())) if bootstrapPathDiffers { - return fmt.Errorf("cluster already bootstrapped to a %v path", usedPath) + return fmt.Errorf("cluster already bootstrapped to %v path", usedPath) } repository, err := git.NewRepository(ghRepository, ghOwner, ghHostname, ghToken, "flux", ghOwner+"@users.noreply.github.com") diff --git a/cmd/flux/bootstrap_gitlab.go b/cmd/flux/bootstrap_gitlab.go index 9bffa71a..1d387ba6 100644 --- a/cmd/flux/bootstrap_gitlab.go +++ b/cmd/flux/bootstrap_gitlab.go @@ -126,7 +126,7 @@ func bootstrapGitLabCmdRun(cmd *cobra.Command, args []string) error { usedPath, bootstrapPathDiffers := checkIfBootstrapPathDiffers(ctx, kubeClient, namespace, filepath.ToSlash(glPath.String())) if bootstrapPathDiffers { - return fmt.Errorf("cluster already bootstrapped to a %v path", usedPath) + return fmt.Errorf("cluster already bootstrapped to %v path", usedPath) } repository, err := git.NewRepository(glRepository, glOwner, glHostname, glToken, "flux", glOwner+"@users.noreply.gitlab.com")