{{- /*
HorizontalPodAutoscaler — Catalyst overlay for the LiveKit Deployment.
DEFAULT FALSE.

The upstream livekit-server chart exposes its own
`livekit-server.autoscaling.enabled` knob (kept defaulted false in
values.yaml). This Catalyst-side HPA template is the forward-
compatibility guard for a Catalyst-authored autoscaling shape (e.g.
scaling on custom signaling-room metrics rather than CPU/memory).

NB: LiveKit's resource constraints — only one instance per physical
node fits because of the SFU port range exclusivity — mean horizontal
scaling typically requires per-node placement, not per-pod scaling.
The HPA shape here is for clusters running multiple LiveKit pods
across multiple nodes; solo-Sovereigns stay at the single-replica
baseline.

Per docs/INVIOLABLE-PRINCIPLES.md #4 every knob (min/max replicas,
CPU/memory targets, target Deployment name) is operator-tunable.
*/}}
{{- if .Values.hpa.enabled }}
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: bp-livekit
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "bp-livekit.labels" . | nindent 4 }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ default (include "bp-livekit.fullname" .) .Values.hpa.targetDeploymentName }}
  minReplicas: {{ .Values.hpa.minReplicas }}
  maxReplicas: {{ .Values.hpa.maxReplicas }}
  metrics:
    {{- if .Values.hpa.targetCPUUtilizationPercentage }}
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: {{ .Values.hpa.targetCPUUtilizationPercentage }}
    {{- end }}
    {{- if .Values.hpa.targetMemoryUtilizationPercentage }}
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: {{ .Values.hpa.targetMemoryUtilizationPercentage }}
    {{- end }}
{{- end }}
