apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .Release.Name }}-server
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "chart_labels" . | indent 4 }}
spec:
  serviceName: {{ .Release.Name }}-server
  replicas: {{ .Values.server.replicas | default 1 }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}-server
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-server
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "{{ .Values.server.metricsPort }}"
        prometheus.io/path: "/metrics"
    spec:
      containers:
        - env:
            - name: GPUSTACK_SERVER_ID
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.name
            - name: GPUSTACK_GATEWAY_PLUGIN_SERVER_URL
              value: "http://{{ printf "%s-higress-plugins.%s.svc.%s:8080" .Release.Name .Release.Namespace .Values.clusterDomain }}"
            - name: GPUSTACK_GATEWAY_INGRESS_CLASS
              value: "{{ .Values.gateway.ingressClassname }}"
            - name: GPUSTACK_GATEWAY_MIRROR_INGRESS_NAME
              value: "{{ .Release.Name }}"
            - name: GPUSTACK_GATEWAY_MODE
              value: incluster
            {{- if index .Values "higress-core" "enabled" }}
            - name: GPUSTACK_GATEWAY_NAMESPACE
              value: "{{ .Release.Namespace }}"
            {{- end }}
            - name: GPUSTACK_DEFAULT_CLUSTER_KUBERNETES
              value: 'true'
            - name: GPUSTACK_DISABLE_WORKER
              value: 'true'
            - name: GPUSTACK_SERVICE_DISCOVERY_NAME
              value: "{{ .Release.Name }}-server"
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: metadata.namespace
          envFrom:
            {{- if .Values.worker.enabled }}
            - secretRef:
                name: registration-token
                optional: false
            {{- end }}
            - secretRef:
                name: {{ .Release.Name }}-jwt-key
                optional: false
            - configMapRef:
                name: server-config
                optional: true
          ports:
            - containerPort: {{ .Values.server.apiPort }}
              name: http
              protocol: TCP
            - containerPort: {{ .Values.server.metricsPort }}
              name: metrics
              protocol: TCP
          image: {{ include "gpustack.image" . }}:{{ include "gpustack.imageTag" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          name: {{ .Release.Name }}-server
          resources: {}
          securityContext:
            allowPrivilegeEscalation: false
            privileged: false
            readOnlyRootFilesystem: false
            runAsNonRoot: false
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
            - mountPath: /var/lib/gpustack
              name: gpustack-data-dir
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccountName: {{ .Release.Name }}-server
      terminationGracePeriodSeconds: 30
      {{- if .Values.server.dataVolume.hostPath }}
      volumes:
        - name: gpustack-data-dir
          hostPath:
            path: {{ .Values.server.dataVolume.hostPath }}
            type: DirectoryOrCreate
      {{- end }}
  {{- if not .Values.server.dataVolume.hostPath }}
  volumeClaimTemplates:
    - metadata:
        name: gpustack-data-dir
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: {{ .Values.server.dataVolume.size | default "10Gi" }}
  {{- end }}
