feat: allow custom metrics configuration of FederatedHPA

Signed-off-by: jwcesign <jwcesign@gmail.com>
This commit is contained in:
jwcesign 2023-07-21 15:24:03 +08:00 committed by jwcesign
parent 48e9a5b551
commit 26ed43eec4
3 changed files with 174 additions and 84 deletions

View File

@ -24,8 +24,6 @@ import (
"strings"
corev1 "k8s.io/api/core/v1"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
@ -162,71 +160,3 @@ func ValidateDNS1123Label(value string, fldPath *field.Path) field.ErrorList {
}
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/core/validation/validation.go
// ValidateNameFunc validates that the provided name is valid for a given resource type.
// Not all resources have the same validation rules for names. Prefix is true
// if the name will have a value appended to it. If the name is not valid,
// this returns a list of descriptions of individual characteristics of the
// value that were not valid. Otherwise this returns an empty list or nil.
type ValidateNameFunc apimachineryvalidation.ValidateNameFunc
// ValidateObjectMeta validates an object's metadata on creation. It expects that name generation has already
// been performed.
// It doesn't return an error for rootscoped resources with namespace, because namespace should already be cleared before.
// TODO: Remove calls to this method scattered in validations of specific resources, e.g., ValidatePodUpdate.
func ValidateObjectMeta(meta *metav1.ObjectMeta, requiresNamespace bool, nameFn ValidateNameFunc, fldPath *field.Path) field.ErrorList {
allErrs := apimachineryvalidation.ValidateObjectMeta(meta, requiresNamespace, apimachineryvalidation.ValidateNameFunc(nameFn), fldPath)
// run additional checks for the finalizer name
for i := range meta.Finalizers {
allErrs = append(allErrs, validateKubeFinalizerName(string(meta.Finalizers[i]), fldPath.Child("finalizers").Index(i))...)
}
return allErrs
}
// validateKubeFinalizerName checks for "standard" names of legacy finalizer
func validateKubeFinalizerName(stringValue string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(strings.Split(stringValue, "/")) == 1 {
if IsStandardFinalizerName(stringValue) {
return append(allErrs, field.Invalid(fldPath, stringValue, "name is neither a standard finalizer name nor is it fully qualified"))
}
}
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/core/helper/helpers.go
var standardFinalizers = sets.NewString(
string(corev1.FinalizerKubernetes),
metav1.FinalizerOrphanDependents,
metav1.FinalizerDeleteDependents,
)
// IsStandardFinalizerName checks if the input string is a standard finalizer name
func IsStandardFinalizerName(str string) bool {
return standardFinalizers.Has(str)
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/staging/src/k8s.io/apimachinery/pkg/api/validation/generic.go
// NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain.
func NameIsDNSSubdomain(name string, prefix bool) []string {
if prefix {
name = maskTrailingDash(name)
}
return validation.IsDNS1123Subdomain(name)
}
// maskTrailingDash replaces the final character of a string with a subdomain safe
// value if it is a dash and if the length of this string is greater than 1. Note that
// this is used when a value could be appended to the string, see ValidateNameFunc
// for more info.
func maskTrailingDash(name string) string {
if len(name) > 1 && strings.HasSuffix(name, "-") {
return name[:len(name)-2] + "a"
}
return name
}

View File

@ -13,9 +13,6 @@ package lifted
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/helper/helpers.go#L261-L264 | func IsStandardResourceName | N |
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/helper/helpers.go#LL266-L276 | var integerResources | Y |
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/helper/helpers.go#L278-L281 | func IsIntegerResourceName | N |
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/core/validation/validation.go | type ValidateNameFunc | N |
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/core/helper/helpers.go | var standardFinalizers | N |
| corehelpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/staging/src/k8s.io/apimachinery/pkg/api/validation/generic.go | func NameIsDNSSubdomain | N |
| corev1helpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/v1/helper/helpers.go#L31-L46 | func IsExtendedResourceName | Y |
| corev1helpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/v1/helper/helpers.go#L48-L51 | func IsPrefixedNativeResource | N |
| corev1helpers.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/v1/helper/helpers.go#L54-L60 | func IsNativeResource | N |
@ -78,6 +75,26 @@ package lifted
| taint_test.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/staging/src/k8s.io/kubectl/pkg/cmd/taint/utils_test.go#L372-L533 | func TestParseTaints | N |
| validateclustertaints.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/validation/validation.go#L3497-L3518 | func validateClusterTaintEffect | Y |
| validateclustertaints.go | https://github.com/kubernetes/kubernetes/blob/release-1.26/pkg/apis/core/validation/validation.go#L5227-L5259 | func ValidateClusterTaints | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L32-37 | const MaxPeriodSeconds | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L32-37 | const MaxStabilizationWindowSeconds | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L103-L119 | func ValidateFederatedHPA | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L55-L78 | func validateFederatedHPASpec | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L80-L101 | func ValidateCrossVersionObjectReference | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L140-L148 | func validateFederatedHPAStatus | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L150-L175 | func validateMetrics | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L177-L188 | func validateBehavior | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L190-L191 | var validSelectPolicyTypes | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L193-L218 | func validateScalingRules | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L220-L221 | var validPolicyTypes | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L223-L239 | func validateScalingPolicy | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L241-L245 | var validMetricSourceTypes | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L247-L339 | func validateMetricSpec | Y |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L340-L352 | func validateObjectSource | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L371-L382 | func validatePodsSource | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L383-L410 | func validateContainerResourceSource | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L412-L430 | func validateResourceSource | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L432-L458 | func validateMetricTarget | N |
| validatingfhpa.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L460-L471 | func validateMetricIdentifier | N |
| validatingmci.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/networking/validation/validation.go#L326-L348 | func ValidateIngressSpec | N |
| validatingmci.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/networking/validation/validation.go#L468C1-L509 | func validateIngressBackend | N |
| validatingmci.go | https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/networking/validation/validation.go#L547C1-L578 | func validateIngressTypedLocalObjectReference | N |

View File

@ -4,6 +4,7 @@ import (
"fmt"
autoscalingv2 "k8s.io/api/autoscaling/v2"
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
pathvalidation "k8s.io/apimachinery/pkg/api/validation/path"
"k8s.io/apimachinery/pkg/util/sets"
@ -12,9 +13,7 @@ import (
autoscalingv1alpha1 "github.com/karmada-io/karmada/pkg/apis/autoscaling/v1alpha1"
)
// This code is directly lifted from the Kubernetes codebase.
// For reference:
// https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L32-37
const (
// MaxPeriodSeconds is the largest allowed scaling policy period (in seconds)
@ -23,14 +22,15 @@ const (
MaxStabilizationWindowSeconds int32 = 3600
)
// ValidateFederatedHPAName can be used to check whether the given autoscaler name is valid.
// Prefix indicates this name will be used as part of generation, in which case trailing dashes are allowed.
var ValidateFederatedHPAName = apivalidation.NameIsDNSSubdomain
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L103-L119
// +lifted:changed
// ValidateFederatedHPA validates a FederatedHPA and returns an
// ErrorList with any errors.
func ValidateFederatedHPA(fhpa *autoscalingv1alpha1.FederatedHPA) field.ErrorList {
errs := field.ErrorList{}
errs = append(errs, ValidateObjectMeta(&fhpa.ObjectMeta, true, ValidateFederatedHPAName, field.NewPath("metadata"))...)
errs = append(errs, apimachineryvalidation.ValidateObjectMeta(&fhpa.ObjectMeta, true, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
// MinReplicasLowerBound represents a minimum value for minReplicas
// 0 when HPA scale-to-zero feature is enabled
@ -42,6 +42,9 @@ func ValidateFederatedHPA(fhpa *autoscalingv1alpha1.FederatedHPA) field.ErrorLis
return errs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L55-L78
// +lifted:changed
func validateFederatedHPASpec(fhpaSpec *autoscalingv1alpha1.FederatedHPASpec, fldPath *field.Path, minReplicasLowerBound int32) field.ErrorList {
allErrs := field.ErrorList{}
@ -67,6 +70,8 @@ func validateFederatedHPASpec(fhpaSpec *autoscalingv1alpha1.FederatedHPASpec, fl
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L80-L101
// ValidateCrossVersionObjectReference validates a CrossVersionObjectReference and returns an
// ErrorList with any errors.
func ValidateCrossVersionObjectReference(ref autoscalingv2.CrossVersionObjectReference, fldPath *field.Path) field.ErrorList {
@ -90,6 +95,9 @@ func ValidateCrossVersionObjectReference(ref autoscalingv2.CrossVersionObjectRef
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L140-L148
// +lifted:changed
// validateFederatedHPAStatus validates an update to status on a FederatedHPA and
// returns an ErrorList with any errors.
func validateFederatedHPAStatus(fhpaStatus *autoscalingv2.HorizontalPodAutoscalerStatus) field.ErrorList {
@ -99,6 +107,9 @@ func validateFederatedHPAStatus(fhpaStatus *autoscalingv2.HorizontalPodAutoscale
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L150-L175
// +lifted:changed
func validateMetrics(metrics []autoscalingv2.MetricSpec, fldPath *field.Path, minReplicas *int32) field.ErrorList {
allErrs := field.ErrorList{}
@ -112,6 +123,8 @@ func validateMetrics(metrics []autoscalingv2.MetricSpec, fldPath *field.Path, mi
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L177-L188
func validateBehavior(behavior *autoscalingv2.HorizontalPodAutoscalerBehavior, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if behavior != nil {
@ -125,9 +138,13 @@ func validateBehavior(behavior *autoscalingv2.HorizontalPodAutoscalerBehavior, f
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L190-L191
var validSelectPolicyTypes = sets.NewString(string(autoscalingv2.MaxChangePolicySelect), string(autoscalingv2.MinChangePolicySelect), string(autoscalingv2.DisabledPolicySelect))
var validSelectPolicyTypesList = validSelectPolicyTypes.List()
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L193-L218
func validateScalingRules(rules *autoscalingv2.HPAScalingRules, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if rules != nil {
@ -155,9 +172,13 @@ func validateScalingRules(rules *autoscalingv2.HPAScalingRules, fldPath *field.P
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L220-L221
var validPolicyTypes = sets.NewString(string(autoscalingv2.PodsScalingPolicy), string(autoscalingv2.PercentScalingPolicy))
var validPolicyTypesList = validPolicyTypes.List()
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L223-L239
func validateScalingPolicy(policy autoscalingv2.HPAScalingPolicy, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if policy.Type != autoscalingv2.PodsScalingPolicy && policy.Type != autoscalingv2.PercentScalingPolicy {
@ -176,12 +197,18 @@ func validateScalingPolicy(policy autoscalingv2.HPAScalingPolicy, fldPath *field
return allErrs
}
// TODO: Karmada only supports cpu/memory resource metrics
var validMetricSourceTypes = sets.NewString(
string(autoscalingv2.ResourceMetricSourceType))
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L241-L245
// +lifted:changed
// TODO: Karmada do not support ExternalMetricSourceType currently
var validMetricSourceTypes = sets.NewString(
string(autoscalingv2.ObjectMetricSourceType), string(autoscalingv2.PodsMetricSourceType),
string(autoscalingv2.ResourceMetricSourceType), string(autoscalingv2.ContainerResourceMetricSourceType))
var validMetricSourceTypesList = validMetricSourceTypes.List()
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L247-L339
// +lifted:changed
func validateMetricSpec(spec autoscalingv2.MetricSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
@ -194,6 +221,20 @@ func validateMetricSpec(spec autoscalingv2.MetricSpec, fldPath *field.Path) fiel
}
typesPresent := sets.NewString()
if spec.Object != nil {
typesPresent.Insert("object")
if typesPresent.Len() == 1 {
allErrs = append(allErrs, validateObjectSource(spec.Object, fldPath.Child("object"))...)
}
}
if spec.Pods != nil {
typesPresent.Insert("pods")
if typesPresent.Len() == 1 {
allErrs = append(allErrs, validatePodsSource(spec.Pods, fldPath.Child("pods"))...)
}
}
if spec.Resource != nil {
typesPresent.Insert("resource")
if typesPresent.Len() == 1 {
@ -201,15 +242,37 @@ func validateMetricSpec(spec autoscalingv2.MetricSpec, fldPath *field.Path) fiel
}
}
if spec.ContainerResource != nil {
typesPresent.Insert("containerResource")
if typesPresent.Len() == 1 {
allErrs = append(allErrs, validateContainerResourceSource(spec.ContainerResource, fldPath.Child("containerResource"))...)
}
}
var expectedField string
switch spec.Type {
// TODO: Karmada only support resource metrics temporarily
case autoscalingv2.ObjectMetricSourceType:
if spec.Object == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("object"), "must populate information for the given metric source"))
}
expectedField = "object"
case autoscalingv2.PodsMetricSourceType:
if spec.Pods == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("pods"), "must populate information for the given metric source"))
}
expectedField = "pods"
case autoscalingv2.ResourceMetricSourceType:
if spec.Resource == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("resource"), "must populate information for the given metric source"))
}
expectedField = "resource"
case autoscalingv2.ContainerResourceMetricSourceType:
if spec.ContainerResource == nil {
// In K8s, there is a feature gate:HPAContainerMetrics, but we don't know about member clusters, so we enable it by default.
allErrs = append(allErrs, field.Required(fldPath.Child("containerResource"), "must populate information for the given metric source"))
}
expectedField = "containerResource"
default:
allErrs = append(allErrs, field.NotSupported(fldPath.Child("type"), spec.Type, validMetricSourceTypesList))
}
@ -224,6 +287,69 @@ func validateMetricSpec(spec autoscalingv2.MetricSpec, fldPath *field.Path) fiel
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L340-L352
func validateObjectSource(src *autoscalingv2.ObjectMetricSource, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, ValidateCrossVersionObjectReference(src.DescribedObject, fldPath.Child("describedObject"))...)
allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
if src.Target.Value == nil && src.Target.AverageValue == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must set either a target value or averageValue"))
}
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L371-L382
func validatePodsSource(src *autoscalingv2.PodsMetricSource, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateMetricIdentifier(src.Metric, fldPath.Child("metric"))...)
allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
if src.Target.AverageValue == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageValue"), "must specify a positive target averageValue"))
}
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L383-L410
func validateContainerResourceSource(src *autoscalingv2.ContainerResourceMetricSource, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(src.Name) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a resource name"))
} else {
allErrs = append(allErrs, ValidateContainerResourceName(string(src.Name), fldPath.Child("name"))...)
}
if len(src.Container) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("container"), "must specify a container"))
} else {
allErrs = append(allErrs, ValidateDNS1123Label(src.Container, fldPath.Child("container"))...)
}
allErrs = append(allErrs, validateMetricTarget(src.Target, fldPath.Child("target"))...)
if src.Target.AverageUtilization == nil && src.Target.AverageValue == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("target").Child("averageUtilization"), "must set either a target raw value or a target utilization"))
}
if src.Target.AverageUtilization != nil && src.Target.AverageValue != nil {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("target").Child("averageValue"), "may not set both a target raw value and a target utilization"))
}
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L412-L430
func validateResourceSource(src *autoscalingv2.ResourceMetricSource, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
@ -244,6 +370,8 @@ func validateResourceSource(src *autoscalingv2.ResourceMetricSource, fldPath *fi
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L432-L458
func validateMetricTarget(mt autoscalingv2.MetricTarget, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
@ -271,3 +399,18 @@ func validateMetricTarget(mt autoscalingv2.MetricTarget, fldPath *field.Path) fi
return allErrs
}
// +lifted:source=https://github.com/kubernetes/kubernetes/blob/release-1.27/pkg/apis/autoscaling/validation/validation.go#L460-L471
func validateMetricIdentifier(id autoscalingv2.MetricIdentifier, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(id.Name) == 0 {
allErrs = append(allErrs, field.Required(fldPath.Child("name"), "must specify a metric name"))
} else {
for _, msg := range pathvalidation.IsValidPathSegmentName(id.Name) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), id.Name, msg))
}
}
return allErrs
}