apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "groombook.fullname" . }}-api
  labels:
    {{- include "groombook.labels" . | nindent 4 }}
    app.kubernetes.io/component: api
spec:
  replicas: {{ .Values.api.replicas }}
  progressDeadlineSeconds: 300
  selector:
    matchLabels:
      {{- include "groombook.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: api
  template:
    metadata:
      labels:
        {{- include "groombook.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: api
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "groombook.serviceAccountName" . }}
      containers:
        - name: api
          image: {{ include "groombook.apiImage" . }}
          imagePullPolicy: {{ .Values.api.image.pullPolicy }}
          ports:
            - containerPort: 3000
              name: http
              protocol: TCP
          env:
            - name: PORT
              value: {{ .Values.api.env.port | quote }}
            - name: NODE_ENV
              value: {{ .Values.api.env.nodeEnv | quote }}
            - name: AUTH_DISABLED
              value: {{ .Values.api.env.authDisabled | quote }}
            {{- if .Values.api.env.corsOrigin }}
            - name: CORS_ORIGIN
              value: {{ .Values.api.env.corsOrigin | quote }}
            {{- end }}
            {{- if .Values.api.env.oidcIssuer }}
            - name: OIDC_ISSUER
              value: {{ .Values.api.env.oidcIssuer | quote }}
            {{- end }}
            {{- if .Values.api.env.oidcAudience }}
            - name: OIDC_AUDIENCE
              value: {{ .Values.api.env.oidcAudience | quote }}
            {{- end }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "groombook.databaseSecretName" . }}
                  key: {{ include "groombook.databaseSecretKey" . }}
          resources:
            {{- toYaml .Values.api.resources | nindent 12 }}
          readinessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: 3000
            initialDelaySeconds: 15
            periodSeconds: 30