{{- if .Values.gateway.autoscaling.enabled }}
{{- if not .Values.global.autoscalingv2API }}
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: {{ include "gateway.name" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "gateway.labels" . | nindent 4 }}
  annotations:
    {{- .Values.gateway.annotations | toYaml | nindent 4 }}
spec:
  minReplicas: {{ .Values.gateway.autoscaling.minReplicas }}
  maxReplicas: {{ .Values.gateway.autoscaling.maxReplicas }}
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ include "gateway.name" . }}
  metrics:
  - type: Resource
    resource:
      name: cpu
      targetAverageUtilization: {{ .Values.gateway.autoscaling.targetCPUUtilizationPercentage }}
---
{{- else }}
{{- if (semverCompare ">=1.23-0" .Capabilities.KubeVersion.GitVersion)}}
apiVersion: autoscaling/v2
{{- else }}
apiVersion: autoscaling/v2beta2
{{- end }}
kind: HorizontalPodAutoscaler
metadata:
  name: {{ include "gateway.name" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "gateway.labels" . | nindent 4 }}
  annotations:
    {{- .Values.gateway.annotations | toYaml | nindent 4 }}
spec:
  minReplicas: {{ .Values.gateway.autoscaling.minReplicas }}
  maxReplicas: {{ .Values.gateway.autoscaling.maxReplicas }}
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ include "gateway.name" . }}
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: {{ .Values.gateway.autoscaling.targetCPUUtilizationPercentage }}
---
{{- end }}
{{- end }}
