{{- if and .Values.portal.enabled .Values.distributed.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "graphscope-store.portal.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: portal
    {{- 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.portal.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: portal
  serviceName: {{ printf "%s-portal-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: portal
        {{- if .Values.portal.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.podLabels "context" $) | nindent 8 }}
        {{- end }}
      annotations:
        {{- if (include "graphscope-store.createConfigmap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.portal.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.portal.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
    spec:
      {{- include "graphscope-store.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.portal.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.portal.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      hostNetwork: {{ .Values.portal.hostNetwork }}
      hostIPC: {{ .Values.portal.hostIPC }}
      {{- if .Values.portal.schedulerName }}
      schedulerName: {{ .Values.portal.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" "portal" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "component" "portal" "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: coordinator
          image: {{ include "graphscope-store.portal.coordinator.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          command:
            - /etc/coordinator/setup.sh
          env:
            - name: CLUSTER_TYPE
              value: "KUBERNETES"
            - name: VERSION
              value: {{ .Chart.Version | quote }}
            - name: GROOT_USERNAME
              value: {{ .Values.auth.username | quote }}
            - name: GROOT_PASSWORD
              value: {{ .Values.auth.password | b64enc | quote }}
            - name: GROOT_GRPC_PORT
              value: {{ .Values.frontend.service.servicePort | quote }}
            - name: GROOT_GREMLIN_PORT
              value: {{ .Values.frontend.service.gremlinPort | quote }}
            - name: GROOT_CYPHER_PORT
              value: {{ .Values.frontend.service.cypherPort | quote }}
            - name: GROOT_STORE_POD_ADMIN_PORT
              value: {{ .Values.store.service.adminPort | quote }}
            - name: GROOT_FRONTEND_POD_SUFFIX
              {{- if .Values.distributed.enabled }}
              value: {{ printf "%s-frontend" (include "graphscope-store.name" .) }}
              {{- else }}
              value: {{ printf "%s-portal" (include "graphscope-store.name" .) }}
              {{- end }}
            - name: GROOT_COORDINATOR_POD_SUFFIX
              {{- if .Values.distributed.enabled }}
              value: {{ printf "%s-coordinator" (include "graphscope-store.name" .) }}
              {{- else }}
              value: {{ printf "%s-portal" (include "graphscope-store.name" .) }}
              {{- end }}
            - name: GROOT_PORTAAL_POD_SUFFIX
              {{- if .Values.distributed.enabled }}
              value: {{ printf "%s-portal" (include "graphscope-store.name" .) }}
              {{- else }}
              value: {{ printf "%s-portal" (include "graphscope-store.name" .) }}
              {{- end }}
            - name: GROOT_STORE_POD_SUFFIX
              {{- if .Values.distributed.enabled }}
              value: {{ printf "%s-store" (include "graphscope-store.name" .) }}
              {{- else }}
              value: {{ printf "%s-portal" (include "graphscope-store.name" .) }}
              {{- end }}
            - name: INSTANCE_NAME
              value: {{ .Release.Name | quote }}
            - name: NAMESPACE
              value: {{ .Release.Namespace | quote }}
            - name: SERVICE_PORT
              value: {{ .Values.portal.service.servicePort | quote }} 
            - name: GRAPHSCOPE_RUNTIME
              value: {{ .Values.portal.runtimePath | quote }}
            - name: STUDIO_WRAPPER_ENDPOINT
              value: {{ .Values.portal.studioWrapperEndpoint | quote }}
            - name: BASEID
              value: {{ .Values.portal.baseId | quote }}
            - name: SOLUTION
              value: "GRAPHSCOPE_INSIGHT"
            {{- range $key, $value := .Values.env }}
            - name: {{ $key }}
              value: {{ $value | quote }}
            {{- end }}
          ports:
            - name: http
              containerPort:  {{ .Values.portal.service.servicePort }}
          {{- if .Values.portal.resources }}
          resources: {{- toYaml .Values.portal.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          volumeMounts:
            - name: runtime
              mountPath: {{ .Values.portal.runtimePath | quote }}
            - name: config
              mountPath: /etc/coordinator/setup.sh
              subPath: portal_setup.sh
      volumes:
        - name: config
          configMap:
            name: {{ include "graphscope-store.configmapName" . }}
            defaultMode: 0755
  {{- if and .Values.portal.persistence.enabled .Values.portal.persistence.existingClaim }}
        - name: runtime
          persistentVolumeClaim:
            claimName: {{ printf "%s" (tpl .Values.portal.persistence.existingClaim .) }}
  {{- else if not .Values.portal.persistence.enabled }}
        - name: runtime
          emptyDir: {}
  {{- else if and .Values.portal.persistence.enabled (not .Values.portal.persistence.existingClaim) }}
  volumeClaimTemplates:
    - metadata:
        name: runtime
        {{- 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.portal.persistence.size | quote }}
        {{ include "graphscope-store.storageClass" . | nindent 8 }}
        {{- if .Values.portal.persistence.selector }}
        selector: {{- include "graphscope-store.tplvalues.render" (dict "value" .Values.portal.persistence.selector "context" $) | nindent 10 }}
        {{- end -}}
  {{- end }}
{{- end }}
