# ─────────────────────────────────────────────────────────────
# EDDI — Kubernetes Quickstart (All-in-One)
#
# Deploy EDDI + MongoDB with a single command:
#
#   kubectl apply -f https://raw.githubusercontent.com/labsai/EDDI/main/k8s/quickstart.yaml
#
# After deploying, generate the vault master key:
#
#   kubectl create secret generic eddi-secrets \
#     --namespace=eddi \
#     --from-literal=EDDI_VAULT_MASTER_KEY="$(openssl rand -base64 24)"
#
# Then restart EDDI to pick up the key:
#
#   kubectl rollout restart deployment/eddi -n eddi
#
# Access EDDI:
#
#   kubectl port-forward svc/eddi 7070:7070 -n eddi
#   open http://localhost:7070
#
# ─────────────────────────────────────────────────────────────

# --- Namespace ---
apiVersion: v1
kind: Namespace
metadata:
  name: eddi
  labels:
    app.kubernetes.io/part-of: eddi

---
# --- ConfigMap ---
apiVersion: v1
kind: ConfigMap
metadata:
  name: eddi-config
  namespace: eddi
  labels:
    app.kubernetes.io/name: eddi
    app.kubernetes.io/component: server
data:
  EDDI_DATASTORE_TYPE: "mongodb"
  MONGODB_CONNECTIONSTRING: "mongodb://mongodb:27017/eddi?retryWrites=true&w=majority&connectTimeoutMS=10000&socketTimeoutMS=30000"
  EDDI_MESSAGING_TYPE: "in-memory"
  QUARKUS_HTTP_CORS_ORIGINS: "http://localhost:3000,http://localhost:7070"
  QUARKUS_OIDC_TENANT_ENABLED: "false"

---
# --- EDDI Secret (placeholder — see instructions above) ---
apiVersion: v1
kind: Secret
metadata:
  name: eddi-secrets
  namespace: eddi
  labels:
    app.kubernetes.io/name: eddi
type: Opaque
stringData:
  EDDI_VAULT_MASTER_KEY: ""

---
# --- MongoDB Service ---
apiVersion: v1
kind: Service
metadata:
  name: mongodb
  namespace: eddi
  labels:
    app.kubernetes.io/name: mongodb
    app.kubernetes.io/component: database
    app.kubernetes.io/part-of: eddi
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app.kubernetes.io/name: mongodb
    app.kubernetes.io/component: database
  ports:
    - name: mongo
      port: 27017
      targetPort: mongo

---
# --- MongoDB StatefulSet ---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
  namespace: eddi
  labels:
    app.kubernetes.io/name: mongodb
    app.kubernetes.io/component: database
    app.kubernetes.io/part-of: eddi
spec:
  serviceName: mongodb
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: mongodb
      app.kubernetes.io/component: database
  template:
    metadata:
      labels:
        app.kubernetes.io/name: mongodb
        app.kubernetes.io/component: database
        app.kubernetes.io/part-of: eddi
    spec:
      securityContext:
        fsGroup: 999
      containers:
        - name: mongodb
          image: mongo:7.0
          ports:
            - containerPort: 27017
              name: mongo
          volumeMounts:
            - name: mongodb-data
              mountPath: /data/db
          resources:
            requests:
              cpu: 100m
              memory: 256Mi
            limits:
              cpu: "1"
              memory: 1Gi
          readinessProbe:
            exec:
              command: ["mongosh", "--eval", "db.adminCommand('ping')"]
            initialDelaySeconds: 5
            periodSeconds: 10
  volumeClaimTemplates:
    - metadata:
        name: mongodb-data
      spec:
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 10Gi

---
# --- EDDI Service ---
apiVersion: v1
kind: Service
metadata:
  name: eddi
  namespace: eddi
  labels:
    app.kubernetes.io/name: eddi
    app.kubernetes.io/component: server
    app.kubernetes.io/part-of: eddi
spec:
  type: ClusterIP
  selector:
    app.kubernetes.io/name: eddi
    app.kubernetes.io/component: server
  ports:
    - name: http
      port: 7070
      targetPort: http

---
# --- EDDI Deployment ---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eddi
  namespace: eddi
  labels:
    app.kubernetes.io/name: eddi
    app.kubernetes.io/version: "6.1.0"
    app.kubernetes.io/component: server
    app.kubernetes.io/part-of: eddi
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app.kubernetes.io/name: eddi
      app.kubernetes.io/component: server
  template:
    metadata:
      labels:
        app.kubernetes.io/name: eddi
        app.kubernetes.io/version: "6.1.0"
        app.kubernetes.io/component: server
        app.kubernetes.io/part-of: eddi
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "7070"
        prometheus.io/path: "/q/metrics"
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 185
        runAsGroup: 185
        fsGroup: 185
      terminationGracePeriodSeconds: 30
      containers:
        - name: eddi
          image: labsai/eddi:6
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 7070
              name: http
              protocol: TCP
          envFrom:
            - configMapRef:
                name: eddi-config
            - secretRef:
                name: eddi-secrets
          startupProbe:
            httpGet:
              path: /q/health/ready
              port: http
            failureThreshold: 30
            periodSeconds: 2
          livenessProbe:
            httpGet:
              path: /q/health/live
              port: http
            initialDelaySeconds: 5
            periodSeconds: 15
            timeoutSeconds: 3
          readinessProbe:
            httpGet:
              path: /q/health/ready
              port: http
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 3
          resources:
            requests:
              cpu: 250m
              memory: 384Mi
            limits:
              cpu: "2"
              memory: 1Gi
