---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: {{ .Values.controller.name }}
  namespace: {{ .Release.Namespace }}
{{ include "smb.labels" . | indent 2 }}
spec:
  replicas: {{ .Values.controller.replicas }}
  selector:
    matchLabels:
      app: {{ .Values.controller.name }}
  template:
    metadata:
{{ include "smb.labels" . | indent 6 }}
        app: {{ .Values.controller.name }}
        {{- if .Values.podLabels }}
{{- toYaml .Values.podLabels | nindent 8 }}
        {{- end }}
{{- if .Values.podAnnotations }}
      annotations:
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
    spec:
{{- with .Values.controller.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
{{- end }}
      hostNetwork: true
      dnsPolicy: {{ .Values.controller.dnsPolicy }}
      serviceAccountName: {{ .Values.serviceAccount.controller }}
      nodeSelector:
{{- with .Values.controller.nodeSelector }}
{{ toYaml . | indent 8 }}
{{- end }}
        kubernetes.io/os: linux
        {{- if .Values.controller.runOnMaster}}
        node-role.kubernetes.io/master: ""
        {{- end}}
        {{- if .Values.controller.runOnControlPlane}}
        node-role.kubernetes.io/control-plane: ""
        {{- end}}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- if .Values.securityContext }}
      securityContext: {{- toYaml .Values.securityContext | nindent 8 }}
      {{- end }}
{{- with .Values.controller.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
      {{- include "smb.pullSecrets" . | indent 6 }}
      containers:
        - name: csi-provisioner
{{- if hasPrefix "/" .Values.image.csiProvisioner.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.csiProvisioner.repository }}:{{ .Values.image.csiProvisioner.tag }}"
{{- else }}
          image: "{{ .Values.image.csiProvisioner.repository }}:{{ .Values.image.csiProvisioner.tag }}"
{{- end }}
          args:
            - "-v=2"
            - "--csi-address=$(ADDRESS)"
            - "--leader-election"
            - "--leader-election-namespace={{ .Release.Namespace }}"
            - "--extra-create-metadata=true"
          env:
            - name: ADDRESS
              value: /csi/csi.sock
          imagePullPolicy: {{ .Values.image.csiProvisioner.pullPolicy }}
          volumeMounts:
            - mountPath: /csi
              name: socket-dir
          resources: {{- toYaml .Values.controller.resources.csiProvisioner | nindent 12 }}
          securityContext:
            readOnlyRootFilesystem: true
        - name: liveness-probe
{{- if hasPrefix "/" .Values.image.livenessProbe.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.livenessProbe.repository }}:{{ .Values.image.livenessProbe.tag }}"
{{- else }}
          image: "{{ .Values.image.livenessProbe.repository }}:{{ .Values.image.livenessProbe.tag }}"
{{- end }}
          args:
            - --csi-address=/csi/csi.sock
            - --probe-timeout=3s
            - --health-port={{ .Values.controller.livenessProbe.healthPort }}
            - --v=2
          imagePullPolicy: {{ .Values.image.livenessProbe.pullPolicy }}
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
          resources: {{- toYaml .Values.controller.resources.livenessProbe | nindent 12 }}
          securityContext:
            readOnlyRootFilesystem: true
        - name: smb
{{- if hasPrefix "/" .Values.image.smb.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.smb.repository }}:{{ .Values.image.smb.tag }}"
{{- else }}
          image: "{{ .Values.image.smb.repository }}:{{ .Values.image.smb.tag }}"
{{- end }}
          imagePullPolicy: {{ .Values.image.smb.pullPolicy }}
          args:
            - "--v={{ .Values.controller.logLevel }}"
            - "--endpoint=$(CSI_ENDPOINT)"
            - "--metrics-address=0.0.0.0:{{ .Values.controller.metricsPort }}"
            - "--drivername={{ .Values.driver.name }}"
            - "--working-mount-dir={{ .Values.controller.workingMountDir }}"
          ports:
            - containerPort: {{ .Values.controller.livenessProbe.healthPort }}
              name: healthz
              protocol: TCP
            - containerPort: {{ .Values.controller.metricsPort }}
              name: metrics
              protocol: TCP
          livenessProbe:
            failureThreshold: 5
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: 30
            timeoutSeconds: 10
            periodSeconds: 30
          env:
            - name: CSI_ENDPOINT
              value: unix:///csi/csi.sock
          securityContext:
            privileged: true
            readOnlyRootFilesystem: true
          volumeMounts:
            - mountPath: /csi
              name: socket-dir
            - mountPath: {{ .Values.controller.workingMountDir }}
              name: tmp-dir
          resources: {{- toYaml .Values.controller.resources.smb | nindent 12 }}
      volumes:
        - name: socket-dir
          emptyDir: {}
        - name: tmp-dir
          emptyDir: {}
