{{- $envVars := include "meet.common.env" (list . .Values.agentSubtitles) -}}
{{- $fullName := include "meet.agentSubtitles.fullname" . -}}
{{- $component := "agent-subtitles" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ $fullName }}
  annotations:
     {{- with .Values.agentSubtitles.dpAnnotations }}
     {{- toYaml . | nindent 4 }}
     {{- end }}
  namespace: {{ .Release.Namespace | quote }}
  labels:
    {{- include "meet.common.labels" (list . $component) | nindent 4 }}
spec:
  replicas: {{ .Values.agentSubtitles.replicas }}
  selector:
    matchLabels:
      {{- include "meet.common.selectorLabels" (list . $component) | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- with .Values.agentSubtitles.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "meet.common.selectorLabels" (list . $component) | nindent 8 }}
    spec:
      {{- if $.Values.image.credentials }}
      imagePullSecrets:
        - name: {{ include "meet.secret.dockerconfigjson.name" (dict "fullname" (include "meet.fullname" .) "imageCredentials" $.Values.image.credentials) }}
      {{- end }}
      shareProcessNamespace: {{ .Values.agentSubtitles.shareProcessNamespace }}
      {{- with .Values.agentSubtitles.podSecurityContext }}
      securityContext:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      containers:
        {{- with .Values.agentSubtitles.sidecars }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        - name: {{ .Chart.Name }}
          image: "{{ (.Values.agentSubtitles.image | default dict).repository | default .Values.agentSubtitles.image.repository }}:{{ (.Values.agentSubtitles.image | default dict).tag | default .Values.agentSubtitles.image.tag }}"
          imagePullPolicy: {{ (.Values.agentSubtitles.image | default dict).pullPolicy | default .Values.agentSubtitles.image.pullPolicy }}
          {{- with .Values.agentSubtitles.command }}
          command:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentSubtitles.args }}
          args:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          env:
            {{- if $envVars }}
            {{- $envVars | indent 12 }}
            {{- end }}
          {{- with .Values.agentSubtitles.securityContext }}
          securityContext:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with .Values.agentSubtitles.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- range $index, $value := .Values.mountFiles }}
            - name: "files-{{ $index }}"
              mountPath: {{ $value.path }}
              subPath: content
            {{- end }}
            {{- range $name, $volume := .Values.agentSubtitles.persistence }}
            - name: "{{ $name }}"
              mountPath: "{{ $volume.mountPath }}"
            {{- end }}
            {{- range .Values.agentSubtitles.extraVolumeMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath | default "" }}
              readOnly: {{ .readOnly }}
            {{- end }}
      {{- with .Values.agentSubtitles.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.agentSubtitles.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.agentSubtitles.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      volumes:
        {{- range $index, $value := .Values.agentSubtitles.mountFiles }}
        - name: "files-{{ $index }}"
          configMap:
            name: "{{ include "meet.fullname" $ }}-files-{{ $index }}"
        {{- end }}
        {{- range $name, $volume := .Values.agentSubtitles.persistence }}
        - name: "{{ $name }}"
          {{- if eq $volume.type "emptyDir" }}
          emptyDir: {}
          {{- else }}
          persistentVolumeClaim:
            claimName: "{{ $fullName }}-{{ $name }}"
          {{- end }}
        {{- end }}
        {{- range .Values.agentSubtitles.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 .Values.agentSubtitles.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 }}
{{ end }}
