apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "zfslocalpv.fullname" . }}-controller
  namespace: {{ .Release.Namespace }}
  {{- with .Values.zfsController.annotations }}
  annotations: {{ toYaml . | nindent 4 }}
  {{- end }}
  labels:
    {{- include "zfslocalpv.zfsController.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "zfslocalpv.zfsController.matchLabels" . | nindent 6 }}
  replicas: {{ .Values.zfsController.replicas }}
  template:
    metadata:
      {{- with .Values.zfsController.podAnnotations }}
      annotations: {{ toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "zfslocalpv.zfsController.labels" . | nindent 8 -}}
        {{- with .Values.zfsController.podLabels}}
        {{ toYaml . | nindent 8 -}}
        {{- end}}
        {{- with .Values.loggingLabels}}
        {{ toYaml . | nindent 8 -}}
        {{- end}}
    spec:
{{- if .Values.zfsController.priorityClass.create }}
      priorityClassName: {{ template "zfslocalpv.zfsController.priorityClassName" . }}
{{- end }}
      serviceAccountName: {{ .Values.serviceAccount.zfsController.name }}
{{- if .Values.zfsController.initContainers }}
      initContainers:
{{- range $key, $value := .Values.zfsController.initContainers }}
        - name: {{ $key }}
{{ toYaml $value | indent 10 }}
{{- end }}
{{- end }}
      containers:
        - name: {{ .Values.zfsController.resizer.name }}
          image: "{{ .Values.zfsController.resizer.image.registry }}{{ .Values.zfsController.resizer.image.repository }}:{{ .Values.zfsController.resizer.image.tag }}"
          args:
            - "--v=5"
            - "--csi-address=$(ADDRESS)"
            {{- include "zfslocalpv.zfsController.leaderElection" . | indent 12 }}
          {{- range .Values.zfsController.resizer.extraArgs }}
            - {{ tpl . $ | quote }}
          {{- end }}
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          imagePullPolicy: {{ .Values.zfsController.resizer.image.pullPolicy }}
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          resources:
            {{- toYaml .Values.zfsController.resources | nindent 12 }}
        - name: {{ .Values.zfsController.snapshotter.name }}
          image: "{{ .Values.zfsController.snapshotter.image.registry }}{{ .Values.zfsController.snapshotter.image.repository }}:{{ .Values.zfsController.snapshotter.image.tag }}"
          imagePullPolicy: {{ .Values.zfsController.snapshotter.image.pullPolicy }}
          args:
            - "--csi-address=$(ADDRESS)"
            {{- include "zfslocalpv.zfsController.leaderElection" . | indent 12 }}
          {{- range .Values.zfsController.snapshotter.extraArgs }}
            - {{ tpl . $ | quote }}
          {{- end }}
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          resources:
            {{- toYaml .Values.zfsController.resources | nindent 12 }}
        - name: {{ .Values.zfsController.snapshotController.name }}
          image: "{{ .Values.zfsController.snapshotController.image.registry }}{{ .Values.zfsController.snapshotController.image.repository }}:{{ .Values.zfsController.snapshotController.image.tag }}"
          args:
            - "--v=5"
            {{- include "zfslocalpv.zfsController.leaderElection" . | indent 12 }}
          {{- range .Values.zfsController.snapshotController.extraArgs }}
            - {{ tpl . $ | quote }}
          {{- end }}
          imagePullPolicy: {{ .Values.zfsController.snapshotController.image.pullPolicy }}
          resources:
            {{- toYaml .Values.zfsController.resources | nindent 12 }}
        - name: {{ .Values.zfsController.provisioner.name }}
          image: "{{ .Values.zfsController.provisioner.image.registry }}{{ .Values.zfsController.provisioner.image.repository }}:{{ .Values.zfsController.provisioner.image.tag }}"
          imagePullPolicy: {{ .Values.zfsController.provisioner.image.pullPolicy }}
          args:
            - "--csi-address=$(ADDRESS)"
            - "--v=5"
            - "--feature-gates=Topology=true"
            - "--strict-topology"
            - "--enable-capacity={{ .Values.feature.storageCapacity }}"
            - "--extra-create-metadata=true"
            - "--default-fstype=ext4"
            {{- include "zfslocalpv.zfsController.leaderElection" . | indent 12 }}
          {{- range .Values.zfsController.provisioner.extraArgs }}
            - {{ tpl . $ | quote }}
          {{- end }}
          env:
            - name: ADDRESS
              value: /var/lib/csi/sockets/pluginproxy/csi.sock
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          resources:
            {{- toYaml .Values.zfsController.resources | nindent 12 }}
        - name: {{ .Values.zfsPlugin.name }}
          image: "{{ .Values.zfsPlugin.image.registry }}{{ .Values.zfsPlugin.image.repository }}:{{ .Values.zfsPlugin.image.tag }}"
          imagePullPolicy: {{ .Values.zfsPlugin.image.pullPolicy }}
          env:
            - name: OPENEBS_CONTROLLER_DRIVER
              value: controller
            - name: OPENEBS_CSI_ENDPOINT
              value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
            - name: OPENEBS_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: OPENEBS_IO_INSTALLER_TYPE
              value: "{{ if (not (hasKey .Values.analytics "installerType")) }}zfs-localpv-helm{{ else }}{{ .Values.analytics.installerType }}{{ end }}"
            - name: OPENEBS_IO_ENABLE_ANALYTICS
              value: "{{ .Values.analytics.enabled }}"
            {{- if .Values.analytics.gaId }}
            - name: GA_ID
              value: {{ .Values.analytics.gaId | quote }}
            {{- end }}
            {{- if .Values.analytics.gaKey }}
            - name: GA_KEY
              value: {{ .Values.analytics.gaKey | quote }}
            {{- end }}
            - name: OPENEBS_IO_ENABLE_BACKUP_GC
              value: "{{ .Values.backupGC.enabled }}"
          args :
            - "--endpoint=$(OPENEBS_CSI_ENDPOINT)"
            - "--plugin=$(OPENEBS_CONTROLLER_DRIVER)"
          volumeMounts:
            - name: socket-dir
              mountPath: /var/lib/csi/sockets/pluginproxy/
          resources:
            {{- toYaml .Values.zfsController.resources | nindent 12 }}
      volumes:
        - name: socket-dir
          emptyDir: {}
{{- if .Values.zfsController.additionalVolumes }}
{{- range $name, $config := .Values.zfsController.additionalVolumes }}
        - name: {{ $name }}
{{- tpl (toYaml $config) $ | nindent 10 }}
{{- end }}
{{- end }}
{{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
{{- end }}
{{- if .Values.zfsController.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.zfsController.nodeSelector | indent 8 }}
{{- end }}
{{- if .Values.zfsController.securityContext }}
      securityContext:
{{ toYaml .Values.zfsController.securityContext | indent 8 }}
{{- end }}
{{- if .Values.zfsController.tolerations }}
      tolerations:
{{ toYaml .Values.zfsController.tolerations | indent 8 }}
{{- end }}
