From 0f9ad6d246ea50e9c9e6d3c73ee747eaa4e1a399 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81d=C3=A1m=20Sis=C3=A1k?= <86593861+dzsak@users.noreply.github.com> Date: Tue, 2 May 2023 14:52:28 +0200 Subject: [PATCH] Bugfix: Too long Kustomization names (#487) --- pkg/dashboard/worker/gitops.go | 26 +++++++++++++++- pkg/dashboard/worker/gitops_test.go | 15 +++++++++ pkg/gitops/bootstrap.go | 47 +++++++++++++++++++++-------- pkg/gitops/bootstrap_test.go | 14 ++++----- 4 files changed, 80 insertions(+), 22 deletions(-) diff --git a/pkg/dashboard/worker/gitops.go b/pkg/dashboard/worker/gitops.go index 688267dc7..a429d0581 100644 --- a/pkg/dashboard/worker/gitops.go +++ b/pkg/dashboard/worker/gitops.go @@ -950,7 +950,7 @@ func kustomizationTemplateAndWrite( } owner, repository := server.ParseRepo(repoName) - kustomizationName := bootstrap.UniqueKustomizationName(repoPerEnv, owner, repository, manifest.Env, manifest.Namespace, manifest.App) + kustomizationName := uniqueKustomizationName(repoPerEnv, owner, repository, manifest.Env, manifest.Namespace, manifest.App) sourceName := fmt.Sprintf("gitops-repo-%s", bootstrap.UniqueName(repoPerEnv, owner, repository, manifest.Env)) kustomizationManifest, err := sync.GenerateKustomizationForApp( manifest.App, @@ -975,3 +975,27 @@ func kustomizationTemplateAndWrite( return nativeGit.Commit(repo, fmt.Sprintf("[Gimlet] %s/%s %s", manifest.Env, manifest.App, "automated deploy")) } + +func uniqueKustomizationName(singleEnv bool, owner string, repoName string, env string, namespace string, appName string) string { + if len(owner) > 10 { + owner = owner[:10] + } + repoName = strings.TrimPrefix(repoName, "gitops-") + + uniqueName := fmt.Sprintf("%s-%s-%s-%s-%s", + strings.ToLower(owner), + strings.ToLower(repoName), + strings.ToLower(env), + strings.ToLower(namespace), + strings.ToLower(appName), + ) + if singleEnv { + uniqueName = fmt.Sprintf("%s-%s-%s-%s", + strings.ToLower(owner), + strings.ToLower(repoName), + strings.ToLower(namespace), + strings.ToLower(appName), + ) + } + return uniqueName +} diff --git a/pkg/dashboard/worker/gitops_test.go b/pkg/dashboard/worker/gitops_test.go index 0eb35736a..351a8ea07 100644 --- a/pkg/dashboard/worker/gitops_test.go +++ b/pkg/dashboard/worker/gitops_test.go @@ -592,3 +592,18 @@ func Test_kustomizationTemplateAndWrite(t *testing.T) { content, _ = nativeGit.Content(repo, "flux/kustomization-myapp.yaml") assert.True(t, len(content) > 1) } + +func Test_uniqueKustomizationName(t *testing.T) { + singleEnv := false + owner := "gimlet-io" + repoName := "gitops-staging-infra" + env := "staging" + namespace := "my-team" + appName := "myapp" + uniqueName := uniqueKustomizationName(singleEnv, owner, repoName, env, namespace, appName) + assert.Equal(t, "gimlet-io-staging-infra-staging-my-team-myapp", uniqueName) + + singleEnv = true + uniqueName = uniqueKustomizationName(singleEnv, owner, repoName, env, namespace, appName) + assert.Equal(t, "gimlet-io-staging-infra-my-team-myapp", uniqueName) +} diff --git a/pkg/gitops/bootstrap.go b/pkg/gitops/bootstrap.go index 3c9591898..3d4f27283 100644 --- a/pkg/gitops/bootstrap.go +++ b/pkg/gitops/bootstrap.go @@ -11,7 +11,9 @@ import ( "github.com/fluxcd/flux2/pkg/manifestgen/install" "github.com/fluxcd/pkg/ssh" + helper "github.com/gimlet-io/gimlet-cli/pkg/git/nativeGit" "github.com/gimlet-io/gimlet-cli/pkg/gitops/sync" + "github.com/go-git/go-git/v5" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" @@ -67,11 +69,21 @@ func GenerateManifests( if shouldGenerateKustomizationAndRepo { host, owner, repoName := ParseRepoURL(gitopsRepoUrl) - gitopsRepoName = fmt.Sprintf("gitops-repo-%s", UniqueName(singleEnv, owner, repoName, env)) - gitopsRepoFileName = gitopsRepoName + ".yaml" + gitopsRepoName = uniqueGitopsRepoName(singleEnv, owner, repoName, env) + gitopsRepoFileName = fmt.Sprintf("gitops-repo-%s.yaml", UniqueName(singleEnv, owner, repoName, env)) secretName := fmt.Sprintf("deploy-key-%s", UniqueName(singleEnv, owner, repoName, env)) secretFileName = secretName + ".yaml" + fluxPath := filepath.Join(env, "flux") + if singleEnv { + fluxPath = "flux" + } + existingGitopsRepoFileName := GitopsRepoFileNameFromRepo(gitopsRepoPath, fluxPath) + if existingGitopsRepoFileName != "" { + gitopsRepoName = strings.TrimSuffix(existingGitopsRepoFileName, ".yaml") + gitopsRepoFileName = existingGitopsRepoFileName + } + syncOpts := sync.Options{ Interval: 15 * time.Second, URL: fmt.Sprintf("ssh://git@%s/%s/%s", host, owner, repoName), @@ -90,10 +102,7 @@ func GenerateManifests( syncOpts.TargetPath = "" } if kustomizationPerApp { - syncOpts.TargetPath = filepath.Join(env, "flux") - if singleEnv { - syncOpts.TargetPath = "flux" - } + syncOpts.TargetPath = fluxPath } syncManifest, err := sync.Generate(syncOpts) if err != nil { @@ -151,25 +160,21 @@ func UniqueName(singleEnv bool, owner string, repoName string, env string) strin return uniqueName } -func UniqueKustomizationName(singleEnv bool, owner string, repoName string, env string, namespace string, appName string) string { +func uniqueGitopsRepoName(singleEnv bool, owner string, repoName string, env string) string { if len(owner) > 10 { owner = owner[:10] } repoName = strings.TrimPrefix(repoName, "gitops-") - uniqueName := fmt.Sprintf("%s-%s-%s-%s-%s", + uniqueName := fmt.Sprintf("%s-%s-%s", strings.ToLower(owner), strings.ToLower(repoName), strings.ToLower(env), - strings.ToLower(namespace), - strings.ToLower(appName), ) if singleEnv { - uniqueName = fmt.Sprintf("%s-%s-%s-%s", + uniqueName = fmt.Sprintf("%s-%s", strings.ToLower(owner), strings.ToLower(repoName), - strings.ToLower(namespace), - strings.ToLower(appName), ) } return uniqueName @@ -255,3 +260,19 @@ func generateDeployKey(host string, name string) (string, []byte, error) { yamlString, err := yaml.Marshal(secret) return string(publicKeyBytes), yamlString, err } + +func GitopsRepoFileNameFromRepo(repoPath string, contentPath string) string { + repo, err := git.PlainOpen(repoPath) + if err == git.ErrRepositoryNotExists { + return "" + } + branch, _ := helper.HeadBranch(repo) + + files, _ := helper.RemoteFolderOnBranchWithoutCheckout(repo, branch, contentPath) + for fileName := range files { + if strings.Contains(fileName, "gitops-repo") { + return fileName + } + } + return "" +} diff --git a/pkg/gitops/bootstrap_test.go b/pkg/gitops/bootstrap_test.go index f69250ba4..41a5b733b 100644 --- a/pkg/gitops/bootstrap_test.go +++ b/pkg/gitops/bootstrap_test.go @@ -6,7 +6,7 @@ import ( "path/filepath" "testing" - "github.com/alecthomas/assert" + "gotest.tools/assert" ) func Test_parseRepoURL(t *testing.T) { @@ -276,17 +276,15 @@ func Test_generateManifestProviderAndAlert(t *testing.T) { } } -func Test_uniqueKustomizationName(t *testing.T) { +func Test_uniqueGitopsRepoName(t *testing.T) { singleEnv := false owner := "gimlet-io" repoName := "gitops-staging-infra" env := "staging" - namespace := "my-team" - appName := "myapp" - uniqueName := UniqueKustomizationName(singleEnv, owner, repoName, env, namespace, appName) - assert.Equal(t, "gimlet-io-staging-infra-staging-my-team-myapp", uniqueName) + uniqueName := uniqueGitopsRepoName(singleEnv, owner, repoName, env) + assert.Equal(t, "gimlet-io-staging-infra-staging", uniqueName) singleEnv = true - uniqueName = UniqueKustomizationName(singleEnv, owner, repoName, env, namespace, appName) - assert.Equal(t, "gimlet-io-staging-infra-my-team-myapp", uniqueName) + uniqueName = uniqueGitopsRepoName(singleEnv, owner, repoName, env) + assert.Equal(t, "gimlet-io-staging-infra", uniqueName) }