# ============================================================
# Velocity-OS — K3s Deployments
# All services in velocity-os namespace.
# GPU: RTX 6000 Blackwell 96GB VRAM — MIG partitioned.
#   MIG slice 0 (48GB): SGLang LLM inference (core-api)
#   MIG slice 1 (48GB): ComfyUI media generation (media-engine)
# ============================================================

---
# ── PostgreSQL (StatefulSet for stable identity) ─────────────
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
  namespace: velocity-os
  labels:
    app: postgres
    tier: database
spec:
  serviceName: postgres
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
        tier: database
    spec:
      containers:
        - name: postgres
          image: ${ECR_REGISTRY}/postgres:15-alpine
          ports:
            - containerPort: 5432
          env:
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: velocity-secrets
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: velocity-secrets
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: velocity-secrets
                  key: POSTGRES_PASSWORD
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          resources:
            requests:
              memory: "1Gi"
              cpu: "500m"
            limits:
              memory: "2Gi"
              cpu: "1000m"
          volumeMounts:
            - name: postgres-data
              mountPath: /var/lib/postgresql/data
          livenessProbe:
            exec:
              command: [pg_isready, -U, velocity]
            initialDelaySeconds: 30
            periodSeconds: 10
          readinessProbe:
            exec:
              command: [pg_isready, -U, velocity]
            initialDelaySeconds: 5
            periodSeconds: 5
      volumes:
        - name: postgres-data
          persistentVolumeClaim:
            claimName: pvc-postgres-data

---
# ── Redis (session cache, future queue) ──────────────────────
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  namespace: velocity-os
  labels:
    app: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - name: redis
          image: ${ECR_REGISTRY}/redis:7-alpine
          ports:
            - containerPort: 6379
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "512Mi"
              cpu: "250m"
          args: ["--maxmemory", "400mb", "--maxmemory-policy", "allkeys-lru"]

---
# ── Core API (FastAPI) ────────────────────────────────────────
apiVersion: apps/v1
kind: Deployment
metadata:
  name: core-api
  namespace: velocity-os
  labels:
    app: core-api
    tier: backend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: core-api
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  template:
    metadata:
      labels:
        app: core-api
        tier: backend
    spec:
      # MIG slice 0: SGLang LLM inference
      # The core-api pod requests MIG slice via resource limit
      runtimeClassName: nvidia
      containers:
        - name: core-api
          image: ${ECR_REGISTRY}/velocity-os/core:latest
          ports:
            - containerPort: 8443
          envFrom:
            - secretRef:
                name: velocity-secrets
            - configMapRef:
                name: velocity-config
          resources:
            requests:
              memory: "1Gi"
              cpu: "500m"
              # RTX 6000 Blackwell MIG 3g.48gb (SGLang slice)
              nvidia.com/mig-3g.48gb: "1"
            limits:
              memory: "2Gi"
              cpu: "1000m"
              nvidia.com/mig-3g.48gb: "1"
          volumeMounts:
            - name: asset-store
              mountPath: /opt/assets
            - name: model-cache
              mountPath: /opt/models
              readOnly: true
          livenessProbe:
            httpGet:
              path: /health
              port: 8443
            initialDelaySeconds: 20
            periodSeconds: 15
          readinessProbe:
            httpGet:
              path: /health
              port: 8443
            initialDelaySeconds: 10
            periodSeconds: 5
      volumes:
        - name: asset-store
          persistentVolumeClaim:
            claimName: pvc-asset-store
        - name: model-cache
          persistentVolumeClaim:
            claimName: pvc-model-cache

---
# ── WebOS (Nginx static + React) ─────────────────────────────
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webos
  namespace: velocity-os
  labels:
    app: webos
    tier: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: webos
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  template:
    metadata:
      labels:
        app: webos
        tier: frontend
    spec:
      containers:
        - name: webos
          image: ${ECR_REGISTRY}/velocity-os/webos:latest
          ports:
            - containerPort: 80
          resources:
            requests:
              memory: "128Mi"
              cpu: "100m"
            limits:
              memory: "256Mi"
              cpu: "250m"
          livenessProbe:
            httpGet:
              path: /health.txt
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 10

---
# ── Media Engine (Dream Weaver Gateway) ──────────────────────
apiVersion: apps/v1
kind: Deployment
metadata:
  name: media-engine
  namespace: velocity-os
  labels:
    app: media-engine
    tier: ai
spec:
  replicas: 1
  selector:
    matchLabels:
      app: media-engine
  template:
    metadata:
      labels:
        app: media-engine
        tier: ai
    spec:
      # MIG slice 1: ComfyUI media generation
      runtimeClassName: nvidia
      containers:
        - name: media-engine
          image: ${ECR_REGISTRY}/velocity-os/media-engine:latest
          ports:
            - containerPort: 8290
          envFrom:
            - secretRef:
                name: velocity-secrets
            - configMapRef:
                name: velocity-config
          resources:
            requests:
              memory: "2Gi"
              cpu: "1000m"
              # RTX 6000 Blackwell MIG 3g.48gb (ComfyUI slice)
              nvidia.com/mig-3g.48gb: "1"
            limits:
              memory: "4Gi"
              cpu: "2000m"
              nvidia.com/mig-3g.48gb: "1"
          volumeMounts:
            - name: model-cache
              mountPath: /opt/models
              readOnly: true
            - name: asset-store
              mountPath: /opt/assets
          livenessProbe:
            httpGet:
              path: /health
              port: 8290
            initialDelaySeconds: 30
            periodSeconds: 30
      volumes:
        - name: model-cache
          persistentVolumeClaim:
            claimName: pvc-model-cache
        - name: asset-store
          persistentVolumeClaim:
            claimName: pvc-asset-store

---
# ── DB Init Job (runs once: schema apply + seed) ─────────────
apiVersion: batch/v1
kind: Job
metadata:
  name: db-init
  namespace: velocity-os
  labels:
    app: db-init
spec:
  # Never auto-restart; operator re-runs manually if needed
  backoffLimit: 0
  template:
    metadata:
      labels:
        app: db-init
    spec:
      restartPolicy: Never
      initContainers:
        # Wait for postgres to be ready before running init
        - name: wait-for-postgres
          image: ${ECR_REGISTRY}/postgres:15-alpine
          command: [sh, -c, "until pg_isready -h postgres -U $(POSTGRES_USER); do echo waiting...; sleep 2; done"]
          envFrom:
            - secretRef:
                name: velocity-secrets
      containers:
        - name: db-init
          image: ${ECR_REGISTRY}/velocity-os/core:latest
          command:
            - sh
            - -c
            - |
              echo "=== Applying schemas ==="
              psql $DATABASE_URL -f /app/db/schema.sql
              psql $DATABASE_URL -f /app/db/schema_addendum.sql
              psql $DATABASE_URL -f /app/db/schema_comms.sql
              psql $DATABASE_URL -f /app/db/schema_crm_canonical.sql
              psql $DATABASE_URL -f /app/oracle/schema_oracle.sql
              psql $DATABASE_URL -f /app/oracle/schema_extension_v2.sql
              echo "=== Seeding synthetic CRM v2 ==="
              python /app/scripts/seed_synthetic_crm.py
              echo "=== DB init complete ==="
          envFrom:
            - secretRef:
                name: velocity-secrets
            - configMapRef:
                name: velocity-config
