apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "rawfile-localpv.fullname" . }}-node
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: "100%"
  selector:
    matchLabels: &selectorLabels
      {{- include "rawfile-localpv.selectorLabels" . | nindent 6 }}
      component: node
  template:
    metadata:
      labels: *selectorLabels
    spec:
      serviceAccount: {{ include "rawfile-localpv.fullname" . }}-driver
      priorityClassName: {{ .Values.node.priorityClassName }}
      tolerations:
        {{- .Values.node.tolerations | toYaml | nindent 8 }}
      volumes:
        - name: registration-dir
          hostPath:
            path: {{ include "rawfile-localpv.node-kubelet-path" . }}plugins_registry
            type: Directory
        - name: socket-dir
          hostPath:
            path: {{ include "rawfile-localpv.node-kubelet-path" . }}plugins/rawfile-localpv
            type: DirectoryOrCreate
        - name: mountpoint-dir
          hostPath:
            path: {{ include "rawfile-localpv.node-kubelet-path" . }}
            type: DirectoryOrCreate
        - name: data-dir
          hostPath:
            path: {{ .Values.node.dataDirPath }}
            type: DirectoryOrCreate
        - name: metadata-dir
          hostPath:
            path: {{ include "rawfile-localpv.metadata-dir-path" . }}
            type: DirectoryOrCreate
        - name: device
          hostPath:
            path: /dev
            type: Directory
      containers:
        - name: csi-driver
          image: "{{ include "rawfile-localpv.node-image" . }}"
          imagePullPolicy: "{{ include "rawfile-localpv.node-pull-policy" . }}"
          securityContext:
            privileged: true
          env:
            - name: PROVISIONER_NAME
              value: "{{ .Values.provisionerName }}"
            - name: CSI_DRIVER__ENDPOINT
              value: unix:///csi/csi.sock
            - name: CSI_DRIVER__NODE_ID
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: CSI_DRIVER__ENABLE_METRICS
              value: {{ .Values.metrics.enabled | toString | quote }}
            - name: CSI_DRIVER__METRICS_PORT
              value: {{ .Values.metrics.port | toString | quote }}
            - name: CSI_DRIVER__METADATA_DIR
              value: {{ include "rawfile-localpv.metadata-dir-path" . }}
            - name: NAMESPACE
              value: {{ .Release.Namespace }}
            - name: LOG_LEVEL
              value: {{ .Values.logLevel }}
            - name: LOG_FORMAT
              value: {{ .Values.logFormat }}
            - name: CSI_DRIVER__PLUGIN_TYPE
              value: node
            {{- if .Values.reservedCapacity }}
            - name: RESERVED_CAPACITY
              value: {{ .Values.reservedCapacity | toString | quote }}
            {{- end }}
            {{- if .Values.capacityOverride }}
            - name: CAPACITY_OVERRIDE
              value: {{ .Values.capacityOverride | toString | quote }}
            {{- end }}
            - name: CSI_DRIVER__GRPC_WORKERS
              value: {{ .Values.node.grpcWorkers | toString | quote }}
            - name: GA_ENABLED
              value: "{{ .Values.global.analytics.enabled }}"
            {{- if .Values.global.analytics.gaId }}
            - name: GA_ID
              value: {{ .Values.global.analytics.gaId | quote }}
            {{- end }}
            {{- if .Values.global.analytics.gaKey }}
            - name: GA_KEY
              value: {{ .Values.global.analytics.gaKey | quote }}
            {{- end }}
            - name: CSI_DRIVER__INTERNAL_IP
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.podIP
            - name: CSI_DRIVER__INTERNAL_PORT
              value: {{ .Values.node.internalGRPC.port | toString | quote }}
            - name: CSI_DRIVER__INTERNAL_GRPC_WORKERS
              value: {{ .Values.node.internalGRPC.workers | toString | quote }}
            - name: CSI_DRIVER__NODE_DS
              value: {{ include "rawfile-localpv.fullname" . }}-node
            {{- if .Values.auth.enabled }}
            - name: CSI_DRIVER__INTERNAL_SIGNATURE
              valueFrom:
                secretKeyRef:
                  name: {{ include "rawfile-localpv.fullname" . }}-secrets
                  key: internal-signature
            {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.port }}
            - name: csi-probe
              containerPort: 9808
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            - name: mountpoint-dir
              mountPath: {{ include "rawfile-localpv.node-kubelet-path" . }}
              mountPropagation: "Bidirectional"
            - name: data-dir
              mountPath: /data
            - name: metadata-dir
              mountPath: {{ include "rawfile-localpv.metadata-dir-path" . }}
            - name: device
              mountPath: /dev
          resources:
            {{- include "rawfile-localpv.controller-resources" . | nindent 12 }}
        - name: node-driver-registrar
          image: {{ printf "%s/%s:%s" (.Values.node.driverRegistrar.image.registry | default .Values.global.k8sImageRegistry) .Values.node.driverRegistrar.image.repository .Values.node.driverRegistrar.image.tag }}
          imagePullPolicy: IfNotPresent
          args:
            - --csi-address=$(ADDRESS)
            - --kubelet-registration-path=$(DRIVER_REG_SOCK_PATH)
            - --health-port=9809
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: DRIVER_REG_SOCK_PATH
              value: {{ include "rawfile-localpv.node-kubelet-path" . }}plugins/rawfile-localpv/csi.sock
          ports:
            - containerPort: 9809
              name: healthz
          livenessProbe:
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: 5
            timeoutSeconds: 5
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration
          resources:
            limits:
              cpu: 500m
              memory: 100Mi
            requests:
              cpu: 10m
              memory: 100Mi
        - name: external-provisioner
          image: {{ printf "%s/%s:%s" (.Values.node.externalProvisioner.image.registry | default .Values.global.k8sImageRegistry) .Values.node.externalProvisioner.image.repository .Values.node.externalProvisioner.image.tag }}
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--feature-gates=Topology=true"
            - "--strict-topology"
            - "--immediate-topology=false"
            - "--timeout=120s"
            - "--enable-capacity=true"
            - "--capacity-for-immediate-binding=true"
            - "--capacity-ownerref-level=1" # DaemonSet
            - "--node-deployment=true"
            - "--extra-create-metadata=true"
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
        - name: external-snapshotter
          image: {{ printf "%s/%s:%s" (.Values.node.externalSnapshotter.image.registry | default .Values.global.k8sImageRegistry) .Values.node.externalSnapshotter.image.repository .Values.node.externalSnapshotter.image.tag }}
          imagePullPolicy: IfNotPresent
          args:
            - "--csi-address=$(ADDRESS)"
            - "--node-deployment=true"
            - "--extra-create-metadata=true"
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
        - name: snapshot-controller
          args:
            - "--v=2"
            - "--enable-distributed-snapshotting=true"
          image: {{ printf "%s/%s:%s" (.Values.node.snapshotController.image.registry | default .Values.global.k8sImageRegistry) .Values.node.snapshotController.image.repository .Values.node.snapshotController.image.tag }}
          imagePullPolicy: IfNotPresent
