A Helm templating question!
You're close. You can use the default function in Helm's Go templating to achieve this. Here's the modified command:
{{ (lookup "apps/v1" "Deployment" $input_namespace $input_deployment_name).spec.template.metadata.annotations "checksum/config" | default "DUMMY_VALUE_HERE" }}
The default function works like this:
- If the value before the pipe (
|) is not empty/null (i.e., it's "truthy"), it will be returned as-is.
- If the value before the pipe is empty/null (i.e., it's "falsy"), the value after
default will be returned instead.
In your case, if the .spec.template.metadata.annotations["checksum/config"] value is empty or null, Helm will return "DUMMY_VALUE_HERE" instead.
Here's a breakdown:
(lookup "apps/v1" "Deployment" $input_namespace $input_deployment_name) performs the lookup and returns the Deployment object (or nil if not found).
.spec.template.metadata.annotations accesses the annotations map within the Deployment's Pod template metadata.
"checksum/config" attempts to retrieve the value associated with the key "checksum/config" from the annotations map. If the key is not present or the value is empty, this will return nil or an empty string.
| default "DUMMY_VALUE_HERE" pipes the result from step 3 to the default function. If the value is empty/null, it returns "DUMMY_VALUE_HERE".
Example:
Suppose the Deployment exists, but checksum/config annotation is missing:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
template:
metadata:
annotations: {}
In this case, (lookup ...).spec.template.metadata.annotations "checksum/config" returns nil. The default function kicks in, and the entire expression returns "DUMMY_VALUE_HERE".
If the annotation is present with an empty value:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
template:
metadata:
annotations:
checksum/config: ""
The expression returns an empty string (""), which is considered "falsy" by Helm. Again, default returns "DUMMY_VALUE_HERE".
Now, if the annotation has a value:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
template:
metadata:
annotations:
checksum/config: "abcdef123456"
The expression returns "abcdef123456", and default is not triggered.
That's it! With default, you can elegantly handle empty or null values in Helm templates.
P.S. Don't forget the {{ }} brackets around the entire expression. I assume you had those in your original template, but just double-checking!