Cloud Native 12 min read

Advanced Helm Chart Techniques: Setting Replica Limits, Sharing Code with Sub‑templates, and Generating ConfigMaps

This article explains how to create and customize Helm charts by setting replica count ceilings, reusing logic with sub‑templates, and generating properly indented ConfigMaps using toYaml and indent functions, providing practical examples and command‑line snippets for Kubernetes deployments.

DevOps Cloud Academy
DevOps Cloud Academy
DevOps Cloud Academy
Advanced Helm Chart Techniques: Setting Replica Limits, Sharing Code with Sub‑templates, and Generating ConfigMaps

Helm is the package manager for Kubernetes. While most users rely on existing charts, many scenarios require creating custom Helm charts to package applications or modify existing ones. The essential files are templates (YAML templates) and _helpers.tpl , which use Go templating and Sprig functions.

Setting the Helm Environment

Run helm create mychart to generate a starter chart with a predefined directory structure:

mychart
├── Chart.yaml
├── charts
├── templates
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── service.yaml
│   ├── serviceaccount.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

1. Setting a Replica Count Ceiling

Challenge

When more than 10 pods are requested, the application fails. We need the chart to cap the replica count at 10.

Solution

Use an if‑else block in templates/deployment.yaml :

{{- if gt (.Values.replicaCount | int) 10 }}
  {{- print 10 }}
{{- else }}
  {{- print .Values.replicaCount }}
{{- end }}

This logic checks whether .Values.replicaCount (converted to an integer) exceeds 10 and returns the appropriate value.

2. Sharing Code with Sub‑templates

Define a reusable snippet in templates/_helpers.tpl :

{{- define "replicaCountCeiling" -}}
  {{- if gt (.Values.replicaCount | int) 10 }}
    {{- print 10 }}
  {{- else }}
    {{- print .Values.replicaCount }}
  {{- end }}
{{- end }}

Then include it in any template, e.g., templates/deployment.yaml :

replicas: {{ include "replicaCountCeiling" . }}

The include function takes the sub‑template name and the root context (.) so the same logic can be reused across Deployments, StatefulSets, etc.

3. Generating YAML Snippets for ConfigMaps

To embed configuration data from values.yaml into a ConfigMap, use the toYaml function:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  config.yaml: |
    {{ .Values.config | toYaml }}

Because toYaml outputs a flat map, the resulting indentation is incorrect. Apply indent to fix it:

data:
  config.yaml: |
    {{ .Values.config | toYaml | indent 4 }}

The final rendered ConfigMap looks like:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  config.yaml: |
    api:
      port: 8080
      url: http://someurl
    db:
      name: mydb
      port: 3306

Conclusion

Helm offers powerful templating capabilities. By mastering functions such as if , define , include , toYaml , and indent , you can build flexible, reusable charts that handle complex deployment requirements.

KubernetesDevOpsHelmTemplatingchartConfigMap
DevOps Cloud Academy
Written by

DevOps Cloud Academy

Exploring industry DevOps practices and technical expertise.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.