Cloud / HELM Interview Questions
How do you use the 'lookup' function in Helm templates for advanced conditional logic?
The lookup function queries the Kubernetes API server during template rendering, enabling charts to adapt based on actual cluster state rather than just values.
Syntax: {{ lookup "apiVersion" "resource" "namespace" "name" }} Returns resource object or nil if not found.
Common use cases with examples:
1. Conditional namespace creation: {{- if not (lookup "v1" "Namespace" "" "my-namespace") }} apiVersion: v1 kind: Namespace metadata: name: my-namespace {{- end }}
2. Check if storage class exists before using it: {{- if (lookup "storage.k8s.io/v1" "StorageClass" "" "fast-storage") }} storageClassName: fast-storage {{- else }} storageClassName: standard {{- end }}
3. Retrieve existing configmap for data merging: {{- $existing := lookup "v1" "ConfigMap" .Release.Namespace "app-config" }} {{- if $existing }} {{- $existingData := $existing.data }} # Merge with existing, preserving user modifications {{- end }}
4. Certificate checking before creating secrets: {{- if not (lookup "cert-manager.io/v1" "Certificate" .Release.Namespace "tls-cert") }} # Create certificate only if missing {{- end }}
Limitations and considerations:
- lookup only works during
helm upgrade --install(not withhelm templateorhelm lint) - Requires RBAC permissions to read the resources being queried
- Can slow down rendering for many lookups (cache is not cluster-wide)
- Results may change between dry-run and actual install (race conditions)
- Cannot mutate state - read-only operation
Debugging lookup: Use {{- $result := lookup "v1" "Pod" .Release.Namespace "my-pod" }} {{- $result | toYaml | nindent 0 }} to inspect what lookup returns.
More Related questions...