Search code examples
kuberneteskubernetes-pvckustomize

How to replace the in-memory storage with persistent storage using kustomize


I'm trying to replace the in-memory storage of Grafana deployment with persistent storage using kustomize. What I'm trying to do is that I'm removing the in-memory storage and then mapping persistent storage. But When I'm deploying it then it is giving me an error.

Error

The Deployment "grafana" is invalid: spec.template.spec.containers[0].volumeMounts[1].name: Not found: "grafana-storage"

Kustomize version

{Version:kustomize/v4.0.5 GitCommit:9e8e7a7fe99ec9fbf801463e8607928322fc5245 BuildDate:2021-03-08T20:53:03Z GoOs:linux GoArch:amd64}

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

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

grafana-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-storage
  namespace: monitoring
  labels:
    billingType: "hourly"
    region: sng01
    zone: sng01
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: ibmc-file-bronze

grafana-patch.yaml

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  <---- 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

please help.


Solution

  • The $patch: delete doesn't seem to work as I would expect.

    It may be nice to open an issue on kustomize github: https://github.com/kubernetes-sigs/kustomize/issues and ask developers about it.


    Although here is the patch I tried, and it seems to work:

    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
    

    Based on https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/add-new-patchStrategy-to-clear-fields-not-present-in-patch.md

    The following should also work in theory:

    spec:
      volumes:
        - $retainKeys:
          - name
          - persistentVolumeClaim
          name: grafana-storage
          persistentVolumeClaim:
            claimName: grafana-storage
    

    But in practise it doesn't, and I think that's because kustomize has its own implementaions of strategic merge (different that k8s).