apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "rawfile-localpv.fullname" . }}-controller
spec:
  replicas: 1
  serviceName: {{ include "rawfile-localpv.fullname" . }}
  selector:
    matchLabels: &selectorLabels
      {{- include "rawfile-localpv.selectorLabels" . | nindent 6 }}
      component: controller
  template:
    metadata:
      labels: *selectorLabels
    spec:
      serviceAccount: {{ include "rawfile-localpv.fullname" . }}-driver
      priorityClassName: {{ .Values.controller.priorityClassName }}
      tolerations:
        {{- .Values.controller.tolerations | toYaml | nindent 8 }}
      volumes:
        - name: socket-dir
          emptyDir: {}
      containers:
        - name: csi-driver
          image: "{{ include "rawfile-localpv.controller-image" . }}"
          imagePullPolicy: "{{ include "rawfile-localpv.controller-pull-policy" . }}"
          args:
            - csi-driver
            - --enable-metrics=false
          env:
            - name: PROVISIONER_NAME
              value: "{{ .Values.provisionerName }}"
            - name: CSI_DRIVER__ENDPOINT
              value: unix:///csi/csi.sock
            - name: CSI_DRIVER__NODE_ID
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: NAMESPACE
              value: {{ .Release.Namespace }}
            - name: LOG_LEVEL
              value: {{ .Values.logLevel }}
            - name: LOG_FORMAT
              value: {{ .Values.logFormat }}
            - name: CSI_DRIVER__PLUGIN_TYPE
              value: controller
            {{- if .Values.reservedCapacity }}
            - name: RESERVED_CAPACITY
              value: {{ .Values.reservedCapacity | toString | quote }}
            {{- end }}
            {{- if .Values.capacityOverride }}
            - name: CAPACITY_OVERRIDE
              value: {{ .Values.capacityOverride | toString | quote }}
            {{- end }}
            - name: CSI_DRIVER__GRPC_WORKERS
              value: {{ .Values.controller.grpcWorkers | toString | quote }}
            - name: GA_ENABLED
              value: "{{ .Values.global.analytics.enabled }}"
            {{- if .Values.global.analytics.gaId }}
            - name: GA_ID
              value: {{ .Values.global.analytics.gaId | quote }}
            {{- end }}
            {{- if .Values.global.analytics.gaKey }}
            - name: GA_KEY
              value: {{ .Values.global.analytics.gaKey | quote }}
            {{- end }}
            - name: CSI_DRIVER__INTERNAL_PORT
              value: {{ .Values.node.internalGRPC.port | toString | quote }}
            - name: CSI_DRIVER__NODE_DS
              value: {{ include "rawfile-localpv.fullname" . }}-node
            {{- if .Values.auth.enabled }}
            - name: CSI_DRIVER__INTERNAL_SIGNATURE
              valueFrom:
                secretKeyRef:
                  name: {{ include "rawfile-localpv.fullname" . }}-secrets
                  key: internal-signature
            {{- end }}
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
          ports:
            - name: csi-probe
              containerPort: 9808
          resources:
            {{- include "rawfile-localpv.controller-resources" . | nindent 12 }}
        - name: external-resizer
          image: {{ printf "%s/%s:%s" (.Values.controller.externalResizer.image.registry | default .Values.global.k8sImageRegistry) .Values.controller.externalResizer.image.repository .Values.controller.externalResizer.image.tag }}
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--handle-volume-inuse-error=false"
            - "--timeout=30s"
          env:
            - name: ADDRESS
              value: /csi/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
