apiVersion: apps/v1
kind: Deployment
metadata:
  name: maya-pipecat
  labels:
    app: maya-pipecat
spec:
  replicas: 90
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 0   # Graceful: don't terminate old pods until new ones are ready
  selector:
    matchLabels:
      app: maya-pipecat
  template:
    metadata:
      labels:
        app: maya-pipecat
      annotations:
        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|daily_common_utils|epoll_wait: Invalid argument|epoll_wait: Bad file descriptor|TrySendError|Disconnected|CallManagerEventSfuSigPresence|SignalSendFailed|MessageSendFailed|took longer than 2s"
              }
            ]
          }]
    spec:
      terminationGracePeriodSeconds: 90   # Time for gunicorn to drain in-flight requests on SIGTERM
      imagePullSecrets:
      - name: ar-pull-secret
      containers:
      - name: maya-pipecat
        image: asia-south1-docker.pkg.dev/mayaresearchgcp/maya-pipecat/maya-pipecat:latest
        imagePullPolicy: Always
        lifecycle:
          preStop:
            exec:
              command: ["/bin/sh", "-c", "sleep 15"]
        ports:
        - containerPort: 3008
        resources:
          requests:
            memory: "3Gi"
            cpu: "2000m"
          limits:
            memory: "12Gi"
            cpu: "8000m"
        livenessProbe:
          httpGet:
            path: /health
            port: 3008
          initialDelaySeconds: 30
          periodSeconds: 15
          timeoutSeconds: 15
        readinessProbe:
          httpGet:
            path: /ready
            port: 3008
          initialDelaySeconds: 15
          periodSeconds: 15
          timeoutSeconds: 10
          failureThreshold: 3
          successThreshold: 1
        envFrom:
        - secretRef:
            name: maya-env-secrets
        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
        volumeMounts:
        - name: gcp-creds
          mountPath: /app/gcp-service-account.json
          subPath: gcp-service-account.json
          readOnly: true
        - name: gcp-creds
          mountPath: /app/gcp-new-service-account.json
          subPath: gcp-new-service-account.json
          readOnly: true
      volumes:
      - name: gcp-creds
        secret:
          secretName: gcp-credentials
---
apiVersion: v1
kind: Service
metadata:
  name: maya-pipecat
  labels:
    app: maya-pipecat
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 3008
    protocol: TCP
  selector:
    app: maya-pipecat
---
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:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - 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
