Abhishek J
Abhishek J

Reputation: 111

Kustomize patch doesn't seem to be following the index order for the container resource

I'm trying to patch/override (remove) a container that is present in base from the overlay. As per the examples shown in the documentation: https://kubectl.docs.kubernetes.io/references/kustomize/kustomization/patches/ the resources follow the order (0 indexed), but that seems to not be working.

base deployment. yaml looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app_name
  labels:
spec:
  template:
    metadata:
    spec:
      containers:
        - name: container1
          image: "image-1"

        - name: container2
          image: "image-2"  

        - name: container3
          image: "image-3"

from the overlay Kustomization.yaml I want to ensure I remove container2. Ideally the path should be /spec/template/spec/containers/1

patches:
  # Disable/Remove container-2 sidecar container from deployment
  - patch: |-
      - op: remove
        path: /spec/template/spec/containers/1
    target:
      kind: Deployment
      name: app_name

but it seems to working with the path /spec/template/spec/containers/2 ie

patches:
  # Disable/Remove container-2 sidecar container from deployment
  - patch: |-
      - op: remove
        path: /spec/template/spec/containers/2
    target:
      kind: Deployment
      name: app_name

Any reason why it can happen? I validated this several times. (also tried changing the position of container2 specs with container3)

Upvotes: 0

Views: 1292

Answers (1)

Abhishek J
Abhishek J

Reputation: 111

Looks like the order of patching matters. I was able to reproduce the expected behaviour when I patched the containers in kustomization.yaml inline in the order of the deployment.yaml

patches:
 - patch: |-
   # patch container 1

  # Disable/Remove container-2 sidecar(indexed 1) container from deployment
  - patch: |-
      - op: remove
        path: /spec/template/spec/containers/1
    target:
      kind: Deployment
      name: app_name

 - patch: |-
   # patch container 3

Previously here's the order I was patching => container3, container1 and then container2 . Hence, with the default merge strategy, the container was indexed at 2 or the 3rd position

Upvotes: 0

Related Questions