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

The upstream openmeter chart does not ship an HPA. Catalyst lands one
behind a single operator toggle so multi-tenant Sovereigns can scale
ingest throughput without diverging from the wrapper. Solo-Sovereigns
stay at the single-replica baseline declared by `openmeter.api.replicaCount`.

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-openmeter-api
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "bp-openmeter.labels" . | nindent 4 }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ default (printf "%s-api" (include "bp-openmeter.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 }}
