<p>I'm trying to replace the in-memory storage of <code>Grafana</code> deployment with <code>persistent storage</code> using <code>kustomize</code>. What I'm trying to do is that I'm removing the <code>in-memory storage</code> and then mapping <code>persistent storage</code>. But When I'm deploying it then it is giving me an error.</p>
<p><strong>Error</strong></p>
<p><strong>The Deployment &quot;grafana&quot; is invalid: spec.template.spec.containers[0].volumeMounts[1].name: Not found: &quot;grafana-storage&quot;</strong></p>
<p><strong>Kustomize version</strong></p>
<p><code>{Version:kustomize/v4.0.5 GitCommit:9e8e7a7fe99ec9fbf801463e8607928322fc5245 BuildDate:2021-03-08T20:53:03Z GoOs:linux GoArch:amd64}</code></p>
<p><strong>kustomization.yaml</strong></p>
<pre><code>apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- https://github.com/prometheus-operator/kube-prometheus
- grafana-pvc.yaml
patchesStrategicMerge:
- grafana-patch.yaml

</code></pre>
<p><strong>grafana-pvc.yaml</strong></p>
<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-storage
  namespace: monitoring
  labels:
    billingType: &quot;hourly&quot;
    region: sng01
    zone: sng01
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: ibmc-file-bronze
</code></pre>
<p><strong>grafana-patch.yaml</strong></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitoring
spec:
  template:
    spec:
      volumes:
        # use persistent storage for storing users instead of in-memory storage
        - $patch: delete  &lt;---- trying to remove the previous volume
          name: grafana-storage
        - name: grafana-storage
          persistentVolumeClaim:
            claimName: grafana-storage
      containers:
        - name: grafana
          volumeMounts:
            - name: grafana-storage
              mountPath: /var/lib/grafana

</code></pre>
<p>please help.</p>

<p>The <code>$patch: delete</code> doesn't seem to work as I would expect.</p>
<p>It may be nice to open an issue on kustomize github: <a href="https://github.com/kubernetes-sigs/kustomize/issues" rel="nofollow noreferrer">https://github.com/kubernetes-sigs/kustomize/issues</a> and ask developers about it.</p>
<hr />
<p>Although here is the patch I tried, and it seems to work:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitoring
spec:
  template:
    spec:
      volumes:
        - name: grafana-storage
          emptyDir: null
          persistentVolumeClaim:
            claimName: grafana-storage
      containers:
        - name: grafana
          volumeMounts:
            - name: grafana-storage
              mountPath: /var/lib/grafana
</code></pre>
<hr />
<p>Based on <a href="https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/add-new-patchStrategy-to-clear-fields-not-present-in-patch.md" rel="nofollow noreferrer">https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/add-new-patchStrategy-to-clear-fields-not-present-in-patch.md</a></p>
<p>The following should also work in theory:</p>
<pre><code>spec:
  volumes:
    - $retainKeys:
      - name
      - persistentVolumeClaim
      name: grafana-storage
      persistentVolumeClaim:
        claimName: grafana-storage
</code></pre>
<p>But in practise it doesn't, and I think that's because kustomize has its own implementaions of strategic merge (different that k8s).</p>
