{{- if eq .Values.controller.kind "DaemonSet" }}
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  labels:
    app: {{ template "nginx-ingress.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    component: "{{ .Values.controller.name }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "nginx-ingress.controller.fullname" . }}
spec:
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  updateStrategy:
{{ toYaml .Values.controller.updateStrategy | indent 4 }}
  minReadySeconds: {{ .Values.controller.minReadySeconds }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/controller-configmap.yaml") . | sha256sum }}
    {{- if .Values.controller.podAnnotations }}
{{ toYaml .Values.controller.podAnnotations | indent 8}}
    {{- end }}
      labels:
        app: {{ template "nginx-ingress.name" . }}
        component: "{{ .Values.controller.name }}"
        release: {{ .Release.Name }}
        {{- if .Values.controller.podLabels }}
{{ toYaml .Values.controller.podLabels | indent 8}}
        {{- end }}
    spec:
      dnsPolicy: {{ .Values.controller.dnsPolicy }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
{{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
{{- if .Values.controller.priorityClassName }}
      priorityClassName: "{{ .Values.controller.priorityClassName }}"
{{- end }}
      containers:
        - name: {{ template "nginx-ingress.name" . }}-{{ .Values.controller.name }}
          image: "{{ .Values.controller.image.repository }}:{{ .Values.controller.image.tag }}"
          imagePullPolicy: "{{ .Values.controller.image.pullPolicy }}"
          {{- if .Values.controller.lifecycle }}
          lifecycle:
{{ toYaml .Values.controller.lifecycle | indent 12 }}
          {{- end }}
          args:
            - /nginx-ingress-controller
            - --default-backend-service={{ if .Values.defaultBackend.enabled }}{{ .Release.Namespace }}/{{ template "nginx-ingress.defaultBackend.fullname" . }}{{ else }}{{ .Values.controller.defaultBackendService }}{{ end }}
          {{- if and (semverCompare ">=0.9.0-beta.1" .Values.controller.image.tag) .Values.controller.publishService.enabled }}
            - --publish-service={{ template "nginx-ingress.controller.publishServicePath" . }}
          {{- end }}
          {{- if (semverCompare ">=0.9.0-beta.1" .Values.controller.image.tag) }}
            - --election-id={{ .Values.controller.electionID }}
          {{- end }}
          {{- if (semverCompare ">=0.9.0-beta.1" .Values.controller.image.tag) }}
            - --ingress-class={{ .Values.controller.ingressClass }}
          {{- end }}
          {{- if (semverCompare ">=0.9.0-beta.1" .Values.controller.image.tag) }}
            - --configmap={{ .Release.Namespace }}/{{ template "nginx-ingress.controller.fullname" . }}
          {{- else }}
            - --nginx-configmap={{ .Release.Namespace }}/{{ template "nginx-ingress.controller.fullname" . }}
          {{- end }}
          {{- if .Values.tcp }}
            - --tcp-services-configmap={{ .Release.Namespace }}/{{ template "nginx-ingress.fullname" . }}-tcp
          {{- end }}
          {{- if .Values.udp }}
            - --udp-services-configmap={{ .Release.Namespace }}/{{ template "nginx-ingress.fullname" . }}-udp
          {{- end }}
          {{- if .Values.controller.scope.enabled }}
            - --watch-namespace={{ default .Release.Namespace .Values.controller.scope.namespace }}
          {{- end }}
          {{- range $key, $value := .Values.controller.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
          {{- end }}
          {{- if (semverCompare ">=0.16.0" .Values.controller.image.tag) }}
          securityContext:
            capabilities:
                drop:
                - ALL
                add:
                - NET_BIND_SERVICE
            # www-data -> 33
            runAsUser: 33
          {{- end }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
          {{- if .Values.controller.extraEnvs }}
{{ toYaml .Values.controller.extraEnvs | indent 12 }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.controller.livenessProbe.port }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.controller.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.livenessProbe.failureThreshold }}
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
              {{- if .Values.controller.daemonset.useHostPort }}
              hostPort: {{ .Values.controller.daemonset.hostPorts.http }}
              {{- end }}
            - name: https
              containerPort: 443
              protocol: TCP
              {{- if .Values.controller.daemonset.useHostPort }}
              hostPort: {{ .Values.controller.daemonset.hostPorts.https }}
              {{- end }}
          {{- if .Values.controller.stats.enabled }}
            - name: stats
              containerPort: 18080
              protocol: TCP
            {{- if .Values.controller.metrics.enabled }}
            - name: metrics
              containerPort: 10254
              protocol: TCP
            {{- end }}
          {{- end }}
          {{- range $key, $value := .Values.tcp }}
            - name: "{{ $key }}-tcp"
              containerPort: {{ $key }}
              protocol: TCP
          {{- end }}
          {{- range $key, $value := .Values.udp }}
            - name: "{{ $key }}-udp"
              containerPort: {{ $key }}
              protocol: UDP
          {{- end }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.controller.readinessProbe.port }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.controller.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.controller.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.controller.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.controller.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.controller.readinessProbe.failureThreshold }}
{{- if (or .Values.controller.customTemplate.configMapName .Values.controller.extraVolumeMounts) }}
          volumeMounts:
{{- end }}
{{- if .Values.controller.customTemplate.configMapName }}
            - mountPath: /etc/nginx/template
              name: nginx-template-volume
              readOnly: true
{{- end }}
{{- if .Values.controller.extraVolumeMounts }}
{{ toYaml .Values.controller.extraVolumeMounts | indent 10}}
{{- end }}
          resources:
{{ toYaml .Values.controller.resources | indent 12 }}
{{- if .Values.controller.extraContainers }}
{{ toYaml .Values.controller.extraContainers | indent 8}}
{{- end }}
{{- if .Values.controller.extraInitContainers }}
      initContainers:
{{ toYaml .Values.controller.extraInitContainers | indent 8}}
{{- end }}
      hostNetwork: {{ .Values.controller.hostNetwork }}
    {{- if .Values.controller.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.controller.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.controller.tolerations }}
      tolerations:
{{ toYaml .Values.controller.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.controller.affinity }}
      affinity:
{{ toYaml .Values.controller.affinity | indent 8 }}
    {{- end }}
      serviceAccountName: {{ template "nginx-ingress.serviceAccountName" . }}
      terminationGracePeriodSeconds: 60
{{- if (or .Values.controller.customTemplate.configMapName .Values.controller.extraVolumes) }}
      volumes:
{{- end }}
{{- if .Values.controller.customTemplate.configMapName }}
        - name: nginx-template-volume
          configMap:
            name: {{ .Values.controller.customTemplate.configMapName }}
            items:
            - key: {{ .Values.controller.customTemplate.configMapKey }}
              path: nginx.tmpl
{{- end }}
{{- if .Values.controller.extraVolumes }}
{{ toYaml .Values.controller.extraVolumes | indent 6}}
{{- end }}
{{- end }}
