---
kind: Service
apiVersion: v1
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: postgrest
  annotations:
    service.beta.kubernetes.io/do-loadbalancer-name: "postgrest-${ZED_KUBE_NAMESPACE}"
    service.beta.kubernetes.io/do-loadbalancer-tls-ports: "443"
    service.beta.kubernetes.io/do-loadbalancer-certificate-id: ${ZED_DO_CERTIFICATE_ID}
    service.beta.kubernetes.io/do-loadbalancer-disable-lets-encrypt-dns-records: "true"
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - name: web
      protocol: TCP
      port: 443
      targetPort: 8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 8080
              protocol: TCP
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-config

---
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: nginx-config
data:
  nginx.conf: |
    events {}

    http {
      server {
        listen 8080;

        location /app/ {
          proxy_pass http://postgrest-app:8080/;
        }

        location /llm/ {
          proxy_pass http://postgrest-llm:8080/;
        }
      }
    }

---
apiVersion: v1
kind: Service
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: postgrest-app
spec:
  selector:
    app: postgrest-app
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

---
apiVersion: v1
kind: Service
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: postgrest-llm
spec:
  selector:
    app: postgrest-llm
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: postgrest-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgrest-app
  template:
    metadata:
      labels:
        app: postgrest-app
    spec:
      containers:
        - name: postgrest
          image: "postgrest/postgrest"
          ports:
            - containerPort: 8080
              protocol: TCP
          env:
            - name: PGRST_SERVER_PORT
              value: "8080"
            - name: PGRST_DB_URI
              valueFrom:
                secretKeyRef:
                  name: database
                  key: url
            - name: PGRST_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: postgrest
                  key: jwt_secret

---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: ${ZED_KUBE_NAMESPACE}
  name: postgrest-llm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgrest-llm
  template:
    metadata:
      labels:
        app: postgrest-llm
    spec:
      containers:
        - name: postgrest
          image: "postgrest/postgrest"
          ports:
            - containerPort: 8080
              protocol: TCP
          env:
            - name: PGRST_SERVER_PORT
              value: "8080"
            - name: PGRST_DB_URI
              valueFrom:
                secretKeyRef:
                  name: llm-database
                  key: url
            - name: PGRST_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: postgrest
                  key: jwt_secret
