apiVersion: apps/v1
kind: Deployment
metadata:
  name: longhorn-driver-deployer
  namespace: {{ include "release_namespace" . }}
  labels: {{- include "longhorn.labels" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: longhorn-driver-deployer
  template:
    metadata:
      labels: {{- include "longhorn.labels" . | nindent 8 }}
        app: longhorn-driver-deployer
    spec:
      initContainers:
        - name: wait-longhorn-manager
          image: {{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.longhorn.manager.registry) }}{{ . }}/{{ end }}{{ .Values.image.longhorn.manager.repository }}:{{ .Values.image.longhorn.manager.tag }}
          command: ['sh', '-c', 'while [ $(curl -m 1 -s -o /dev/null -w "%{http_code}" http://longhorn-backend:9500/v1) != "200" ]; do echo waiting; sleep 2; done']
      containers:
        - name: longhorn-driver-deployer
          image: {{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.longhorn.manager.registry) }}{{ . }}/{{ end }}{{ .Values.image.longhorn.manager.repository }}:{{ .Values.image.longhorn.manager.tag }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
          - longhorn-manager
          - -d
          {{- if eq .Values.longhornDriver.log.format "json" }}
          - -j
          {{- end }}
          - deploy-driver
          - --manager-image
          - "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.longhorn.manager.registry) }}{{ . }}/{{ end }}{{ .Values.image.longhorn.manager.repository }}:{{ .Values.image.longhorn.manager.tag }}"
          - --manager-url
          - http://longhorn-backend:9500/v1
          env:
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: SERVICE_ACCOUNT
            valueFrom:
              fieldRef:
                fieldPath: spec.serviceAccountName
          {{- if .Values.csi.kubeletRootDir }}
          - name: KUBELET_ROOT_DIR
            value: {{ .Values.csi.kubeletRootDir }}
          {{- end }}
          {{- if .Values.csi.podAntiAffinityPreset }}
          - name: CSI_POD_ANTI_AFFINITY_PRESET
            value: {{ .Values.csi.podAntiAffinityPreset }}
          {{- end }}
          {{- if and .Values.image.csi.attacher.repository .Values.image.csi.attacher.tag }}
          - name: CSI_ATTACHER_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.attacher.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.attacher.repository }}:{{ .Values.image.csi.attacher.tag }}"
          {{- end }}
          {{- if and .Values.image.csi.provisioner.repository .Values.image.csi.provisioner.tag }}
          - name: CSI_PROVISIONER_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.provisioner.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.provisioner.repository }}:{{ .Values.image.csi.provisioner.tag }}"
          {{- end }}
          {{- if and .Values.image.csi.nodeDriverRegistrar.repository .Values.image.csi.nodeDriverRegistrar.tag }}
          - name: CSI_NODE_DRIVER_REGISTRAR_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.nodeDriverRegistrar.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.nodeDriverRegistrar.repository }}:{{ .Values.image.csi.nodeDriverRegistrar.tag }}"
          {{- end }}
          {{- if and .Values.image.csi.resizer.repository .Values.image.csi.resizer.tag }}
          - name: CSI_RESIZER_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.resizer.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.resizer.repository }}:{{ .Values.image.csi.resizer.tag }}"
          {{- end }}
          {{- if and .Values.image.csi.snapshotter.repository .Values.image.csi.snapshotter.tag }}
          - name: CSI_SNAPSHOTTER_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.snapshotter.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.snapshotter.repository }}:{{ .Values.image.csi.snapshotter.tag }}"
          {{- end }}
          {{- if and .Values.image.csi.livenessProbe.repository .Values.image.csi.livenessProbe.tag }}
          - name: CSI_LIVENESS_PROBE_IMAGE
            value: "{{ with (coalesce .Values.global.imageRegistry (include "registry_url" .) .Values.image.csi.livenessProbe.registry) }}{{ . }}/{{ end }}{{ .Values.image.csi.livenessProbe.repository }}:{{ .Values.image.csi.livenessProbe.tag }}"
          {{- end }}
          {{- if .Values.csi.attacherReplicaCount }}
          - name: CSI_ATTACHER_REPLICA_COUNT
            value: {{ .Values.csi.attacherReplicaCount | quote }}
          {{- end }}
          {{- if .Values.csi.provisionerReplicaCount }}
          - name: CSI_PROVISIONER_REPLICA_COUNT
            value: {{ .Values.csi.provisionerReplicaCount | quote }}
          {{- end }}
          {{- if .Values.csi.resizerReplicaCount }}
          - name: CSI_RESIZER_REPLICA_COUNT
            value: {{ .Values.csi.resizerReplicaCount | quote }}
          {{- end }}
          {{- if .Values.csi.snapshotterReplicaCount }}
          - name: CSI_SNAPSHOTTER_REPLICA_COUNT
            value: {{ .Values.csi.snapshotterReplicaCount | quote }}
          {{- end }}
          {{- if .Values.enableGoCoverDir }}
          - name: GOCOVERDIR
            value: /go-cover-dir/
          volumeMounts:
          - name: go-cover-dir
            mountPath: /go-cover-dir/
          {{- end }}
          {{- include "longhorn.timezoneEnv" . | nindent 10 }}

      {{- with (coalesce .Values.global.imagePullSecrets .Values.privateRegistry.registrySecret) }}
      imagePullSecrets:
        {{- $imagePullSecrets := list }}
        {{- if kindIs "string" . }}
          {{- $imagePullSecrets = append $imagePullSecrets (dict "name" .) }}
        {{- else }}
          {{- range . }}
            {{- if kindIs "string" . }}
                {{- $imagePullSecrets = append $imagePullSecrets (dict "name" .) }}
            {{- else }}
                {{- $imagePullSecrets = append $imagePullSecrets . }}
            {{- end }}
          {{- end }}
        {{- end }}
        {{- toYaml $imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if .Values.longhornDriver.priorityClass }}
      priorityClassName: {{ .Values.longhornDriver.priorityClass | quote }}
      {{- end }}
      {{- if or .Values.global.tolerations .Values.longhornDriver.tolerations .Values.global.cattle.windowsCluster.enabled }}
      tolerations:
        {{- if and .Values.global.cattle.windowsCluster.enabled .Values.global.cattle.windowsCluster.tolerations }}
{{ toYaml .Values.global.cattle.windowsCluster.tolerations | indent 6 }}
        {{- end }}
        {{- if or .Values.global.tolerations .Values.longhornDriver.tolerations }}
{{ default .Values.global.tolerations .Values.longhornDriver.tolerations | toYaml | indent 6 }}
        {{- end }}
      {{- end }}
      {{- if or .Values.global.nodeSelector .Values.longhornDriver.nodeSelector .Values.global.cattle.windowsCluster.enabled }}
      nodeSelector:
        {{- if and .Values.global.cattle.windowsCluster.enabled .Values.global.cattle.windowsCluster.nodeSelector }}
{{ toYaml .Values.global.cattle.windowsCluster.nodeSelector | indent 8 }}
        {{- end }}
        {{- if or .Values.global.nodeSelector .Values.longhornDriver.nodeSelector }}
{{ default .Values.global.nodeSelector .Values.longhornDriver.nodeSelector | toYaml | indent 8 }}
        {{- end }}
      {{- end }}
      serviceAccountName: longhorn-service-account
      securityContext:
        runAsUser: 0
      {{- if .Values.enableGoCoverDir }}
      volumes:
      - name: go-cover-dir
        hostPath:
          path: /go-cover-dir/
          type: DirectoryOrCreate
      {{- end }}
