Fix round-tripping HPAv2 resources via v1 with object metrics

This commit is contained in:
Adrian Moisey 2026-03-18 19:40:03 +01:00
parent 0eb9650790
commit 0fc32838ff
No known key found for this signature in database
GPG key ID: 41AE4AE32747C7CF

View file

@ -22,6 +22,7 @@ import (
autoscalingv1 "k8s.io/api/autoscaling/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/kubernetes/pkg/apis/autoscaling"
"k8s.io/kubernetes/pkg/apis/core"
@ -82,15 +83,22 @@ func Convert_autoscaling_ObjectMetricSource_To_v1_ObjectMetricSource(in *autosca
func Convert_v1_ObjectMetricSource_To_autoscaling_ObjectMetricSource(in *autoscalingv1.ObjectMetricSource, out *autoscaling.ObjectMetricSource, s conversion.Scope) error {
var metricType autoscaling.MetricTargetType
var targetValue *resource.Quantity
if in.AverageValue == nil {
metricType = autoscaling.ValueMetricType
targetValue = &in.TargetValue
} else {
metricType = autoscaling.AverageValueMetricType
// Only preserve non-zero targetValue for averageValue metrics.
// The v1 type cannot omit the value field, and serializes value:"0", which fails validation on round-trip.
if !in.TargetValue.IsZero() {
targetValue = &in.TargetValue
}
}
out.Target = autoscaling.MetricTarget{
Type: metricType,
Value: &in.TargetValue,
Value: targetValue,
AverageValue: in.AverageValue,
}
out.DescribedObject = autoscaling.CrossVersionObjectReference{