apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "turnstone.fullname" . }}-server
  labels:
    {{- include "turnstone.labels" . | nindent 4 }}
    app.kubernetes.io/component: server
spec:
  replicas: {{ .Values.server.replicas }}
  selector:
    matchLabels:
      {{- include "turnstone.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: server
  template:
    metadata:
      labels:
        {{- include "turnstone.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: server
    spec:
      serviceAccountName: {{ include "turnstone.serviceAccountName" . }}
      containers:
        - name: server
          image: {{ include "turnstone.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - turnstone-server
            - --host
            - "0.0.0.0"
            - --port
            - {{ .Values.server.service.port | quote }}
          ports:
            - name: http
              containerPort: {{ .Values.server.service.port }}
              protocol: TCP
          envFrom:
            - configMapRef:
                name: {{ include "turnstone.fullname" . }}-config
            - secretRef:
                name: {{ include "turnstone.llm.secretName" . }}
                optional: true
          env:
            - name: TURNSTONE_DB_URL
              value: "postgresql+psycopg://$(TURNSTONE_DB_USER):$(POSTGRES_PASSWORD)@$(TURNSTONE_DB_HOST):$(TURNSTONE_DB_PORT)/$(TURNSTONE_DB_NAME)"
          {{- if and .Values.auth.enabled .Values.auth.existingSecret }}
            - name: TURNSTONE_AUTH_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.existingSecret }}
                  key: TURNSTONE_AUTH_TOKEN
          {{- end }}
          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 15
            periodSeconds: 20
          resources:
            {{- toYaml .Values.server.resources | nindent 12 }}
