Skip to content

Commit

Permalink
fix(appset): Matrix Generator Override not Working for Booleans (argo…
Browse files Browse the repository at this point in the history
…proj#14498) (argoproj#14573)

* Fix AppSet matrix generator parameter override

Signed-off-by: Alexander Bellhäuser <[email protected]>

* Add test case for parameter override fix

Signed-off-by: Alexander Bellhäuser <[email protected]>

---------

Signed-off-by: Alexander Bellhäuser <[email protected]>
  • Loading branch information
alexbde authored Aug 23, 2023
1 parent 15e3eb8 commit e97a4f9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 4 deletions.
8 changes: 4 additions & 4 deletions applicationset/generators/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (m *MatrixGenerator) GenerateParams(appSetGenerator *argoprojiov1alpha1.App

if appSet.Spec.GoTemplate {
tmp := map[string]interface{}{}
if err := mergo.Merge(&tmp, a); err != nil {
return nil, fmt.Errorf("failed to merge params from the first generator in the matrix generator with temp map: %w", err)
if err := mergo.Merge(&tmp, b, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("failed to merge params from the second generator in the matrix generator with temp map: %w", err)
}
if err := mergo.Merge(&tmp, b); err != nil {
return nil, fmt.Errorf("failed to merge params from the first generator in the matrix generator with the second: %w", err)
if err := mergo.Merge(&tmp, a, mergo.WithOverride); err != nil {
return nil, fmt.Errorf("failed to merge params from the second generator in the matrix generator with the first: %w", err)
}
res = append(res, tmp)
} else {
Expand Down
22 changes: 22 additions & 0 deletions applicationset/generators/matrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,28 @@ func TestMatrixGenerateGoTemplate(t *testing.T) {
{"a": "2", "b": "2"},
},
},
{
name: "parameter override: first list elements take precedence",
baseGenerators: []argoprojiov1alpha1.ApplicationSetNestedGenerator{
{
List: &argoprojiov1alpha1.ListGenerator{
Elements: []apiextensionsv1.JSON{
{Raw: []byte(`{"booleanFalse": false, "booleanTrue": true, "stringFalse": "false", "stringTrue": "true"}`)},
},
},
},
{
List: &argoprojiov1alpha1.ListGenerator{
Elements: []apiextensionsv1.JSON{
{Raw: []byte(`{"booleanFalse": true, "booleanTrue": false, "stringFalse": "true", "stringTrue": "false"}`)},
},
},
},
},
expected: []map[string]interface{}{
{"booleanFalse": false, "booleanTrue": true, "stringFalse": "false", "stringTrue": "true"},
},
},
{
name: "returns error if there is less than two base generators",
baseGenerators: []argoprojiov1alpha1.ApplicationSetNestedGenerator{
Expand Down
82 changes: 82 additions & 0 deletions docs/operator-manual/applicationset/Generators-Matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,88 @@ In the 2nd child generator, the label selector with label `kubernetes.io/environ
So in the above example, clusters with the label `kubernetes.io/environment: prod` will have only prod-specific configuration (ie. `prod/config.json`) applied to it, wheres clusters
with the label `kubernetes.io/environment: dev` will have only dev-specific configuration (ie. `dev/config.json`)

## Overriding parameters from one child generator in another child generator

The Matrix Generator allows parameters with the same name to be defined in multiple child generators. This is useful, for example, to define default values for all stages in one generator and override them with stage-specific values in another generator. The example below generates a Helm-based application using a matrix generator with two git generators: the first provides stage-specific values (one directory per stage) and the second provides global values for all stages.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: parameter-override-example
spec:
generators:
- matrix:
generators:
- git:
repoURL: https://github.com/example/values.git
revision: HEAD
files:
- path: "**/stage.values.yaml"
- git:
repoURL: https://github.com/example/values.git
revision: HEAD
files:
- path: "global.values.yaml"
goTemplate: true
template:
metadata:
name: example
spec:
project: default
source:
repoURL: https://github.com/example/example-app.git
targetRevision: HEAD
path: .
helm:
values: |
{{ `{{ . | mustToPrettyJson }}` }}
destination:
server: in-cluster
namespace: default
```
Given the following structure/content of the example/values repository:
```
├── test
│ └── stage.values.yaml
│ stageName: test
│ cpuRequest: 100m
│ debugEnabled: true
├── staging
│ └── stage.values.yaml
│ stageName: staging
├── production
│ └── stage.values.yaml
│ stageName: production
│ memoryLimit: 512Mi
│ debugEnabled: false
└── global.values.yaml
cpuRequest: 200m
memoryLimit: 256Mi
debugEnabled: true
```
The matrix generator above would yield the following results:
```yaml
- stageName: test
cpuRequest: 100m
memoryLimit: 256Mi
debugEnabled: true

- stageName: staging
cpuRequest: 200m
memoryLimit: 256Mi
debugEnabled: true

- stageName: production
cpuRequest: 200m
memoryLimit: 512Mi
debugEnabled: false
```
## Example: Two Git Generators Using `pathParamPrefix`

The matrix generator will fail if its children produce results containing identical keys with differing values.
Expand Down

0 comments on commit e97a4f9

Please sign in to comment.