Helm charts
Kubernetes
if clause
YAML
conditional logic

If clause in helm chart

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Helm templates support conditional rendering with if, else if, and else. These clauses are essential when chart behavior varies by environment, feature flag, or optional resource settings.

Correct conditional usage keeps charts flexible and avoids invalid Kubernetes manifests.

Core Sections

1) Basic if syntax

yaml
1{{- if .Values.service.enabled }}
2apiVersion: v1
3kind: Service
4metadata:
5  name: {{ include "app.fullname" . }}
6{{- end }}

The block renders only when value is truthy.

2) else and else if

yaml
1{{- if eq .Values.env "prod" }}
2replicas: 5
3{{- else if eq .Values.env "staging" }}
4replicas: 2
5{{- else }}
6replicas: 1
7{{- end }}

Use branching for environment-specific defaults.

3) Boolean and nil-safe checks

yaml
{{- if and .Values.ingress (hasKey .Values.ingress "enabled") .Values.ingress.enabled }}
# ingress manifest
{{- end }}

Guard against missing nested keys.

4) Conditionals inside lists

yaml
1env:
2  - name: APP_ENV
3    value: {{ .Values.env | quote }}
4{{- if .Values.debug }}
5  - name: DEBUG
6    value: "true"
7{{- end }}

Indentation control is critical in YAML templates.

5) Use helpers for complex logic

Move complex conditions to _helpers.tpl to keep manifests readable.

6) Production checklist for Helm conditional templating

Turning a working snippet into production-ready behavior requires explicit validation beyond unit examples. Start by defining measurable acceptance criteria for correctness, reliability, and performance. Correctness should include at least one golden input-output case and one edge case. Reliability should include how failures are surfaced and whether retries are safe. Performance should be measured with representative input size, not tiny toy examples that hide scaling issues. Once these criteria are written down, keep them close to the code so maintainers know what guarantees must hold during refactors.

Operational readiness also depends on environment clarity. Document runtime version constraints, required configuration keys, and any external dependencies such as services, files, or credentials. Most regressions in this class of problem are not algorithmic; they come from environment drift, dependency upgrades, or subtle API behavior changes. Add one smoke test that runs in CI and one failure-mode check that verifies observability. The failure-mode check should confirm that logs and error messages are actionable, not generic. If a team member cannot quickly identify the failing component from logs, incident response will be slower than necessary.

A pragmatic rollout sequence is:

  1. Run static checks and tests in CI.
  2. Execute a smoke test with realistic data shape.
  3. Trigger one expected failure mode and verify logging.
  4. Deploy behind a feature flag or staged rollout when possible.
  5. Monitor defined metrics during a stabilization window.
bash
1# Example release hygiene
2make lint
3make test
4./scripts/smoke_check.sh

Finally, define ownership and rollback up front. Specify who responds when checks fail, what threshold triggers rollback, and which fallback mode keeps user-facing behavior acceptable. Even small utilities should have explicit limits and non-goals recorded in documentation. That prevents accidental overextension and helps future contributors decide whether to iterate on the existing approach or replace it. Revisit this checklist after framework upgrades, because behavior assumptions that were once valid can change with new runtime defaults or deprecations.

Common Pitfalls

  • Producing invalid YAML due to bad indentation in conditional blocks.
  • Accessing nested values without nil checks.
  • Embedding too much logic directly in resource templates.
  • Forgetting whitespace trimming ({{- and -}}) around conditionals.
  • Using string values ("false") where boolean values are expected.

Summary

if clauses in Helm are powerful for optional resources and environment-specific rendering. Keep conditions safe and readable, validate with helm template, and test multiple values files to ensure output remains valid YAML.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.