apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "woodpecker-agent.fullname" . }}
  labels:
    {{- include "woodpecker-agent.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "woodpecker-agent.fullname" . }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "woodpecker-agent.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "woodpecker-agent.selectorLabels" . | nindent 8 }}
    spec:
      {{- if and (semverCompare ">=1.19-0" .Capabilities.KubeVersion.GitVersion) (.Values.topologySpreadConstraints) }}
      {{- with .Values.topologySpreadConstraints }}
      topologySpreadConstraints:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "woodpecker-agent.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: 3000
              protocol: TCP
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if or (.Values.persistence.enabled) (.Values.extraVolumeMounts) }}
          volumeMounts:
            {{- if .Values.persistence.enabled }}
            - name: agent-config
              mountPath: {{ .Values.persistence.mountPath }}
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
          {{- end }}
          env:
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          envFrom:
            {{- if .Values.mapAgentSecret }}
            - 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 }}
      {{- if or (and (.Values.persistence.enabled) (.Values.persistence.existingClaim)) (.Values.extraVolumeMounts) }}
      volumes:
        {{- if .Values.persistence.enabled }}
        {{- if .Values.persistence.existingClaim }}
        - name: agent-config
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim }}
        {{- end }}
        {{- end }}
        {{- if .Values.extraVolumes }}
          {{- toYaml .Values.extraVolumes | nindent 6 }}
        {{- end }}
      {{- end }}
      {{- with .Values.dnsConfig }}
      dnsConfig:
        {{- toYaml . | nindent 8 }}
      {{- end }}

{{- if and (not .Values.persistence.existingClaim) .Values.persistence.enabled }}
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: agent-config
        namespace: {{ $.namespace }}
        annotations:
        {{- range $key, $value := .Values.persistence.annotations }}
          {{ $key }}: {{ $value }}
        {{- end }}
      spec:
        accessModes:
        {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
        {{- end }}
      {{- if .Values.persistence.storageClass }}
      {{- if (eq "-" .Values.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.persistence.storageClass }}"
      {{- end }}
      {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size }}
{{- end }}
