<p>I want to patch (overwrite) <strong>list</strong> in <strong>kubernetes</strong> manifest with <strong>Kustomize</strong>.
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.</p>
<p><strong>How can I replace only specific items in the list and the res of the items in list stay untouched?</strong></p>
<p>I found these two resources:<br />
<a href="https://github.com/kubernetes-sigs/kustomize/issues/581" rel="noreferrer">https://github.com/kubernetes-sigs/kustomize/issues/581</a><br />
<a href="https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md" rel="noreferrer">https://github.com/kubernetes/community/blob/master/contributors/devel/sig-api-machinery/strategic-merge-patch.md</a><br />
but wasn't able to make desired solution of it.</p>
<p>exmaple code:
orig-file.yaml</p>
<pre><code>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: &quot;#alerts&quot;
          sendResolved: true
          apiURL:
            name: slack-webhook-url
            key: address
</code></pre>
<p>patch.yaml:</p>
<pre><code>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
</code></pre>
<p>kustomization.yaml</p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- orig-file.yaml

patchesStrategicMerge:
- patch.yaml
</code></pre>
<p>What I get:</p>
<pre><code>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
</code></pre>
<p>What I want:</p>
<pre><code>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: &quot;#alerts&quot;
          sendResolved: true
          apiURL:
            name: slack-webhook-url
            key: address
  test: brase-yourself
</code></pre>

<p>What you can do is to use <a href="https://github.com/kubernetes-sigs/kustomize/blob/master/examples/jsonpatch.md" rel="noreferrer">jsonpatch</a> instead of <code>patchesStrategicMerge</code>, so in your case:</p>
<pre><code>cat &lt;&lt;EOF &gt;./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
</code></pre>
<p>patch:</p>
<pre><code>cat &lt;&lt;EOF &gt;./patch.yaml
- op: replace
  path: /spec/receivers/0/slackConfigs/0/username
  value: Karl
EOF
</code></pre>
