1

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.

Salvatore D'angelo
  • 1,019
  • 3
  • 14
  • 39
  • Have you tried to move `namespace.yaml` to a separate directory and just refer to that namespace in the `kustomization.yaml` ? – Jonas Aug 05 '21 at 16:21
  • I don't think this change anything. When I run ```kubectl delete -k kustomize/app1``` it analyzes the kustomization.yaml and if it refers to namespace.yml it simply calls its removal that automatically remove app2. I think that if Kustomize doesn't support this feature there is anything I can do. The same problem exists on Kubernetes (without Kustomize). I hoped Kustomized addressed this issue but I think no progress has been done from this perspective. – Salvatore D'angelo Aug 05 '21 at 16:42
  • I did not mean that you should refer to `namespace.yaml` but to the namespace it creates, e.g. with `namespace: my-namespace` See some examples on https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/ – Jonas Aug 05 '21 at 16:44
  • This is what I am doing right now and I describe it in the message. Now my kustomization.yaml only refer to the namespace ```namespace: mynamespace``` then outside the two folders above ```kustomize/app1``` and ```kustomize/app2``` I have a namespace ```namespace.yml``` and I run ```kubectl apply -f namespace.yml``` before the deployments and ```kubectl delete -f namespace.yml``` at the end. But I don't like this extra commands. – Salvatore D'angelo Aug 05 '21 at 16:49

1 Answers1

2

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388