Salvatore D'angelo
Salvatore D'angelo

Reputation: 1149

How to avoid not empty namespace deletion with Kubernetes Kustomize

I have a Kubernetes project managed by Kustomized (Kubernetes). This project deploys two deployments in the same namespace.

Basically, I have the following directory structure:

kustomize -> app1 -> kustomization.yaml
kustomize -> app1 -> namespace.yaml
kustomize -> app1 -> app1.yaml

kustomize -> app2 -> kustomization.yaml
kustomize -> app2 -> namespace.yaml
kustomize -> app2 -> app2.yaml

The files namespace.yaml create in both the case the same namespace so that the first application deployed, create the namespace and the second reuse it. Obviously, the problem is when I try to remove only one of these applications:

kubectl delete -k kustomize/app1

remove both the applications because the namespace is removed and app2 too. An easy solution to this problem is to move namespace.yaml outside the folders and just call it standalone. However, this approach requires user must remember to run:

kubectl apply -f namespace.yaml

before of:

kubectl apply -k kustomize/app1
kubectl apply -k kustomize/app2

I know another possible solution is via script. My question is that exists a way to better manage namespace removal with Kustomize so that it is removed only if it is empty.

Upvotes: 2

Views: 1285

Answers (1)

Jonas
Jonas

Reputation: 128797

You can have this directory structure:

kustomize -> ns -> namespace.yaml

kustomize -> app1 -> kustomization.yaml
kustomize -> app1 -> app1.yaml

kustomize -> app2 -> kustomization.yaml
kustomize -> app2 -> app2.yaml

Also you can add a kustomization.yaml at the root, so that you only need this to apply all:

kubectl -k kustomize/

That will create the namespace and both apps.

And you can still delete only one app if you want:

kubectl delete -k kustomize/app1

And since you don't have an namespace.yaml in that directory, it does not delete the namespace.

Upvotes: 4

Related Questions