# SPDX-License-Identifier: Apache-2.0
# Copyright 2022 The HuggingFace Authors.

{{- define "hpaWorker" -}}
{{- if .workerValues.autoscaling.enabled }}
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  labels: {{ include "labels.worker" (merge (dict "workerValues" .workerValues) $ ) | nindent 4 }}
  name: "{{ include "name" . }}-worker-{{ .workerValues.deployName }}"
  namespace: {{ .Release.Namespace }}
spec:
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 0
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
        - type: Pods
          value: 1
          periodSeconds: 60
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: "{{ include "name" . }}-worker-{{ .workerValues.deployName }}"
  minReplicas: {{ .workerValues.autoscaling.minReplicas }}
  maxReplicas: {{ .workerValues.autoscaling.maxReplicas }}
  metrics:
    {{- range $index, $target := .workerValues.autoscaling.targets }}
    - type: External
      external:
        metric:
          name: {{ $target.targetQueueName | quote }}
          selector:
            matchLabels:
              worker_size: {{ $target.targetWorkerSize | quote }}
        target:
          value: {{ $target.targetQueueLength | int }}
          type: Value
    {{- end }}
{{- end }}
{{- end -}}
