Merge pull request #18508 from mqliang/bindingValidation

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2015-12-17 03:22:18 -08:00
commit 5ac7f31064
3 changed files with 24 additions and 11 deletions

View File

@ -1225,6 +1225,22 @@ func ValidatePodStatusUpdate(newPod, oldPod *api.Pod) field.ErrorList {
return allErrs 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. // ValidatePodTemplate tests if required fields in the pod template are set.
func ValidatePodTemplate(pod *api.PodTemplate) field.ErrorList { func ValidatePodTemplate(pod *api.PodTemplate) field.ErrorList {
allErrs := ValidateObjectMeta(&pod.ObjectMeta, true, ValidatePodName, field.NewPath("metadata")) allErrs := ValidateObjectMeta(&pod.ObjectMeta, true, ValidatePodName, field.NewPath("metadata"))

View File

@ -26,6 +26,7 @@ import (
etcderr "k8s.io/kubernetes/pkg/api/errors/etcd" etcderr "k8s.io/kubernetes/pkg/api/errors/etcd"
"k8s.io/kubernetes/pkg/api/rest" "k8s.io/kubernetes/pkg/api/rest"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/fields" "k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/kubelet/client" "k8s.io/kubernetes/pkg/kubelet/client"
"k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/labels"
@ -35,7 +36,6 @@ import (
podrest "k8s.io/kubernetes/pkg/registry/pod/rest" podrest "k8s.io/kubernetes/pkg/registry/pod/rest"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/storage" "k8s.io/kubernetes/pkg/storage"
"k8s.io/kubernetes/pkg/util/validation/field"
) )
// PodStorage includes storage for pods and all sub resources // PodStorage includes storage for pods and all sub resources
@ -93,8 +93,8 @@ func NewStorage(
Storage: storageInterface, Storage: storageInterface,
} }
statusStore := *store
statusStore := *store
statusStore.UpdateStrategy = pod.StatusStrategy statusStore.UpdateStrategy = pod.StatusStrategy
return PodStorage{ return PodStorage{
@ -132,15 +132,12 @@ var _ = rest.Creater(&BindingREST{})
// Create ensures a pod is bound to a specific host. // 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) { func (r *BindingREST) Create(ctx api.Context, obj runtime.Object) (out runtime.Object, err error) {
binding := obj.(*api.Binding) binding := obj.(*api.Binding)
// TODO: move me to a binding strategy // TODO: move me to a binding strategy
if len(binding.Target.Kind) != 0 && binding.Target.Kind != "Node" { if errs := validation.ValidatePodBinding(binding); len(errs) != 0 {
// TODO: When validation becomes versioned, this gets more complicated. return nil, errs.ToAggregate()
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"))})
} }
err = r.assignPod(ctx, binding.Name, binding.Target.Name, binding.Annotations) err = r.assignPod(ctx, binding.Name, binding.Target.Name, binding.Annotations)
out = &unversioned.Status{Status: unversioned.StatusSuccess} out = &unversioned.Status{Status: unversioned.StatusSuccess}
return return

View File

@ -482,14 +482,14 @@ func TestEtcdCreateBinding(t *testing.T) {
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{}, Target: api.ObjectReference{},
}, },
errOK: func(err error) bool { return errors.IsInvalid(err) }, errOK: func(err error) bool { return err != nil },
}, },
"badKind": { "badKind": {
binding: api.Binding{ binding: api.Binding{
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"}, ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
Target: api.ObjectReference{Name: "machine1", Kind: "unknown"}, 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": { "emptyKind": {
binding: api.Binding{ binding: api.Binding{