<p>I see <code>patchesStrategicMerge</code>in my kustomization.yaml file, but not getting it clearly, like whats its need or why we require that?</p>
<p>kustomization.yaml</p>
<pre class="lang-yaml prettyprint-override"><code>resources:
- a.yaml

patchesStrategicMerge:
- b.yaml
- c.yaml
</code></pre>
<p>I went through this: <a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing" rel="noreferrer">https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/#customizing</a> and
<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></p>

Hari Kishore
<p>This comes in handy you <em>inherit</em> from some base and want to apply partial changes to said base. That way, you can have one source YAML file and perform different customizations based on it, without having to recreate the entire resource. That is one key selling point of <a href="https://kubernetes.io/docs/tasks/manage-kubernetes-objects/kustomization/" rel="noreferrer">kustomize</a>.</p>
<p>The purpose of the <code>Strategic Merge Patch</code> is to be able to patch <em>rich objects</em> partially, instead of replacing them entirely.</p>
<p>Imagine you have a list, of object.</p>
<pre class="lang-yaml prettyprint-override"><code>mylist:
 - name: foo
 - name: bar
 - name: baz
</code></pre>
<p>How could you change one of the items in the list? With a standard merge patch, you could only replace the entire list. But with a strategic merge patch, you can target one element in the list based on some property, in this case only the name makes sense as it's the only property.</p>
<pre><code>mylist:
 - $patch: delete
   name: foo
</code></pre>
<p>In the above example, I used the strategic merge patch to remove the item in the list with the name foo.</p>
<p>Here is another example, suppose I have the following project structure.</p>
<pre><code>sample
├── base
│   ├── kustomization.yaml
│   └── pod.yaml
└── layers
    └── dev
        ├── kustomization.yaml
        └── patch.yaml
</code></pre>
<p>In the base, is my full pod definition. While in the layers, I can create multiple layers for different environments, in this case I have only one for dev.</p>
<p>The kustomization.yaml in the base folder looks like this.</p>
<pre><code>resources:
 - pod.yaml
</code></pre>
<p>If I execute the base itself with dry run I get this.</p>
<pre class="lang-bash prettyprint-override"><code>kubectl apply -k sample/base --dry-run=client -o yaml
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp
  namespace: default
spec:
  containers:
  - image: nginx
    name: nginx
  - command:
    - sleep
    - infinity
  - image: busybox
    name: sidecar
</code></pre>
<p>The kustomization.yaml in the dev folder looks like this.</p>
<pre class="lang-yaml prettyprint-override"><code>bases:
  - ../../base
patchesStrategicMerge:
  - patch.yaml
</code></pre>
<p>And the patch looks like this. I want to enable debug logging for the sidecar. Thefore I am using a merge directive to change its arguments without changing the image. I also want to keep the nginx container.</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
    - $patch: merge
      name: sidecar
      args: [log-level, debug]
</code></pre>
<pre class="lang-bash prettyprint-override"><code>kubectl apply -k sample/layers/dev/ --dry-run=client -o yaml
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: myapp
  namespace: default
spec:
  containers:
  - args:
    - log-level
    - debug
    command:
    - sleep
    - infinity
    image: busybox
    name: sidecar
  - image: nginx
    name: nginx
</code></pre>
<p><em>Note: The command and args don't make much sense, It's just to illustrate the strategic merge patch.</em></p>
