Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deep-merge of values, and allow empty values file #812

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions flux_local/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ def _lookup_value_reference(

return found_value

def deep_merge(base: dict, override: dict) -> dict:
"""
Recursively merge two dictionaries, similar to how Helm merges values.
Lists are replaced entirely (Helm behavior).
"""
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge(result[key], value)
else:
result[key] = value
return result


def _update_helmrelease_values(
ref: ValuesReference,
Expand All @@ -196,15 +209,17 @@ def _update_helmrelease_values(
inner_values[parts[-1]] = found_value
else:
obj = yaml.load(found_value, Loader=yaml.SafeLoader)
if not obj or not isinstance(obj, dict):
# Handle empty YAML file case
if obj is None:
obj = {}
if not isinstance(obj, dict):
raise InputException(
f"Expected '{ref.name}' field '{ref.target_path}' values to be valid yaml, found {type(values)}"
)
values.update(obj)
values = deep_merge(values, obj)

return values


def expand_value_references(
helm_release: HelmRelease, kustomization: Kustomization
) -> HelmRelease:
Expand Down Expand Up @@ -286,3 +301,4 @@ def expand_postbuild_substitute_reference(
_LOGGER.debug("update_postbuild_substitutions=%s", values)
ks.update_postbuild_substitutions(values)
return ks

7 changes: 6 additions & 1 deletion tests/testdata/cluster8/apps/podinfo-values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ data:
redis:
enabled: true
repository: public.ecr.aws/docker/library/redis
tag: 7.0.6
tag: 7.0.5
ingress:
enabled: true
className: nginx
Expand All @@ -18,3 +18,8 @@ data:
paths:
- path: /
pathType: ImplementationSpecific
empty-values.yaml: ""
patch-values.yaml: |-
redis:
tag: 7.0.6

7 changes: 7 additions & 0 deletions tests/testdata/cluster8/apps/podinfo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ spec:
valuesFrom:
- kind: ConfigMap
name: podinfo-values
- kind: ConfigMap
name: podinfo-values
valuesKey: empty-values.yaml
- kind: ConfigMap
name: podinfo-values
valuesKey: patch-values.yaml
- kind: Secret
name: podinfo-tls-values
valuesKey: crt
targetPath: tls.crt
optional: true

24 changes: 22 additions & 2 deletions tests/tool/__snapshots__/test_build.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,12 @@
valuesFrom:
- kind: ConfigMap
name: podinfo-values
- kind: ConfigMap
name: podinfo-values
valuesKey: empty-values.yaml
- kind: ConfigMap
name: podinfo-values
valuesKey: patch-values.yaml
- kind: Secret
name: podinfo-tls-values
optional: true
Expand All @@ -956,11 +962,15 @@
---
apiVersion: v1
data:
empty-values.yaml: ""
patch-values.yaml: |-
redis:
tag: 7.0.6
values.yaml: |-
redis:
enabled: true
repository: public.ecr.aws/docker/library/redis
tag: 7.0.6
tag: 7.0.5
ingress:
enabled: true
className: nginx
Expand Down Expand Up @@ -6333,6 +6343,12 @@
valuesFrom:
- kind: ConfigMap
name: podinfo-values
- kind: ConfigMap
name: podinfo-values
valuesKey: empty-values.yaml
- kind: ConfigMap
name: podinfo-values
valuesKey: patch-values.yaml
- kind: Secret
name: podinfo-tls-values
optional: true
Expand All @@ -6341,11 +6357,15 @@
---
apiVersion: v1
data:
empty-values.yaml: ""
patch-values.yaml: |-
redis:
tag: 7.0.6
values.yaml: |-
redis:
enabled: true
repository: public.ecr.aws/docker/library/redis
tag: 7.0.6
tag: 7.0.5
ingress:
enabled: true
className: nginx
Expand Down
Loading