Of course! This is a very common requirement in Helm templates. You need to capture the output first and then use a function to provide a default value if it's empty.
The most idiomatic and concise ways to do this in Helm are with the or or default functions.
Here is the breakdown.
The Core Problem
Your original command tries to get and print the value in a single motion.
{{- get (lookup ...).spec.template.metadata.annotations "checksum/config" -}}
To check the value before printing it, you first need to store it in a variable.
Solution 1: Using the or Function (Recommended)
The or function returns the first of its arguments that is not an "empty" value (i.e., not false, 0, "", or nil). This is perfect for your use case.
Step 1: Store the result of the get command in a variable.
Step 2: Use or to provide the dummy value as a fallback.
{{- /* 1. Look up the Deployment and get the specific annotation */ -}}
{{- $checksum := get (lookup "apps/v1" "Deployment" .Release.Namespace .Name).spec.template.metadata.annotations "checksum/config" -}}
{{- /* 2. Use 'or' to provide a default value if $checksum is empty or nil */ -}}
{{- or $checksum "dummy-checksum-value" | quote }}
I've replaced $input_namespace and $input_deployment_name with common Helm values (.Release.Namespace, .Name), but you can use your own variables.
Explanation:
$checksum := get ...: This part is the same as your original logic, but instead of printing the result, it saves it to the variable $checksum.
or $checksum "dummy-checksum-value": This is the key part.
- If
$checksum has a value (e.g., "abcde123"), or will see it's not empty and return it.
- If
$checksum is empty ("") or nil, or will skip it and return the next argument, "dummy-checksum-value".
| quote: This is good practice to ensure the final output is a properly quoted string, which is often what you need in YAML.
Solution 2: Using the default Function
The default function is also designed for this. It's typically used in a pipeline.
{{- /* 1. Look up the Deployment and get the specific annotation */ -}}
{{- $checksum := get (lookup "apps/v1" "Deployment" .Release.Namespace .Name).spec.template.metadata.annotations "checksum/config" -}}
{{- /* 2. Use 'default' to provide a fallback value */ -}}
{{- $checksum | default "dummy-checksum-value" | quote }}
Explanation:
The logic is very similar to or. The pipeline (|) passes the value of $checksum to the default function. If $checksum is empty, default returns its argument ("dummy-checksum-value"). Otherwise, it returns the original value of $checksum.
Solution 3: Using an if/else Block (More Verbose)
If you need more complex logic, a full if/else block is the clearest way to write it.
{{- $checksum := get (lookup "apps/v1" "Deployment" .Release.Namespace .Name).spec.template.metadata.annotations "checksum/config" -}}
{{- if $checksum }}
{{- $checksum | quote }}
{{- else }}
{{- "dummy-checksum-value" | quote }}
{{- end }}
Explanation:
This is the most explicit approach. The if $checksum condition checks if the variable is "truthy" (not nil, false, 0, or an empty string). It's easier to read for complex scenarios but more verbose for a simple default.
Important Consideration: Handling a Missing Deployment
A major risk with your original command is that the lookup itself could fail (e.g., the Deployment doesn't exist yet on a fresh install). This would cause a templating error like nil pointer evaluating spec.
A more robust solution checks if the lookup returned an object at all before trying to access its fields.
Here is a best-practice, production-ready example:
{{- /* Initialize a variable for the final value */ -}}
{{- $finalChecksum := "default-value-if-deployment-not-found" -}}
{{- /* 1. Look up the deployment and store the entire object */ -}}
{{- $deployment := lookup "apps/v1" "Deployment" .Release.Namespace .Name -}}
{{- /* 2. Only if the deployment exists, try to get the annotation */ -}}
{{- if $deployment -}}
{{- /* 3. Get the annotation, using 'or' to handle a missing annotation */ -}}
{{- $finalChecksum = get $deployment.spec.template.metadata.annotations "checksum/config" | or "default-value-if-annotation-is-empty" -}}
{{- end -}}
{{- /* 4. Print the final, safe value */ -}}
myChecksum: {{ $finalChecksum | quote }}
This pattern is much safer because:
- It handles the case where the Deployment doesn't exist.
- It handles the case where the Deployment exists but the
checksum/config annotation is missing or empty.
- It provides different default values for each failure case, which can be great for debugging.