apiVersion: apps/v1
kind: Deployment
metadata:
  name: alert-processor-api
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: alert-processor-api
    app.kubernetes.io/component: api
    app.kubernetes.io/part-of: bakery-ia
spec:
  replicas: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: alert-processor-api
      app.kubernetes.io/component: api
  template:
    metadata:
      labels:
        app.kubernetes.io/name: alert-processor-api
        app.kubernetes.io/component: api
    spec:
      initContainers:
      - name: wait-for-migration
        image: postgres:17-alpine
        command:
        - sh
        - -c
        - |
          echo "Waiting for alert-processor database and migrations to be ready..."
          until pg_isready -h $ALERT_PROCESSOR_DB_HOST -p $ALERT_PROCESSOR_DB_PORT -U $ALERT_PROCESSOR_DB_USER; do
            echo "Database not ready yet, waiting..."
            sleep 2
          done
          echo "Database is ready!"
          echo "Waiting for migrations to complete..."
          sleep 10
          echo "Ready to start API service"
        env:
        - name: ALERT_PROCESSOR_DB_HOST
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: ALERT_PROCESSOR_DB_HOST
        - name: ALERT_PROCESSOR_DB_PORT
          valueFrom:
            configMapKeyRef:
              name: bakery-config
              key: DB_PORT
        - name: ALERT_PROCESSOR_DB_USER
          valueFrom:
            secretKeyRef:
              name: database-secrets
              key: ALERT_PROCESSOR_DB_USER
      containers:
      - name: alert-processor-api
        image: bakery/alert-processor:latest
        command: ["python", "-m", "uvicorn", "app.api_server:app", "--host", "0.0.0.0", "--port", "8010"]
        ports:
        - containerPort: 8010
          name: http
        envFrom:
        - configMapRef:
            name: bakery-config
        - secretRef:
            name: database-secrets
        - secretRef:
            name: redis-secrets
        - secretRef:
            name: jwt-secrets
        resources:
          requests:
            memory: "256Mi"
            cpu: "100m"
          limits:
            memory: "512Mi"
            cpu: "500m"
        readinessProbe:
          httpGet:
            path: /health
            port: 8010
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        livenessProbe:
          httpGet:
            path: /health
            port: 8010
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
  name: alert-processor-api
  namespace: bakery-ia
  labels:
    app.kubernetes.io/name: alert-processor-api
    app.kubernetes.io/component: api
    app.kubernetes.io/part-of: bakery-ia
spec:
  selector:
    app.kubernetes.io/name: alert-processor-api
    app.kubernetes.io/component: api
  ports:
  - name: http
    port: 8010
    targetPort: 8010
    protocol: TCP
  type: ClusterIP
