{{- if .Values.gateway.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "loki.gatewayFullname" . }}
  labels:
    {{- include "loki.gatewayLabels" . | nindent 4 }}
  {{- with .Values.loki.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
spec:
{{- if not .Values.gateway.autoscaling.enabled }}
  replicas: {{ .Values.gateway.replicas }}
{{- end }}
{{- with .Values.gateway.deploymentStrategy }}
  strategy:
{{ toYaml . | trim | indent 4 }}
{{- end }}
  revisionHistoryLimit: {{ .Values.loki.revisionHistoryLimit }}
  selector:
    matchLabels:
      {{- include "loki.gatewaySelectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- include "loki.gatewayConfigChecksum" . | nindent 8 }}
        {{- with .Values.loki.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.gateway.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "loki.gatewaySelectorLabels" . | nindent 8 }}
        {{- with .Values.loki.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- with .Values.gateway.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "loki.serviceAccountName" . }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gateway.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- include "loki.gatewayPriorityClassName" . | nindent 6 }}
      securityContext:
        {{- toYaml .Values.gateway.podSecurityContext | nindent 8 }}
      terminationGracePeriodSeconds: {{ .Values.gateway.terminationGracePeriodSeconds }}
      containers:
        - name: nginx
          image: {{ include "loki.gatewayImage" . }}
          imagePullPolicy: {{ .Values.gateway.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          {{- with .Values.gateway.extraEnv }}
          env:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.gateway.extraEnvFrom }}
          envFrom:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          readinessProbe:
            {{- toYaml .Values.gateway.readinessProbe | nindent 12 }}
          livenessProbe:
            {{- toYaml .Values.gateway.livenessProbe | nindent 12 }}
          securityContext:
            {{- toYaml .Values.gateway.containerSecurityContext | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /etc/nginx
            {{- if .Values.gateway.basicAuth.enabled }}
            - name: auth
              mountPath: /etc/nginx/secrets
            {{- end }}
            - name: tmp
              mountPath: /tmp
            - name: docker-entrypoint-d-override
              mountPath: /docker-entrypoint.d
            {{- if .Values.gateway.extraVolumeMounts }}
            {{- toYaml .Values.gateway.extraVolumeMounts | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.gateway.resources | nindent 12 }}
        {{- if .Values.gateway.extraContainers }}
        {{- toYaml .Values.gateway.extraContainers | nindent 8}}
        {{- end }}
      {{- with .Values.gateway.affinity }}
      affinity:
        {{- tpl . $ | nindent 8 }}
      {{- end }}
      {{- with .Values.gateway.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gateway.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.gateway.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "loki.gatewayFullname" . }}
        {{- if .Values.gateway.basicAuth.enabled }}
        - name: auth
          secret:
            secretName: {{ include "loki.gatewayAuthSecret" . }}
        {{- end }}
        - name: tmp
          emptyDir: {}
        - name: docker-entrypoint-d-override
          emptyDir: {}
        {{- if .Values.gateway.extraVolumes }}
        {{- toYaml .Values.gateway.extraVolumes | nindent 8 }}
        {{- end }}
{{- end }}