mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 22:46:12 +00:00
Merge pull request #16677 from DirectXMan12/feature/add-scale-validator
[WIP] Add Validators for Scale Objects
This commit is contained in:
commit
8594c20333
@ -596,3 +596,14 @@ func ValidatePodSelectorRequirement(sr extensions.PodSelectorRequirement) errs.V
|
||||
allErrs = append(allErrs, apivalidation.ValidateLabelName(sr.Key, "key")...)
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func ValidateScale(scale *extensions.Scale) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&scale.ObjectMeta, true, apivalidation.NameIsDNSSubdomain).Prefix("metadata")...)
|
||||
|
||||
if scale.Spec.Replicas < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("spec.replicas", scale.Spec.Replicas, "must be non-negative"))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
@ -1233,6 +1233,70 @@ func TestValidateClusterAutoscaler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateScale(t *testing.T) {
|
||||
successCases := []extensions.Scale{
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "frontend",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.ScaleSpec{
|
||||
Replicas: 1,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "frontend",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.ScaleSpec{
|
||||
Replicas: 10,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "frontend",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.ScaleSpec{
|
||||
Replicas: 0,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, successCase := range successCases {
|
||||
if errs := ValidateScale(&successCase); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
errorCases := []struct {
|
||||
scale extensions.Scale
|
||||
msg string
|
||||
}{
|
||||
{
|
||||
scale: extensions.Scale{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: "frontend",
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
Spec: extensions.ScaleSpec{
|
||||
Replicas: -1,
|
||||
},
|
||||
},
|
||||
msg: "must be non-negative",
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range errorCases {
|
||||
if errs := ValidateScale(&c.scale); len(errs) == 0 {
|
||||
t.Errorf("expected failure for %s", c.msg)
|
||||
} else if !strings.Contains(errs[0].Error(), c.msg) {
|
||||
t.Errorf("unexpected error: %v, expected: %s", errs[0], c.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newInt(val int) *int {
|
||||
p := new(int)
|
||||
*p = val
|
||||
|
@ -30,6 +30,8 @@ import (
|
||||
etcdgeneric "k8s.io/kubernetes/pkg/registry/generic/etcd"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
|
||||
extvalidation "k8s.io/kubernetes/pkg/apis/extensions/validation"
|
||||
)
|
||||
|
||||
// DeploymentStorage includes dummy storage for Deployments and for Scale subresource.
|
||||
@ -154,6 +156,11 @@ func (r *ScaleREST) Update(ctx api.Context, obj runtime.Object) (runtime.Object,
|
||||
if !ok {
|
||||
return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
|
||||
}
|
||||
|
||||
if errs := extvalidation.ValidateScale(scale); len(errs) > 0 {
|
||||
return nil, false, errors.NewInvalid("scale", scale.Name, errs)
|
||||
}
|
||||
|
||||
deployment, err := (*r.registry).GetDeployment(ctx, scale.Name)
|
||||
if err != nil {
|
||||
return nil, false, errors.NewNotFound("scale", scale.Name)
|
||||
|
@ -30,6 +30,8 @@ import (
|
||||
"k8s.io/kubernetes/pkg/registry/generic"
|
||||
|
||||
"k8s.io/kubernetes/pkg/apis/extensions"
|
||||
|
||||
extvalidation "k8s.io/kubernetes/pkg/apis/extensions/validation"
|
||||
)
|
||||
|
||||
// Container includes dummy storage for RC pods and experimental storage for Scale.
|
||||
@ -90,6 +92,11 @@ func (r *ScaleREST) Update(ctx api.Context, obj runtime.Object) (runtime.Object,
|
||||
if !ok {
|
||||
return nil, false, errors.NewBadRequest(fmt.Sprintf("wrong object passed to Scale update: %v", obj))
|
||||
}
|
||||
|
||||
if errs := extvalidation.ValidateScale(scale); len(errs) > 0 {
|
||||
return nil, false, errors.NewInvalid("scale", scale.Name, errs)
|
||||
}
|
||||
|
||||
rc, err := (*r.registry).GetController(ctx, scale.Name)
|
||||
if err != nil {
|
||||
return nil, false, errors.NewNotFound("scale", scale.Name)
|
||||
|
Loading…
Reference in New Issue
Block a user