apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "aitbc-coordinator.fullname" . }}
  labels:
    {{- include "aitbc-coordinator.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "aitbc-coordinator.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "aitbc-coordinator.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "aitbc-coordinator.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.targetPort }}
              protocol: TCP
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          env:
            - name: APP_ENV
              value: {{ .Values.config.appEnv }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "aitbc-coordinator.fullname" . }}
                  key: database-url
            - name: ALLOW_ORIGINS
              value: {{ .Values.config.allowOrigins | quote }}
            {{- if .Values.config.receiptSigningKeyHex }}
            - name: RECEIPT_SIGNING_KEY_HEX
              valueFrom:
                secretKeyRef:
                  name: {{ include "aitbc-coordinator.fullname" . }}
                  key: receipt-signing-key
            {{- end }}
            {{- if .Values.config.receiptAttestationKeyHex }}
            - name: RECEIPT_ATTESTATION_KEY_HEX
              valueFrom:
                secretKeyRef:
                  name: {{ include "aitbc-coordinator.fullname" . }}
                  key: receipt-attestation-key
            {{- end }}
          volumeMounts:
            - name: config
              mountPath: /app/.env
              subPath: .env
      volumes:
        - name: config
          configMap:
            name: {{ include "aitbc-coordinator.fullname" . }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
