{{- if .Values.worker.enable }}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name:  {{ include "node-feature-discovery.fullname" . }}-worker
  namespace: {{ include "node-feature-discovery.namespace" . }}
  labels:
    {{- include "node-feature-discovery.labels" . | nindent 4 }}
    role: worker
  {{- with .Values.worker.daemonsetAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  revisionHistoryLimit: {{ .Values.worker.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "node-feature-discovery.selectorLabels" . | nindent 6 }}
      role: worker
  template:
    metadata:
      labels:
        {{- include "node-feature-discovery.selectorLabels" . | nindent 8 }}
        role: worker
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/nfd-worker-conf.yaml") . | sha256sum }}
        {{- with .Values.worker.annotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      dnsPolicy: ClusterFirstWithHostNet
    {{- with .Values.priorityClassName }}
      priorityClassName: {{ . }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "node-feature-discovery.worker.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.worker.podSecurityContext | nindent 8 }}
      hostNetwork: {{ .Values.worker.hostNetwork }}
      containers:
      - name: worker
        securityContext:
          {{- toYaml .Values.worker.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        livenessProbe:
          grpc:
            port: {{ .Values.worker.healthPort | default "8082" }}
        {{- with .Values.worker.livenessProbe.initialDelaySeconds }}
          initialDelaySeconds: {{ . }}
        {{- end }}
        {{- with .Values.worker.livenessProbe.failureThreshold }}
          failureThreshold: {{ . }}
        {{- end }}
        {{- with .Values.worker.livenessProbe.periodSeconds }}
          periodSeconds: {{ . }}
        {{- end }}
        {{- with .Values.worker.livenessProbe.timeoutSeconds }}
          timeoutSeconds: {{ . }}
        {{- end }}
        readinessProbe:
          grpc:
            port: {{ .Values.worker.healthPort | default "8082" }}
        {{- with .Values.worker.readinessProbe.initialDelaySeconds }}
          initialDelaySeconds: {{ . }}
        {{- end }}
        {{- with .Values.worker.readinessProbe.failureThreshold }}
          failureThreshold: {{ . }}
        {{- end }}
        {{- with .Values.worker.readinessProbe.periodSeconds }}
          periodSeconds: {{ . }}
        {{- end }}
        {{- with .Values.worker.readinessProbe.timeoutSeconds }}
          timeoutSeconds: {{ . }}
        {{- end }}
        {{- with .Values.worker.readinessProbe.successThreshold }}
          successThreshold: {{ . }}
        {{- end }}
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_UID
          valueFrom:
            fieldRef:
              fieldPath: metadata.uid
      {{- with .Values.worker.extraEnvs }}
        {{- toYaml . | nindent 8 }}
      {{- end}}
        resources:
        {{- toYaml .Values.worker.resources | nindent 12 }}
        command:
        - "nfd-worker"
        args:
        # Go over featureGate and add the feature-gate flag
        {{- range $key, $value := .Values.featureGates }}
        - "-feature-gates={{ $key }}={{ $value }}"
        {{- end }}
        - "-metrics={{ .Values.worker.metricsPort | default "8081"}}"
        - "-grpc-health={{ .Values.worker.healthPort | default "8082" }}"
        {{- with .Values.worker.extraArgs }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        ports:
          - containerPort: {{ .Values.worker.metricsPort | default "8081"}}
            name: metrics
          - containerPort: {{ .Values.worker.healthPort | default "8082" }}
            name: health
        volumeMounts:
        - name: host-boot
          mountPath: "/host-boot"
          readOnly: true
        - name: host-os-release
          mountPath: "/host-etc/os-release"
          readOnly: true
        - name: host-sys
          mountPath: "/host-sys"
          readOnly: true
        - name: host-usr-lib
          mountPath: "/host-usr/lib"
          readOnly: true
        - name: host-lib
          mountPath: "/host-lib"
          readOnly: true
        - name: host-proc-swaps
          mountPath: "/host-proc/swaps"
          readOnly: true
        {{- if .Values.worker.mountUsrSrc }}
        - name: host-usr-src
          mountPath: "/host-usr/src"
          readOnly: true
        {{- end }}
        - name: features-d
          mountPath: "/etc/kubernetes/node-feature-discovery/features.d/"
          readOnly: true
        - name: nfd-worker-conf
          mountPath: "/etc/kubernetes/node-feature-discovery"
          readOnly: true
      volumes:
        - name: host-boot
          hostPath:
            path: "/boot"
        - name: host-os-release
          hostPath:
            path: "/etc/os-release"
        - name: host-sys
          hostPath:
            path: "/sys"
        - name: host-usr-lib
          hostPath:
            path: "/usr/lib"
        - name: host-lib
          hostPath:
            path: "/lib"
        - name: host-proc-swaps
          hostPath:
            path: "/proc/swaps"
        {{- if .Values.worker.mountUsrSrc }}
        - name: host-usr-src
          hostPath:
            path: "/usr/src"
        {{- end }}
        - name: features-d
          hostPath:
            path: "/etc/kubernetes/node-feature-discovery/features.d/"
        - name: nfd-worker-conf
          configMap:
            name: {{ include "node-feature-discovery.fullname" . }}-worker-conf
            items:
              - key: nfd-worker.conf
                path: nfd-worker.conf
      {{- with .Values.worker.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.worker.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.worker.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.worker.priorityClassName }}
      priorityClassName: {{ . | quote }}
    {{- end }}
{{- end }}
