{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.queryCoord.autoscaling.hpa.enabled }}
apiVersion: {{ include "common.capabilities.hpa.apiVersion" ( dict "context" $ ) }}
kind: HorizontalPodAutoscaler
metadata:
  name: {{ template "milvus.query-coordinator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: milvus
    app.kubernetes.io/component: query-coordinator
  {{- if or .Values.commonAnnotations .Values.queryCoord.autoscaling.hpa.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.queryCoord.autoscaling.hpa.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  scaleTargetRef:
    apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
    kind: Deployment
    name: {{ template "milvus.query-coordinator.fullname" . }}
  minReplicas: {{ .Values.queryCoord.autoscaling.hpa.minReplicas }}
  maxReplicas: {{ .Values.queryCoord.autoscaling.hpa.maxReplicas }}
  metrics:
    {{- if .Values.queryCoord.autoscaling.hpa.targetCPU }}
    - type: Resource
      resource:
        name: cpu
        {{- if semverCompare "<1.23-0" (include "common.capabilities.kubeVersion" .) }}
        targetAverageUtilization: {{ .Values.queryCoord.autoscaling.hpa.targetCPU }}
        {{- else }}
        target:
          type: Utilization
          averageUtilization: {{ .Values.queryCoord.autoscaling.hpa.targetCPU }}
        {{- end }}
    {{- end }}
    {{- if .Values.queryCoord.autoscaling.hpa.targetMemory }}
    - type: Resource
      resource:
        name: memory
        {{- if semverCompare "<1.23-0" (include "common.capabilities.kubeVersion" .) }}
        targetAverageUtilization: {{ .Values.queryCoord.autoscaling.hpa.targetMemory }}
        {{- else }}
        target:
          type: Utilization
          averageUtilization: {{ .Values.queryCoord.autoscaling.hpa.targetMemory }}
        {{- end }}
    {{- end }}
{{- end }}
