kuberneteskustomize

How to call a Kustomize patch after namespace transformation?


How to make kustomize run a patch after namespace transformation was done?

Example, to reproduce:

extensionconfig.yaml

apiVersion: runtime.cluster.x-k8s.io/v1alpha1
kind: ExtensionConfig
metadata:
  name: my-extensionconfig

kustomization.yaml

resources:
- extensionconfig.yaml

namespace: foooo

output:

❯ kubectl kustomize  .
apiVersion: runtime.cluster.x-k8s.io/v1alpha1
kind: ExtensionConfig
metadata:
  name: my-extensionconfig
  namespace: foooo

Now namespace "foooo" is set. Now I want to update this resource with the same kustomization.yaml file, and do an additional patch.

Related question: How to exclude a resource when using namespace in kustomization.yaml?


Solution

  • My answer is based on the solution of user larsks https://stackoverflow.com/a/79269650/633961

    transformers are called after namespace transformers.

    input: extensionconfig.yaml

    apiVersion: runtime.cluster.x-k8s.io/v1alpha1
    kind: ExtensionConfig
    metadata:
      name: my-extensionconfig
    

    kustomization.yaml:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    namespace: foo
    resources:
    - extensionconfig.yaml
    
    transformers:
    - patch.yaml
    

    patch.yaml

    apiVersion: builtin
    jsonOp: |-
      - op: remove
        path: /metadata/namespace
    kind: PatchJson6902Transformer
    metadata:
      name: patch6902
    target:
      name: my-extensionconfig
    

    Result:

    ❯ kustomize build .          
    
    apiVersion: runtime.cluster.x-k8s.io/v1alpha1
    kind: ExtensionConfig
    metadata:
      name: my-extensionconfig
    

    --> no "namespace". Problem solved, I am happy.