{{- if .Values.topologyUpdater.enable -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "node-feature-discovery.fullname" . }}-topology-updater
  namespace: {{ include "node-feature-discovery.namespace" . }}
  labels:
    {{- include "node-feature-discovery.labels" . | nindent 4 }}
    role: topology-updater
  {{- with .Values.topologyUpdater.daemonsetAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  revisionHistoryLimit: {{ .Values.topologyUpdater.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "node-feature-discovery.selectorLabels" . | nindent 6 }}
      role: topology-updater
  template:
    metadata:
      labels:
        {{- include "node-feature-discovery.selectorLabels" . | nindent 8 }}
        role: topology-updater
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/nfd-topologyupdater-conf.yaml") . | sha256sum }}
        {{- with .Values.topologyUpdater.annotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "node-feature-discovery.topologyUpdater.serviceAccountName" . }}
      dnsPolicy: {{ .Values.topologyUpdater.dnsPolicy }}
    {{- with .Values.priorityClassName }}
      priorityClassName: {{ . }}
    {{- end }}
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      securityContext:
        {{- toYaml .Values.topologyUpdater.podSecurityContext | nindent 8 }}
      hostNetwork: {{ .Values.topologyUpdater.hostNetwork }}
      containers:
      - name: topology-updater
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: "{{ .Values.image.pullPolicy }}"
        livenessProbe:
          httpGet:
            path: /healthz
            port: http
        {{- with .Values.topologyUpdater.livenessProbe.initialDelaySeconds }}
          initialDelaySeconds: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.livenessProbe.failureThreshold }}
          failureThreshold: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.livenessProbe.periodSeconds }}
          periodSeconds: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.livenessProbe.timeoutSeconds }}
          timeoutSeconds: {{ . }}
        {{- end }}
        readinessProbe:
          httpGet:
            path: /healthz
            port: http
        {{- with .Values.topologyUpdater.readinessProbe.initialDelaySeconds }}
          initialDelaySeconds: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.readinessProbe.failureThreshold }}
          failureThreshold: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.readinessProbe.periodSeconds }}
          periodSeconds: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.readinessProbe.timeoutSeconds }}
          timeoutSeconds: {{ . }}
        {{- end }}
        {{- with .Values.topologyUpdater.readinessProbe.successThreshold }}
          successThreshold: {{ . }}
        {{- end }}
        env:
        - name: NODE_NAME
          valueFrom:
            fieldRef:
              fieldPath: spec.nodeName
        - name: NODE_ADDRESS
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
      {{- with .Values.topologyUpdater.extraEnvs }}
        {{- toYaml . | nindent 8 }}
      {{- end}}
        command:
          - "nfd-topology-updater"
        args:
          - "-podresources-socket=/host-var/lib/kubelet-podresources/kubelet.sock"
          {{- if .Values.topologyUpdater.updateInterval | empty | not }}
          - "-sleep-interval={{ .Values.topologyUpdater.updateInterval }}"
          {{- else }}
          - "-sleep-interval=3s"
          {{- end }}
          {{- if .Values.topologyUpdater.watchNamespace | empty | not }}
          - "-watch-namespace={{ .Values.topologyUpdater.watchNamespace }}"
          {{- else }}
          - "-watch-namespace=*"
          {{- end }}
          {{- if not .Values.topologyUpdater.podSetFingerprint }}
          - "-pods-fingerprint=false"
          {{- end }}
          {{- if .Values.topologyUpdater.kubeletConfigPath | empty | not }}
          - "-kubelet-config-uri=file:///host-var/kubelet-config"
          {{- end }}
          {{- if .Values.topologyUpdater.kubeletStateDir | empty }}
          # Disable kubelet state tracking by giving an empty path
          - "-kubelet-state-dir="
          {{- end }}
          - "-port={{ .Values.topologyUpdater.port | default "8080"}}"
          {{- with .Values.topologyUpdater.extraArgs }}
          {{- toYaml . | nindent 10 }}
          {{- end }}
        ports:
          - containerPort: {{ .Values.topologyUpdater.port | default "8080"}}
            name: http
        volumeMounts:
        {{- if .Values.topologyUpdater.kubeletConfigPath | empty | not }}
        - name: kubelet-config
          mountPath: /host-var/kubelet-config
        {{- end }}
        - name: kubelet-podresources-sock
          mountPath: /host-var/lib/kubelet-podresources/kubelet.sock
        - name: host-sys
          mountPath: /host-sys
        {{- if .Values.topologyUpdater.kubeletStateDir | empty | not }}
        - name: kubelet-state-files
          mountPath: /host-var/lib/kubelet
          readOnly: true
        {{- end }}
        - name: nfd-topology-updater-conf
          mountPath: "/etc/kubernetes/node-feature-discovery"
          readOnly: true

        resources:
      {{- toYaml .Values.topologyUpdater.resources | nindent 12 }}
        securityContext:
      {{- toYaml .Values.topologyUpdater.securityContext | nindent 12 }}
      volumes:
      - name: host-sys
        hostPath:
          path: "/sys"
      {{- if .Values.topologyUpdater.kubeletConfigPath | empty | not }}
      - name: kubelet-config
        hostPath:
          path: {{ .Values.topologyUpdater.kubeletConfigPath }}
      {{- end }}
      - name: kubelet-podresources-sock
        hostPath:
          {{- if .Values.topologyUpdater.kubeletPodResourcesSockPath | empty | not }}
          path: {{ .Values.topologyUpdater.kubeletPodResourcesSockPath }}
          {{- else }}
          path: /var/lib/kubelet/pod-resources/kubelet.sock
          {{- end }}
      {{- if .Values.topologyUpdater.kubeletStateDir | empty | not }}
      - name: kubelet-state-files
        hostPath:
          path: {{ .Values.topologyUpdater.kubeletStateDir }}
      {{- end }}
      - name: nfd-topology-updater-conf
        configMap:
          name: {{ include "node-feature-discovery.fullname" . }}-topology-updater-conf
          items:
            - key: nfd-topology-updater.conf
              path: nfd-topology-updater.conf

    {{- with .Values.topologyUpdater.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.topologyUpdater.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.topologyUpdater.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
{{- end }}
