Skip to content

Commit

Permalink
Merge pull request #1060 from Preisschild/feat/targetpath-allow-overw…
Browse files Browse the repository at this point in the history
…rite

Allow overwriting inline values with targetPath
  • Loading branch information
stefanprodan authored Sep 13, 2024
2 parents e05c4ff + c07f108 commit 94748ca
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion docs/spec/v2/helmreleases.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ Changes to the combined values will trigger a new Helm release.
`.spec.valuesFrom` is an optional list to refer to ConfigMap and Secret
resources from which to take values. The values are merged in the order given,
with the later values overwriting earlier, and then [inline values](#inline-values)
overwriting those.
overwriting those. When `targetPath` is set, it will overwrite everything before,
including inline values.

An item on the list offers the following subkeys:

Expand Down
3 changes: 2 additions & 1 deletion docs/spec/v2beta2/helmreleases.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ Changes to the combined values will trigger a new Helm release.
`.spec.valuesFrom` is an optional list to refer to ConfigMap and Secret
resources from which to take values. The values are merged in the order given,
with the later values overwriting earlier, and then [inline values](#inline-values)
overwriting those.
overwriting those. When `targetPath` is set, it will overwrite everything before,
including inline values.

An item on the list offers the following subkeys:

Expand Down
7 changes: 5 additions & 2 deletions internal/chartutil/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ const (

// ChartValuesFromReferences attempts to construct new chart values by resolving
// the provided references using the client, merging them in the order given.
// If provided, the values map is merged in last. Overwriting values from
// references. It returns the merged values, or an ErrValuesReference error.
// If provided, the values map is merged in last overwriting values from references,
// unless a reference has a targetPath specified, in which case it will overwrite all.
// It returns the merged values, or an ErrValuesReference error.
func ChartValuesFromReferences(ctx context.Context, client kubeclient.Client, namespace string,
values map[string]interface{}, refs ...v2.ValuesReference) (chartutil.Values, error) {

Expand Down Expand Up @@ -234,6 +235,8 @@ func ChartValuesFromReferences(ctx context.Context, client kubeclient.Client, na
}

if ref.TargetPath != "" {
result = transform.MergeMaps(result, values)

// TODO(hidde): this is a bit of hack, as it mimics the way the option string is passed
// to Helm from a CLI perspective. Given the parser is however not publicly accessible
// while it contains all logic around parsing the target path, it is a fair trade-off.
Expand Down
41 changes: 41 additions & 0 deletions internal/chartutil/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,47 @@ other: values
},
},
},
{
name: "target path precedence over all",
resources: []runtime.Object{
mockConfigMap("values", map[string]string{
"values.yaml": `flat: value
nested:
configuration:
- one
- two
- three
`,
}),
mockSecret("values", map[string][]byte{"key": []byte("value")}),
},
references: []v2.ValuesReference{
{
Kind: kindSecret,
Name: "values",
ValuesKey: "key",
TargetPath: "nested.configuration[0]",
},
{
Kind: kindConfigMap,
Name: "values",
},
},

values: `
nested:
configuration:
- list
- item
- option
`,
want: chartutil.Values{
"flat": "value",
"nested": map[string]interface{}{
"configuration": []interface{}{"value", "item", "option"},
},
},
},
{
name: "target path for string type array item",
resources: []runtime.Object{
Expand Down

0 comments on commit 94748ca

Please sign in to comment.