Skip to content

Commit

Permalink
Bugfix: Too long Kustomization names (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzsak committed May 2, 2023
1 parent 714f194 commit 0f9ad6d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 22 deletions.
26 changes: 25 additions & 1 deletion pkg/dashboard/worker/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
15 changes: 15 additions & 0 deletions pkg/dashboard/worker/gitops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
47 changes: 34 additions & 13 deletions pkg/gitops/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
}
14 changes: 6 additions & 8 deletions pkg/gitops/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"path/filepath"
"testing"

"github.com/alecthomas/assert"
"gotest.tools/assert"
)

func Test_parseRepoURL(t *testing.T) {
Expand Down Expand Up @@ -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)
}

0 comments on commit 0f9ad6d

Please sign in to comment.