indent and nindent
🔹 1. indent Function
Usage:
Adds N spaces at the beginning of each line in the given string.
Does not add a newline before the string.
Commonly used when you need to shift a block of text to the right.
✅ Example:
Output:
🔹 2. nindent Function (newline + indent)
Usage:
Same as indent, but also adds a newline before the indented string.
Very useful when you're inserting a block of content inside YAML structure, and need to start on a new line with proper indentation.
✅ Example:
Output:
Without nindent, the YAML would be invalid because the list item would not be properly indented under env:.
🆚 Summary: indent vs nindent
🔁 Common Use in Helm Templates
toYaml converts map/list to YAML
nindent 8 indents it under env: correctly with 8 spaces
Purpose of {{- and -}} in Helm Templates
These are used to control whitespace (spaces, tabs, newlines) in your rendered YAML files.
🔹 Syntax Variants:
📌 Why is this important in Helm?
Helm templates output YAML, which is indent-sensitive. Uncontrolled whitespace can result in:
❌ Invalid YAML
❌ Unwanted blank lines
❌ Bad formatting
✅ Example: No Trimming
If .Values.env is:
Then the rendered output might look like:
❗Note the extra blank line between env: and the list.
✅ Example: Trimming with {{- and -}}
Now Helm trims the newline before the toYaml, and you get:
🎯 Clean YAML. No unwanted lines.
🔁 Practical Rule of Thumb
👇 Real Example in a Helm Template
yaml
CopyEdit
✅ This ensures:
No blank line before or after the loop
Proper YAML alignment
In Helm, .Files is a built-in object that allows you to access, read, and manipulate files inside your chart’s files/ directory at template rendering time.
🔍 .Files – What It Is
.Files refers to the set of all non-template files stored in the files/ directory of a Helm chart.
These are not deployed directly as standalone Kubernetes resources, but are accessible programmatically from your templates.
🧩 Typical Use Cases
Load a config file into a ConfigMap
Inject scripts, certs, or binary blobs into pods
Read YAML or JSON and parse as dict
Base64 encode content for Secrets
📁 Example Chart Structure
📘 Common .Files Functions
✅ Examples
1. Load File Content into ConfigMap
2. Base64 Encode for Secret
3. Iterate Over Multiple Files Using Glob
⚠️ Important Notes
Files must reside inside the files/ directory to be accessible via .Files.
You cannot access files from templates/, charts/, or outside the chart root.
.Files.Get returns an empty string if the file is not found—no error is thrown.
🧠 Summary
complete working Helm chart example using .Files to load a config file into a ConfigMap and a shell script into a Secret.
📁 Helm Chart Structure (Example)
🔹 1. files/myconfig.yaml
🔹 2. files/startup.sh
🔹 3. templates/configmap.yaml
✅ This loads files/myconfig.yaml content into the ConfigMap.
🔹 4. templates/secret.yaml
✅ This encodes files/startup.sh in Base64 and stores it in a Secret.
🔹 5. values.yaml
This chart doesn’t require values for .Files, but you can add values for naming or conditional loading if you want.
# Example of optional enhancements
🔹 Optional Enhancement: Make .Files File Name Configurable
Modify configmap.yaml
✅ How to Install and Test
helm install mychart ./mychart
Then check:
In Helm templates, if, else if, and else blocks allow you to add conditional logic to dynamically render Kubernetes YAML based on the values you pass in values.yaml. Helm templates use Go templating syntax.
🔰 1. Beginner Level: Basic if and else
📌 Syntax:
✅ Example: Enable a config only if a feature flag is true
values.yaml:
configmap.yaml:
🧾 Explanation:
If .Values.featureEnabled is true, it outputs:
feature: "enabled"
Else:
feature: "disabled"
🔁 2. Intermediate Level: else if and nested logic
📌 Syntax:
✅ Example: Set logging level based on user choice
values.yaml:
configmap.yaml:
🧾 Explanation:
You check multiple conditions using else if and Go's eq function.
eq .Values.logLevel "debug" returns true if the logLevel is "debug".
🔄 3. Advanced Level: Checking empty values, lists, maps, and nested structures
🔍 A. Check if a value exists or is not empty
values.yaml:
configmap.yaml:
🧾 Explanation:
Helm treats empty strings, zero, false, and nil as "false".
So if optionalSetting is empty, the else branch is used.
📚 B. Check if a list has items
values.yaml:
configmap.yaml:
🧾 Explanation:
If allowedUsers is not empty, it loops and prints them.
Else, it returns an empty list.
🔐 C. Nested if for complex structures
values.yaml:
configmap.yaml:
🧾 Explanation:
First, you check if auth.enabled is true.
Then, based on auth.method, decide the type.
✅ Summary Table
🧠 Tips
Always close your if blocks with {{- end }}.
Use {{- (note the dash) to trim whitespace for clean YAML.
Use quote and default for safety:
Nesting too deep? Refactor using define and template.
Let's now dive deep into using logical operators like and, or, and not with if, else if, and else in Helm templates, from beginner to advanced.
🚦 Logical Operators in Helm (if conditions)
Helm templates are powered by Go templates, and they support logical operators like:
🧱 1. Basic Usage of and and or
✅ Example: and
values.yaml:
deployment.yaml:
🧾 Explanation:
and returns true only if all conditions are true.
✅ Example: or
values.yaml:
deployment.yaml:
🧾 Explanation:
or returns true if any one condition is true.
🔂 2. not with if
values.yaml:
deployment.yaml:
🔁 3. Combine Multiple Conditions (and, or, eq)
✅ Advanced Example: Multiple logic combinations
values.yaml:
configmap.yaml:
🧾 Explanation:
Checks all 3:
env is "prod"
userRole is "admin"
enableFeature is true
💡 Nesting and, or, not (Complex Conditions)
Example:
🧾 This renders someFlag: true if:
env is "prod" AND feature is enabled
OR env is "staging"
🔐 Condition on Map Key Existence (Advanced)
Sometimes you want to conditionally check if a key in a map exists and is not empty:
✅ Summary Table: Logical Operators with if
🛠 Real-World Use Case: Toggle ingress
values.yaml:
ingress.yaml:
haskey
The hasKey function in Helm templates (inherited from Go templates) is used to check whether a map contains a specific key.
🔍 Purpose of hasKey
To safely check if a key exists in a map before trying to access it.
Prevents errors like:
error calling include: can't evaluate field <key> in type <nil>
📘 Syntax
MAP: A map-like object (e.g., .Values, .Values.someMap)
KEY: A string or variable representing the key
🔁 It returns:
true → if the map contains the key
false → if the key is missing
✅ Simple Example
values.yaml
configmap.yaml
🧾 Explanation:
Checks if .Values.features map has the key "logging"
Only then tries to access .Values.features.logging
⚠️ Why hasKey is Important?
❌ Wrong:
✅ Correct:
🧪 Example with Nested Maps
values.yaml
Template:
🔁 Dynamic Key Check (with variables)
🔐 hasKey with required for Validation
{{ required "Missing config!" (hasKey .Values "config") }}
This ensures that the chart fails fast with a useful error if a key is missing.
⚡ Recap: When to Use hasKey
🧠 Bonus Tip
For safety: