In the vast landscape of infrastructure as code (IaC), Helm templates offer a powerful tool for dynamic and repeatable deployments in Kubernetes. One of Helm's standout features is its capability to handle ranges, enabling you to create highly flexible and scalable templates. Today, we explore Helm template ranges through an illustrative example.

To begin, it's crucial to understand that Helm ranges allow you to generate multiple instances of a chart template based on a given set of data. This functionality is achieved using the `.Files` function in combination with `range` and `index` functions. Let's delve into an example that illustrates this beautifully.

Defining a Range in Helm Templates
In this section, we'll see how to define a range in a Helm template. We'll create a simple example that generates multiple instances of a deployment based on a list of strings.

First, we define our values file with a list of strings we want to use as chart instances. This list could represent different environments, applications, or any other set of distinct data. Here's a simple `values.yaml` file:
```yaml envs: - dev - staging - production ```
Using `.Files` and `range` to Generate Instances

Now, we'll use the `.Files` function along with `range` to generate multiple instances of our chart for each environment. In our `templates/deployment.yaml`, we might write something like this:
```yaml {{- range $index, $env := .Values.envs }} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: 1 selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- end }} ```
In this code, the `range` function iterates over the `$envs` list from our values file. For each environment, it creates a new deployment with a unique name and label based on the environment name.
Indexing to Control Instance Behavior

In Helm, the `index` function provides a zero-based index for each item in the range, which you can use to control the instance's behavior based on its position in the list. For instance, you could use the index to set a replica count based on the environment:
```yaml {{- range $index, $env := .Values.envs }} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: {{ add1 $index }} # adjust replicas based on index selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- end }} ```
Here, the `add1` function is used to increment the replica count for each subsequent environment.
Advanced Ranges:Conditional Instance Generation

Now that we've shown how to generate instances based on a range, let's explore a more advanced use case – conditional instance generation. Perhaps you want to create deployments for some, but not all, environments.
To achieve this, you can use the `if` and `else` functions along with `range` to control instance generation conditionally. For example, you might want to skip the `dev` environment to ensure only production-like environments are deployed:









```yaml {{- range $index, $env := .Values.envs }} {{- if ne $env "dev" -}} apiVersion: apps/v1 kind: Deployment metadata: name: my-app-{{ $env }} spec: replicas: {{ add1 $index }} selector: matchLabels: app: my-app-{{ $env }} template: metadata: labels: app: my-app-{{ $env }} spec: containers: - name: my-app image: my-image:{{ $env }} {{- else -}} {{- end }} {{- end }} ```
In this code, the `if` statement checks if the current environment is not equal to `dev` (`ne $env "dev"`). If the condition is true, it generates the deployment. Otherwise, it skips to the end of the conditional block, skipping the deployment creation for the `dev` environment.
With Helm's powerful TPL language, you can create incredibly flexible and dynamic templates, bringing your IaC to the next level. Our example has barely scratched the surface of what's possible with Helm template ranges.
So, why wait? Dive into the world of Helm template ranges today and start unlocking the full potential of your Kubernetes deployments. Goodbye, static charts! Hello, dynamic Kubernetes deployments!