Skip to content

Commit 2c5c23e

Browse files
committed
Fix original override not respected
Signed-off-by: KS. Yim <[email protected]>
1 parent 5366cc1 commit 2c5c23e

File tree

5 files changed

+75
-17
lines changed

5 files changed

+75
-17
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ require (
7777
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
7878
github.com/golang/protobuf v1.5.2 // indirect
7979
github.com/google/btree v1.0.1 // indirect
80-
github.com/google/go-cmp v0.5.6 // indirect
80+
github.com/google/go-cmp v0.5.8 // indirect
8181
github.com/google/go-github/v29 v29.0.2 // indirect
8282
github.com/google/go-github/v38 v38.0.0 // indirect
8383
github.com/google/go-querystring v1.1.0 // indirect
@@ -128,7 +128,7 @@ require (
128128
github.com/xanzy/ssh-agent v0.2.1 // indirect
129129
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca // indirect
130130
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
131-
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 // indirect
131+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
132132
golang.org/x/net v0.7.0 // indirect
133133
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f // indirect
134134
golang.org/x/sys v0.5.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
417417
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
418418
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
419419
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
420+
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
421+
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
420422
github.com/google/go-github/v29 v29.0.2 h1:opYN6Wc7DOz7Ku3Oh4l7prmkOMwEcQxpFtxdU8N8Pts=
421423
github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
422424
github.com/google/go-github/v38 v38.0.0 h1:l/BalRp6dmFh/SFbl32RrlaVvbByhxpy+/LY0sv9isM=
@@ -966,6 +968,8 @@ golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMk
966968
golang.org/x/exp v0.0.0-20210220032938-85be41e4509f/go.mod h1:I6l2HNBLBZEcrOoCpyKLdY2lHoRZ8lI4x60KMCQDft4=
967969
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2 h1:Or4Ra3AAlhUlNn8WmIzw2Yq2vUHSkrP6E2e/FIESpF8=
968970
golang.org/x/exp v0.0.0-20210901193431-a062eea981d2/go.mod h1:a3o/VtDNHN+dCVLEpzjjUHOzR+Ln3DHX056ZPzoZGGA=
971+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
972+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
969973
golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs=
970974
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
971975
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=

pkg/argocd/git.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,23 +259,29 @@ func writeOverrides(app *v1alpha1.Application, wbc *WriteBackConfig, gitC git.Cl
259259
}
260260
}
261261

262-
override, err := marshalParamsOverride(app)
263-
if err != nil {
264-
return
265-
}
266-
267262
// If the target file already exist in the repository, we will check whether
268263
// our generated new file is the same as the existing one, and if yes, we
269264
// don't proceed further for commit.
265+
var override []byte
266+
var originalData []byte
270267
if targetExists {
271-
data, err := os.ReadFile(targetFile)
268+
originalData, err = os.ReadFile(targetFile)
272269
if err != nil {
273270
return err, false
274271
}
275-
if string(data) == string(override) {
272+
override, err = marshalParamsOverride(app, originalData)
273+
if err != nil {
274+
return
275+
}
276+
if string(originalData) == string(override) {
276277
logCtx.Debugf("target parameter file and marshaled data are the same, skipping commit.")
277278
return nil, true
278279
}
280+
} else {
281+
override, err = marshalParamsOverride(app, nil)
282+
if err != nil {
283+
return
284+
}
279285
}
280286

281287
err = os.WriteFile(targetFile, override, 0600)

pkg/argocd/update.go

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package argocd
33
import (
44
"context"
55
"fmt"
6+
"golang.org/x/exp/slices"
67
"path/filepath"
78
"strings"
89
"sync"
@@ -375,7 +376,7 @@ func setAppImage(app *v1alpha1.Application, img *image.ContainerImage) error {
375376

376377
// marshalParamsOverride marshals the parameter overrides of a given application
377378
// into YAML bytes
378-
func marshalParamsOverride(app *v1alpha1.Application) ([]byte, error) {
379+
func marshalParamsOverride(app *v1alpha1.Application, originalData []byte) ([]byte, error) {
379380
var override []byte
380381
var err error
381382

@@ -385,21 +386,46 @@ func marshalParamsOverride(app *v1alpha1.Application) ([]byte, error) {
385386
if app.Spec.Source.Kustomize == nil {
386387
return []byte{}, nil
387388
}
388-
params := kustomizeOverride{
389+
390+
var params kustomizeOverride
391+
newParams := kustomizeOverride{
389392
Kustomize: kustomizeImages{
390393
Images: &app.Spec.Source.Kustomize.Images,
391394
},
392395
}
396+
397+
if len(originalData) == 0 {
398+
override, err = yaml.Marshal(newParams)
399+
break
400+
}
401+
err := yaml.Unmarshal(originalData, &params)
402+
if err != nil {
403+
override, err = yaml.Marshal(newParams)
404+
break
405+
}
406+
mergeKustomizeOverride(&params, &newParams)
393407
override, err = yaml.Marshal(params)
394408
case ApplicationTypeHelm:
395409
if app.Spec.Source.Helm == nil {
396410
return []byte{}, nil
397411
}
398-
params := helmOverride{
412+
var params helmOverride
413+
newParams := helmOverride{
399414
Helm: helmParameters{
400415
Parameters: app.Spec.Source.Helm.Parameters,
401416
},
402417
}
418+
419+
if len(originalData) == 0 {
420+
override, err = yaml.Marshal(newParams)
421+
break
422+
}
423+
err := yaml.Unmarshal(originalData, &params)
424+
if err != nil {
425+
override, err = yaml.Marshal(newParams)
426+
break
427+
}
428+
mergeHelmOverride(&params, &newParams)
403429
override, err = yaml.Marshal(params)
404430
default:
405431
err = fmt.Errorf("unsupported application type")
@@ -411,6 +437,28 @@ func marshalParamsOverride(app *v1alpha1.Application) ([]byte, error) {
411437
return override, nil
412438
}
413439

440+
func mergeHelmOverride(t *helmOverride, o *helmOverride) {
441+
for _, param := range o.Helm.Parameters {
442+
idx := slices.IndexFunc(t.Helm.Parameters, func(tp v1alpha1.HelmParameter) bool { return tp.Name == param.Name })
443+
if idx != -1 {
444+
t.Helm.Parameters[idx] = param
445+
continue
446+
}
447+
t.Helm.Parameters = append(t.Helm.Parameters, param)
448+
}
449+
}
450+
451+
func mergeKustomizeOverride(t *kustomizeOverride, o *kustomizeOverride) {
452+
for _, image := range *o.Kustomize.Images {
453+
idx := t.Kustomize.Images.Find(image)
454+
if idx != -1 {
455+
(*t.Kustomize.Images)[idx] = image
456+
continue
457+
}
458+
*t.Kustomize.Images = append(*t.Kustomize.Images, image)
459+
}
460+
}
461+
414462
func getWriteBackConfig(app *v1alpha1.Application, kubeClient *kube.KubernetesClient, argoClient ArgoCD) (*WriteBackConfig, error) {
415463
wbc := &WriteBackConfig{}
416464
// Default write-back is to use Argo CD API

pkg/argocd/update_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ kustomize:
10941094
},
10951095
}
10961096

1097-
yaml, err := marshalParamsOverride(&app)
1097+
yaml, err := marshalParamsOverride(&app, nil)
10981098
require.NoError(t, err)
10991099
assert.NotEmpty(t, yaml)
11001100
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(yaml)))
@@ -1120,7 +1120,7 @@ kustomize:
11201120
},
11211121
}
11221122

1123-
yaml, err := marshalParamsOverride(&app)
1123+
yaml, err := marshalParamsOverride(&app, nil)
11241124
require.NoError(t, err)
11251125
assert.Empty(t, yaml)
11261126
assert.Equal(t, "", strings.TrimSpace(string(yaml)))
@@ -1170,7 +1170,7 @@ helm:
11701170
},
11711171
}
11721172

1173-
yaml, err := marshalParamsOverride(&app)
1173+
yaml, err := marshalParamsOverride(&app, nil)
11741174
require.NoError(t, err)
11751175
assert.NotEmpty(t, yaml)
11761176
assert.Equal(t, strings.TrimSpace(strings.ReplaceAll(expected, "\t", " ")), strings.TrimSpace(string(yaml)))
@@ -1196,7 +1196,7 @@ helm:
11961196
},
11971197
}
11981198

1199-
yaml, err := marshalParamsOverride(&app)
1199+
yaml, err := marshalParamsOverride(&app, nil)
12001200
require.NoError(t, err)
12011201
assert.Empty(t, yaml)
12021202
})
@@ -1227,7 +1227,7 @@ helm:
12271227
},
12281228
}
12291229

1230-
_, err := marshalParamsOverride(&app)
1230+
_, err := marshalParamsOverride(&app, nil)
12311231
assert.Error(t, err)
12321232
})
12331233
}

0 commit comments

Comments
 (0)