@@ -8,8 +8,10 @@ import (
8
8
"os"
9
9
"path"
10
10
"path/filepath"
11
+ "strings"
11
12
"text/template"
12
13
14
+ "helm.sh/helm/pkg/chartutil"
13
15
"sigs.k8s.io/kustomize/api/konfig"
14
16
"sigs.k8s.io/kustomize/api/types"
15
17
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
@@ -297,6 +299,62 @@ func writeOverrides(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Cl
297
299
298
300
var _ changeWriter = writeOverrides
299
301
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
+
300
358
// writeKustomization writes any changes required for updating one or more images to a kustomization.yml
301
359
func writeKustomization (app * v1alpha1.Application , wbc * WriteBackConfig , gitC git.Client ) (err error , skip bool ) {
302
360
logCtx := log .WithContext ().AddField ("application" , app .GetName ())
0 commit comments