apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "node-problem-detector.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "node-problem-detector.name" . }}
    helm.sh/chart: {{ include "node-problem-detector.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    {{- range $key, $val := .Values.labels }}
    {{ $key }}: {{ $val | quote }}
    {{- end}}
spec:
  updateStrategy:
    type: {{ .Values.updateStrategy }}
{{- if eq .Values.updateStrategy "RollingUpdate"}}
    rollingUpdate:
      maxUnavailable: {{ .Values.maxUnavailable }}
{{- end}}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "node-problem-detector.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app: {{ include "node-problem-detector.name" . }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "node-problem-detector.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app: {{ include "node-problem-detector.name" . }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/custom-config-configmap.yaml") . | sha256sum }}
        scheduler.alpha.kubernetes.io/critical-pod: ''
{{- if .Values.annotations }}
{{ toYaml .Values.annotations | indent 8 }}
{{- end }}
    spec:
      serviceAccountName: {{ template "node-problem-detector.serviceAccountName" . }}
      hostNetwork: {{ .Values.hostNetwork }}
      terminationGracePeriodSeconds: 30
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image:  "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy | default "IfNotPresent" | quote }}
          command:
            - "/bin/sh"
            - "-c"
            - "exec /node-problem-detector --logtostderr --config.system-log-monitor={{- range $index, $monitor := .Values.settings.log_monitors }}{{if ne $index 0}},{{end}}{{ $monitor }}{{- end }} {{- if .Values.settings.custom_plugin_monitors }} --custom-plugin-monitors={{- range $index, $monitor := .Values.settings.custom_plugin_monitors }}{{if ne $index 0}},{{end}}{{ $monitor }}{{- end }} {{- end }} --prometheus-address={{ .Values.settings.prometheus_address }} --prometheus-port={{ .Values.settings.prometheus_port }} --k8s-exporter-heartbeat-period={{ .Values.settings.heartBeatPeriod }}"
{{- if .Values.securityContext }}
          securityContext:
{{ toYaml .Values.securityContext | indent 12 }}
{{- end }}
          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  fieldPath: spec.nodeName
{{- if .Values.env }}
{{ toYaml .Values.env | indent 12 }}
{{- end }}
          volumeMounts:
            - name: log
              mountPath: {{ .Values.hostpath.logdir }}
            - name: localtime
              mountPath: /etc/localtime
              readOnly: true
            - name: custom-config
              mountPath: /custom-config
              readOnly: true
{{- if .Values.extraVolumeMounts }}
{{ toYaml .Values.extraVolumeMounts | indent 12 }}
{{- end }}
          ports:
            - containerPort: {{ .Values.settings.prometheus_port }}
              name: exporter
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
{{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
{{- end }}
      volumes:
        - name: log
          hostPath:
            path: {{ .Values.hostpath.logdir }}
        - name: localtime
          hostPath:
            path: /etc/localtime
            type: "FileOrCreate"
        - name: custom-config
          configMap:
            name: {{ include "node-problem-detector.customConfig" . }}
{{- if .Values.extraVolumes }}
{{ toYaml .Values.extraVolumes | indent 8 }}
{{- end }}
