{{ if .Values.orm.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: orm
spec:
  replicas: {{ .Values.orm.replicas }}
  selector:
    matchLabels:
      app: orm
  template:
    metadata:
      name: orm
      labels:
        app: orm
    spec:
      terminationGracePeriodSeconds: 1
      containers:
      - name: orm
        image: docker.io/goalprofit/orm:{{.Values.orm.version}}
        imagePullPolicy: Always
        resources:
          limits:
            memory: "{{.Values.orm.ram_limit}}"
          requests:
            memory: 1Gi
        env:
          {{- if or .Values.orm.olap_storage .Values.orm.olap_auth }}          
          - name: OLAP_USER
            valueFrom:
              secretKeyRef:
                name: secret-admin
                key: username
                optional: false
          - name: OLAP_PASSWORD
            valueFrom:
              secretKeyRef:
                name: secret-admin
                key: password
                optional: false
          {{ end }}
          {{ if .Values.orm.olap_storage }}
          - name: PERSIST_TYPE
            value: OLAP
          {{ else if .Values.orm.postgres_storage }}
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-storage-credentials
                key: username
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-storage-credentials
                key: password
          - name: POSTGRES_HOST
            valueFrom:
              secretKeyRef:
                name: postgres-storage-credentials
                key: host
          - name: POSTGRES_PORT
            valueFrom:
              secretKeyRef:
                name: postgres-storage-credentials
                key: port
          - name: POSTGRES_DATABASE
            valueFrom:
              secretKeyRef:
                name: postgres-storage-credentials
                key: database
          - name: PERSIST_TYPE
            value: POSTGRES
          {{ end }}
          {{ if .Values.orm.olap_auth }}
          - name: AUTHENTICATOR_TYPE
            value: OLAP
          {{ end }}
          - name: OLAP_URL
            value: http://server:8080/graphql
          - name: SERVER_API_URL
            value: http://server-api:8080
          - name: TOKIO_WORKER_THREADS
            value: "1024"
          - name: API_ROUTER_SCHEMA_UPDATER_URL
            value: {{ .Values.orm.schema_updater_url }}
          - name: ORM_RESTARTER_URL
            value: {{ .Values.orm.restarter_url }}
          - name: IMPORT_FILE_MAX_SIZE_MIB
            value: "{{ .Values.orm.import_file_max_size_mib }}"
        volumeMounts:
        - mountPath: /app/data
          name: customer
          subPath: data/orm
        ports:
        - containerPort: 3000
        readinessProbe:
          {{ if eq .Values.orm.readiness_check "1" }}
          tcpSocket:
            port: 3000
          {{ else if eq .Values.orm.readiness_check "2" }}
          httpGet:
            path: /_readiness
            port: 3000
          {{ end }}
          initialDelaySeconds: 5
          periodSeconds: 5
      imagePullSecrets:
      - name: regcred
        
      volumes:
      - name: customer
        {{ include "app.customer_volume" . | nindent 8}}
      {{ include "app.affinity" . | nindent 6}}
---
apiVersion: v1
kind: Service
metadata:
  name: orm
spec:
  type: ClusterIP
  selector:
    app: orm
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 3000
{{ if .Values.orm.postgres_storage }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgweb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgweb
  template:
    metadata:
      labels:
        app: pgweb
    spec:
      containers:
        - name: pgweb
          image: sosedoff/pgweb
          ports:
          - containerPort: 8081
          env:
            - name: PGWEB_DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: postgres-storage-credentials
                  key: url
---
apiVersion: v1
kind: Service
metadata:
  name: pgweb
spec:
  selector:
    app: pgweb
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8081
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: pgweb
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    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: /pgweb(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: pgweb
                port:
                  number: 80
{{ end }}
{{ end }}
