apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "aws-node-termination-handler.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "aws-node-termination-handler.labels" . | indent 4 }}
spec:
  updateStrategy:
{{ toYaml .Values.updateStrategy | indent 4 }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "aws-node-termination-handler.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      {{- if .Values.podAnnotations }}
      annotations:
      {{- range $key, $value := .Values.podAnnotations }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
      {{- end }}
      labels:
        app.kubernetes.io/name: {{ include "aws-node-termination-handler.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        k8s-app: aws-node-termination-handler
    spec:
      volumes:
        - name: "uptime"
          hostPath:
            path: "{{ .Values.procUptimeFile }}"
      priorityClassName: "{{ .Values.priorityClassName }}"
      affinity:
        nodeAffinity:
          # NOTE(jaypipes): Change when we complete
          # https://github.com/aws/aws-node-termination-handler/issues/8
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: {{ .Values.nodeSelectorTermsOs | default "beta.kubernetes.io/os" | quote }}
                    operator: In
                    values:
                      - linux
                  - key: {{ .Values.nodeSelectorTermsArch | default "beta.kubernetes.io/arch" | quote }}
                    operator: In
                    values:
                      - amd64
                      - arm
                      - arm64
      serviceAccountName: {{ template "aws-node-termination-handler.serviceAccountName" . }}
      hostNetwork: true
      dnsPolicy: {{ .Values.dnsPolicy }}
      containers:
        - name: {{ include "aws-node-termination-handler.name" . }}
          image: {{ .Values.image.repository}}:{{ .Values.image.tag }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            readOnlyRootFilesystem: true
            runAsNonRoot: true
            runAsUser: {{ .Values.securityContext.runAsUserID }}
            runAsGroup: {{ .Values.securityContext.runAsGroupID }}
            allowPrivilegeEscalation: false
          volumeMounts:
            - name: "uptime"
              mountPath: "/proc/uptime"
              readOnly: true
          env:
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: spec.nodeName
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: SPOT_POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: DELETE_LOCAL_DATA
            value: {{ .Values.deleteLocalData | quote }}
          - name: IGNORE_DAEMON_SETS
            value: {{ .Values.ignoreDaemonSets | quote }}
          - name: GRACE_PERIOD
            value: {{ .Values.gracePeriod | quote }}
          - name: POD_TERMINATION_GRACE_PERIOD
            value: {{ .Values.podTerminationGracePeriod | quote }}
          - name: INSTANCE_METADATA_URL
            value: {{ .Values.instanceMetadataURL | quote }}
          - name: NODE_TERMINATION_GRACE_PERIOD
            value: {{ .Values.nodeTerminationGracePeriod | quote }}
          - name: WEBHOOK_URL
            value: {{ .Values.webhookURL | quote }}
          - name: WEBHOOK_HEADERS
            value: {{ .Values.webhookHeaders | quote }}
          - name: WEBHOOK_TEMPLATE
            value: {{ .Values.webhookTemplate | quote }}
          - name: DRY_RUN
            value: {{ .Values.dryRun | quote }}
          - name: ENABLE_SPOT_INTERRUPTION_DRAINING
            value: {{ .Values.enableSpotInterruptionDraining | quote }}
          - name: ENABLE_SCHEDULED_EVENT_DRAINING
            value: {{ .Values.enableScheduledEventDraining | quote }}
          - name: METADATA_TRIES
            value: {{ .Values.metadataTries | quote }}
          - name: CORDON_ONLY
            value: {{ .Values.cordonOnly | quote }}
          - name: TAINT_NODE
            value: {{ .Values.taintNode | quote }}
          - name: JSON_LOGGING
            value: {{ .Values.jsonLogging | quote }}
          - name: WEBHOOK_PROXY
            value: {{ .Values.webhookProxy | quote }}
          - name: ENABLE_PROMETHEUS_SERVER
            value: {{ .Values.enablePrometheusServer | quote }}
          - name: PROMETHEUS_SERVER_PORT
            value: {{ .Values.prometheusServerPort | quote }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
