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

{{- if .Values.gateway.enabled }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "grafana-mimir.gateway.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.gateway.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/part-of: grafana-mimir
    app.kubernetes.io/component: gateway
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.gateway.replicaCount }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.gateway.podLabels .Values.commonLabels $versionLabel ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: grafana-mimir
      app.kubernetes.io/component: gateway
  {{- if .Values.gateway.updateStrategy }}
  strategy: {{- toYaml .Values.gateway.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: grafana-mimir
        app.kubernetes.io/component: gateway
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/gateway/configmap-http.yaml") . | sha256sum }}
        {{- if .Values.gateway.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.gateway.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "grafana-mimir.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.gateway.automountServiceAccountToken }}
      {{- if .Values.gateway.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.gateway.podAffinityPreset "component" "gateway" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.gateway.podAntiAffinityPreset "component" "gateway" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.gateway.nodeAffinityPreset.type "key" .Values.gateway.nodeAffinityPreset.key "values" .Values.gateway.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.gateway.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.priorityClassName }}
      priorityClassName: {{ .Values.gateway.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.gateway.schedulerName }}
      schedulerName: {{ .Values.gateway.schedulerName }}
      {{- end }}
      {{- if .Values.gateway.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.gateway.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.gateway.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: gateway
          image: {{ include "grafana-mimir.gateway.image" . }}
          imagePullPolicy: {{ .Values.gateway.image.pullPolicy | quote }}
          {{- if .Values.gateway.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.gateway.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.gateway.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /bin/bash
            - -ec
            - |
              # Generate resolver data
              echo resolver $(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf) ";" > /opt/bitnami/nginx/conf/resolvers.conf
              /opt/bitnami/scripts/nginx/entrypoint.sh /opt/bitnami/scripts/nginx/run.sh
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.gateway.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.gateway.image.debug | quote }}
            {{- if .Values.gateway.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.gateway.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.gateway.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.gateway.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.gateway.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.gateway.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.gateway.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: {{ .Values.gateway.containerPorts.http }}
              name: http
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.gateway.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.gateway.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.gateway.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.gateway.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.gateway.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.gateway.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.gateway.resources }}
          resources: {{- toYaml .Values.gateway.resources | nindent 12 }}
          {{- else if ne .Values.gateway.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.gateway.resourcesPreset) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/nginx/conf
              subPath: app-conf-dir
            - name: config
              mountPath: /opt/bitnami/nginx/conf/nginx.conf
              subPath: nginx.conf
            {{- if .Values.gateway.auth.enabled }}
            - name: htpasswd
              mountPath: /bitnami/nginx/secrets/
            {{- end }}
            {{- if .Values.gateway.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.gateway.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.gateway.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.gateway.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config
          configMap:
            name: {{ include "grafana-mimir.gateway.fullname" . }}
      {{- if .Values.gateway.auth.enabled }}
        - name: htpasswd
          secret:
            secretName: {{ include "grafana-mimir.gateway.secretName" . }}
            items:
              - key: htpasswd
                path: .htpasswd
      {{- end }}
      {{- if .Values.gateway.extraVolumes }}
      {{- include "common.tplvalues.render" (dict "value" .Values.gateway.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
{{- end }}
