{{- if .Values.distributed.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "graphscope-store.store.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: store
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  podManagementPolicy: {{ .Values.podManagementPolicy }}
  replicas: {{ .Values.store.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: store
  serviceName: {{ printf "%s-store-headless" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  updateStrategy: {{- include "common.tplvalues.render" (dict "value" .Values.updateStrategy "context" $ ) | nindent 4 }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: store
        {{- if .Values.store.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.store.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        {{- if (include "graphscope-store.createConfigmap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.store.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.store.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "graphscope-store.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.store.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.store.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.store.hostNetwork }}
      hostIPC: {{ .Values.store.hostIPC }}
      {{- if .Values.store.schedulerName }}
      schedulerName: {{ .Values.store.schedulerName | quote }}
      {{- end }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "component" "store" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "store" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.dnsPolicy }}
      dnsPolicy: {{ .Values.dnsPolicy | quote }}
      {{- end }}
      {{- if .Values.dnsConfig }}
      dnsConfig: {{- include "common.tplvalues.render" (dict "value" .Values.dnsConfig "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "graphscope-store.serviceAccountName" . }}
      {{- if .Values.initContainers }}
      initContainers:
        {{- if .Values.initContainers }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.initContainers "context" $ ) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: default
          image: {{ include "graphscope-store.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- end }}
          env:
            - name: ROLE
              value: "store"
            - name: STORE_COUNT
              value: "{{ .Values.store.replicaCount }}"
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            {{- if .Values.store.service.adminPort }}
            - name: GROOT_STORE_POD_ADMIN_PORT
              value: {{ .Values.store.service.adminPort | quote }}
            {{- end }}
          ports:
            - name: inner-rpc
              containerPort: 55555
            - name: gaia-rpc
              containerPort: 60000
            - name: gaia-engine
              containerPort: 60001
          {{- if .Values.store.resources }}
          resources: {{- toYaml .Values.store.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: gaia-rpc
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: gaia-rpc
          {{- end }}
          {{- if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: gaia-rpc
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: {{ .Values.storeDataPath }}
            - name: config
              mountPath: /etc/groot/groot.config.tpl
              subPath: groot.config
            - name: config
              mountPath: /etc/groot/setup.sh
              subPath: setup.sh
            - name: log
              mountPath: {{ .Values.logPersistence.mountPath }}
            {{- if .Values.store.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.store.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
      volumes:
        - name: config
          configMap:
            name: {{ include "graphscope-store.configmapName" . }}
            defaultMode: 0755
        {{- if .Values.store.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.store.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
        {{- if and .Values.store.persistence.enabled .Values.store.persistence.existingClaim }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ printf "%s" (tpl .Values.store.persistence.existingClaim .) }}
        {{- else if not .Values.store.persistence.enabled }}
        - name: data
          emptyDir: {}
        {{- end }}
        {{- if not .Values.logPersistence.enabled }}
        - name: log
          emptyDir: {}
        {{- else if .Values.logPersistence.existingClaim }}
        - name: log
          persistentVolumeClaim:
            claimName: {{ printf "%s" (tpl .Values.logPersistence.existingClaim .) }}
        {{- end }}
  volumeClaimTemplates:
    {{- if and .Values.store.persistence.enabled (not .Values.store.persistence.existingClaim) }}
    - metadata:
        name: data
        {{- if .Values.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
        {{- if .Values.persistence.labels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.labels "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.store.persistence.size | quote }}
        {{ include "graphscope-store.storageClass" . | nindent 8 }}
        {{- if .Values.store.persistence.selector }}
        selector: {{- include "graphscope-store.tplvalues.render" (dict "value" .Values.store.persistence.selector "context" $) | nindent 10 }}
        {{- end -}}
    {{- end }}
    {{- if and .Values.logPersistence.enabled (not .Values.logPersistence.existingClaim) }}
    - metadata:
        name: log
        {{- if .Values.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.annotations "context" $) | nindent 10 }}
        {{- end }}
        {{- if .Values.persistence.labels }}
        labels: {{- include "common.tplvalues.render" (dict "value" .Values.persistence.labels "context" $) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.logPersistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.logPersistence.size | quote }}
        {{ include "graphscope-store.storageClass" . | nindent 8 }}
        {{- if .Values.logPersistence.selector }}
        selector: {{- include "graphscope-store.tplvalues.render" (dict "value" .Values.logPersistence.selector "context" $) | nindent 10 }}
        {{- end -}}
    {{- end }}
{{- end -}}
