{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if eq .Values.kind "DaemonSet" }}
{{- $useHostPort := .Values.daemonset.useHostPort -}}
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: controller
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: controller
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  {{- if .Values.updateStrategy }}
  updateStrategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  minReadySeconds: {{ .Values.minReadySeconds }}
  template:
    metadata:
      {{- if .Values.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.podAnnotations "context" $ ) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: controller
    spec:
      {{- include "nginx-ingress-controller.imagePullSecrets" . | nindent 6 }}
      dnsPolicy: {{ .Values.dnsPolicy }}
      {{- if .Values.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.dnsConfig "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "controller" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.hostNetwork }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- toYaml .Values.topologySpreadConstraints | nindent 8 }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      serviceAccountName: {{ template "nginx-ingress-controller.serviceAccountName" . }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      initContainers:
        - name: prepare-nginx-folder
          image: {{ include "nginx-ingress-controller.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              #!/bin/bash
              cp -r /etc/nginx/* /nginx
              cp -r /etc/ingress-controller/* /ingress-controller
          volumeMounts:
            - name: empty-dir
              mountPath: /nginx
              subPath: app-nginx-dir
            - name: empty-dir
              mountPath: /ingress-controller
              subPath: app-controller-dir
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: controller
          image: {{ include "nginx-ingress-controller.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --default-backend-service={{ ternary (printf "$(POD_NAMESPACE)/%s" (include "nginx-ingress-controller.defaultBackend.fullname" .)) .Values.defaultBackendService .Values.defaultBackend.enabled }}
          {{- if .Values.publishService.enabled }}
            - --publish-service={{ include "nginx-ingress-controller.publishServicePath" . }}
          {{- end }}
            - --http-port={{ .Values.containerPorts.http }}
            - --https-port={{ .Values.containerPorts.https }}
            - --healthz-port={{ .Values.containerPorts.metrics }}
            - --election-id={{ .Values.electionID }}
            - --controller-class={{ .Values.ingressClassResource.controllerClass }}
            - --configmap={{ default "$(POD_NAMESPACE)" .Values.configMapNamespace }}/{{ include "common.names.fullname" . }}
          {{- if .Values.tcp }}
            - --tcp-services-configmap={{ default "$(POD_NAMESPACE)" .Values.tcpConfigMapNamespace }}/{{ include "common.names.fullname" . }}-tcp
          {{- end }}
          {{- if .Values.udp }}
            - --udp-services-configmap={{ default "$(POD_NAMESPACE)" .Values.udpConfigMapNamespace }}/{{ include "common.names.fullname" . }}-udp
          {{- end }}
          {{- if .Values.scope.enabled }}
            - --watch-namespace={{ default "$(POD_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 }}
          {{- if .Values.watchIngressWithoutClass }}
            - --watch-ingress-without-class=true
          {{- end }}
          {{- if .Values.metrics.enabled }}
            - --enable-metrics
          {{- end }}
          {{- range $key, $value := .Values.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
          {{- end }}
          {{- end }}
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: metrics
              scheme: HTTP
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: metrics
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
              protocol: TCP
              {{- if $useHostPort }}
              hostPort: {{ .Values.daemonset.hostPorts.http }}
              {{- end }}
            - name: https
              containerPort: {{ .Values.containerPorts.https }}
              protocol: TCP
              {{- if $useHostPort }}
              hostPort: {{ .Values.daemonset.hostPorts.https }}
              {{- end }}
            - name: metrics
              containerPort: {{ .Values.containerPorts.metrics }}
              protocol: TCP
            {{- 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 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.customTemplate.configMapName }}
            - mountPath: /etc/nginx/template
              name: nginx-template-volume
              readOnly: true
            {{- end }}
            - name: empty-dir
              mountPath: /etc/nginx
              subPath: app-nginx-dir
            - name: empty-dir
              mountPath: /etc/ingress-controller
              subPath: app-controller-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /tmp/nginx
              subPath: app-tmp-dir
            {{- if .Values.maxmindLicenseKey }}
            - name: empty-dir
              mountPath: /etc/ingress-controller/geoip
              subPath: app-geoip-dir
            {{- end }}
            - name: empty-dir
              mountPath: /etc/ingress-controller/telemetry
              subPath: app-telemetry-dir
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        {{- 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 }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
