{{- if .Values.linux.enabled}}
kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: {{ .Values.linux.dsName }}
  namespace: {{ .Release.Namespace }}
{{ include "azuredisk.labels" . | indent 2 }}
{{- with .Values.linux.labels }}
{{ . | toYaml | indent 4 }}
{{- end }}
{{- with .Values.linux.annotations }}
  annotations:
{{ . | toYaml | indent 4 }}
{{- end }}
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: {{ .Values.node.maxUnavailable }}
    type: RollingUpdate
  selector:
    matchLabels:
      app: {{ .Values.linux.dsName }}
  template:
    metadata:
{{ include "azuredisk.labels" . | indent 6 }}
        app: {{ .Values.linux.dsName }}
        {{- if .Values.workloadIdentity.clientID }}
        azure.workload.identity/use: "true"
        {{- end }}
{{- with .Values.linux.podLabels }}
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.linux.podAnnotations }}
      annotations:
{{ toYaml . | indent 8 }}
{{- end }}
    spec:
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.linux.hostNetwork }}
      serviceAccountName: {{ .Values.serviceAccount.node }}
      nodeSelector:
        kubernetes.io/os: linux
{{- with .Values.linux.nodeSelector }}
{{ toYaml . | indent 8 }}
{{- end }}
      affinity:
{{- with .Values.linux.affinity }}
{{ toYaml . | indent 8 }}
{{- end }}
        nodeAffinity:
{{ toYaml .Values.linux.nodeAffinity | indent 10 }}
      priorityClassName: system-node-critical
      securityContext:
        seccompProfile:
          type: RuntimeDefault
{{- with .Values.linux.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
      {{- include "azuredisk.pullSecrets" . | indent 6 }}
      containers:
        - name: liveness-probe
          volumeMounts:
            - mountPath: /csi
              name: socket-dir
{{- if hasPrefix "/" .Values.image.livenessProbe.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.livenessProbe.repository }}:{{ .Values.image.livenessProbe.tag }}"
{{- else }}
          image: "{{ .Values.image.livenessProbe.repository }}:{{ .Values.image.livenessProbe.tag }}"
{{- end }}
          args:
            - --csi-address=/csi/csi.sock
            - --probe-timeout=3s
            - --health-port={{ .Values.node.livenessProbe.healthPort }}
            - --v=2
          resources: {{- toYaml .Values.linux.resources.livenessProbe | nindent 12 }}
        - name: node-driver-registrar
{{- if hasPrefix "/" .Values.image.nodeDriverRegistrar.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.nodeDriverRegistrar.repository }}:{{ .Values.image.nodeDriverRegistrar.tag }}"
{{- else }}
          image: "{{ .Values.image.nodeDriverRegistrar.repository }}:{{ .Values.image.nodeDriverRegistrar.tag }}"
{{- end }}
          args:
            - --csi-address=$(ADDRESS)
            - --kubelet-registration-path=$(DRIVER_REG_SOCK_PATH)
            - --v=2
{{- if .Values.linux.enableRegistrationProbe }}
          livenessProbe:
            exec:
              command:
                - /csi-node-driver-registrar
                - --kubelet-registration-path=$(DRIVER_REG_SOCK_PATH)
                - --mode=kubelet-registration-probe
            initialDelaySeconds: 30
            timeoutSeconds: 15
{{- end }}
          env:
            - name: ADDRESS
              value: /csi/csi.sock
            - name: DRIVER_REG_SOCK_PATH
              value: {{ .Values.linux.kubelet }}/plugins/{{ .Values.driver.name }}/csi.sock
          volumeMounts:
            - name: socket-dir
              mountPath: /csi
            - name: registration-dir
              mountPath: /registration
          resources: {{- toYaml .Values.linux.resources.nodeDriverRegistrar | nindent 12 }}
        - name: azuredisk
{{- if hasPrefix "/" .Values.image.azuredisk.repository }}
          image: "{{ .Values.image.baseRepo }}{{ .Values.image.azuredisk.repository }}:{{ .Values.image.azuredisk.tag }}"
{{- else }}
          image: "{{ .Values.image.azuredisk.repository }}:{{ .Values.image.azuredisk.tag }}"
{{- end }}
          args:
            - "--v={{ .Values.node.logLevel }}"
            - "--endpoint=$(CSI_ENDPOINT)"
            - "--nodeid=$(KUBE_NODE_NAME)"
            - "--enable-perf-optimization={{ .Values.linux.enablePerfOptimization }}"
            - "--drivername={{ .Values.driver.name }}"
            - "--volume-attach-limit={{ .Values.driver.volumeAttachLimit }}"
            - "--cloud-config-secret-name={{ .Values.node.cloudConfigSecretName }}"
            - "--cloud-config-secret-namespace={{ .Values.node.cloudConfigSecretNamespace }}"
            - "--custom-user-agent={{ .Values.driver.customUserAgent }}"
            - "--user-agent-suffix={{ .Values.driver.userAgentSuffix }}"
            - "--allow-empty-cloud-config={{ .Values.node.allowEmptyCloudConfig }}"
            - "--support-zone={{ .Values.node.supportZone }}"
            - "--get-node-info-from-labels={{ .Values.linux.getNodeInfoFromLabels }}"
            - "--get-nodeid-from-imds={{ .Values.node.getNodeIDFromIMDS }}"
            - "--enable-otel-tracing={{ .Values.linux.otelTracing.enabled }}"
            - "--kms-addr={{ .Values.global.keyServiceName }}.{{ .Values.global.keyServiceNamespace | default .Release.Namespace }}:{{ .Values.global.keyServicePort }}"
          ports:
            - containerPort: {{ .Values.node.livenessProbe.healthPort }}
              name: healthz
              protocol: TCP
          livenessProbe:
            failureThreshold: 5
            httpGet:
              path: /healthz
              port: healthz
            initialDelaySeconds: 30
            timeoutSeconds: 10
            periodSeconds: 30
          env:
            - name: AZURE_CREDENTIAL_FILE
              valueFrom:
                configMapKeyRef:
                  name: {{ .Values.azureCredentialFileConfigMap }}
                  key: path
                  optional: true
            - name: CSI_ENDPOINT
              value: unix:///csi/csi.sock
            {{- if ne .Values.driver.httpsProxy "" }}
            - name: HTTPS_PROXY
              value: {{ .Values.driver.httpsProxy }}
            {{- end }}
            {{- if ne .Values.driver.httpProxy "" }}
            - name: HTTP_PROXY
              value: {{ .Values.driver.httpProxy }}
            {{- end }}
            {{- if ne .Values.driver.noProxy "" }}
            - name: NO_PROXY
              value: {{ .Values.driver.noProxy }}
            {{- end }}
            - name: KUBE_NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: AZURE_GO_SDK_LOG_LEVEL
              value: {{ .Values.driver.azureGoSDKLogLevel }}
            {{- if eq .Values.cloud "AzureStackCloud" }}
            - name: AZURE_ENVIRONMENT_FILEPATH
              value: /etc/kubernetes/azurestackcloud.json
            {{- end }}
            {{- if .Values.linux.otelTracing.enabled }}
            - name: OTEL_SERVICE_NAME
              value: {{ .Values.linux.otelTracing.otelServiceName }}
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: {{ .Values.linux.otelTracing.otelExporterEndpoint }}
            {{- end }}
          imagePullPolicy: {{ .Values.image.azuredisk.pullPolicy }}
          securityContext:
            privileged: true
          volumeMounts:
            - mountPath: /csi
              name: socket-dir
            - mountPath: {{ .Values.linux.kubelet }}/
              mountPropagation: Bidirectional
              name: mountpoint-dir
            - mountPath: /dev
              name: device-dir
            - mountPath: /sys/bus/scsi/devices
              name: sys-devices-dir
            - mountPath: /sys/class/
              name: sys-class
            - name: cryptsetup
              mountPath: /run/cryptsetup
            {{- if eq .Values.cloud "AzureStackCloud" }}
            - name: ssl
              mountPath: /etc/ssl/certs
              readOnly: true
            {{- end }}
            {{- if eq .Values.linux.distro "fedora" }}
            - name: ssl
              mountPath: /etc/ssl/certs
              readOnly: true
            - name: ssl-pki
              mountPath: /etc/pki/ca-trust/extracted
              readOnly: true
            {{- end }}
          resources: {{- toYaml .Values.linux.resources.azuredisk | nindent 12 }}
      volumes:
        - hostPath:
            path: {{ .Values.linux.kubelet }}/plugins/{{ .Values.driver.name }}
            type: DirectoryOrCreate
          name: socket-dir
        - hostPath:
            path: {{ .Values.linux.kubelet }}/
            type: DirectoryOrCreate
          name: mountpoint-dir
        - hostPath:
            path: {{ .Values.linux.kubelet }}/plugins_registry/
            type: DirectoryOrCreate
          name: registration-dir
        - hostPath:
            path: /dev
            type: Directory
          name: device-dir
        - hostPath:
            path: /sys/bus/scsi/devices
            type: Directory
          name: sys-devices-dir
        - hostPath:
            path: /sys/class/
            type: Directory
          name: sys-class
        {{- if eq .Values.cloud "AzureStackCloud" }}
        - name: ssl
          hostPath:
            path: /etc/ssl/certs
        {{- end }}
        {{- if eq .Values.linux.distro "fedora" }}
        - name: ssl
          hostPath:
            path: /etc/ssl/certs
        - name: ssl-pki
          hostPath:
            path: /etc/pki/ca-trust/extracted
        {{- end }}
        - name: cryptsetup
          hostPath:
            path: /run/cryptsetup
            type: Directory
{{- end -}}
