Skip to content

Commit dc1696d

Browse files
committed
Fix API doc and tolerance field handling when gate is flipped.
1 parent 2dd9eda commit dc1696d

File tree

6 files changed

+259
-138
lines changed

6 files changed

+259
-138
lines changed

pkg/apis/autoscaling/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ type HPAScalingRules struct {
174174
// replicas (e.g. 0.01 for 1%). Must be greater than or equal to zero. If not
175175
// set, the default cluster-wide tolerance is applied (by default 10%).
176176
//
177+
// For example, if autoscaling is configured with a memory consumption target of 100Mi,
178+
// and scale-down and scale-up tolerances of 5% and 1% respectively, scaling will be
179+
// triggered when the actual consumption falls below 95Mi or exceeds 101Mi.
180+
//
177181
// This is an alpha field and requires enabling the HPAConfigurableTolerance
178182
// feature gate.
179183
//

pkg/apis/autoscaling/validation/validation.go

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ import (
2323
pathvalidation "k8s.io/apimachinery/pkg/api/validation/path"
2424
"k8s.io/apimachinery/pkg/util/sets"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
26-
utilfeature "k8s.io/apiserver/pkg/util/feature"
2726
"k8s.io/kubernetes/pkg/apis/autoscaling"
2827
corevalidation "k8s.io/kubernetes/pkg/apis/core/v1/validation"
2928
apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
30-
"k8s.io/kubernetes/pkg/features"
3129
)
3230

3331
const (
@@ -103,48 +101,16 @@ func ValidateCrossVersionObjectReference(ref autoscaling.CrossVersionObjectRefer
103101

104102
// ValidateHorizontalPodAutoscaler validates a HorizontalPodAutoscaler and returns an
105103
// ErrorList with any errors.
106-
func ValidateHorizontalPodAutoscaler(autoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
104+
func ValidateHorizontalPodAutoscaler(autoscaler *autoscaling.HorizontalPodAutoscaler, opts HorizontalPodAutoscalerSpecValidationOptions) field.ErrorList {
107105
allErrs := apivalidation.ValidateObjectMeta(&autoscaler.ObjectMeta, true, ValidateHorizontalPodAutoscalerName, field.NewPath("metadata"))
108-
109-
// MinReplicasLowerBound represents a minimum value for minReplicas
110-
// 0 when HPA scale-to-zero feature is enabled
111-
var minReplicasLowerBound int32
112-
113-
if utilfeature.DefaultFeatureGate.Enabled(features.HPAScaleToZero) {
114-
minReplicasLowerBound = 0
115-
} else {
116-
minReplicasLowerBound = 1
117-
}
118-
119-
opts := HorizontalPodAutoscalerSpecValidationOptions{
120-
AllowTolerance: utilfeature.DefaultMutableFeatureGate.Enabled(features.HPAConfigurableTolerance),
121-
MinReplicasLowerBound: minReplicasLowerBound,
122-
}
123-
124106
allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(autoscaler.Spec, field.NewPath("spec"), opts)...)
125107
return allErrs
126108
}
127109

128110
// ValidateHorizontalPodAutoscalerUpdate validates an update to a HorizontalPodAutoscaler and returns an
129111
// ErrorList with any errors.
130-
func ValidateHorizontalPodAutoscalerUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler) field.ErrorList {
112+
func ValidateHorizontalPodAutoscalerUpdate(newAutoscaler, oldAutoscaler *autoscaling.HorizontalPodAutoscaler, opts HorizontalPodAutoscalerSpecValidationOptions) field.ErrorList {
131113
allErrs := apivalidation.ValidateObjectMetaUpdate(&newAutoscaler.ObjectMeta, &oldAutoscaler.ObjectMeta, field.NewPath("metadata"))
132-
133-
// minReplicasLowerBound represents a minimum value for minReplicas
134-
// 0 when HPA scale-to-zero feature is enabled or HPA object already has minReplicas=0
135-
var minReplicasLowerBound int32
136-
137-
if utilfeature.DefaultFeatureGate.Enabled(features.HPAScaleToZero) || (oldAutoscaler.Spec.MinReplicas != nil && *oldAutoscaler.Spec.MinReplicas == 0) {
138-
minReplicasLowerBound = 0
139-
} else {
140-
minReplicasLowerBound = 1
141-
}
142-
143-
opts := HorizontalPodAutoscalerSpecValidationOptions{
144-
AllowTolerance: utilfeature.DefaultMutableFeatureGate.Enabled(features.HPAConfigurableTolerance),
145-
MinReplicasLowerBound: minReplicasLowerBound,
146-
}
147-
148114
allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(newAutoscaler.Spec, field.NewPath("spec"), opts)...)
149115
return allErrs
150116
}
@@ -162,8 +128,6 @@ func ValidateHorizontalPodAutoscalerStatusUpdate(newAutoscaler, oldAutoscaler *a
162128
// HorizontalPodAutoscalerSpecValidationOptions contains the different settings for
163129
// HorizontalPodAutoscaler spec validation.
164130
type HorizontalPodAutoscalerSpecValidationOptions struct {
165-
// Allow setting a tolerance on HPAScalingRules.
166-
AllowTolerance bool
167131
// The minimum value for minReplicas.
168132
MinReplicasLowerBound int32
169133
}
@@ -234,12 +198,8 @@ func validateScalingRules(rules *autoscaling.HPAScalingRules, fldPath *field.Pat
234198
allErrs = append(allErrs, policyErrs...)
235199
}
236200
}
237-
238-
if rules.Tolerance != nil && !opts.AllowTolerance {
239-
allErrs = append(allErrs, field.Forbidden(fldPath.Child("tolerance"), "tolerance is not supported when the HPAConfigurableTolerance feature gate is not enabled"))
240-
}
241-
if rules.Tolerance != nil && rules.Tolerance.Sign() < 0 {
242-
allErrs = append(allErrs, field.Invalid(fldPath.Child("tolerance"), rules.Tolerance, "must be greater or equal to zero"))
201+
if rules.Tolerance != nil {
202+
allErrs = append(allErrs, apivalidation.ValidateNonnegativeQuantity(*rules.Tolerance, fldPath.Child("tolerance"))...)
243203
}
244204
}
245205
return allErrs

0 commit comments

Comments
 (0)