apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "nzbget.fullname" . }}
  labels:
    {{- include "nzbget.labels" . | nindent 4 }}
  annotations:
spec:
  serviceName: {{ .Chart.Name }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "nzbget.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "nzbget.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "nzbget.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: nzbget-metrics
        image: harbor.ervine.dev/public/x86_64/alpine/nzbget-exporter:v0.1-3.13.5
        imagePullPolicy: IfNotPresent
        ports:
        - name: nzbget-metrics
          containerPort: 9452
          protocol: TCP
        env:
          - name: NZBGET_HOST
            value: "http://127.0.0.1:6789"
          - name: NZBGET_METRIC_NAMESPACE
            value: media
          - name: LOG_LEVEL
            value: debug
      - name: {{ .Chart.Name }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
          - name: nzbg
            containerPort: 6789
            protocol: TCP
        livenessProbe:
          failureThreshold: 3
          initialDelaySeconds: 3
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: nzbg
        readinessProbe:
          failureThreshold: 3
          initialDelaySeconds: 3
          periodSeconds: 10
          successThreshold: 1
          tcpSocket:
            port: nzbg
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        volumeMounts:
        - name: nzbget-config
          mountPath: /config
        - name: media
          mountPath: /media
        - name: local-download
          mountPath: /downloads
      volumes:
      - name: media
        persistentVolumeClaim:
          claimName: {{ .Values.mediaPvc }}
      - name: local-download
        emptyDir: {}
      {{- if and .Values.config.persistence.enabled .Values.config.persistence.existingClaim }}
      - name: nzbget-config
        persistentVolumeClaim:
          {{- with .Values.config.persistence.existingClaim }}
          claimName: {{ tpl . $ }}
          {{- end }}
      {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
