apiVersion: apps/v1
kind: Deployment
metadata:
  name: maya-pipecat
  labels:
    app: maya-pipecat
spec:
  replicas: 6
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 50%
      maxUnavailable: 25%
  selector:
    matchLabels:
      app: maya-pipecat
  template:
    metadata:
      labels:
        app: maya-pipecat
      annotations:
        # Filter out Daily SDK Rust-level noise + uvloop epoll noise from Datadog logs
        ad.datadoghq.com/maya-pipecat.logs: |
          [{
            "source": "maya-pipecat",
            "service": "maya-pipecat",
            "log_processing_rules": [
              {
                "type": "exclude_at_match",
                "name": "exclude_noise",
                "pattern": "Failed to send LogLine|ResponseCanceled|Metrics failed to get snapshot|daily_core|epoll_wait: Invalid argument|epoll_wait: Bad file descriptor"
              }
            ]
          }]
    spec:
      # Schedule on high-CPU nodes (c7g.16xlarge - 64 vCPU, 128GB)
      nodeSelector:
        eks.amazonaws.com/nodegroup: pipecat-highcpu
      containers:
      - name: maya-pipecat
        image: 878302603534.dkr.ecr.ap-south-1.amazonaws.com/maya-pipecat:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 3008
        resources:
          # 2 workers + DD DaemonSet (agent removed from container)
          requests:
            memory: "6Gi"
            cpu: "2000m"
          limits:
            memory: "8Gi"
            cpu: "8000m"
        livenessProbe:
          httpGet:
            path: /health
            port: 3008
          initialDelaySeconds: 30
          periodSeconds: 15
          timeoutSeconds: 15
        readinessProbe:
          httpGet:
            path: /ready
            port: 3008
          initialDelaySeconds: 15
          periodSeconds: 5
          timeoutSeconds: 10
          # Pod removed from Service after 3 consecutive failures (15s)
          # Pod added back after 1 success
          failureThreshold: 3
          successThreshold: 1
        env:
        - name: DD_AGENT_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP
        - name: REDIS_ENABLED
          value: "true"
        - name: REDIS_URL
          valueFrom:
            secretKeyRef:
              name: maya-redis-secret
              key: redis-url
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
---
apiVersion: v1
kind: Service
metadata:
  name: maya-pipecat
  labels:
    app: maya-pipecat
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 3008
    protocol: TCP
  selector:
    app: maya-pipecat
---
# HPA based on CPU (CPU-bound app - VAD inference)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: maya-pipecat-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: maya-pipecat
  minReplicas: 30
  maxReplicas: 120
  metrics:
  # Primary: CPU (app is CPU-bound due to VAD)
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  # Secondary: Memory (scale when memory pressure increases)
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 15
      policies:
      - type: Pods
        value: 4
        periodSeconds: 15
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 1
        periodSeconds: 60
