apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "emby.fullname" . }}
  labels:
    {{- include "emby.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "emby.fullname" . }}
  selector:
    matchLabels:
      {{- include "emby.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "emby.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "emby.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          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
          - name: UID
            valueFrom:
              configMapKeyRef:
                name: emby-settings
                key: uid
          - name: GID
            valueFrom:
              configMapKeyRef:
                name: emby-settings
                key: gid
          ports:
            - name: http
              containerPort: 8096
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 8096
          readinessProbe:
            tcpSocket:
              port: 8096
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - mountPath: /data/media
            name: emby-media-pv
          - mountPath: /config
            name: emby-config
      restartPolicy: Always
      volumes:
      - name: emby-media-pv
        persistentVolumeClaim:
          claimName: {{ .Values.config.mediaPvc }}
      - name: emby-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 }}
