{{ if or .Values.de2.enabled .Values.fusion.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: server-api-history
spec:
  replicas: 1
  selector:
    matchLabels:
      app: server-api-history
  template:
    metadata:
      name: server-api-history
      labels:
        app: server-api-history
    spec:
      terminationGracePeriodSeconds: 1
      containers:
      - name: server-api-history
        image: docker.io/goalprofit/server-api:{{.Values.server_api_history.version}}
        imagePullPolicy: Always
        env:
          - name: SERVICE_LOGIN
            valueFrom:
              secretKeyRef:
                name: secret-admin
                key: username
                optional: false
          - name: SERVICE_PASSWORD
            valueFrom:
              secretKeyRef:
                name: secret-admin
                key: password
                optional: false
          - name: REDIS_URL
            value: server-api-redis-master:6379
          - name: REDIS_USER
            value: default
          - name: REDIS_PASSWORD
            valueFrom:
              secretKeyRef:
                name: server-api-redis
                key: redis-password
                optional: true
          - name: QUERY_CORES
            value: "{{.Values.server_api.query_cores}}"
          - name: QUERY_TIME_LOGGING
            value: "{{.Values.server_api.query_time_logging}}"
          - name: QUERY_LOGGING
            value: "{{.Values.server_api.query_logging}}"
          - name: CODES_IN_CHUNK
            value: "{{.Values.server_api.codes_in_chunk}}"
          - name: OLAP_TIMEOUT
            value: "{{.Values.server_api.olap_timeout}}"
          - name: NODE_OPTIONS
            value: "--max-old-space-size={{.Values.server_api.node_max_old_space_size}}"
      imagePullSecrets:
      - name: regcred
        
      {{ include "app.affinity" . | nindent 6}}
---
apiVersion: v1
kind: Service
metadata:
  name: server-api-history
spec:
  type: ClusterIP
  selector:
    app: server-api-history
  ports:
    - name: http
      protocol: TCP
      port: 8080
      targetPort: 8080
    {{ if .Values.server_api.debug }}
    - name: debug
      protocol: TCP
      port: 9229
      targetPort: 9229
    {{ end }}
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: history-api-to-server-api
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/configuration-snippet: |
      auth_request /authenticator/check-basic;
      error_page 401 =302 /authenticator/login?redirect=$request_uri;
spec:
  ingressClassName: nginx
  rules:
    - host: {{ .Values.domain }}
      http:
        paths:
          - path: /history-api
            pathType: Prefix
            backend:
              service:
                name: server-api-history
                port:
                  number: 8080
{{ end }}