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

{{- if and .Values.query.enabled .Values.query.autoscaling.enabled }}
apiVersion: {{ include "common.capabilities.hpa.apiVersion" ( dict "context" $ ) }}
kind: HorizontalPodAutoscaler
metadata:
  name: {{ include "thanos.query.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: query
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if .Values.query.autoscaling.behavior }}
  behavior:
    {{- toYaml .Values.query.autoscaling.behavior | nindent 4 }}
  {{- end }}
  scaleTargetRef:
    apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
    kind: Deployment
    name: {{ include "thanos.query.fullname" . }}
  minReplicas: {{ .Values.query.autoscaling.minReplicas }}
  maxReplicas: {{ .Values.query.autoscaling.maxReplicas }}
  metrics:
    {{- if .Values.query.autoscaling.targetMemory }}
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: {{ .Values.query.autoscaling.targetMemory }}
    {{- end }}
    {{- if .Values.query.autoscaling.targetCPU }}
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: {{ .Values.query.autoscaling.targetCPU }}
    {{- end }}
    {{- range $targetPodMetric := .Values.query.autoscaling.targetPodMetrics }}
    - type: Pods
      pods:
        metric:
          name: {{ $targetPodMetric.name }}
        target:
          type: AverageValue
          averageValue: {{ $targetPodMetric.averageValue }}
    {{- end }}
{{- end }}
