apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "gitea.fullname" . }}
  labels:
    {{- include "gitea.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "gitea.selectorLabels" . | nindent 6 }}
  serviceName: {{ include "gitea.fullname" . }}
  template:
    metadata:
      labels:
        {{- include "gitea.selectorLabels" . | nindent 8 }}
    spec:
    {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      serviceAccountName: {{ include "gitea.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: http
              containerPort: 3000
              protocol: TCP
            - name: ssh
              containerPort: 22
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: 3000
            timeoutSeconds: 2
            failureThreshold: 5
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
          readinessProbe:
            tcpSocket:
              port: 3000
            timeoutSeconds: 2
            failureThreshold: 5
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
          - name: gitea-pv
            mountPath: /data
      volumes:
      {{- if and .Values.config.persistence.enabled .Values.config.persistence.existingClaim }}
      - name: gitea-pv
        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 }}
{{- if and .Values.config.persistence.enabled (not .Values.config.persistence.existingClaim) }}
  volumeClaimTemplates:
  - metadata:
      name: gitea-pv
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: {{ .Values.config.storageSize }}
      storageClassName: {{ .Values.config.persistence.storageClassName | quote }}
{{- end -}}
