{{- 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:
  selector:
    matchLabels:
      {{- include "node-feature-discovery.selectorLabels" . | nindent 6 }}
      role: worker
  template:
    metadata:
      labels:
        {{- include "node-feature-discovery.selectorLabels" . | nindent 8 }}
        role: worker
      {{- with .Values.worker.annotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      dnsPolicy: ClusterFirstWithHostNet
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "node-feature-discovery.worker.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.worker.podSecurityContext | nindent 8 }}
      containers:
        - name: worker
          securityContext:
          {{- toYaml .Values.worker.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          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
          resources:
        {{- toYaml .Values.worker.resources | nindent 12 }}
          command:
            - "nfd-worker"
          args:
        {{- if not .Values.enableNodeFeatureApi }}
            - "-server={{ include "node-feature-discovery.fullname" . }}-master:{{ .Values.master.service.port }}"
            - "-enable-nodefeature-api=false"
        {{- end }}
{{- if .Values.tls.enable }}
            - "-ca-file=/etc/kubernetes/node-feature-discovery/certs/ca.crt"
            - "-key-file=/etc/kubernetes/node-feature-discovery/certs/tls.key"
            - "-cert-file=/etc/kubernetes/node-feature-discovery/certs/tls.crt"
{{- end }}
            - "-metrics={{ .Values.worker.metricsPort | default "8081"}}"
          ports:
            - name: metrics
              containerPort: {{ .Values.worker.metricsPort | default "8081"}}
          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
        {{- if .Values.worker.mountUsrSrc }}
            - name: host-usr-src
              mountPath: "/host-usr/src"
              readOnly: true
        {{- end }}
            - name: source-d
              mountPath: "/etc/kubernetes/node-feature-discovery/source.d/"
              readOnly: true
            - 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
{{- if .Values.tls.enable }}
            - name: nfd-worker-cert
              mountPath: "/etc/kubernetes/node-feature-discovery/certs"
              readOnly: true
{{- end }}
      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"
        {{- if .Values.worker.mountUsrSrc }}
        - name: host-usr-src
          hostPath:
            path: "/usr/src"
        {{- end }}
        - name: source-d
          hostPath:
            path: "/etc/kubernetes/node-feature-discovery/source.d/"
        - 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
{{- if .Values.tls.enable }}
        - name: nfd-worker-cert
          secret:
            secretName: nfd-worker-cert
{{- end }}
    {{- 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 }}