In this guide, we explore how to simplify and optimize your Helm chart templates using “with” blocks. Using a ConfigMap example, we demonstrate how setting the scope can reduce repetition when referencing values defined in your values.yaml file.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Example Values
Consider the following configuration in your values.yaml file:Basic ConfigMap Template Without “with” Blocks
A basic ConfigMap template (e.g., configmap.yaml) might look like this:.), and you must explicitly traverse the hierarchy, which can lead to repetitive expressions such as .Values.app.ui.bg.
By using “with” blocks, you can simplify your templates by setting a local scope. This reduces repetition and makes your templates cleaner and easier to maintain.
Simplifying Templates with the “with” Block
You can set the scope to.Values.app to simplify your references. Here’s how you can refactor the ConfigMap template:
.Values.app, allowing access to nested properties directly (e.g., .ui.bg instead of .Values.app.ui.bg).
Nesting “with” Blocks for Even Cleaner Templates
For better organization and readability, you can further nest “with” blocks to separate UI and database settings. For example:.Values.app.ui and .Values.app.db respectively. Note that you can use $.Release.Name to access the release name from the root context, as the current scope has been altered.
Using nested “with” blocks not only cleans up your templates but also clarifies the relationship between different configuration sections.