kuberneteskustomize

Is there a way to use a central patch for multiple kustomization.yaml files?


Say I have 2 kustomization.yaml files, where each one defines a resource of the same kind/namespace/name. But I want to have a single patch that applies to each.

Directory structure:

- patches
- directory1
  - kustomization.yaml
  - service-a.yaml
- directory2
  - kustomization.yaml
  - service-a.yaml

directory1/kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: numaflow-system

resources:
  - service-a.yaml # defines Service "Service-A" in namespace "Namespace-A"

directory2/kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: numaflow-system

resources:
  - service-a.yaml # defines Service "Service-A" in namespace "Namespace-A"

In my patches directory I want to have a patch for "Service-A" that applies to both. We can also imagine that there are many more patches in that same directory for other resources that are each defined in directory1 and directory2.

Is there any way to reference that patches directory from each kustomization.yaml file? Looking to do this in a way that doesn't require having to separately list all individual patch files in each kustomization.yaml file.

We attempted to update a kustomization.yaml to include:

patchesStrategicMerge:
  - ../patches

But perhaps a directory can't be referenced there?


Solution

  • tl;dr: You can do what you want using kustomize components, but I am worried that this solution is plastering over a problem caused by the organization of your repository.

    Given a layout like this:

    .
    ├── directory1
    │   ├── kustomization.yaml
    │   └── service-a.yaml
    ├── directory2
    │   ├── kustomization.yaml
    │   └── service-a.yaml
    └── patches
        └── mypatch
            └── kustomization.yaml
    

    Where patches/mypatch/kustomization.yaml looks like (note the apiVersion and kind):

    apiVersion: kustomize.config.k8s.io/v1alpha1
    kind: Component
    
    patches:
    - patch: |-
        apiVersion: v1
        kind: Service
        metadata:
          name: postgresql
          labels:
            patched_label: is very exciting
    

    We can write then write directory1/kustomization.yaml like this:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    namespace: mynamespace
    
    resources:
    - service-a.yaml
    
    components:
    - ../patches/mypatch
    

    And similarly for directory2.

    So assuming that directory1/service-a.yaml looks like this:

    apiVersion: v1
    kind: Service
    metadata:
      name: postgresql
    spec:
      ports:
      - name: postgresql
        port: 5432
      selector:
        name: postgresql
    

    Running kustomize build directory1 will produce as output:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        patched_label: is very exciting
      name: postgresql
      namespace: mynamespace
    spec:
      ports:
      - name: postgresql
        port: 5432
      selector:
        name: postgresql
    

    How does that look?