apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "tei.fullname" . }}
  labels:
    {{- include "tei.labels" . | nindent 4 }}
    {{- if .Values.labels }}
      {{ toYaml .Values.labels | indent 4 }}
    {{- end }}
  {{- if .Values.annotations }}
  annotations:
    {{ toYaml .Values.annotations | indent 4 }}
  {{- end }}
spec:
  serviceName: {{ template "tei.fullname" . }}-headless
  podManagementPolicy: Parallel
  replicas: {{ .Values.replicaCount | default 1 }}
  selector:
    matchLabels:
      {{- include "tei.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "tei.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "tei.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: {{ .Chart.Name }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        args:
        - "--model-id"
        - "{{ .Values.modelId }}"
        {{- range .Values.extraArgs }}
        - {{ . | quote }}
        {{- end }}
        ports:
        - containerPort: 80
          name: http
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        {{- if .Values.extraEnv }}
        env:
          {{ toYaml .Values.extraEnv | indent 8 }}
        {{- end }}
        volumeMounts:
        - name: data-volume
          mountPath: {{ .Values.persistence.mountPath }}
        {{- if .Values.volumeMounts }}
          {{ toYaml .Values.volumeMounts | indent 8 }}
        {{- end }}
      volumes:
      {{- if not .Values.persistence.enabled }}
      - name: data-volume
        emptyDir: {}
      {{- end }}
      {{- with .Values.volumes }}
        {{- toYaml . | nindent 8 }}
      {{- 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 }}
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
  - metadata:
      name: data-volume
    spec:
      accessModes: [ "ReadWriteOnce" ]
      {{- if .Values.persistence.persistentVolumeClaim.storageClass }}
      {{- if (eq "-" .Values.persistence.persistentVolumeClaim.storageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.persistence.persistentVolumeClaim.storageClass }}"
      {{- end }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.persistence.persistentVolumeClaim.size }}
  {{- end }}
