apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "sonarr.fullname" . }}
  labels:
    {{- include "sonarr.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "sonarr.fullname" . }}
  selector:
    matchLabels:
      {{- include "sonarr.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "sonarr.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "sonarr.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - args:
        - exportarr
        - sonarr
        name: sonarr-metrics
        image: ghcr.io/onedr0p/exportarr:latest
        imagePullPolicy: IfNotPresent
        ports:
        - name: sonarr-metrics
          containerPort: 9707
          protocol: TCP
        env:
          - name: PORT
            value: "9707"
          - name: URL
            value: "http://127.0.0.1:8989"
          - name: ENABLE_EPISODE_QUALITY_METRICS
            value: "true"
          - name: APIKEY
            value: "{{ .Values.metrics.apikey }}"
      - name: {{ .Chart.Name }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
          - name: sonarr
            containerPort: 8989
            protocol: TCP
        livenessProbe:
          httpGet:
            path: /
            port: 8989
        readinessProbe:
          httpGet:
            path: /
            port: 8989
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        volumeMounts:
        - name: sonarr-config
          mountPath: /config
        - name: sonarr-media
          mountPath: /tv
          subPath: tv
        - name: sonarr-media
          mountPath: /tv-kids
          subPath: tv-kids
      volumes:
      - emptyDir: {}
        name: sonarr-local-config
      - name: sonarr-media
        persistentVolumeClaim:
          claimName: {{ .Values.mediaPvc }}
      - name: sonarr-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 }}
