apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "woodpecker-server.fullname" . }}
  {{- with .Values.statefulSet.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    {{- include "woodpecker-server.labels" . | nindent 4 }}
    {{- with .Values.statefulSet.labels }}
    {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  serviceName: {{ include "woodpecker-server.fullname" . }}-headless
  revisionHistoryLimit: {{ .Values.statefulSet.revisionHistoryLimit }}
  replicas: {{ .Values.statefulSet.replicaCount }}
  updateStrategy:
    {{- toYaml .Values.updateStrategy | nindent 4 }}
  selector:
    matchLabels:
      {{- include "woodpecker-server.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "woodpecker-server.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "woodpecker-server.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        {{- if .Values.initContainers }}
        {{- tpl (toYaml .Values.initContainers) $ | nindent 8 }}
        {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.registry }}/{{ .Values.image.repository }}:{{ .Values.image.tag | default (printf "v%s" .Chart.AppVersion) }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.command }}
          command:
            {{- toYaml . | trim | nindent 12 }}
          {{- end }}
          {{- with .Values.args }}
          args:
            {{- toYaml . | trim | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
            - name: grpc
              containerPort: 9000
              protocol: TCP
            {{- if .Values.metrics.enabled }}
            - name: metrics
              containerPort: {{ .Values.metrics.port }}
              protocol: TCP
            {{- end }}
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8000
            {{- if .Values.probes }}
            {{- with .Values.probes.liveness }}
            timeoutSeconds: {{ .timeoutSeconds | default 10 }}
            periodSeconds: {{ .periodSeconds | default 10 }}
            successThreshold: {{ .successThreshold | default 1 }}
            failureThreshold: {{ .failureThreshold | default 3 }}
            {{- end }}
            {{- end }}
          readinessProbe:
            httpGet:
              path: /healthz
              port: 8000
            {{- if .Values.probes }}
            {{- with .Values.probes.readiness }}
            timeoutSeconds: {{ .timeoutSeconds | default 10 }}
            periodSeconds: {{ .periodSeconds | default 10 }}
            successThreshold: {{ .successThreshold | default 1 }}
            failureThreshold: {{ .failureThreshold | default 3 }}
            {{- end }}
            {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            {{- if .Values.persistentVolume.enabled }}
            - name: data
              mountPath: {{ .Values.persistentVolume.mountPath }}
            {{- end }}
            {{- with .Values.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
            {{- if .Values.metrics.enabled }}
            - name: WOODPECKER_METRICS_SERVER_ADDR
              value: ":{{ .Values.metrics.port }}"
            {{- end }}
          envFrom:
            {{- if .Values.createAgentSecret }}
            - secretRef:
                name: woodpecker-default-agent-secret
            {{- end }}
            {{- range .Values.extraSecretNamesForEnvFrom }}
            - secretRef:
                name: {{ . }}
            {{- 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 }}
      {{- with .Values.extraVolumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  {{- if .Values.persistentVolume.enabled }}
  volumeClaimTemplates:
  - apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: data
    spec:
      accessModes: [ "ReadWriteOnce" ]
    {{- if .Values.persistentVolume.storageClass }}
    {{- if (eq "-" .Values.persistentVolume.storageClass) }}
      storageClassName: ""
    {{- else }}
      storageClassName: {{ .Values.persistentVolume.storageClass }}
    {{- end }}
    {{- end }}
      resources:
        requests:
          storage: {{ .Values.persistentVolume.size }}
  {{- end }}
