kubernetesnamespaceskustomize

kustomization namespace overwrite doesn't work for me


My k8s YAML manifests are organized like this:

.
├── app
│   ├── base
│   │   └── kustomization.yaml
|   |   └── my-configmap.yaml
|   |   └── my-namespace.yaml
|   |   └── my-deploy.yaml  
|   |   └── my-svc.yaml  
│   ├── overlay
│       └── kustomization.yaml
        └── new-namespace.yaml

=== The "base" ===

The app/base/kustomization.yaml is:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - my-configmap.yaml
  - my-deploy.yaml
  - my-namespace.yaml
  - my-svc.yaml

In each YAML manifest inside base/ directory, the namespace field is specified. For example my-service.yaml specified namespace my-ns:

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: my-svc
  name: my-svc
  namespace: my-ns
spec:

=== The "Overlay" ===

I would like to overwrite namespace of my objects using overlay.

The app/overlay/kustomization.yaml is:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../base
  - new-namespace.yaml
buildMetadata:
  - managedByLabel
transformers:
  - |-
    apiVersion: builtin
    kind: NamespaceTransformer
    metadata:
      name: namespace-transformer
    namespace: xii
    setRoleBindingSubjects: allServiceAccounts

That app/overlay/new-namespace.yaml is:

---
apiVersion: v1
kind: Namespace
metadata:
  name: xii

==== Result ====

Then, under path app/overlay/, I run command kustomize build . | cat, the output shows me all the namespace of my YAML manifests are still use the original one defined in "base" instead of being overwritten by overlay value xii.

Why? Do I misunderstand the behaviour of Kustomization namespace transformer?

I also tried a simpler approach:

I also tried removing the transformers completely, instead, simply use namespace: xii in overlay's kustomizationl.yaml like below:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
  - ../base
  - new-namespace.yaml
buildMetadata:
  - managedByLabel
namespace: xii

When build it, I get error:

Error: namespace transformation produces ID conflict: [{"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{"config.kubernetes.io/origin":"path: ../base/my-namespace.yaml\n","internal.config.kubernetes.io/previousKinds":"Namespace","internal.config.kubernetes.io/previousNames":"my-ns","internal.config.kubernetes.io/previousNamespaces":"_non_namespaceable_"},"name":"xii"}} {"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{"config.kubernetes.io/origin":"path: new-namespace.yaml\n"},"name":"xii"}}]

Solution

  • As per this git repository

    The interaction between the namespace override in the kustomization is the reason why you are receiving the error.The namespace resources themselves as well as yaml’s

    Everything functions as intended if you modify the components to standard Kustomizations and include them as resources in overlays/local/kustomization.yaml:

    For future reference :

    Refer to this gitlink and See namespaceTransformer spec: