# Gateway deployment
# Multi-channel gateway with automatic container provisioning
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
    - name: http
      protocol: TCP
      port: 3000
      targetPort: http
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway
  labels:
    app: gateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gateway
  template:
    metadata:
      labels:
        app: gateway
    spec:
      serviceAccountName: gateway

      initContainers:
        - name: wait-for-postgres
          image: busybox:1.36
          command: ['sh', '-c', 'until nc -z postgres 5432; do echo waiting for postgres; sleep 2; done;']
        - name: wait-for-dragonfly
          image: busybox:1.36
          command: ['sh', '-c', 'until nc -z dragonfly 6379; do echo waiting for dragonfly; sleep 2; done;']
        - name: wait-for-qdrant
          image: busybox:1.36
          command: ['sh', '-c', 'until nc -z qdrant 6333; do echo waiting for qdrant; sleep 2; done;']
        - name: wait-for-iceberg-catalog
          image: busybox:1.36
          command: ['sh', '-c', 'until nc -z iceberg-catalog 8181; do echo waiting for iceberg-catalog; sleep 2; done;']

      volumes:
        - name: config
          configMap:
            name: gateway-config
        - name: secrets
          secret:
            secretName: gateway-secrets

      containers:
        - name: gateway
          image: ghcr.io/dexorder/gateway:latest
          imagePullPolicy: Always

          ports:
            - name: http
              containerPort: 3000
              protocol: TCP

          volumeMounts:
            - name: config
              mountPath: /config/config.yaml
              subPath: config.yaml
              readOnly: true
            - name: secrets
              mountPath: /config/secrets.yaml
              subPath: secrets.yaml
              readOnly: true

          env:
            - name: CONFIG_PATH
              value: "/config/config.yaml"
            - name: SECRETS_PATH
              value: "/config/secrets.yaml"

          resources:
            requests:
              memory: "512Mi"
              cpu: "100m"
            limits:
              memory: "2Gi"
              cpu: "500m"

          livenessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 10
            periodSeconds: 30

          readinessProbe:
            httpGet:
              path: /health
              port: http
            initialDelaySeconds: 5
            periodSeconds: 10
