kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: {{ include "cinder-csi.name" . }}-nodeplugin
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "cinder-csi.nodeplugin.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "cinder-csi.nodeplugin.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "cinder-csi.nodeplugin.labels" . | nindent 8 }}
    spec:
      serviceAccount: csi-cinder-node-sa
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
        - name: node-driver-registrar
          image: "{{ .Values.csi.nodeDriverRegistrar.image.repository }}:{{ .Values.csi.nodeDriverRegistrar.image.tag }}"
          imagePullPolicy: {{ .Values.csi.nodeDriverRegistrar.image.pullPolicy }}
          args:
            - "-v={{ .Values.logVerbosityLevel }}"
            - "--csi-address=$(ADDRESS)"
            - "--kubelet-registration-path=$(DRIVER_REG_SOCK_PATH)"
            {{- if .Values.csi.nodeDriverRegistrar.extraArgs }}
            {{- with .Values.csi.nodeDriverRegistrar.extraArgs }}
            {{- tpl . $ | trim | nindent 12 }}
            {{- end }}
            {{- end }}
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: DRIVER_REG_SOCK_PATH
              value: {{ .Values.csi.plugin.nodePlugin.kubeletDir }}/plugins/cinder.csi.openstack.org/csi.sock
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration
          resources: {{ toYaml .Values.csi.nodeDriverRegistrar.resources | nindent 12 }}
        - name: liveness-probe
          image: "{{ .Values.csi.livenessprobe.image.repository }}:{{ .Values.csi.livenessprobe.image.tag }}"
          imagePullPolicy: {{ .Values.csi.livenessprobe.image.pullPolicy }}
          args:
            - "-v={{ .Values.logVerbosityLevel }}"
            - --csi-address=/csi/csi.sock
            {{- if .Values.csi.livenessprobe.extraArgs }}
            {{- with .Values.csi.livenessprobe.extraArgs }}
            {{- tpl . $ | trim | nindent 12 }}
            {{- end }}
            {{- end }}
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
          resources: {{ toYaml .Values.csi.livenessprobe.resources | nindent 12 }}
        - name: cinder-csi-plugin
          securityContext:
            privileged: true
            capabilities:
              add: ["SYS_ADMIN"]
            allowPrivilegeEscalation: true
          image: "{{ .Values.csi.plugin.image.repository }}:{{ .Values.csi.plugin.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.csi.plugin.image.pullPolicy }}
          args:
            - /bin/cinder-csi-plugin
            - "-v={{ .Values.logVerbosityLevel }}"
            - "--endpoint=$(CSI_ENDPOINT)"
            - "--cloud-config=$(CLOUD_CONFIG)"
            - "--kms-addr={{ .Values.csi.kms.keyServiceName }}.{{ .Values.csi.kms.keyServiceNamespace | default .Release.Namespace }}:{{ .Values.csi.kms.keyServicePort }}"
            {{- if .Values.csi.plugin.extraArgs }}
            {{- with .Values.csi.plugin.extraArgs }}
            {{- tpl . $ | trim | nindent 12 }}
            {{- end }}
            {{- end }}
          env:
            - name: CSI_ENDPOINT
              value: unix://csi/csi.sock
            - name: CLOUD_CONFIG
              value: /etc/kubernetes/{{ .Values.secret.filename }}
          ports:
            - containerPort: 9808
              name: healthz
              protocol: TCP
          # The probe
          livenessProbe:
            failureThreshold: {{ .Values.csi.livenessprobe.failureThreshold }}
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: {{ .Values.csi.livenessprobe.initialDelaySeconds }}
            timeoutSeconds: {{ .Values.csi.livenessprobe.timeoutSeconds }}
            periodSeconds: {{ .Values.csi.livenessprobe.periodSeconds }}
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            - name: kubelet-dir
              mountPath: {{ .Values.csi.plugin.nodePlugin.kubeletDir }}
              mountPropagation: "Bidirectional"
            - name: pods-probe-dir
              mountPath: /dev
              mountPropagation: "HostToContainer"
            - name: cloud-config
              mountPath: /etc/kubernetes/{{ .Values.secret.filename }}
              readOnly: true
              subPath: {{ .Values.secret.filename }}
          resources: {{ toYaml .Values.csi.plugin.resources | nindent 12 }}
      volumes:
        - name: socket-dir
          hostPath:
            path: {{ .Values.csi.plugin.nodePlugin.kubeletDir }}/plugins/cinder.csi.openstack.org
            type: DirectoryOrCreate
        - name: registration-dir
          hostPath:
            path: {{ .Values.csi.plugin.nodePlugin.kubeletDir }}/plugins_registry/
            type: Directory
        - name: kubelet-dir
          hostPath:
            path: {{ .Values.csi.plugin.nodePlugin.kubeletDir }}
            type: Directory
        # - name: pods-cloud-data
        #   hostPath:
        #     path: /var/lib/cloud/data
        #     type: Directory
        - name: pods-probe-dir
          hostPath:
            path: /dev
            type: Directory
        - name: cloud-config
        {{- if .Values.secret.enabled }}
          secret:
            secretName: {{ .Values.secret.name }}
        {{- else }}
          hostPath:
            path: /etc/kubernetes
        {{- end }}
      affinity: {{ toYaml .Values.csi.plugin.nodePlugin.affinity | nindent 8 }}
      nodeSelector: {{ toYaml .Values.csi.plugin.nodePlugin.nodeSelector | nindent 8 }}
      tolerations: {{ toYaml .Values.csi.plugin.nodePlugin.tolerations | nindent 8 }}
      imagePullSecrets: {{ toYaml .Values.imagePullSecrets | nindent 8 }}
    {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
