apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "tautulli.fullname" . }}
  labels:
    {{- include "tautulli.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ include "tautulli.fullname" . }}
  selector:
    matchLabels:
      {{- include "tautulli.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "tautulli.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "tautulli.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 }}
          ports:
            - name: tautulli
              containerPort: 8181
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: 8181
          readinessProbe:
            httpGet:
              path: /
              port: 8181
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - name: tautulli-config
            mountPath: /config
          - mountPath: /var/tmp
            name: tmp
      initContainers:
      - command:
        - chown 
        - -R 
        - "1027:1027"
        - /config
        image: busybox
        imagePullPolicy: Always
        name: init
        resources:
          limits:
            cpu: 200m
            memory: 256Mi
          requests:
            cpu: 50m
            memory: 256Mi
        volumeMounts:
        - mountPath: /config
          name: tautulli-config
      volumes:
      - name: tautulli-config
        persistentVolumeClaim:
          claimName: {{ .Values.config.configPvc }}
      - name: tmp
        emptyDir: {}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
