Keep your Helm charts DRY (Don’t Repeat Yourself) by consolidating common code blocks in a helper file.
Moving Common Labels to a Helper File
To address this redundancy, you can transfer the shared lines to a helper file (commonly named_helpers.tpl). The underscore in the filename tells Helm to ignore this file when generating Kubernetes manifests. For example, if you start with a Service template like this:
define directive. In your _helpers.tpl, add:
.) to the template call passes the current context into the helper file. Without it, the helper template wouldn’t have access to critical values like .Release.Name.
Applying Named Templates in Deployment Manifests
The same approach works for Deployment manifests where the labels are used in multiple locations. Initially, you might attempt to reuse the helper template like this:Using the include Function for Proper Indentation
To resolve the indentation problem, use theinclude function instead of template. The include function allows the output to be piped to other functions like indent. Ensure your helper template in _helpers.tpl is defined as follows:
Final Example
Below is an example of the final output with actual values replacing the template directives:include with proper indentation not only enhances code maintainability but also minimizes the risk of errors and inconsistencies in your Helm charts.
For more details on managing Helm charts and Kubernetes manifests, visit the Helm Documentation and Kubernetes Concepts.