kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: {{ include "openstack-manila-csi.controllerplugin.fullname" . }}
  labels:
    app: {{ include "openstack-manila-csi.name" . }}
    chart: {{ include "openstack-manila-csi.chart" . }}
    component: {{ .Values.controllerplugin.name }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  serviceName: {{ include "openstack-manila-csi.controllerplugin.fullname" . }}
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "openstack-manila-csi.name" . }}
      component: {{ .Values.controllerplugin.name }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ include "openstack-manila-csi.name" . }}
        chart: {{ include "openstack-manila-csi.chart" . }}
        component: {{ .Values.controllerplugin.name }}
        release: {{ .Release.Name }}
        heritage: {{ .Release.Service }}
    spec:
      serviceAccountName: {{ include "openstack-manila-csi.serviceAccountName.controllerplugin" . }}
      containers:
        {{- range .Values.shareProtocols }}
        - name: {{ .protocolSelector | lower }}-provisioner
          image: "{{ $.Values.controllerplugin.provisioner.image.repository }}:{{ $.Values.controllerplugin.provisioner.image.tag }}"
          args:
            - "--v=5"
            - "--csi-address=$(ADDRESS)"
            {{- if $.Values.csimanila.topologyAwarenessEnabled }}
            - "--feature-gates=Topology=true"
            {{- end }}
          env:
            - name: ADDRESS
              value: "unix:///var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}/csi-controllerplugin.sock"
          imagePullPolicy: {{ $.Values.controllerplugin.provisioner.image.pullPolicy }}
          volumeMounts:
            - name: {{ .protocolSelector | lower }}-plugin-dir
              mountPath: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
          resources:
{{ toYaml $.Values.controllerplugin.provisioner.resources | indent 12 }}
        - name: {{ .protocolSelector | lower }}-snapshotter
          image: "{{ $.Values.controllerplugin.snapshotter.image.repository }}:{{ $.Values.controllerplugin.snapshotter.image.tag }}"
          args:
            - "--v=5"
            - "--csi-address=$(ADDRESS)"
          env:
            - name: ADDRESS
              value: "unix:///var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}/csi-controllerplugin.sock"
          imagePullPolicy: {{ $.Values.controllerplugin.snapshotter.image.pullPolicy }}
          volumeMounts:
            - name: {{ .protocolSelector | lower }}-plugin-dir
              mountPath: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
          resources:
{{ toYaml $.Values.controllerplugin.snapshotter.resources | indent 12 }}
        - name: {{ .protocolSelector | lower }}-nodeplugin
          securityContext:
            privileged: true
            capabilities:
              add: ["SYS_ADMIN"]
            allowPrivilegeEscalation: true
          image: "{{ $.Values.csimanila.image.repository }}:{{ $.Values.csimanila.image.tag | default $.Chart.AppVersion }}"
          command: ["/bin/sh", "-c",
            '/bin/manila-csi-plugin
            --v=5
            --nodeid=$(NODE_ID)
            {{- if $.Values.csimanila.topologyAwarenessEnabled }}
            --with-topology
            --nodeaz={{ $.Values.csimanila.nodeAZ }}
            {{- end }}
            {{- if $.Values.csimanila.runtimeConfig.enabled }}
            --runtime-config-file=/runtimeconfig/runtimeconfig.json
            {{- end }}
            --endpoint=$(CSI_ENDPOINT)
            --drivername=$(DRIVER_NAME)
            --share-protocol-selector=$(MANILA_SHARE_PROTO)
            --fwdendpoint=$(FWD_CSI_ENDPOINT)
            {{- if .compatibilitySettings }}
            --compatibility-settings={{ .compatibilitySettings }}
            {{- end }}'
          ]
          env:
            - name: DRIVER_NAME
              value: {{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            - name: NODE_ID
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: CSI_ENDPOINT
              value: "unix:///var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}/csi-controllerplugin.sock"
            - name: FWD_CSI_ENDPOINT
              value: "unix://{{ .fwdNodePluginEndpoint.dir }}/{{ .fwdNodePluginEndpoint.sockFile }}"
            - name: MANILA_SHARE_PROTO
              value: "{{ .protocolSelector }}"
          imagePullPolicy: {{ $.Values.csimanila.image.pullPolicy }}
          volumeMounts:
            - name: {{ .protocolSelector | lower }}-plugin-dir
              mountPath: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            - name: {{ .protocolSelector | lower }}-fwd-plugin-dir
              mountPath: {{ .fwdNodePluginEndpoint.dir }}
            - name: pod-mounts
              mountPath: /var/lib/kubelet/pods
              mountPropagation: Bidirectional
            {{- if $.Values.csimanila.runtimeConfig.enabled }}
            - name: {{ .protocolSelector | lower }}-runtime-config-dir
              mountPath: /runtimeconfig
              readOnly: true
            {{- end }}
          resources:
{{ toYaml $.Values.controllerplugin.nodeplugin.resources | indent 12 }}
        {{- end }}
      volumes:
        {{- range .Values.shareProtocols }}
        - name: {{ .protocolSelector | lower }}-plugin-dir
          hostPath:
            path: /var/lib/kubelet/plugins/{{ printf "%s.%s" .protocolSelector $.Values.driverName | lower }}
            type: DirectoryOrCreate
        - name: {{ .protocolSelector | lower }}-fwd-plugin-dir
          hostPath:
            path: {{ .fwdNodePluginEndpoint.dir }}
            type: Directory
        {{- if $.Values.csimanila.runtimeConfig.enabled }}
        - name: {{ .protocolSelector | lower }}-runtime-config-dir
          configMap:
            name: manila-csi-runtimeconf-cm
        {{- end }}
        {{- end }}
        - name: pod-mounts
          hostPath:
            path: /var/lib/kubelet/pods
            type: Directory
    {{- if .Values.controllerplugin.affinity }}
      affinity: {{ toYaml .Values.controllerplugin.affinity | nindent 8 }}
    {{- end }}
    {{- if .Values.controllerplugin.nodeSelector }}
      nodeSelector: {{ toYaml .Values.controllerplugin.nodeSelector | nindent 8 }}
    {{- end }}
    {{- if .Values.controllerplugin.tolerations }}
      tolerations: {{ toYaml .Values.controllerplugin.tolerations | nindent 8 }}
    {{- end }}
