{{- $sharedEnvVars := default (dict) .Values.celeryTranscribe.envVars -}}
{{- $component := "celery-transcribe" -}}

{{- range $idx, $instance := .Values.celeryTranscribe.instances }}
{{- $instanceValues := merge $instance $.Values.celeryTranscribe }}
{{- $fullName := printf "%s-%s" (include "meet.celeryTranscribe.fullname" $) $instance.name }}
{{- $extraInstanceEnvVars := default (dict) $instance.extraEnvVars -}}
{{- $mergedInstanceEnvVars := merge $extraInstanceEnvVars $sharedEnvVars -}}
{{- $fakeInstanceObjectForEnvHelper := dict "envVars" $mergedInstanceEnvVars -}}
{{- $envVars := include "meet.common.env" (list . $fakeInstanceObjectForEnvHelper) -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $fullName }}
  annotations:
   {{- with $instanceValues.dpAnnotations }}
   {{- toYaml . | nindent 4 }}
   {{- end }}
  namespace: {{ $.Release.Namespace | quote }}
  labels:
    {{- include "meet.common.labels" (list $ $component) | nindent 4 }}
    instance: {{ $instance.name }}
spec:
  replicas: {{ $instanceValues.replicas }}
  selector:
    matchLabels:
      {{- include "meet.common.selectorLabels" (list $ $component) | nindent 6 }}
      instance: {{ $instance.name }}
  template:
    metadata:
      annotations:
        {{- with $instanceValues.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "meet.common.selectorLabels" (list $ $component) | nindent 8 }}
        instance: {{ $instance.name }}
    spec:
      {{- if $.Values.image.credentials }}
      imagePullSecrets:
        - name: {{ include "meet.secret.dockerconfigjson.name" (dict "fullname" (include "meet.fullname" $) "imageCredentials" $.Values.image.credentials) }}
      {{- end }}
      shareProcessNamespace: {{ $instanceValues.shareProcessNamespace }}
      {{- with $instanceValues.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        {{- with $instanceValues.sidecars }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: {{ $.Chart.Name }}
          image: "{{ ($instanceValues.image | default dict).repository | default $.Values.image.repository }}:{{ ($instanceValues.image | default dict).tag | default $.Values.image.tag }}"
          imagePullPolicy: {{ ($instanceValues.image | default dict).pullPolicy | default $.Values.image.pullPolicy }}
          {{- with $instanceValues.command }}
          command:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $instanceValues.args }}
          args:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          env:
            {{- if $envVars }}
            {{- $envVars | indent 12 }}
            {{- end }}
          {{- with $instanceValues.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ $instanceValues.service.targetPort }}
              protocol: TCP
          {{- if $instanceValues.probes.liveness }}
          livenessProbe:
            {{- include "meet.probes.abstract" (merge $instanceValues.probes.liveness (dict "targetPort" $instanceValues.service.targetPort)) | nindent 12 }}
          {{- end }}
          {{- if $instanceValues.probes.readiness }}
          readinessProbe:
            {{- include "meet.probes.abstract" (merge $instanceValues.probes.readiness (dict "targetPort" $instanceValues.service.targetPort)) | nindent 12 }}
          {{- end }}
          {{- if $instanceValues.probes.startup }}
          startupProbe:
            {{- include "meet.probes.abstract" (merge $instanceValues.probes.startup (dict "targetPort" $instanceValues.service.targetPort)) | nindent 12 }}
          {{- end }}
          {{- with $instanceValues.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- range $index, $value := $.Values.mountFiles }}
            - name: "files-{{ $index }}"
              mountPath: {{ $value.path }}
              subPath: content
            {{- end }}
            {{- range $name, $volume := $instanceValues.persistence }}
            - name: "{{ $name }}"
              mountPath: "{{ $volume.mountPath }}"
            {{- end }}
            {{- range $instanceValues.extraVolumeMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
            {{- end }}
      {{- with $instanceValues.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $instanceValues.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with $instanceValues.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- range $index, $value := $.Values.mountFiles }}
        - name: "files-{{ $index }}"
          configMap:
            name: "{{ include "meet.fullname" $ }}-files-{{ $index }}"
        {{- end }}
        {{- range $name, $volume := $instanceValues.persistence }}
        - name: "{{ $name }}"
          {{- if eq $volume.type "emptyDir" }}
          emptyDir: {}
          {{- else }}
          persistentVolumeClaim:
            claimName: "{{ $fullName }}-{{ $name }}"
          {{- end }}
        {{- end }}
        {{- range $instanceValues.extraVolumes }}
        - name: {{ .name }}
          {{- if .existingClaim }}
          persistentVolumeClaim:
            claimName: {{ .existingClaim }}
          {{- else if .hostPath }}
          hostPath:
            {{ toYaml .hostPath | nindent 12 }}
          {{- else if .csi }}
          csi:
            {{- toYaml .csi | nindent 12 }}
          {{- else if .configMap }}
          configMap:
            {{- toYaml .configMap | nindent 12 }}
          {{- else if .emptyDir }}
          emptyDir:
            {{- toYaml .emptyDir | nindent 12 }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- end }}
---
{{ if $instanceValues.pdb.enabled }}
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: {{ $fullName }}
  namespace: {{ $.Release.Namespace | quote }}
spec:
  maxUnavailable: 1
  selector:
    matchLabels:
      {{- include "meet.common.selectorLabels" (list $ $component) | nindent 6 }}
      instance: {{ $instance.name }}
---
{{ end }}
{{- end }}
