{{- if .Values.api.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "josie-health.componentName" (dict "name" .Values.api.name) }}
  labels:
    {{- include "josie-health.componentLabels" (dict "component" .Values.api.name "root" .) | nindent 4 }}
spec:
  replicas: {{ .Values.api.replicas }}
  selector:
    matchLabels:
      {{- include "josie-health.componentSelectorLabels" (dict "component" .Values.api.name) | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "josie-health.componentSelectorLabels" (dict "component" .Values.api.name) | nindent 8 }}
    spec:
      {{- if .Values.api.migration.enabled }}
      serviceAccountName: josie-health-migrate
      initContainers:
        - name: trigger-migration
          image: image-registry.openshift-image-registry.svc:5000/openshift/cli:latest
          command:
            - /bin/sh
            - -c
            - |
              oc delete job josie-health-api-migrate --ignore-not-found
              oc apply -f /config/migration-job.yaml
          volumeMounts:
            - name: migration-job
              mountPath: /config
      {{- end }}
      containers:
        - name: {{ .Values.api.name }}
          image: {{ include "josie-health.image" (dict "root" . "image" .Values.api.image) }}
          imagePullPolicy: {{ .Values.api.image.pullPolicy }}
          ports:
            - containerPort: {{ .Values.api.port }}
              protocol: TCP
          env:
            {{- range $key, $value := .Values.api.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
            - name: REDIS_HOST
              value: {{ include "josie-health.componentName" (dict "name" .Values.api.redis.name) }}
            - name: REDIS_PORT
              value: {{ .Values.api.redis.port | quote }}
          {{- if .Values.api.envFrom }}
          envFrom:
            {{- toYaml .Values.api.envFrom | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.api.resources | nindent 12 }}
      {{- if .Values.api.migration.enabled }}
      volumes:
        - name: migration-job
          configMap:
            name: josie-health-api-migration-job
      {{- end }}
{{- end }}
