{{- if eq .Values.kind "DaemonSet" }}
{{- $useHostPort := .Values.daemonset.useHostPort -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "nginx-ingress.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "nginx-ingress.labels" . | nindent 4 }}
    component: {{ .Values.name }}
    {{- if .Values.commonLabels }}
    {{- include "nginx-ingress.tplValue" (dict "value" .Values.commonLabels "context" $) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations:
    {{- range $key, $value := .Values.commonAnnotations }}
    {{ $key }}: {{ include "nginx-ingress.tplValue" (dict "value" $value "context" $) | quote }}
    {{- end }}
  {{- end }}
spec:
  selector:
    matchLabels: {{- include "nginx-ingress.matchLabels" . | nindent 6 }}
      component: {{ .Values.name }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  minReadySeconds: {{ .Values.minReadySeconds }}
  template:
    metadata:
      {{- if .Values.podAnnotations }}
      annotations: {{- include "nginx-ingress.annotations" (dict "annotations" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "nginx-ingress.labels" . | nindent 8 }}
        component: {{ .Values.name }}
        {{- if .Values.podLabels }}
        {{- include "nginx-ingress.tplValue" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
{{- include "nginx-ingress-controller.imagePullSecrets" . | indent 6 }}
      dnsPolicy: {{ .Values.dnsPolicy }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "nginx-ingress.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "nginx-ingress.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "nginx-ingress.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.hostNetwork }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{ toYaml .Values.topologySpreadConstraints | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "nginx-ingress.serviceAccountName" . }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- if .Values.extraInitContainers }}
      initContainers: {{- toYaml .Values.extraInitContainers | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ template "nginx-ingress.name" . }}-{{ .Values.name }}
          image: {{ template "nginx-ingress-controller.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.lifecycle }}
          lifecycle: {{- toYaml .Values.lifecycle | nindent 12 }}
          {{- end }}
          args:
            - /nginx-ingress-controller
            - --default-backend-service={{ if .Values.defaultBackend.enabled }}{{ .Release.Namespace }}/{{ template "nginx-ingress.defaultBackend.fullname" . }}{{ else }}{{ .Values.defaultBackendService }}{{ end }}
          {{- if .Values.publishService.enabled }}
            - --publish-service={{ template "nginx-ingress.publishServicePath" . }}
          {{- end }}
            - --election-id={{ .Values.electionID }}
            - --ingress-class={{ .Values.ingressClass }}
            - --configmap={{ .Release.Namespace }}/{{ template "nginx-ingress.fullname" . }}
          {{- 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.scope.enabled }}
            - --watch-namespace={{ default .Release.Namespace .Values.scope.namespace }}
          {{- end }}
          {{- if .Values.maxmindLicenseKey }}
            - --maxmind-license-key={{ .Values.maxmindLicenseKey }}
          {{- end }}
          {{- if and (.Values.reportNodeInternalIp) (.Values.hostNetwork)}}
            - --report-node-internal-ip-address={{ .Values.reportNodeInternalIp }}
          {{- end }}
          {{- range $key, $value := .Values.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
          {{- end }}
          securityContext:
            capabilities:
              drop: ["ALL"]
              add: ["NET_BIND_SERVICE"]
            runAsUser: {{ .Values.securityContext.runAsUser }}
            allowPrivilegeEscalation: {{ .Values.image.allowPrivilegeEscalation }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.extraEnvs }}
            {{- toYaml .Values.extraEnvs | nindent 12 }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.livenessProbe.port }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.readinessProbe.port }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPort.http }}
              protocol: TCP
              {{- if .Values.daemonset.useHostPort }}
              hostPort: {{ .Values.daemonset.hostPorts.http }}
              {{- end }}
            - name: https
              containerPort: {{ .Values.containerPort.https }}
              protocol: TCP
              {{- if .Values.daemonset.useHostPort }}
              hostPort: {{ .Values.daemonset.hostPorts.https }}
              {{- end }}
            {{- if .Values.metrics.enabled }}
            - name: metrics
              containerPort: 10254
              protocol: TCP
            {{- end }}
            {{- range $key, $value := .Values.tcp }}
            - name: "{{ $key }}-tcp"
              containerPort: {{ $key }}
              protocol: TCP
              {{- if $useHostPort }}
              hostPort: {{ $key }}
              {{- end }}
            {{- end }}
            {{- range $key, $value := .Values.udp }}
            - name: "{{ $key }}-udp"
              containerPort: {{ $key }}
              protocol: UDP
              {{- if $useHostPort }}
              hostPort: {{ $key }}
              {{- end }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          {{- if (or .Values.customTemplate.configMapName .Values.extraVolumeMounts) }}
          volumeMounts:
            {{- if .Values.customTemplate.configMapName }}
            - mountPath: /etc/nginx/template
              name: nginx-template-volume
              readOnly: true
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
          {{- end }}
        {{- if .Values.extraContainers }}
        {{- toYaml .Values.extraContainers | nindent 8 }}
        {{- end }}
      {{- if (or .Values.customTemplate.configMapName .Values.extraVolumes) }}
      volumes:
        {{- if .Values.customTemplate.configMapName }}
        - name: nginx-template-volume
          configMap:
            name: {{ .Values.customTemplate.configMapName }}
            items:
              - key: {{ .Values.customTemplate.configMapKey }}
                path: nginx.tmpl
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
      {{- end }}
{{- end }}
