mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-29 21:29:24 +00:00
Added HorizontalPodAutoscaler object to experimental API.
Added HorizontalPodAutoscaler object to experimental API. Related to #12087.
This commit is contained in:
67
pkg/expapi/validation/validation.go
Normal file
67
pkg/expapi/validation/validation.go
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
Copyright 2015 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
apivalidation "k8s.io/kubernetes/pkg/api/validation"
|
||||
"k8s.io/kubernetes/pkg/expapi"
|
||||
errs "k8s.io/kubernetes/pkg/util/fielderrors"
|
||||
)
|
||||
|
||||
// ValidateHorizontalPodAutoscaler 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.
|
||||
func ValidateHorizontalPodAutoscalerName(name string, prefix bool) (bool, string) {
|
||||
// TODO: finally move it to pkg/api/validation and use nameIsDNSSubdomain function
|
||||
return apivalidation.ValidateReplicationControllerName(name, prefix)
|
||||
}
|
||||
|
||||
func validateHorizontalPodAutoscalerSpec(autoscaler expapi.HorizontalPodAutoscalerSpec) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
if autoscaler.MinCount < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("minCount", autoscaler.MinCount, `must be non-negative`))
|
||||
}
|
||||
if autoscaler.MaxCount < autoscaler.MinCount {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("maxCount", autoscaler.MaxCount, `must be bigger or equal to minCount`))
|
||||
}
|
||||
if autoscaler.ScaleRef == nil {
|
||||
allErrs = append(allErrs, errs.NewFieldRequired("scaleRef"))
|
||||
}
|
||||
resource := autoscaler.Target.Resource.String()
|
||||
if resource != string(api.ResourceMemory) && resource != string(api.ResourceCPU) {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("target.resource", resource, "resource not supported by autoscaler"))
|
||||
}
|
||||
quantity := autoscaler.Target.Quantity.Value()
|
||||
if quantity < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("target.quantity", quantity, "must be non-negative"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateHorizontalPodAutoscaler(autoscaler *expapi.HorizontalPodAutoscaler) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&autoscaler.ObjectMeta, true, ValidateHorizontalPodAutoscalerName).Prefix("metadata")...)
|
||||
allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(autoscaler.Spec)...)
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateHorizontalPodAutoscalerUpdate(newAutoscler, oldAutoscaler *expapi.HorizontalPodAutoscaler) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
allErrs = append(allErrs, apivalidation.ValidateObjectMetaUpdate(&newAutoscler.ObjectMeta, &oldAutoscaler.ObjectMeta).Prefix("metadata")...)
|
||||
allErrs = append(allErrs, validateHorizontalPodAutoscalerSpec(newAutoscler.Spec)...)
|
||||
return allErrs
|
||||
}
|
||||
129
pkg/expapi/validation/validation_test.go
Normal file
129
pkg/expapi/validation/validation_test.go
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
Copyright 2014 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package validation
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"k8s.io/kubernetes/pkg/api"
|
||||
"k8s.io/kubernetes/pkg/api/resource"
|
||||
"k8s.io/kubernetes/pkg/expapi"
|
||||
)
|
||||
|
||||
func TestValidateHorizontalPodAutoscaler(t *testing.T) {
|
||||
successCases := []expapi.HorizontalPodAutoscaler{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
ScaleRef: &expapi.SubresourceReference{
|
||||
Subresource: "scale",
|
||||
},
|
||||
MinCount: 1,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceCPU, resource.MustParse("0.8")},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, successCase := range successCases {
|
||||
if errs := ValidateHorizontalPodAutoscaler(&successCase); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
errorCases := map[string]expapi.HorizontalPodAutoscaler{
|
||||
"must be non-negative": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
ScaleRef: &expapi.SubresourceReference{
|
||||
Subresource: "scale",
|
||||
},
|
||||
MinCount: -1,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceCPU, resource.MustParse("0.8")},
|
||||
},
|
||||
},
|
||||
"must be bigger or equal to minCount": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
ScaleRef: &expapi.SubresourceReference{
|
||||
Subresource: "scale",
|
||||
},
|
||||
MinCount: 7,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceCPU, resource.MustParse("0.8")},
|
||||
},
|
||||
},
|
||||
"invalid value": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
ScaleRef: &expapi.SubresourceReference{
|
||||
Subresource: "scale",
|
||||
},
|
||||
MinCount: 1,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceCPU, resource.MustParse("-0.8")},
|
||||
},
|
||||
},
|
||||
"resource not supported": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
ScaleRef: &expapi.SubresourceReference{
|
||||
Subresource: "scale",
|
||||
},
|
||||
MinCount: 1,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceName("NotSupportedResource"), resource.MustParse("0.8")},
|
||||
},
|
||||
},
|
||||
"required value": {
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "myautoscaler",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: expapi.HorizontalPodAutoscalerSpec{
|
||||
MinCount: 1,
|
||||
MaxCount: 5,
|
||||
Target: expapi.TargetConsumption{api.ResourceCPU, resource.MustParse("0.8")},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range errorCases {
|
||||
errs := ValidateHorizontalPodAutoscaler(&v)
|
||||
if len(errs) == 0 {
|
||||
t.Errorf("expected failure for %s", k)
|
||||
} else if !strings.Contains(errs[0].Error(), k) {
|
||||
t.Errorf("unexpected error: %v, expected: %s", errs[0], k)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user