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: {{ template "registry_url" . }}{{ .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: {{ template "registry_url" . }}{{ .Values.image.longhorn.manager.repository }}:{{ .Values.image.longhorn.manager.tag }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
          - longhorn-manager
          - -d
          - deploy-driver
          - --manager-image
          - "{{ template "registry_url" . }}{{ .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 and .Values.image.csi.attacher.repository .Values.image.csi.attacher.tag }}
          - name: CSI_ATTACHER_IMAGE
            value: "{{ template "registry_url" . }}{{ .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: "{{ template "registry_url" . }}{{ .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: "{{ template "registry_url" . }}{{ .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: "{{ template "registry_url" . }}{{ .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: "{{ template "registry_url" . }}{{ .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: "{{ template "registry_url" . }}{{ .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.privateRegistry.registrySecret }}
      imagePullSecrets:
      - name: {{ .Values.privateRegistry.registrySecret }}
      {{- end }}
      {{- if .Values.longhornDriver.priorityClass }}
      priorityClassName: {{ .Values.longhornDriver.priorityClass | quote }}
      {{- end }}
      {{- if or .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 .Values.longhornDriver.tolerations }}
{{ toYaml .Values.longhornDriver.tolerations | indent 6 }}
        {{- end }}
      {{- end }}
      {{- if or .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 .Values.longhornDriver.nodeSelector }}
{{ toYaml .Values.longhornDriver.nodeSelector | indent 8 }}
        {{- end }}
      {{- end }}
      serviceAccountName: longhorn-service-account
      securityContext:
        runAsUser: 0