apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "jellyfin.fullname" . }}
  labels:
    {{- include "jellyfin.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "jellyfin.fullname" . }}
  selector:
    matchLabels:
      {{- include "jellyfin.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "jellyfin.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "jellyfin.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          env:
            - name: NODE_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: spec.nodeName
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: jellyfin
              containerPort: 8096
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: 8096
          readinessProbe:
            httpGet:
              path: /
              port: 8096
          startupProbe:
            httpGet:
              path: /
              port: 8096
            failureThreshold: 30
            periodSeconds: 10
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - mountPath: /data/media
            name: jellyfin-media
          - mountPath: /config
            name: jellyfin-config
      restartPolicy: Always
      volumes:
      - name: jellyfin-media
        persistentVolumeClaim:
            claimName: {{ .Values.config.mediaPvc }}
      - name: jellyfin-config
        persistentVolumeClaim:
            claimName: {{ .Values.config.configPvc }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
