apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: vibecarbon-app
    component: application
spec:
  replicas: 2
  # 900s (15m) — cold k8s deploys race the rollout window: `kubectl set image`
  # triggers the rollout BEFORE the db StatefulSet is Ready (a cold CSI attach
  # plus the image pull), and the app's readiness probe (/api/health/ready)
  # stays 503 until the db answers and its migrations are applied. On the slow
  # HA path the sideload + db install once consumed >29m; the old
  # 120s deadline tripped before the db existed and the subsequent
  # `kubectl rollout status` failed even though pods were healthy by then.
  # 900s covers worst-observed cold path; warm rollouts still finish in <60s
  # so this only matters on the cold cycle.
  progressDeadlineSeconds: 900
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  selector:
    matchLabels:
      app: vibecarbon-app
  template:
    metadata:
      labels:
        app: vibecarbon-app
        component: application
    spec:
      terminationGracePeriodSeconds: 30
      serviceAccountName: vibecarbon-app
      # The migrate step (src/server/db/migrate-cli.ts): applies db/migrations
      # as the owner, postgres, then creates or updates the app's own role,
      # app_service, with APP_DB_PASSWORD. It is the only container in the pod
      # that reads the owner password, and its env is not shared with the app
      # container. Replicas take turns on the migrations advisory lock; on a
      # standby (read-only database) it exits 0 at once. While the db is not
      # up yet it waits; a failed migration exits 1.
      initContainers:
      - name: migrate
        image: ghcr.io/{{GITHUB_OWNER}}/{{PROJECT_NAME}}:main
        imagePullPolicy: IfNotPresent
        command: ["node", "dist/server/migrate-cli.js"]
        securityContext:
          runAsNonRoot: true
          runAsUser: 1000
          runAsGroup: 1000
          readOnlyRootFilesystem: true
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
        env:
        - name: NODE_ENV
          value: production
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_HOST
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_PORT
        - name: DB_NAME
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_NAME
        - name: DB_USER
          value: postgres
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: vibecarbon-infra-secrets
              key: DB_PASSWORD
        - name: APP_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: vibecarbon-app-secrets
              key: APP_DB_PASSWORD
        resources:
          requests:
            cpu: 50m
            memory: 128Mi
          limits:
            cpu: 500m
            memory: 256Mi
        volumeMounts:
        - name: tmp
          mountPath: /tmp
      containers:
      - name: app
        image: ghcr.io/{{GITHUB_OWNER}}/{{PROJECT_NAME}}:main
        imagePullPolicy: IfNotPresent
        securityContext:
          runAsNonRoot: true
          runAsUser: 1000
          runAsGroup: 1000
          readOnlyRootFilesystem: true
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
        ports:
        - containerPort: 3000
          name: http
        envFrom:
        - configMapRef:
            name: vibecarbon-config
        # The app's own runtime keys (auth, admin, billing/OAuth/SMTP, its
        # storage keys) live in vibecarbon-app-secrets, built from one
        # allowlist (src/lib/deploy/app-runtime-env.js); envFrom makes them
        # available without a per-key list here. Infra passwords live in
        # vibecarbon-infra-secrets, which the app never reads whole.
        # Explicit env entries below still win on conflict.
        - secretRef:
            name: vibecarbon-app-secrets
        env:
        - name: NODE_ENV
          value: production
        - name: SITE_URL
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: SITE_URL
        # Auth runs in-process (src/server/auth/): its public URL is SITE_URL,
        # and BETTER_AUTH_SECRET (session signing key) arrives through the
        # vibecarbon-app-secrets envFrom above.
        - name: BETTER_AUTH_URL
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: SITE_URL
        # The API server talks to Postgres directly (src/server/db/client.ts),
        # as its own role, app_service, with APP_DB_PASSWORD from the app's
        # Secret: never as the owner, whose password only the migrate init
        # container reads. Named explicitly, not left to envFrom, so a stray
        # DB_HOST in the Secret (e.g. the local-dev localhost) can never win.
        # The host is the `db` Service (k8s/base/db/service.yaml).
        - name: DB_HOST
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_HOST
        - name: DB_PORT
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_PORT
        - name: DB_NAME
          valueFrom:
            configMapKeyRef:
              name: vibecarbon-config
              key: DB_NAME
        - name: DB_USER
          value: app_service
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: vibecarbon-app-secrets
              key: APP_DB_PASSWORD
        resources:
          requests:
            cpu: 100m
            memory: 256Mi
          limits:
            cpu: 1000m
            memory: 512Mi
        startupProbe:
          httpGet:
            path: /api/health
            port: http
          failureThreshold: 5
          periodSeconds: 1
        livenessProbe:
          httpGet:
            path: /api/health
            port: http
          initialDelaySeconds: 10
          periodSeconds: 30
          timeoutSeconds: 5
          failureThreshold: 5
        readinessProbe:
          httpGet:
            path: /api/health/ready
            port: http
          initialDelaySeconds: 0
          periodSeconds: 3
          timeoutSeconds: 3
          failureThreshold: 3
        lifecycle:
          preStop:
            exec:
              command:
              - sh
              - -c
              - sleep 5
        volumeMounts:
        - name: tmp
          mountPath: /tmp
      volumes:
      - name: tmp
        emptyDir: {}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: vibecarbon-app
              topologyKey: kubernetes.io/hostname
      securityContext:
        seccompProfile:
          type: RuntimeDefault
