Kubernetes Kustomize patching - Can't patch a file located in base

2/7/2022

I have a huge patch file that I want to apply to specific overlays. I usually patch files under overlays as it is supposed to be. But the file is same and I do not want to copy it to each overlay. If I could keep my patch file app-new-manifest.yaml under base and patch it under overlay with a single line in kustomization.yaml, it would be awesome.

├── base
│   ├── app-new-manifest.yaml # I am trying to patch this
│   ├── kustomization.yaml
│   ├── app
│   │   ├── app.yaml
│   │   └── kustomization.yaml
└── overlay
    └── environment1
    │    ├── kustomization.yaml # I want to patch app-new-manifest.yaml in base
    └── environment2
    │    ├── kustomization.yaml # No patch. app.yaml will be as is
    └── environment3
        ├── kustomization.yaml # I want to patch app-new-manifest.yaml in base

When I'm trying to do so, I get this error:

'/base/app/app-new-manifest.yaml' is not in or below '/overlays/environment1'

Which means, when you patch, the patch file has to be located under overlay not base. Is there any workaround to do this? Because copying the same file to each environment does not make sense to me.

Any ideas around this will highly be appreciated, thanks!

Edit:

Add /base/app/kustomization.yaml

resources:
  - app.yaml

Add /overlays/environment1/kustomization.yaml

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

resources:
  - ../../base/app

patchesStrategicMerge:
  - ../../base/app/app-new-manifest.yaml # Patch new manifest

kustomize version:

{Version:kustomize/v4.2.0 GitCommit:d53a2ad45d04b0264bcee9e19879437d851cb778 BuildDate:2021-07-01T00:44:28+01:00 GoOs:darwin GoArch:amd64}
-- titanic
kubernetes
kustomize

1 Answer

2/8/2022

You can't include a file that is outside of you current directory, but you can include another directory that has a kustomize.yaml file. So organize your layout like this:

.
├── base
└── overlay
    ├── patched_based
    ├── environment1
    ├── environment2
    └── environment3

In overlay/patched_base, place your patch file and a kustomization file like:

resources:
  - ../base

patchesStrategicMerge:
  - app-new-manifest.yaml

In overlay/environment1 and overlay/environment3, you have:

resources:
   - ../patched_base

Whereas in overlay/environment2, you have:

resources:
  - ../../base

I think this solves all your requirements:

  • You only need a single instance of the patch
  • You can choose to use the patch or not from each individual overlay
-- larsks
Source: StackOverflow