How to make nested variables optional in Helm
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding Nested Variables in Helm
Helm is a popular package manager for Kubernetes that simplifies the deployment and management of applications in a Kubernetes cluster. One of the core concepts in Helm is the use of templates, which allow you to create customizable and dynamic YAML files. A significant feature of Helm templates is their ability to handle complex configurations using nested variables. However, handling nested variables can sometimes be cumbersome, especially when you want to make some of them optional. This article explores various techniques to make nested variables optional in Helm, enhancing flexibility and ease of configuration.
YAML and Templates in Helm
To understand the need for optional nested variables, consider how configurations are structured in Helm charts. In Helm, your configuration typically resides in the values.yaml file:
Here, annotations are nested under service. These nested variables add complexity but are also powerful in configuring your application.
Making Nested Variables Optional
While working with Helm, you might want some of these nested variables to be optional. For instance, what if you want the annotations to be user-configurable or completely removable?
Technique 1: Using default Function
Helm provides a default function that allows you to specify default values if the variable is not set:
Here, default is used to provide a fallback value for missing keys, ensuring that the template doesn't fail due to unset variables.
Technique 2: Conditional Logic with if Statements
Alternatively, you can use conditional statements to determine whether a nested variable should be added to the template:
In this case, the block within if only executes if annotations exist under service, making the entire construct optional.
Example: Making Annotations Fully Optional
Consider the full service template where annotations are optional:
In this example, the block under annotations is only added if annotations exist in values.yaml. The toYaml function, along with nindent, formats the annotations correctly, ensuring proper YAML indentation.
Best Practices
When dealing with nested variables in Helm, follow these guidelines:
- Use Defaults: Use
defaultvalues to avoid runtime errors. - Conditional Logic: Employ
ifconditions to make sections optional. - Tidy Templates: Keep templates clean and readable by avoiding deep nesting of logic.
- Document Defaults: Clearly document default values and expected configurations for easier maintenance.
Quick Reference
- Use
defaultwhen an individual nested value should fall back to something safe. - Use
ifwhen an entire block should disappear if the parent value is missing. - Use
toYamlwithnindentwhen you want to render an optional nested map cleanly.
Conclusion
Handling nested variables in Helm templates becomes significantly easier when employing techniques to make these variables optional. By leveraging functions such as default and employing conditional logic carefully, you can craft flexible Helm charts that cater to a wide range of configurations. Adhering to best practices will ensure that your charts remain robust, maintainable, and easy to understand.
This article has detailed the methods to address optional nested variables, but thorough testing and good documentation are essential when implementing these concepts in real-world Helm charts, as they enhance predictability and usability.
Related reading
- How to make use of Kubernetes port names?
- How to manage page cache resources when running Kafka in Kubernetes
- How to manage persistent connections in kubernetes
- How to manage pod scheduling in aws EKS?
- How to make Terraform to read AWS Credentials file?
- How to make the tensorflow hub embeddings servable using tensorflow serving?
- How to merge kubectl config file with /.kube/config?
- How to mimic '--volumes-from' in Kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.