I have recently been looking to template a Kubernetes operator definition file, to avoid repeating a chunk of configuration that can’t be neatly handled by the operator itself. For various reasons, Kustomize is the tool that’s available for me to do this - if you don’t have these constraints, there might be better tools for the job!
If you do have these constraints, however, here are some tips that might be of use to you:
- Kustomize can’t use a
patchesStrategicMergeon custom resources. See here and here for more details on why.
- Instead, in order to successfully do any patching, you will have to use
the awkwardly named
patchesJson6902type of patch.
patchesStrategicMerge, the JSON patching is somewhat naive - you will need to provide the exact path in the resource definition that you want to make a change to - see here for an example.
- I found reading the JSON patching RFC to be quite useful in understanding exactly what was and wasn’t possible.
- You can’t use wildcards in a JSON patch - so if you want to replace the same sub-element in each object in an array of objects, you will have to specify each point in the array that you want the replacement to occur at individually.
- There are some operations described in the RFC that are not described in the
Kustomize docs. From my experience so far, it is still possible to use them.
I would hope - judging by the name - that
patchesJson6902would faithfully implement RFC 6902, but I haven’t looked at the code to confirm this.