@@ -23,11 +23,9 @@ import (
23
23
pathvalidation "k8s.io/apimachinery/pkg/api/validation/path"
24
24
"k8s.io/apimachinery/pkg/util/sets"
25
25
"k8s.io/apimachinery/pkg/util/validation/field"
26
- utilfeature "k8s.io/apiserver/pkg/util/feature"
27
26
"k8s.io/kubernetes/pkg/apis/autoscaling"
28
27
corevalidation "k8s.io/kubernetes/pkg/apis/core/v1/validation"
29
28
apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
30
- "k8s.io/kubernetes/pkg/features"
31
29
)
32
30
33
31
const (
@@ -103,48 +101,16 @@ func ValidateCrossVersionObjectReference(ref autoscaling.CrossVersionObjectRefer
103
101
104
102
// ValidateHorizontalPodAutoscaler validates a HorizontalPodAutoscaler and returns an
105
103
// ErrorList with any errors.
106
- func ValidateHorizontalPodAutoscaler (autoscaler * autoscaling.HorizontalPodAutoscaler ) field.ErrorList {
104
+ func ValidateHorizontalPodAutoscaler (autoscaler * autoscaling.HorizontalPodAutoscaler , opts HorizontalPodAutoscalerSpecValidationOptions ) field.ErrorList {
107
105
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
-
124
106
allErrs = append (allErrs , validateHorizontalPodAutoscalerSpec (autoscaler .Spec , field .NewPath ("spec" ), opts )... )
125
107
return allErrs
126
108
}
127
109
128
110
// ValidateHorizontalPodAutoscalerUpdate validates an update to a HorizontalPodAutoscaler and returns an
129
111
// ErrorList with any errors.
130
- func ValidateHorizontalPodAutoscalerUpdate (newAutoscaler , oldAutoscaler * autoscaling.HorizontalPodAutoscaler ) field.ErrorList {
112
+ func ValidateHorizontalPodAutoscalerUpdate (newAutoscaler , oldAutoscaler * autoscaling.HorizontalPodAutoscaler , opts HorizontalPodAutoscalerSpecValidationOptions ) field.ErrorList {
131
113
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
-
148
114
allErrs = append (allErrs , validateHorizontalPodAutoscalerSpec (newAutoscaler .Spec , field .NewPath ("spec" ), opts )... )
149
115
return allErrs
150
116
}
@@ -162,8 +128,6 @@ func ValidateHorizontalPodAutoscalerStatusUpdate(newAutoscaler, oldAutoscaler *a
162
128
// HorizontalPodAutoscalerSpecValidationOptions contains the different settings for
163
129
// HorizontalPodAutoscaler spec validation.
164
130
type HorizontalPodAutoscalerSpecValidationOptions struct {
165
- // Allow setting a tolerance on HPAScalingRules.
166
- AllowTolerance bool
167
131
// The minimum value for minReplicas.
168
132
MinReplicasLowerBound int32
169
133
}
@@ -234,12 +198,8 @@ func validateScalingRules(rules *autoscaling.HPAScalingRules, fldPath *field.Pat
234
198
allErrs = append (allErrs , policyErrs ... )
235
199
}
236
200
}
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" ))... )
243
203
}
244
204
}
245
205
return allErrs
0 commit comments