kuberneteskustomize

How to override a namespace override


In the following scenario I have my containers defined in ../base/.

In this /dev/ directory I want to start all the deployments and statefulsets in namespace dev.

The rub is that I also want to run the local-path-storage CSI in the local-path-storage namespace. kustomize will override it and create it in the "dev" namespace.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: dev
bases:
  - ../base    
resources:
  - local-path-storage.yaml

How can I undo the namespace override for just local-path-storage.yaml?


Solution

  • It is possible since kustomize 4.5.6 by adding a namespaceTransformer. You want to set the field unsetOnly to true.

    Here is an example:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    bases:
     - ../base    
    resources:
     - local-path-storage.yaml
    transformers:
      - |-
        apiVersion: builtin
        kind: NamespaceTransformer
        metadata:
          name: notImportantHere
          namespace: dev
        unsetOnly: true
    

    This should set the namespace to dev for all resources that DO NOT have a namespace set.

    Link to namespaceTransformer spec: https://kubectl.docs.kubernetes.io/references/kustomize/builtins/#_namespacetransformer_