{{- if .Values.contour.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ printf "%s-contour" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: contour
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.contour.replicaCount }}
  {{- if .Values.contour.updateStrategy }}
  strategy: {{- toYaml .Values.contour.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: contour
  template:
    metadata:
      {{- if or .Values.configInline .Values.contour.podAnnotations .Values.commonAnnotations }}
      annotations:
        {{- if .Values.contour.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.contour.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.configInline }}
        checksum/config: {{ include (print $.Template.BasePath "/contour/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.commonAnnotations }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 8 }}
        {{- end }}
      {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: contour
        {{- if .Values.contour.podLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.contour.podLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "common.images.pullSecrets" ( dict "images" (list .Values.contour.image) "global" .Values.global) | nindent 6 }}
      {{- if .Values.contour.priorityClassName }}
      priorityClassName: {{ .Values.contour.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.contour.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.contour.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.contour.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.contour.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.contour.podAffinityPreset "component" "contour" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.contour.podAntiAffinityPreset "component" "contour" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.contour.nodeAffinityPreset.type "key" .Values.contour.nodeAffinityPreset.key "values" .Values.contour.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.contour.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.contour.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.contour.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.contour.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.contour.schedulerName }}
      schedulerName: {{ .Values.contour.schedulerName | quote }}
      {{- end }}
      {{- if .Values.contour.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.contour.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.contour.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.contour.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.contour.initContainers }}
      initContainers: {{- include "common.tplvalues.render" ( dict "value" .Values.contour.initContainers "context" $ ) | nindent 6 }}
      {{- end }}
      containers:
        - name: contour
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.contour.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.contour.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - contour
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.contour.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.contour.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - serve
            - --incluster
            - --xds-address=0.0.0.0
            - --xds-port={{ .Values.contour.containerPorts.xds }}
            - --http-port={{ .Values.contour.containerPorts.metrics }}
            - --envoy-service-http-port={{ .Values.envoy.containerPorts.http }}
            - --envoy-service-https-port={{ .Values.envoy.containerPorts.https }}
            - --contour-cafile=/certs/ca.crt
            - --contour-cert-file=/certs/tls.crt
            - --contour-key-file=/certs/tls.key
            - --config-path=/config/contour.yaml
            - --envoy-service-name={{ printf "%s-envoy" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
            - --leader-election-resource-name={{ include "common.names.namespace" . }}
            {{- if .Values.contour.debug }}
            - --debug
            {{- end }}
            - --kubernetes-debug={{ .Values.contour.kubernetesDebug }}
            {{- if (include "contour.isIngressClassNameDefined" .) }}
            - --ingress-class-name={{ include "contour.ingressClassName" . }}
            {{- end }}
            {{- if .Values.contour.rootNamespaces }}
            - --root-namespaces={{ .Values.contour.rootNamespaces }}
            {{- end }}
            {{- if .Values.contour.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.contour.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          image: {{ include "common.images.image" ( dict "imageRoot" .Values.contour.image "global" .Values.global) }}
          imagePullPolicy: {{ .Values.contour.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.contour.containerPorts.xds }}
              name: xds
              protocol: TCP
            - containerPort: {{ .Values.contour.containerPorts.metrics }}
              name: metrics
              protocol: TCP
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.contour.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.contour.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.contour.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.contour.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.contour.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.contour.containerPorts.metrics }}
            initialDelaySeconds: {{ .Values.contour.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.contour.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.contour.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.contour.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.contour.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.contour.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.contour.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.contour.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.contour.containerPorts.metrics }}
            initialDelaySeconds: {{ .Values.contour.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.contour.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.contour.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.contour.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.contour.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.contour.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.contour.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.contour.startupProbe.enabled }}
          startupProbe:
            httpGet:
              path: /healthz
              port: {{ .Values.contour.containerPorts.metrics }}
            initialDelaySeconds: {{ .Values.contour.startupProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.contour.startupProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.contour.startupProbe.timeoutSeconds }}
            successThreshold: {{ .Values.contour.startupProbe.successThreshold }}
            failureThreshold: {{ .Values.contour.startupProbe.failureThreshold }}
          {{- end }}
          {{- end }}
          resources: {{ toYaml .Values.contour.resources | nindent 12 }}
          volumeMounts:
            - name: contourcert
              mountPath: /certs
              readOnly: true
            - name: contour-config
              mountPath: /config
              readOnly: true
            {{- if .Values.contour.extraVolumeMounts }}
              {{- include "common.tplvalues.render" ( dict "value" .Values.contour.extraVolumeMounts "context" $ ) | nindent 12 }}
            {{- end }}
          env:
            - name: CONTOUR_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.contour.image.debug .Values.diagnosticMode.enabled) | quote }}
            {{- if .Values.contour.extraEnvVars }}
              {{- include "common.tplvalues.render" (dict "value" .Values.contour.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.contour.extraEnvVarsCM .Values.contour.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.contour.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsCM "context" $ ) }}
            {{- end }}
            {{- if .Values.contour.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" ( dict "value" .Values.contour.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          {{- if .Values.contour.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.contour.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
        {{- if .Values.contour.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.contour.sidecars "context" $) | nindent 8 }}
        {{- end }}
      dnsPolicy: ClusterFirst
      serviceAccountName: {{ include "contour.contourServiceAccountName" . }}
      {{- if .Values.contour.podSecurityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.contour.podSecurityContext.fsGroup }}
      {{- end }}
      volumes:
        - name: contourcert
          secret:
            secretName: {{ include "contour.contour.certs-secret.name" . }}
        - name: contour-config
          configMap:
            name: {{ include "contour.configMapName" . }}
            defaultMode: 0644
            items:
              - key: contour.yaml
                path: contour.yaml
        {{- if .Values.contour.extraVolumes }}
          {{- include "common.tplvalues.render" ( dict "value" .Values.contour.extraVolumes "context" $ ) | nindent 8 }}
        {{- end }}
{{- end }}
