{{ if .Values.server.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: server
spec:
  replicas: 1
  serviceName: server
  selector:
    matchLabels:
      app: server
  template:
    metadata:
      name: server
      labels:
        app: server
    spec:
      # initContainers:
      # - name: init-save
      #   image: docker.io/goalprofit/server
      #   command:
      #     - sh
      #     - -c
      #     - curl -X POST server:8080/save || true
      terminationGracePeriodSeconds: 60
      containers:
      - name: olap
        image: {{.Values.server.image}}:{{.Values.server.version}}
        imagePullPolicy: Always
#        args:
#          - /data/{{.Values.customer}}/olap-rust/olap-rust
#        workingDir: /data/{{.Values.customer}}/olap-rust
        resources:
          limits:
            memory: "{{.Values.server.ram_limit}}"
          requests:
            memory: 1Gi
        env:
          - name: CUSTOMER
            value: "{{.Values.customer}}"
          - name: PORTAL
            value: "{{.Values.protocol}}{{.Values.domain}}"
          - name: RUST_BACKTRACE
            value: "1"
{{ if .Values.local_user_id }}
          - name: LOCAL_USER_ID
            value: "{{.Values.local_user_id}}"
{{ end }}
{{ if .Values.local_user_name }}
          - name: LOCAL_USER_NAME
            value: "{{.Values.local_user_name}}"
{{ end }}
          - name: SERVICE_LOGIN
            valueFrom:
              secretKeyRef:
                name: secret-service
                key: username
                optional: false
          - name: SERVICE_PASSWORD
            valueFrom:
              secretKeyRef:
                name: secret-service
                key: password
                optional: false
          - name: COOKIE_SESSION_NAME
            value: {{.Values.cookie_session_name}}
          - name: COOKIE_SESSION_NO_EXPIRES
            value: {{.Values.cookie_session_no_expires | quote}}
          - name: COOKIE_SESSION_MAX_AGE
            value: {{.Values.cookie_session_max_age | quote}}
          - name: COOKIE_SESSION_HTTP_ONLY
            value: {{.Values.cookie_session_http_only | quote}}
          - name: COOKIE_SESSION_SECURE
            value: {{.Values.cookie_session_secure | quote}}
          - name: COOKIE_SESSION_DOMAIN
            value: {{.Values.cookie_session_domain}}
          - name: COOKIE_SESSION_KEY
            valueFrom:
              secretKeyRef:
                name: cookie-session
                key: key
          - name: MYSQL_USER
            valueFrom:
              secretKeyRef:
                name: mysql
                key: user
                optional: true
          - name: MYSQL_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mysql
                key: password
                optional: true
          - name: MYSQL_HOST
            valueFrom:
              secretKeyRef:
                name: mysql
                key: host
                optional: true
          - name: MYSQL_PORT
            valueFrom:
              secretKeyRef:
                name: mysql
                key: port
                optional: true
          - name: MYSQL_DATABASE
            valueFrom:
              secretKeyRef:
                name: mysql
                key: database
                optional: true
          - name: MYSQL_SLAVE_INDEX
            value: "{{.Values.server.mysql_slave_index}}"
          - name: ORM_GRAPHIQL_ENDPOINT
            value: "{{.Values.server.orm.graphiql_endpoint}}"
          - name: ORM_QUERY_CORES
            value: "{{.Values.server.orm.query_cores}}"
          - name: ORM_QUERY_LOGGING
            value: "{{.Values.server.orm.query_logging}}"
          - name: ORM_QUERY_TIME_LOGGING
            value: "{{.Values.server.orm.query_time_logging}}"
        volumeMounts:
        - mountPath: /work
          name: customer
          subPath: data
        - mountPath: /in
          name: customer
          subPath: in
        - mountPath: /out
          name: customer
          subPath: out
        ports:
          - containerPort: 8080
{{ if .Values.server.orm.enabled }}
          - containerPort: 3000
{{ end }}
        readinessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 10
#        livenessProbe:
#          httpGet:
#            path: /pages/
#            port: 8080
#          initialDelaySeconds: 10
#          periodSeconds: 20
#          timeoutSeconds: 10
#          failureThreshold: 5
        lifecycle:
          preStop:
            exec:
              command:
                - curl
                - -X
                - POST
                - --retry
                - "3"
                - --retry-delay
                - "5"
                - --max-time
                - "120"
                - localhost:8080/save
              # timeoutSeconds: 125

      imagePullSecrets:
      - name: regcred

      volumes:
      - name: customer
        {{ include "app.customer_volume" . | nindent 8}}
      {{ include "app.affinity" . | nindent 6}}
---
apiVersion: v1
kind: Service
metadata:
  name: server
spec:
  type: ClusterIP
  selector:
    app: server
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
{{ if .Values.server.orm.enabled }}
    - name: orm
      protocol: TCP
      port: 3000
      targetPort: 3000
{{ end }}
#---
#apiVersion: v1
#kind: Service
#metadata:
#  name: server-ref
#  namespace: loki-stack
#spec:
#  type: ExternalName
#  externalName: server.{{.Values.customer}}
{{ end }}
