From 9ae020fa23ba91615e5d4708a6ad7c0edc80d4d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Wozi=C5=84ski?= Date: Sun, 13 Feb 2022 00:48:16 +0100 Subject: [PATCH] refactor(WBC): Target field in the WriteBackConfig --- pkg/argocd/git.go | 4 ++-- pkg/argocd/update.go | 8 ++++++++ pkg/common/constants.go | 3 +++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/argocd/git.go b/pkg/argocd/git.go index 785498d5..ce32cd92 100644 --- a/pkg/argocd/git.go +++ b/pkg/argocd/git.go @@ -247,10 +247,10 @@ func commitChangesGit(app *v1alpha1.Application, wbc *WriteBackConfig, changeLis return nil } -func writeOverrides(app *v1alpha1.Application, _ *WriteBackConfig, gitC git.Client) (err error, skip bool) { +func writeOverrides(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool) { logCtx := log.WithContext().AddField("application", app.GetName()) targetExists := true - targetFile := path.Join(gitC.Root(), app.Spec.Source.Path, fmt.Sprintf(".argocd-source-%s.yaml", app.Name)) + targetFile := path.Join(gitC.Root(), wbc.Target) _, err = os.Stat(targetFile) if err != nil { if !os.IsNotExist(err) { diff --git a/pkg/argocd/update.go b/pkg/argocd/update.go index 92f16fd9..c92730dc 100644 --- a/pkg/argocd/update.go +++ b/pkg/argocd/update.go @@ -67,6 +67,7 @@ type WriteBackConfig struct { GitCommitEmail string GitCommitMessage string KustomizeBase string + Target string } // The following are helper structs to only marshal the fields we require @@ -415,6 +416,7 @@ func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesCl // Default write-back is to use Argo CD API wbc.Method = WriteBackApplication wbc.ArgoClient = argoClient + wbc.Target = parseDefaultTarget(app.Name, app.Spec.Source.Path) // If we have no update method, just return our default method, ok := app.Annotations[common.WriteBackMethodAnnotation] @@ -446,6 +448,12 @@ func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesCl return wbc, nil } +func parseDefaultTarget(appName string, path string) string { + defaultTargetFile := fmt.Sprintf(common.DefaultTargetFilePattern, appName) + + return filepath.Join(path, defaultTargetFile) +} + func parseTarget(target string, sourcePath string) (kustomizeBase string) { if target == common.KustomizationPrefix { return filepath.Join(sourcePath, ".") diff --git a/pkg/common/constants.go b/pkg/common/constants.go index a342cd4a..527b2009 100644 --- a/pkg/common/constants.go +++ b/pkg/common/constants.go @@ -54,6 +54,9 @@ const ( KustomizationPrefix = "kustomization" ) +// DefaultTargetFilePattern configurations related to the write-back functionality +const DefaultTargetFilePattern = ".argocd-source-%s.yaml" + // The default Git commit message's template const DefaultGitCommitMessage = `build: automatic update of {{ .AppName }}