{{- if .Values.frontend.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "papercrate.fullname" . }}-frontend
  labels:
    {{- include "papercrate.labels" . | nindent 4 }}
    app.kubernetes.io/component: frontend
spec:
  replicas: {{ .Values.frontend.replicaCount }}
  selector:
    matchLabels:
      {{- include "papercrate.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: frontend
  template:
    metadata:
      labels:
        {{- include "papercrate.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: frontend
    spec:
      {{- if or .Values.global.imagePullSecrets .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- range .Values.global.imagePullSecrets }}
        - name: {{ . }}
        {{- end }}
        {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
        {{- end }}
      {{- end }}
      containers:
        - name: frontend
          image: {{ include "papercrate.frontend.image" . }}:{{ include "papercrate.image.tag" (dict "image" .Values.frontend.image "Chart" .Chart) }}
          imagePullPolicy: {{ .Values.frontend.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.frontend.service.port }}
              protocol: TCP
          env:
            - name: API_PROXY_PASS
              value: {{ printf "http://%s-backend:%d" (include "papercrate.fullname" . ) (int (.Values.backend.service.port)) | quote }}
            {{- range .Values.frontend.env.extra }}
            - name: {{ .name }}
              value: {{ .value | quote }}
            {{- end }}
          {{- if or .Values.frontend.envFrom .Values.frontend.env.existingSecret }}
          envFrom:
            {{- if .Values.frontend.env.existingSecret }}
            - secretRef:
                name: {{ .Values.frontend.env.existingSecret }}
            {{- end }}
            {{- range .Values.frontend.envFrom }}
            - {{ toYaml . | nindent 12 | trim }}
            {{- end }}
          {{- end }}
          {{- with .Values.frontend.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.frontend.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.frontend.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.frontend.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
{{- end }}
