How to create a namespace if it doesn't exists from HELM templates?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When deploying applications on Kubernetes using Helm charts, a common requirement is to ensure that a specific namespace exists before creating resources within it. For those who are new to Helm or need guidance on namespaces, this article provides a detailed explanation of creating a namespace within a Helm template if it does not already exist.
Technical Overview
Kubernetes is a powerful container orchestration system, and namespaces are a way to organize and manage resources within a Kubernetes cluster. Helm is a package manager for Kubernetes, which simplifies the deployment of applications. When deploying applications, ensuring that the target namespace exists beforehand is crucial for successful deployment.
Using Helm Templates to Handle Namespaces
Helm allows for the templating of Kubernetes resources. This flexibility can be used to create namespaces dynamically if they don't exist. Below is a step-by-step guide on how to achieve this.
Step 1: Create a Custom Helper Template
Helm uses Go templates, and you can leverage this powerful templating engine to manage namespaces. A common practice is to create a _helpers.tpl file to manage template blocks that can be reused across different templates in the chart.
Contents of _helpers.tpl:
Step 2: Use the Helper Template in Kubernetes Manifests
To use the helper template, reference it in the templates directory. Here is an example using a deployment manifest:
Contents of deployment.yaml:
Step 3: Update values.yaml
values.yaml is where Helm stores default values for templates. Ensure it includes a namespace key:
Step 4: Adding a Conditional Namespace Create Block (Optional)
To ensure the namespace isn't created if it already exists, dependency on Kubernetes' idempotent API behavior or pre-install hooks can be used instead of revisioning and logic in Helm templates. Helm does not natively support conditionally existing resources without the engine expecting a deterministic output, which makes pre-install hooks the preferable choice here when in the chart lifecycle:
Contents of namespace.yaml:
Key Points to Remember
Here is a summary table of the key points:
| Key Points | Details |
| Namespace | Logical partition within a Kubernetes cluster for separating resources. |
| Helm Chart | A collection of files describing a set of Kubernetes resources. |
| _helpers.tpl | Template file for defining reusable blocks in Helm charts. |
| Values.yaml | The default configuration for the Helm chart, allows overriding of values. |
| Idempotency | Kubernetes API supports it, allowing safe application of the same manifest multiple times. |
| Pre-install hooks | Mechanism in Helm to execute operations before the main install step. |
Conclusion
Handling namespaces efficiently ensures that your applications can be deployed without issues related to resource separation in Kubernetes. By leveraging Helm templates and Kubernetes hooks, you can create namespaces dynamically within your Helm charts, ensuring the environment is always set up correctly for your deployments.

