Skip to content

Commit 6e42184

Browse files
Add support for Argocd-Vault-Plugin together with Helm-values writeback
Signed-off-by: Arnaud van Gelder <arnaud.vangelder@fivetran.com>
1 parent 82d59ef commit 6e42184

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

pkg/argocd/argocd.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ func getApplicationType(app *v1alpha1.Application) ApplicationType {
527527
return ApplicationTypeKustomize
528528
} else if sourceType == v1alpha1.ApplicationSourceTypeHelm {
529529
return ApplicationTypeHelm
530+
} else if st, set := app.Annotations[common.WriteBackTargetAnnotation]; set &&
531+
strings.HasPrefix(st, common.HelmPrefix) &&
532+
sourceType == v1alpha1.ApplicationSourceTypePlugin {
533+
return ApplicationTypeHelm
530534
} else {
531535
return ApplicationTypeUnsupported
532536
}

pkg/argocd/git.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"os"
99
"path"
1010
"path/filepath"
11+
"strings"
1112
"text/template"
1213

14+
"helm.sh/helm/pkg/chartutil"
1315
"sigs.k8s.io/kustomize/api/konfig"
1416
"sigs.k8s.io/kustomize/api/types"
1517
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
@@ -297,6 +299,62 @@ func writeOverrides(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Cl
297299

298300
var _ changeWriter = writeOverrides
299301

302+
// writeHelm writes any changes required for updating one or more images to a values.yaml
303+
func writeHelm(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool) {
304+
logCtx := log.WithContext().AddField("application", app.GetName())
305+
306+
fileName := filepath.Join(gitC.Root(), wbc.HelmBase)
307+
308+
logCtx.Infof("updating valuesfile %s", fileName)
309+
310+
valuesFile, err := chartutil.ReadValuesFile(fileName)
311+
if err != nil {
312+
panic(err)
313+
}
314+
315+
// As we've defined the ApplicationSourceTypePlugin combined with WriteBackTargetAnnotation starting with Helm
316+
// it is actually safe to use app.Spec.Source.Helm.Parameters for updating values.yaml
317+
parameters := app.Spec.Source.Helm.Parameters
318+
319+
// Update all image tag and image repository values
320+
for _, parameter := range parameters {
321+
fullYamlPath := strings.Split(parameter.Name, ".")
322+
yamlPath := strings.Join(fullYamlPath[0:len(fullYamlPath)-1], ".") // remove the last path to be able to update the value of it later on
323+
324+
image, err := valuesFile.Table(yamlPath)
325+
if err != nil {
326+
logCtx.Errorf("could not find path %s", yamlPath)
327+
}
328+
329+
valuePath := fullYamlPath[len(fullYamlPath)-1]
330+
image[valuePath] = parameter.Value
331+
}
332+
333+
// Save the valuesfile back to the original file; first truncate the file and write back the content
334+
newValuesFile, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
335+
if err != nil {
336+
logCtx.Errorf("Cannot open file %s to write values to", fileName)
337+
return err, false
338+
}
339+
defer newValuesFile.Close()
340+
341+
err = newValuesFile.Truncate(0)
342+
if err != nil {
343+
logCtx.Errorf("Cannot truncate valuesfile %s", fileName)
344+
return err, false
345+
}
346+
347+
err = valuesFile.Encode(newValuesFile)
348+
if err != nil {
349+
logCtx.Errorf("Unable to write data into the valuesfile %s", fileName)
350+
return err, false
351+
}
352+
353+
return nil, false
354+
}
355+
356+
var _ changeWriter = writeHelm
357+
300358
// writeKustomization writes any changes required for updating one or more images to a kustomization.yml
301359
func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Client) (err error, skip bool) {
302360
logCtx := log.WithContext().AddField("application", app.GetName())

pkg/argocd/update.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type WriteBackConfig struct {
6969
GitCommitEmail string
7070
GitCommitMessage string
7171
KustomizeBase string
72+
HelmBase string
7273
Target string
7374
GitRepo string
7475
}
@@ -488,6 +489,9 @@ func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesCl
488489
if target, ok := app.Annotations[common.WriteBackTargetAnnotation]; ok && strings.HasPrefix(target, common.KustomizationPrefix) {
489490
wbc.KustomizeBase = parseTarget(target, app.Spec.Source.Path)
490491
}
492+
if target, ok := app.Annotations[common.WriteBackTargetAnnotation]; ok && strings.HasPrefix(target, common.HelmPrefix) {
493+
wbc.HelmBase = parseHelmTarget(target, app.Spec.Source.Path)
494+
}
491495
if err := parseGitConfig(app, kubeClient, wbc, creds); err != nil {
492496
return nil, err
493497
}
@@ -514,6 +518,17 @@ func parseTarget(target string, sourcePath string) (kustomizeBase string) {
514518
}
515519
}
516520

521+
// parseHelmTarget is a temporary function to avoid slice-out-of-bounds errors when common.KustomizationPrefix isn't set
522+
func parseHelmTarget(target string, sourcePath string) (kustomizeBase string) {
523+
if target == common.HelmPrefix {
524+
return filepath.Join(sourcePath, common.DefaultHelmTargetFile)
525+
} else if base := target[len(common.HelmPrefix)+1:]; strings.HasPrefix(base, "/") {
526+
return base[1:]
527+
} else {
528+
return filepath.Join(sourcePath, base)
529+
}
530+
}
531+
517532
func parseGitConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesClient, wbc *WriteBackConfig, creds string) error {
518533
branch, ok := app.Annotations[common.GitBranchAnnotation]
519534
if ok {
@@ -565,6 +580,8 @@ func commitChanges(app *v1alpha1.Application, wbc *WriteBackConfig, changeList [
565580
// if the kustomize base is set, the target is a kustomization
566581
if wbc.KustomizeBase != "" {
567582
return commitChangesGit(app, wbc, changeList, writeKustomization)
583+
} else if wbc.HelmBase != "" {
584+
return commitChangesGit(app, wbc, changeList, writeHelm)
568585
}
569586
return commitChangesGit(app, wbc, changeList, writeOverrides)
570587
default:

pkg/common/constants.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,15 @@ const (
5353
GitRepositoryAnnotation = ImageUpdaterAnnotationPrefix + "/git-repository"
5454
WriteBackTargetAnnotation = ImageUpdaterAnnotationPrefix + "/write-back-target"
5555
KustomizationPrefix = "kustomization"
56+
HelmPrefix = "helm"
5657
)
5758

5859
// DefaultTargetFilePattern configurations related to the write-back functionality
5960
const DefaultTargetFilePattern = ".argocd-source-%s.yaml"
6061

62+
// DefaultHelmTargetFile configurations related to the write-back using helm functionality
63+
const DefaultHelmTargetFile = "values.yaml"
64+
6165
// The default Git commit message's template
6266
const DefaultGitCommitMessage = `build: automatic update of {{ .AppName }}
6367

0 commit comments

Comments
 (0)