You're encountering a common Helm templating challenge: safely accessing nested fields that might be missing or nil. The get function expects its first argument to be a map, and if D.spec.template.metadata.annotations is nil, it will error.
To handle this gracefully and provide a default value, you need to ensure that:
- The
annotations map itself is not nil (if it's missing entirely from the object).
- The specific key (
"checksum/config") within the annotations map is handled (if it's missing, but the map exists).
Here's the most robust way to do it:
{{- $input_namespace := "my-namespace" }}
{{- $input_deployment_name := "my-deployment" }}
{{- $default_checksum_value := "none" }} # Your desired default value
{{- $checksum := $default_checksum_value }} # Initialize with the default
{{- $deployment := lookup "apps/v1" "Deployment" $input_namespace $input_deployment_name }}
{{- if $deployment }}
{{- /* Get the annotations map, defaulting to an empty dict if it's nil */}}
{{- $annotations := default (dict) $deployment.spec.template.metadata.annotations }}
{{- /* Get the specific checksum key, defaulting to the default value if it's nil/empty */}}
{{- $checksum = default $default_checksum_value (get $annotations "checksum/config") }}
{{- end }}
{{- $checksum }}
Explanation:
$default_checksum_value := "none": We define your desired default value clearly at the beginning.
$checksum := $default_checksum_value: We initialize the $checksum variable with the default value. This ensures that if the lookup fails or any subsequent step doesn't find the value, $checksum will already hold the default.
$deployment := lookup ...: You perform your lookup as before.
{{- if $deployment }}: It's good practice to check if the lookup returned an object. If the deployment doesn't exist, $deployment will be nil, and trying to access its fields (.spec, etc.) would cause an error.
$annotations := default (dict) $deployment.spec.template.metadata.annotations: This is the crucial part for handling a missing annotations map:
$deployment.spec.template.metadata.annotations tries to get the annotations field.
- If this field is
nil (meaning no annotations were defined for the template at all), the default (dict) function will return an empty dictionary ({}).
- If the field does exist, it will return the actual annotations map.
- Now,
$annotations is guaranteed to be a map (either the actual one or an empty one), so get can be safely called on it.
$checksum = default $default_checksum_value (get $annotations "checksum/config"):
get $annotations "checksum/config" attempts to retrieve the value for the key "checksum/config" from the $annotations map.
- If the key exists, its value is returned.
- If the key doesn't exist (or if
$annotations was an empty dict from the previous step), get will return nil.
- The outer
default $default_checksum_value then catches this nil (or empty string, etc.) and replaces it with your $default_checksum_value.
{{- end }}: Closes the if $deployment block.
{{- $checksum }}: Finally, print the resulting $checksum value.
This approach is robust because it handles both potential missing parts of the path: the annotations map itself, and the specific key within that map.