Artur Ferfecki
Artur Ferfecki

Reputation: 195

Patching list in kubernetes manifest with Kustomize

I want to patch (overwrite) list in kubernetes manifest with Kustomize. I am using patchesStrategicMerge method. When I patch the parameters which are not in list the patching works as expected - only addressed parameters in patch.yaml are replaced, rest is untouched. When I patch list the whole list is replaced.

How can I replace only specific items in the list and the res of the items in list stay untouched?

I found these two resources:
https://github.com/kubernetes-sigs/kustomize/issues/581
https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md
but wasn't able to make desired solution of it.

exmaple code: orig-file.yaml

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: alertmanager-slack-config
  namespace: system-namespace
spec:
  test: test
  other: other-stuff
  receivers:
    - name: default
      slackConfigs:
        - name: slack
          username: test-user
          channel: "#alerts"
          sendResolved: true
          apiURL:
            name: slack-webhook-url
            key: address

patch.yaml:

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: alertmanager-slack-config
  namespace: system-namespace
spec:
  test: brase-yourself
  receivers:
    - name: default
      slackConfigs:
        - name: slack
          username: Karl

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- orig-file.yaml

patchesStrategicMerge:
- patch.yaml

What I get:

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: alertmanager-slack-config
  namespace: system-namespace
spec:
  other: other-stuff
  receivers:
  - name: default
    slackConfigs:
    - name: slack
      username: Karl
  test: brase-yourself

What I want:

apiVersion: monitoring.coreos.com/v1alpha1
kind: AlertmanagerConfig
metadata:
  name: alertmanager-slack-config
  namespace: system-namespace
spec:
  other: other-stuff
  receivers:
    - name: default
      slackConfigs:
        - name: slack
          username: Karl
          channel: "#alerts"
          sendResolved: true
          apiURL:
            name: slack-webhook-url
            key: address
  test: brase-yourself

Upvotes: 10

Views: 27998

Answers (1)

mehowthe
mehowthe

Reputation: 841

What you can do is to use jsonpatch instead of patchesStrategicMerge, so in your case:

cat <<EOF >./kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- orig-file.yaml

patches:
  - path: patch.yaml
    target:
      group: monitoring.coreos.com
      version: v1alpha1
      kind: AlertmanagerConfig
      name: alertmanager-slack-config

EOF

patch:

cat <<EOF >./patch.yaml
- op: replace
  path: /spec/receivers/0/slackConfigs/0/username
  value: Karl
EOF

Upvotes: 16

Related Questions