Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 04763ab

Browse files
committed
remove TODO:binding validation
1 parent 9c0e777 commit 04763ab

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

pkg/api/validation/validation.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,22 @@ func ValidatePodStatusUpdate(newPod, oldPod *api.Pod) field.ErrorList {
12241224
return allErrs
12251225
}
12261226

1227+
// ValidatePodBinding tests if required fields in the pod binding are legal.
1228+
func ValidatePodBinding(binding *api.Binding) field.ErrorList {
1229+
allErrs := field.ErrorList{}
1230+
1231+
if len(binding.Target.Kind) != 0 && binding.Target.Kind != "Node" {
1232+
// TODO: When validation becomes versioned, this gets more complicated.
1233+
allErrs = append(allErrs, field.NotSupported(field.NewPath("target", "kind"), binding.Target.Kind, []string{"Node", "<empty>"}))
1234+
}
1235+
if len(binding.Target.Name) == 0 {
1236+
// TODO: When validation becomes versioned, this gets more complicated.
1237+
allErrs = append(allErrs, field.Required(field.NewPath("target", "name")))
1238+
}
1239+
1240+
return allErrs
1241+
}
1242+
12271243
// ValidatePodTemplate tests if required fields in the pod template are set.
12281244
func ValidatePodTemplate(pod *api.PodTemplate) field.ErrorList {
12291245
allErrs := ValidateObjectMeta(&pod.ObjectMeta, true, ValidatePodName, field.NewPath("metadata"))

pkg/registry/pod/etcd/etcd.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
etcderr "k8s.io/kubernetes/pkg/api/errors/etcd"
2727
"k8s.io/kubernetes/pkg/api/rest"
2828
"k8s.io/kubernetes/pkg/api/unversioned"
29+
"k8s.io/kubernetes/pkg/api/validation"
2930
"k8s.io/kubernetes/pkg/fields"
3031
"k8s.io/kubernetes/pkg/kubelet/client"
3132
"k8s.io/kubernetes/pkg/labels"
@@ -35,7 +36,6 @@ import (
3536
podrest "k8s.io/kubernetes/pkg/registry/pod/rest"
3637
"k8s.io/kubernetes/pkg/runtime"
3738
"k8s.io/kubernetes/pkg/storage"
38-
"k8s.io/kubernetes/pkg/util/validation/field"
3939
)
4040

4141
// PodStorage includes storage for pods and all sub resources
@@ -93,8 +93,8 @@ func NewStorage(
9393

9494
Storage: storageInterface,
9595
}
96-
statusStore := *store
9796

97+
statusStore := *store
9898
statusStore.UpdateStrategy = pod.StatusStrategy
9999

100100
return PodStorage{
@@ -132,15 +132,12 @@ var _ = rest.Creater(&BindingREST{})
132132
// Create ensures a pod is bound to a specific host.
133133
func (r *BindingREST) Create(ctx api.Context, obj runtime.Object) (out runtime.Object, err error) {
134134
binding := obj.(*api.Binding)
135+
135136
// TODO: move me to a binding strategy
136-
if len(binding.Target.Kind) != 0 && binding.Target.Kind != "Node" {
137-
// TODO: When validation becomes versioned, this gets more complicated.
138-
return nil, errors.NewInvalid("binding", binding.Name, field.ErrorList{field.NotSupported(field.NewPath("target", "kind"), binding.Target.Kind, []string{"Node", "<empty>"})})
139-
}
140-
if len(binding.Target.Name) == 0 {
141-
// TODO: When validation becomes versioned, this gets more complicated.
142-
return nil, errors.NewInvalid("binding", binding.Name, field.ErrorList{field.Required(field.NewPath("target", "name"))})
137+
if errs := validation.ValidatePodBinding(binding); len(errs) != 0 {
138+
return nil, errs.ToAggregate()
143139
}
140+
144141
err = r.assignPod(ctx, binding.Name, binding.Target.Name, binding.Annotations)
145142
out = &unversioned.Status{Status: unversioned.StatusSuccess}
146143
return

pkg/registry/pod/etcd/etcd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,14 @@ func TestEtcdCreateBinding(t *testing.T) {
482482
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
483483
Target: api.ObjectReference{},
484484
},
485-
errOK: func(err error) bool { return errors.IsInvalid(err) },
485+
errOK: func(err error) bool { return err != nil },
486486
},
487487
"badKind": {
488488
binding: api.Binding{
489489
ObjectMeta: api.ObjectMeta{Namespace: api.NamespaceDefault, Name: "foo"},
490490
Target: api.ObjectReference{Name: "machine1", Kind: "unknown"},
491491
},
492-
errOK: func(err error) bool { return errors.IsInvalid(err) },
492+
errOK: func(err error) bool { return err != nil },
493493
},
494494
"emptyKind": {
495495
binding: api.Binding{

0 commit comments

Comments
 (0)