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.
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
