mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 23:37:01 +00:00
Merge pull request #18508 from mqliang/bindingValidation
Auto commit by PR queue bot
This commit is contained in:
commit
5ac7f31064
@ -1225,6 +1225,22 @@ func ValidatePodStatusUpdate(newPod, oldPod *api.Pod) field.ErrorList {
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidatePodBinding tests if required fields in the pod binding are legal.
|
||||
func ValidatePodBinding(binding *api.Binding) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
|
||||
if len(binding.Target.Kind) != 0 && binding.Target.Kind != "Node" {
|
||||
// TODO: When validation becomes versioned, this gets more complicated.
|
||||
allErrs = append(allErrs, field.NotSupported(field.NewPath("target", "kind"), binding.Target.Kind, []string{"Node", "<empty>"}))
|
||||
}
|
||||
if len(binding.Target.Name) == 0 {
|
||||
// TODO: When validation becomes versioned, this gets more complicated.
|
||||
allErrs = append(allErrs, field.Required(field.NewPath("target", "name")))
|
||||
}
|
||||
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// ValidatePodTemplate tests if required fields in the pod template are set.
|
||||
func ValidatePodTemplate(pod *api.PodTemplate) field.ErrorList {
|
||||
allErrs := ValidateObjectMeta(&pod.ObjectMeta, true, ValidatePodName, field.NewPath("metadata"))
|
||||
|
@ -26,6 +26,7 @@ import (
|
||||
etcderr "k8s.io/kubernetes/pkg/api/errors/etcd"
|
||||
"k8s.io/kubernetes/pkg/api/rest"
|
||||
"k8s.io/kubernetes/pkg/api/unversioned"
|
||||
"k8s.io/kubernetes/pkg/api/validation"
|
||||
"k8s.io/kubernetes/pkg/fields"
|
||||
"k8s.io/kubernetes/pkg/kubelet/client"
|
||||
"k8s.io/kubernetes/pkg/labels"
|
||||
@ -35,7 +36,6 @@ import (
|
||||
podrest "k8s.io/kubernetes/pkg/registry/pod/rest"
|
||||
"k8s.io/kubernetes/pkg/runtime"
|
||||
"k8s.io/kubernetes/pkg/storage"
|
||||
"k8s.io/kubernetes/pkg/util/validation/field"
|
||||
)
|
||||
|
||||
// PodStorage includes storage for pods and all sub resources
|
||||
@ -93,8 +93,8 @@ func NewStorage(
|
||||
|
||||
Storage: storageInterface,
|
||||
}
|
||||
statusStore := *store
|
||||
|
||||
statusStore := *store
|
||||
statusStore.UpdateStrategy = pod.StatusStrategy
|
||||
|
||||
return PodStorage{
|
||||
@ -132,15 +132,12 @@ var _ = rest.Creater(&BindingREST{})
|
||||
// Create ensures a pod is bound to a specific host.
|
||||
func (r *BindingREST) Create(ctx api.Context, obj runtime.Object) (out runtime.Object, err error) {
|
||||
binding := obj.(*api.Binding)
|
||||
|
||||
// TODO: move me to a binding strategy
|
||||
if len(binding.Target.Kind) != 0 && binding.Target.Kind != "Node" {
|
||||
// TODO: When validation becomes versioned, this gets more complicated.
|
||||
return nil, errors.NewInvalid("binding", binding.Name, field.ErrorList{field.NotSupported(field.NewPath("target", "kind"), binding.Target.Kind, []string{"Node", "<empty>"})})
|
||||
}
|
||||
if len(binding.Target.Name) == 0 {
|
||||
// TODO: When validation becomes versioned, this gets more complicated.
|
||||
return nil, errors.NewInvalid("binding", binding.Name, field.ErrorList{field.Required(field.NewPath("target", "name"))})
|
||||
if errs := validation.ValidatePodBinding(binding); len(errs) != 0 {
|
||||
return nil, errs.ToAggregate()
|
||||
}
|
||||
|
||||
err = r.assignPod(ctx, binding.Name, binding.Target.Name, binding.Annotations)
|
||||
out = &unversioned.Status{Status: unversioned.StatusSuccess}
|
||||
return
|
||||
|
@ -482,14 +482,14 @@ func TestEtcdCreateBinding(t *testing.T) {
|
||||
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
|
||||
Target: api.ObjectReference{},
|
||||
},
|
||||
errOK: func(err error) bool { return errors.IsInvalid(err) },
|
||||
errOK: func(err error) bool { return err != nil },
|
||||
},
|
||||
"badKind": {
|
||||
binding: api.Binding{
|
||||
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
|
||||
Target: api.ObjectReference{Name: "machine1", Kind: "unknown"},
|
||||
},
|
||||
errOK: func(err error) bool { return errors.IsInvalid(err) },
|
||||
errOK: func(err error) bool { return err != nil },
|
||||
},
|
||||
"emptyKind": {
|
||||
binding: api.Binding{
|
||||
|
Loading…
Reference in New Issue
Block a user