8

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
Jonas
  • 121,568
  • 97
  • 310
  • 388
Artur Ferfecki
  • 148
  • 1
  • 2
  • 15

1 Answers1

12

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
mehowthe
  • 761
  • 6
  • 14
  • Thanks for the answer! Do you know about any method/workaround how to achieve the same result by using patchesStrategicMerge? – Artur Ferfecki Jun 10 '21 at 13:05
  • This should work if the CRD defines correct merge keys for all list types. – whiskeysierra Aug 17 '21 at 16:49
  • 1
    @whiskeysierra Is there a way to override merging behavior when CRD doesn't define merge keys correctly? – Slava Fomin II Aug 20 '21 at 13:12
  • 1
    @ArturFerfecki they don't intend to support appending to arbitrary lists: https://github.com/kubernetes-sigs/kustomize/issues/3265#issuecomment-733335803 I think we only have jsonpatch... – reegnz May 06 '22 at 09:24