{{- if and .Values.server.enabled .Values.server.statefulSet.enabled -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: {{ .Release.Namespace }}
{{- if .Values.server.annotations }}
  annotations:
{{ toYaml .Values.server.annotations | indent 4 }}
{{- end }}
  labels:
    {{- include "victoria-logs.server.labels" . | nindent 4 }}
    {{- with .Values.server.extraLabels }}
    {{ toYaml .}}
    {{- end}}
  name: {{ template "victoria-logs.server.fullname" . }}
spec:
  serviceName: {{ template "victoria-logs.server.fullname" . }}
  selector:
    matchLabels:
      {{- include "victoria-logs.server.matchLabels" . | nindent 6 }}
  replicas: 1
  podManagementPolicy: {{ .Values.server.podManagementPolicy }}
  template:
    metadata:
    {{- if .Values.server.podAnnotations }}
      annotations:
{{ toYaml .Values.server.podAnnotations | indent 8 }}
    {{- end }}
      labels:
        {{- include "victoria-logs.server.labels" . | nindent 8 }}
        {{- range $key, $value := .Values.server.podLabels }}
        {{ $key }}: {{ $value | quote }}
        {{- end }}
    spec:
{{- if .Values.server.priorityClassName }}
      priorityClassName: "{{ .Values.server.priorityClassName }}"
{{- end }}
{{- if .Values.server.schedulerName }}
      schedulerName: "{{ .Values.server.schedulerName }}"
{{- end }}
      {{- if eq (include "victoria-logs.hasInitContainer" . ) "true" }}
      initContainers:
        {{- include "victoria-logs.initContiners" . | nindent 8 }}
      {{- end }}
      containers:
        - name: {{ template "victoria-logs.name" . }}-{{ .Values.server.name }}
          {{- with  .Values.server.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
          image: "{{ .Values.server.image.repository }}:{{ .Values.server.image.tag }}"
          imagePullPolicy: "{{ .Values.server.image.pullPolicy }}"
          {{- if .Values.server.containerWorkingDir }}
          workingDir: {{ .Values.server.containerWorkingDir }}
          {{- end }}
          args:
            - {{ printf "%s=%s" "--retentionPeriod" (toString .Values.server.retentionPeriod) | quote}}
            - {{ printf "%s=%s" "--storageDataPath" .Values.server.persistentVolume.mountPath | quote}}
          {{- range $key, $value := .Values.server.extraArgs }}
            - --{{ $key }}={{ $value }}
          {{- end }}
          {{- with .Values.server.env }}
          env: {{ toYaml . | nindent 10 }}
          {{- end }}
          ports:
            - name: http
              containerPort: 9428
          {{- with $.Values.server.livenessProbe }}
          livenessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $.Values.server.readinessProbe }}
          readinessProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- with $.Values.server.startupProbe }}
          startupProbe:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.server.resources | nindent 12 }}
          volumeMounts:
            - name: server-volume
              mountPath: {{ .Values.server.persistentVolume.mountPath }}
              subPath: {{ .Values.server.persistentVolume.subPath }}
          {{- range .Values.server.extraHostPathMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.server.extraConfigmapMounts }}
            - name: {{ $.Values.server.name }}-{{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- range .Values.server.extraSecretMounts }}
            - name: {{ .name }}
              mountPath: {{ .mountPath }}
              subPath: {{ .subPath }}
              readOnly: {{ .readOnly }}
          {{- end }}
          {{- with .Values.server.extraVolumeMounts }}
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.server.extraContainers }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
       {{ toYaml .Values.imagePullSecrets | indent 2 }}
    {{- end }}
    {{- if .Values.server.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.server.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.server.podSecurityContext }}
      securityContext:
{{ toYaml .Values.server.podSecurityContext | indent 8 }}
    {{- end }}
    {{- if .Values.server.tolerations }}
      tolerations:
{{ toYaml .Values.server.tolerations | indent 8 }}
    {{- end }}
    {{- with .Values.server.affinity }}
      affinity: {{ toYaml . | nindent 8 }}
    {{- end }}
      terminationGracePeriodSeconds: {{ .Values.server.terminationGracePeriodSeconds }}
      volumes:
        {{- if not .Values.server.persistentVolume.enabled }}
        - name: server-volume
          emptyDir: {}
        {{- else }}
        {{- if .Values.server.persistentVolume.existingClaim }}
        - name: server-volume
          persistentVolumeClaim:
            claimName: {{ .Values.server.persistentVolume.existingClaim }}
        {{- end }}
        {{- end }}
        {{- with .Values.server.extraVolumes }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
  {{- if and .Values.server.persistentVolume.enabled (not .Values.server.persistentVolume.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: server-volume
        {{- if .Values.server.persistentVolume.annotations }}
        annotations:
{{ toYaml .Values.server.persistentVolume.annotations | indent 10 }}
        {{- end }}
      spec:
        accessModes:
{{ toYaml .Values.server.persistentVolume.accessModes | indent 10 }}
        resources:
          requests:
            storage: "{{ .Values.server.persistentVolume.size }}"
      {{- if .Values.server.persistentVolume.storageClass }}
      {{- if (eq "-" .Values.server.persistentVolume.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.server.persistentVolume.storageClass }}"
      {{- end }}
      {{- end }}
      {{- with .Values.server.persistentVolume.matchLabels }}
        selector:
          matchLabels:
            {{- toYaml . | nindent 12 }}
      {{- end }}
{{- end }}
{{- end }}
