{{- if .Values.gateway.enabled }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "grafana-loki.gateway.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    app.kubernetes.io/part-of: grafana-loki
    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 }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/part-of: grafana-loki
      app.kubernetes.io/component: gateway
  {{- if .Values.gateway.updateStrategy }}
  strategy: {{- toYaml .Values.gateway.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.commonLabels }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 8 }}
        {{- end }}
        {{- if .Values.gateway.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.gateway.podLabels "context" $) | nindent 8 }}
        {{- end }}
        app.kubernetes.io/part-of: grafana-loki
        app.kubernetes.io/component: gateway
      annotations:
        checksum/configmap: {{ include (print $.Template.BasePath "/gateway/configmap-http.yaml") . | sha256sum }}
        {{- if .Values.gateway.podAnnotations }}
        annotations: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "grafana-loki.imagePullSecrets" . | nindent 6 }}
      {{- 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" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.gateway.podAntiAffinityPreset "component" "gateway" "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: {{- omit .Values.gateway.podSecurityContext "enabled" | toYaml | 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-loki.gateway.image" . }}
          imagePullPolicy: {{ .Values.gateway.image.pullPolicy | quote }}
          {{- if .Values.gateway.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.gateway.containerSecurityContext "enabled" | toYaml | 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.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- else if .Values.gateway.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.gateway.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.livenessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- else if .Values.gateway.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.gateway.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.gateway.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: http
          {{- else if .Values.gateway.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.gateway.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.gateway.resources }}
          resources: {{- toYaml .Values.gateway.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - 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: config
          configMap:
            name: {{ include "grafana-loki.gateway.fullname" . }}
      {{- if .Values.gateway.auth.enabled }}
        - name: htpasswd
          secret:
            secretName: {{ include "grafana-loki.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 }}
