apiVersion: apps/v1
kind: Deployment
metadata:
  name: longhorn-driver-deployer
  namespace: {{ .Release.Namespace }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: longhorn-driver-deployer
  template:
    metadata:
      labels:
        app: longhorn-driver-deployer
    spec:
      initContainers:
        - name: wait-longhorn-manager
          image: "{{ .Values.image.longhorn.manager }}:{{ .Values.image.longhorn.managerTag }}"
          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: "{{ .Values.image.longhorn.manager }}:{{ .Values.image.longhorn.managerTag }}"
          imagePullPolicy: Always
          command:
          - longhorn-manager
          - -d
          - deploy-driver
          - --manager-image
          - "{{ .Values.image.longhorn.manager }}:{{ .Values.image.longhorn.managerTag }}"
          - --manager-url
          - http://longhorn-backend:9500/v1
          - --driver
          - "{{ .Values.driver }}"
          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
          - name: FLEXVOLUME_DIR
            value: {{ .Values.persistence.flexvolumePath }}
          {{- if .Values.csi.kubeletRootDir }}
          - name: KUBELET_ROOT_DIR
            value: {{ .Values.csi.kubeletRootDir }}
          {{- end }}
          {{- if .Values.csi.attacherImage }}
          - name: CSI_ATTACHER_IMAGE
            value: {{ .Values.csi.attacherImage }}
          {{- end }}
          {{- if .Values.csi.provisionerImage }}
          - name: CSI_PROVISIONER_IMAGE
            value: {{ .Values.csi.provisionerImage }}
          {{- end }}
          {{- if .Values.csi.driverRegistrarImage }}
          - name: CSI_DRIVER_REGISTRAR_IMAGE
            value: {{ .Values.csi.driverRegistrarImage }}
          {{- end }}
          {{- if .Values.csi.attacherReplicaCount }}
          - name: CSI_ATTACHER_REPLICA_COUNT
            value: "{{ .Values.csi.attacherReplicaCount }}"
          {{- end }}
          {{- if .Values.csi.provisionerReplicaCount }}
          - name: CSI_PROVISIONER_REPLICA_COUNT
            value: "{{ .Values.csi.provisionerReplicaCount }}"
          {{- end }}
      serviceAccountName: longhorn-service-account
