Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ Accepts either a YAML string or a list of init container maps and indents output
Resolve pod and container security contexts by layering chart values over global defaults.
*/}}
{{- define "network-bootstrapper.securityContexts" -}}
{{- $root := . -}}
{{- $globalValues := ($root.Values.global | default (dict)) -}}
{{- $globalSecurityContexts := dig "securityContexts" $globalValues (dict) -}}
{{- $pod := mergeOverwrite (deepCopy (dig "pod" $globalSecurityContexts (dict))) (default (dict) $root.Values.podSecurityContext) -}}
{{- $container := mergeOverwrite (deepCopy (dig "container" $globalSecurityContexts (dict))) (default (dict) $root.Values.securityContext) -}}
{{- dict "pod" $pod "container" $container | toYaml -}}
{{- $ctx := index . "ctx" -}}
{{- $dest := index . "dest" -}}
{{- $globalValues := ($ctx.Values.global | default (dict)) -}}
{{- $globalSecurityContexts := default (dict) (get $globalValues "securityContexts") -}}
{{- $podDefaults := default (dict) (get $globalSecurityContexts "pod") -}}
{{- $containerDefaults := default (dict) (get $globalSecurityContexts "container") -}}
Comment on lines +88 to +90
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While the use of get and default is correct, you could use the dig function to make this code more concise. The dig function is designed for safely accessing potentially missing keys in nested maps, which is exactly what's being done here. This would also make the implementation more consistent with the previous version of the code.

{{- $globalSecurityContexts := dig "securityContexts" $globalValues (dict) -}}
{{- $podDefaults := dig "pod" $globalSecurityContexts (dict) -}}
{{- $containerDefaults := dig "container" $globalSecurityContexts (dict) -}}

{{- $_ := set $dest "pod" (mergeOverwrite (deepCopy $podDefaults) (default (dict) $ctx.Values.podSecurityContext)) -}}
{{- $_ := set $dest "container" (mergeOverwrite (deepCopy $containerDefaults) (default (dict) $ctx.Values.securityContext)) -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ spec:
{{- toYaml . | nindent 8 }}
{{- end }}
serviceAccountName: {{ include "network-bootstrapper.serviceAccountName" . }}
{{- $securityContexts := include "network-bootstrapper.securityContexts" . | fromYaml }}
{{- $securityContexts := dict -}}
{{- include "network-bootstrapper.securityContexts" (dict "ctx" . "dest" $securityContexts) -}}
{{- $podSecurityContext := index $securityContexts "pod" }}
{{- $containerSecurityContext := index $securityContexts "container" }}
{{- if $podSecurityContext }}
Expand Down
14 changes: 8 additions & 6 deletions charts/network/charts/network-nodes/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ Accepts either a YAML string or a list of init container maps and indents output
Resolve pod and container security contexts using global defaults plus chart overrides.
*/}}
{{- define "nodes.securityContexts" -}}
{{- $root := . -}}
{{- $globalValues := ($root.Values.global | default (dict)) -}}
{{- $globalSecurityContexts := dig "securityContexts" $globalValues (dict) -}}
{{- $pod := mergeOverwrite (deepCopy (dig "pod" $globalSecurityContexts (dict))) (default (dict) $root.Values.podSecurityContext) -}}
{{- $container := mergeOverwrite (deepCopy (dig "container" $globalSecurityContexts (dict))) (default (dict) $root.Values.securityContext) -}}
{{- dict "pod" $pod "container" $container | toYaml -}}
{{- $ctx := index . "ctx" -}}
{{- $dest := index . "dest" -}}
{{- $globalValues := ($ctx.Values.global | default (dict)) -}}
{{- $globalSecurityContexts := default (dict) (get $globalValues "securityContexts") -}}
{{- $podDefaults := default (dict) (get $globalSecurityContexts "pod") -}}
{{- $containerDefaults := default (dict) (get $globalSecurityContexts "container") -}}
Comment on lines +137 to +139
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For conciseness and consistency, consider using the dig function here. It's well-suited for safely accessing nested map keys and would make the code more compact, similar to the previous implementation.

{{- $globalSecurityContexts := dig "securityContexts" $globalValues (dict) -}}
{{- $podDefaults := dig "pod" $globalSecurityContexts (dict) -}}
{{- $containerDefaults := dig "container" $globalSecurityContexts (dict) -}}

{{- $_ := set $dest "pod" (mergeOverwrite (deepCopy $podDefaults) (default (dict) $ctx.Values.podSecurityContext)) -}}
{{- $_ := set $dest "container" (mergeOverwrite (deepCopy $containerDefaults) (default (dict) $ctx.Values.securityContext)) -}}
{{- end -}}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ spec:
{{- $initContainers := .Values.initContainers | default (dict) }}
{{- $sharedInitContainers := get $initContainers "shared" }}
{{- $rpcInitContainers := get $initContainers "rpc" }}
{{- $securityContexts := include "nodes.securityContexts" . | fromYaml }}
{{- $securityContexts := dict -}}
{{- include "nodes.securityContexts" (dict "ctx" . "dest" $securityContexts) -}}
{{- $podSecurityContext := index $securityContexts "pod" }}
{{- $containerSecurityContext := index $securityContexts "container" }}
podManagementPolicy: Parallel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ spec:
{{- $initContainers := .Values.initContainers | default (dict) }}
{{- $sharedInitContainers := get $initContainers "shared" }}
{{- $validatorInitContainers := get $initContainers "validator" }}
{{- $securityContexts := include "nodes.securityContexts" . | fromYaml }}
{{- $securityContexts := dict -}}
{{- include "nodes.securityContexts" (dict "ctx" . "dest" $securityContexts) -}}
{{- $podSecurityContext := index $securityContexts "pod" }}
{{- $containerSecurityContext := index $securityContexts "container" }}
podManagementPolicy: Parallel
Expand Down
Loading