How to create a namespace if it doesn't exists from HELM templates?
Asked Answered
U

4

69

I have a kind: Namespace template YAML, as per below:

apiVersion: v1
kind: Namespace
metadata:
  name: {{ .Values.namespace }}
  namespace: ""

How do I make helm install create the above-given namespace ({{ .Values.namespace }}) if and only if above namespace ({{ .Values.namespace }}) doesn't exits in the pointed Kubernetes cluster?

Ultramicroscope answered 10/8, 2018 at 9:45 Comment(0)
T
34

For helm2 it's best to avoiding creating the namespace as part of your chart content if at all possible and letting helm manage it. helm install with the --namespace=<namespace_name> option should create a namespace for you automatically. You can reference that namespace in your chart with {{ .Release.Namespace }}. There's currently only one example of creating a namespace in the public helm/charts repo and it uses a manual flag for checking whether to create it

For helm3 functionality has changed and there's a github issue on this

Tessi answered 10/8, 2018 at 10:56 Comment(5)
In case of the helm- umbrella deployment how to handle. Because in that case there are multiple namespaces we need.Redintegration
You could do something to create a namespace only if the user says so - like in github.com/helm/charts/blob/…Tessi
The flag --namespace does not create namespace automatically at the version 3.0. But this feature is going to be returned at the version 3.1Polinski
I doesn't seems to be added back at 3.1.1Prologue
What if a chart contains multiple components which should be placed in more than one namespace? The only option is creating them "outside" of the chart?Philologian
F
120

This feature is implemented in helm >= 3.2 (Pull Request)

Use --create-namespace in addition to --namespace <namespace>

Freiman answered 23/3, 2020 at 16:45 Comment(4)
Yes but then it fails if the namespace already exists...Sundown
I think this not true (anymore?). From the doc: -create-namespace create the release namespace if not presentBert
Nope, it still fails. I have a strict definition of namespace in my deployment. When I do not use any flag, it works fine but helm is shown in the default namespace. If I pass -n - it fails because namespace does not exist, if with --create-namespace Error: create: failed to create: namespaces "test" not found :|Riplex
Something to be aware of. If you run helm delete ... this will NOT delete the namespace it created. You'll have to manually delete the namespace using kubectlWendiewendin
T
34

For helm2 it's best to avoiding creating the namespace as part of your chart content if at all possible and letting helm manage it. helm install with the --namespace=<namespace_name> option should create a namespace for you automatically. You can reference that namespace in your chart with {{ .Release.Namespace }}. There's currently only one example of creating a namespace in the public helm/charts repo and it uses a manual flag for checking whether to create it

For helm3 functionality has changed and there's a github issue on this

Tessi answered 10/8, 2018 at 10:56 Comment(5)
In case of the helm- umbrella deployment how to handle. Because in that case there are multiple namespaces we need.Redintegration
You could do something to create a namespace only if the user says so - like in github.com/helm/charts/blob/…Tessi
The flag --namespace does not create namespace automatically at the version 3.0. But this feature is going to be returned at the version 3.1Polinski
I doesn't seems to be added back at 3.1.1Prologue
What if a chart contains multiple components which should be placed in more than one namespace? The only option is creating them "outside" of the chart?Philologian
A
34

There are some differences in Helm commands due to different versions.

For Helm 2, just use --namespace; for Helm 3, need to use --namespace and --create-namespace.

Helm 2 Example:

helm install stable/nginx-ingress --name ingress-nginx --namespace ingress-nginx --wait

Helm 3 Example:

helm install ingress-nginx stable/nginx-ingress --namespace ingress-nginx --create-namespace --wait
Anachronism answered 16/1, 2021 at 15:42 Comment(2)
what happens if namespace already exist, but I used --create-namespaceCarp
@Carp nothing, it will only create the namespace if it is no created already.Dalhousie
A
4

For terraform users, set create_namespace attribute to true:

resource "helm_release" "kube_prometheus_stack" {
  name       = ...
  repository = ...
  chart      = ...
  namespace  = ...
  create_namespace = true
}
Airiness answered 22/12, 2021 at 15:16 Comment(1)
Solved my problem, thanks.Boo

© 2022 - 2024 — McMap. All rights reserved.