apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "pod-cleaner.fullname" . }}
  labels:
    {{- include "pod-cleaner.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels:
      {{- include "pod-cleaner.selectorLabels" . | nindent 6 }}
  {{- if .Values.updateStrategy }}
  updateStrategy:
    {{ toYaml .Values.updateStrategy | nindent 4 | trim }}
  {{- end }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "pod-cleaner.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.image.pullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "pod-cleaner.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args:
            - "--metric-path={{ .Values.podMonitor.path }}"
            - "--metric-port=0.0.0.0:{{ .Values.service.port }}"
            - "--log-file={{ .Values.podCleaner.logfile }}"
            - "--log={{ .Values.podCleaner.logLevel }}"
          ports:
            - name: {{ include "pod-cleaner.metrics.portName" . }}
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            httpGet:
              path: {{ .Values.podMonitor.path }}
              port: {{ include "pod-cleaner.metrics.portName" . }}
          readinessProbe:
            httpGet:
              path: {{ .Values.podMonitor.path }}
              port: {{ include "pod-cleaner.metrics.portName" . }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- if or .Values.nodeAffinity }}
      affinity:
        {{- toYaml .Values.nodeAffinity | nindent 8 }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{ tpl (toYaml . ) $ | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- tpl (toYaml . ) $ | nindent 8 }}
      {{- end }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{ tpl (toYaml . ) $ | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "pod-cleaner.serviceAccountName" . }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
