{{ include "common.storage.hostPathValidate" .Values }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}-plex
  labels:
    app.kubernetes.io/name: {{ template "common.names.name" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
spec:
  replicas: {{ (default 1 .Values.replicas) }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "common.names.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template: {{ include "common.deployment.pod.metadata" . | nindent 4 }}
    spec:
      hostNetwork: {{ .Values.hostNetwork }}
    {{- if .Values.hostNetwork }}
      dnsPolicy: ClusterFirstWithHostNet
    {{- end }}
      hostname: {{ template "common.names.fullname" . }}
      {{- if hasKey .Values "global" }}
        {{- if hasKey .Values.global "ixChartContext" }}
          {{- if .Values.global.ixChartContext.addNvidiaRuntimeClass }}
      runtimeClassName: {{ .Values.global.ixChartContext.nvidiaRuntimeClassName }}
          {{- end }}
        {{- end }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          {{ include "common.resources.limitation" . | nindent 10 }}
          {{ if .Values.enablePlexPass }}
            {{ include "common.containers.imageConfig" .Values.plexpassImage | nindent 10 }}
          {{ else }}
            {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          {{ end }}
          volumeMounts: {{ include "common.storage.configureAppVolumeMountsInContainer" .Values | nindent 12 }}
            {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
            - name: extrappvolume-{{ $index }}
              mountPath: {{ $hostPathConfiguration.mountPath }}
              {{ if $hostPathConfiguration.readOnly }}
              readOnly: {{ $hostPathConfiguration.readOnly }}
              {{ end }}
            {{ end }}
          ports:
          - name: pms
            protocol: TCP
            containerPort: 32400
          {{- if .Values.hostNetwork }}
            hostPort: {{ .Values.plexServiceTCP.port }}
          {{- else }}
            hostPort: null
          {{- end }}
          - name: plex-dlna
            protocol: TCP
            containerPort: 32469
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          - name: plex-dlna-udp
            protocol: UDP
            containerPort: 1900
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          - name: plex-gdm1
            protocol: UDP
            containerPort: 32410
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          - name: plex-gdm2
            protocol: UDP
            containerPort: 32412
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          - name: plex-gdm3
            protocol: UDP
            containerPort: 32413
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          - name: plex-gdm4
            protocol: UDP
            containerPort: 32414
          {{- if not .Values.hostNetwork }}
            hostPort: null
          {{- end }}
          env:
            - name: KUBE_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            {{ $envList := (default list .Values.environmentVariables) }}
            {{ if and .Values.gpuConfiguration (hasKey .Values.gpuConfiguration "nvidia.com/gpu") (gt (get .Values.gpuConfiguration "nvidia.com/gpu" | toDecimal) 0) }}
            {{ $envList = mustAppend $envList (dict "name" "NVIDIA_DRIVER_CAPABILITIES" "value" "all") }}
            {{ end }}
            {{ $envList = mustAppend $envList (dict "name" "TZ" "value" .Values.timezone) }}
            {{ $envList = mustAppend $envList (dict "name" "PLEX_CLAIM" "value" .Values.claimToken) }}
            {{ $envList = mustAppend $envList (dict "name" "PMS_INTERNAL_ADDRESS" "value" (printf "http://%s:32400" (include "common.names.fullname" .))) }}
            {{ $envList = mustAppend $envList (dict "name" "PMS_IMAGE" "value" (printf "%s:%s" .Values.image.repository .Values.image.tag))}}
            {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) | nindent 12 }}
          readinessProbe:
            tcpSocket:
              port: 32400
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
            successThreshold: 2
          livenessProbe:
            tcpSocket:
              port: 32400
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
            successThreshold: 1
          startupProbe:
            tcpSocket:
              port: 32400
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 2
            failureThreshold: 60
            successThreshold: 1
      {{ include "common.networking.dnsConfiguration" .Values | nindent 6 }}
      volumes: {{ include "common.storage.configureAppVolumes" .Values | nindent 8 }}
        {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
        - name: extrappvolume-{{ $index }}
          hostPath:
            path: {{ $hostPathConfiguration.hostPath }}
        {{ end }}
