apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ include "node-local-dns.fullname" . }}
  namespace: kube-system
  {{- with .Values.daemonsetAnnotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    {{- include "node-local-dns.labels" . | nindent 4 }}
  {{- with .Values.daemonsetLabels }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
  updateStrategy:
    rollingUpdate:
      maxUnavailable: 10%
  selector:
    matchLabels:
      k8s-app: {{ include "node-local-dns.name" . }}
  template:
    metadata:
      labels:
      {{- if .Values.podLabels }}
        {{- toYaml .Values.podLabels | nindent 8 }}
      {{- end }}
        k8s-app: {{ include "node-local-dns.name" . }}
      {{- if or .Values.podAnnotations (not .Values.serviceMonitor.enabled) }}
      annotations:
      {{- end }}
         {{- if and (not .Values.serviceMonitor.enabled) .Values.prometheusScraping.enabled }}
        prometheus.io/port: "9253"
        prometheus.io/scrape: "true"
        {{- end }}
        {{- if .Values.podAnnotations }}
          {{- toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.affinity }}
      affinity: {{- toYaml .Values.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets: {{ toYaml .Values.imagePullSecrets | nindent 8 }}
      {{- end }}
      priorityClassName: system-node-critical
      serviceAccountName: {{ include "node-local-dns.serviceAccountName" . }}
      hostNetwork: {{ .Values.config.setupIptables }}
      dnsPolicy: Default  # Don't use cluster DNS.
      tolerations:
      - key: "CriticalAddonsOnly"
        operator: "Exists"
      - effect: "NoExecute"
        operator: "Exists"
      - effect: "NoSchedule"
        operator: "Exists"
      containers:
      - name: node-cache
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        {{- with .Values.resources }}
        resources: {{- toYaml . | nindent 10 }}
        {{- end }}
        args:
        {{- if .Values.config.setupIptables }}
          - "-localip"
          - "{{ .Values.config.localDns }},{{ .Values.config.dnsServer }}"
        {{- else }}
          - "-localip"
          - "{{ .Values.config.localDns }}"
        {{- end }}
          - "-conf"
          - "/etc/Corefile"
          - "-upstreamsvc"
        {{- if .Values.config.customUpstreamsvc }}
          - "{{ .Values.config.customUpstreamsvc }}"
        {{- else }}
          - "{{ include "node-local-dns.fullname" . }}-upstream"
        {{- end }}
          - "-skipteardown={{ .Values.config.skipTeardown }}"
          - "-setupinterface={{ .Values.config.setupInterface }}"
          - "-setupiptables={{ .Values.config.setupIptables }}"
        {{- if .Values.config.setupIptables }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 10 }}
        {{- end }}
        ports:
        - containerPort: 53
          name: dns
          protocol: UDP
        - containerPort: 53
          name: dns-tcp
          protocol: TCP
        - containerPort: 9253
          name: metrics
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /health
            port: {{ .Values.config.healthPort }}
          initialDelaySeconds: 60
          timeoutSeconds: 5
        volumeMounts:
        - mountPath: /run/xtables.lock
          name: xtables-lock
          readOnly: false
        - name: config-volume
          mountPath: /etc/coredns
        - name: kube-dns-config
          mountPath: /etc/kube-dns
      volumes:
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate
      - name: kube-dns-config
        configMap:
          name: {{ include "node-local-dns.fullname" . }}
          optional: true
      - name: config-volume
        configMap:
          name: {{ include "node-local-dns.fullname" . }}
          items:
            - key: Corefile
              path: Corefile.base
