# =============================================================================
# AWOOOI HorizontalPodAutoscaler
# =============================================================================
# K3.2 2026-03-28: HPA for API/Web (based on VPA recommendations)
# P3 2026-03-29: maxReplicas 4 → 6 (首席架構師建議)
# K-Worker 2026-03-29: Worker HPA (min:1 max:3, CPU 指標)
# Deployed by: Claude Code (首席架構師)
# VPA 建議: API target 100m, Web target 63m
# =============================================================================
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: awoooi-api-hpa
  namespace: awoooi-prod
  labels:
    app.kubernetes.io/name: awoooi
    app.kubernetes.io/component: api
  annotations:
    description: "API 水平自動擴展 (2-6 replicas, 70% CPU)"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: awoooi-api
  minReplicas: 2
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Pods
          value: 1
          periodSeconds: 120
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: awoooi-web-hpa
  namespace: awoooi-prod
  labels:
    app.kubernetes.io/name: awoooi
    app.kubernetes.io/component: web
  annotations:
    description: "Web 水平自動擴展 (2-6 replicas, 70% CPU)"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: awoooi-web
  minReplicas: 2
  maxReplicas: 6
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 60
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Pods
          value: 1
          periodSeconds: 120
---
# =============================================================================
# Worker HPA（K-Worker 2026-03-29）
# =============================================================================
# 前置條件: ADR-038/039 Wave 1 完成（XCLAIM + terminationGracePeriodSeconds:90）
# 說明: Worker 消費 Redis Streams，使用 CPU 指標（未來可升級 KEDA）
# =============================================================================
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: awoooi-worker-hpa
  namespace: awoooi-prod
  labels:
    app.kubernetes.io/name: awoooi
    app.kubernetes.io/component: worker
  annotations:
    description: "Worker 水平自動擴展 (1-3 replicas, 70% CPU)"
    note: "未來可升級 KEDA Redis Stream 指標按 Queue 長度擴縮"
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: awoooi-worker
  minReplicas: 1
  maxReplicas: 3
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 120
      policies:
        - type: Pods
          value: 1
          periodSeconds: 120
    scaleDown:
      stabilizationWindowSeconds: 600
      policies:
        - type: Pods
          value: 1
          periodSeconds: 300
