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

{{- if .Values.defaultBackend.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ printf "%s-default-backend" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.defaultBackend.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: default-backend
  {{- 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.defaultBackend.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: default-backend
  replicas: {{ .Values.defaultBackend.replicaCount }}
  {{- if .Values.defaultBackend.updateStrategy }}
  strategy: {{- toYaml .Values.defaultBackend.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      {{- if or .Values.defaultBackend.podAnnotations .Values.commonAnnotations }}
      {{- $podAnnotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.defaultBackend.podAnnotations .Values.commonAnnotations ) "context" . ) }}
      annotations: {{- include "common.tplvalues.render" (dict "value" $podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: default-backend
    spec:
      {{- include "common.images.pullSecrets" ( dict "images" (list .Values.defaultBackend.image) "global" .Values.global) | nindent 6 }}
      {{- if .Values.defaultBackend.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.defaultBackend.priorityClassName }}
      priorityClassName: {{ .Values.defaultBackend.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.defaultBackend.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.defaultBackend.podAffinityPreset "component" "default-backend" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.defaultBackend.podAntiAffinityPreset "component" "default-backend" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.defaultBackend.nodeAffinityPreset.type "key" .Values.defaultBackend.nodeAffinityPreset.key "values" .Values.defaultBackend.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.defaultBackend.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.defaultBackend.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.defaultBackend.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.defaultBackend.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: false
      serviceAccountName: {{ include "envoy.envoyServiceAccountName" . }}
      {{- if .Values.defaultBackend.schedulerName }}
      schedulerName: {{ .Values.defaultBackend.schedulerName | quote }}
      {{- end }}
      {{- if .Values.defaultBackend.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.defaultBackend.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.defaultBackend.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.defaultBackend.initContainers }}
      initContainers: {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.initContainers "context" $ ) | nindent 6 }}
      {{- end }}
      containers:
        - name: default-backend
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.defaultBackend.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.defaultBackend.image.pullPolicy | quote }}
          {{- if .Values.defaultBackend.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.defaultBackend.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultBackend.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultBackend.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.args "context" $) | nindent 12 }}
          {{- else }}
          args:
          {{- range $key, $value := .Values.defaultBackend.extraArgs }}
            {{- if $value }}
            - --{{ $key }}={{ $value }}
            {{- else }}
            - --{{ $key }}
            {{- end }}
          {{- end }}
          {{- end }}
          {{- if .Values.defaultBackend.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultBackend.extraEnvVars }}
          env: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.extraEnvVars "context" $) | nindent 12 }}
          {{- end }}
          {{- if or .Values.defaultBackend.extraEnvVarsCM .Values.defaultBackend.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.defaultBackend.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.defaultBackend.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          {{- if .Values.defaultBackend.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.defaultBackend.livenessProbe.enabled }}
          livenessProbe:
            tcpSocket:
              port: http
            initialDelaySeconds: {{ .Values.defaultBackend.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.defaultBackend.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.defaultBackend.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.defaultBackend.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.defaultBackend.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.defaultBackend.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.defaultBackend.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: http
              scheme: HTTP
            initialDelaySeconds: {{ .Values.defaultBackend.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.defaultBackend.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.defaultBackend.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.defaultBackend.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.defaultBackend.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.defaultBackend.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.defaultBackend.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.defaultBackend.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /
              port: http
              scheme: HTTP
            initialDelaySeconds: {{ .Values.defaultBackend.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.defaultBackend.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.defaultBackend.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.defaultBackend.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.defaultBackend.startupProbe.failureThreshold }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.defaultBackend.containerPorts.http }}
              protocol: TCP
          {{- if .Values.defaultBackend.resources }}
          resources: {{- toYaml .Values.defaultBackend.resources | nindent 12 }}
          {{- else if ne .Values.defaultBackend.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.defaultBackend.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if .Values.defaultBackend.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.extraVolumeMounts "context" $ ) | nindent 12 }}
          {{- end }}
        {{- if .Values.defaultBackend.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.defaultBackend.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" ( dict "value" .Values.defaultBackend.extraVolumes "context" $ ) | nindent 6 }}
      {{- end }}
{{- end }}
