Is there a way to make kustomize merge headings in a yaml file instead of completely replacing that heading with the overlay yaml

3/27/2019

i have a base yaml and an overlay yaml and using "kustomize" i want to merge these two yaml. what happens with me is that on running kustomize build there comes an output but it is not expected why? because the kustomize instead of filling the custom information from the overlay yaml replaces the whole heading tag of the base with the overlay "containers" in my case. The intended behaviour that i need is the it should somehow fill in the missing information using the overlay yaml instead of replacement.

base yaml:

apiVersion: v1
kind: Pod
metadata:
  name: temp
  labels:
    tier: temp
spec:
  containers:
  - name: temp
    image:  temp
    imagePullPolicy: Always
    command: temp
    args:
      temp
    envFrom:
    - configMapRef:
        name: temp
    volumeMounts:
  volumes:

overlay yaml:

apiVersion: v1
kind: Pod
metadata:
  name: temp
  labels:
    tier: temp
spec:
  containers:
    volumeMounts:
    - name: temppathname
      mountPath: /temppath
  volumes:
  - name: temppathname
    hostPath:
      type: temp
      path: temppath

Expected result after kustomize build:

apiVersion: v1
kind: Pod
metadata:
  name: temp
  labels:
    tier: temp
spec:
  containers:
  - name: temp
    image:  temp
    imagePullPolicy: Always
    command: temp
    args:
      ["sleep 9000"]
    envFrom:
    - configMapRef:
        name: temp
    volumeMounts:
    - name: temppathname
      mountPath: /temppath
  volumes:
  - name: temppathname
    hostPath:
      type: temp
      path: temppath

what i'm getting:

apiVersion: v1
kind: Pod
metadata:
  labels:
    tier: temp
  name: temp
spec:
  containers:
    volumeMounts:
    - name: temppathname
      mountPath: /temppath
  volumes:
  - name: temppathname
    hostPath:
      type: temp
      path: temppath
-- UmairAhmad
kubernetes
kustomize
merge
yaml

2 Answers

6/5/2019

I find that the easiest way to deal with this is to use JSONPatch. I would remove the empty fields of the base as in:

apiVersion: v1
kind: Pod
metadata:
  name: temp
  labels:
    tier: temp
spec:
  containers:
  - name: temp
    image:  temp
    imagePullPolicy: Always
    command: temp
    args:
      temp
    envFrom:
    - configMapRef:
        name: temp

Then in your overlay create a new patch, for example named create_volume.yml:

- op: add
  path: /spec/volumes/-
  value:
    name: temppathname
    hostPath:
     type: temp
     path: temppath

- op: add
  path: /spec/containers/0/volumeMounts/-
  value:
    name: temppathname
    mountPath: /temppath

And finally in the overlay kustomization.yml add:

patchesJson6902:
- target:
    version: v1
    kind: Pod
    name: temp
  path: create_volume.yml

If it doesn't work you might have to play with the API group in the patch target. I only patched deployments until now and my target would be:

- target:
    group: apps
    version: v1
    kind: Deployment
    name: temp
  path: create_volume.yml
-- ITChap
Source: StackOverflow

3/27/2019

In your base.yaml the value for the key containers is a sequence (node). In your overlay.yaml the value for the key containers is a mapping. Of course those two cannot be merged.

Not knowing kustomize at all, it seems logical that because those cannot be merged, the overlay replaces that whole sequence node with the mapping node. Your expectation that the mapping of the overlay is merged with a mapping that happens to be an item (in this case the only item) in the sequence of the base seems completely arbitrary. Which item would need to be taken if there had been multiple items? The first? The last? The last one before item five that is a mapping?

If your overlay.yaml looked like:

apiVersion: v1
kind: Pod
metadata:
  name: temp
  labels:
    tier: temp
spec:
  containers:
  - volumeMounts:     # < created a sequence item here by inserting an item indicator
    - name: temppathname
      mountPath: /temppath
  volumes:
  - name: temppathname
    hostPath:
      type: temp
      path: temppath

then I could understand your expectation (and maybe the above change can be applied to make it work, I don't have a way to test).

-- Anthon
Source: StackOverflow