{{- /*
Copyright Drycc Community.
SPDX-License-Identifier: APACHE-2.0
*/}}

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