diff --git a/pkg/api/validation.go b/pkg/api/validation.go index d58cbdf0521..e3572d4a3c4 100644 --- a/pkg/api/validation.go +++ b/pkg/api/validation.go @@ -246,3 +246,14 @@ func ValidateManifest(manifest *ContainerManifest) []error { allErrs.Append(validateContainers(manifest.Containers, allVolumes)...) return []error(allErrs) } + +func ValidateService(service *Service) []error { + allErrs := errorList{} + if service.ID == "" { + allErrs.Append(fmt.Errorf("ID should not be empty: %#v", *service)) + } + if len(service.Selector) == 0 { + allErrs.Append(fmt.Errorf("Service %#v missing a selector", *service)) + } + return []error(allErrs) +} diff --git a/pkg/api/validation_test.go b/pkg/api/validation_test.go index 34bf26bc1ee..84ae5ff20c5 100644 --- a/pkg/api/validation_test.go +++ b/pkg/api/validation_test.go @@ -262,3 +262,36 @@ func TestValidateManifest(t *testing.T) { } } } + +func TestValidateService(t *testing.T) { + errs := ValidateService(&Service{ + JSONBase: JSONBase{ID: "foo"}, + Selector: map[string]string{ + "foo": "bar", + }, + }) + if len(errs) != 0 { + t.Errorf("Unexpected non-zero error list: %#v", errs) + } + + errs = ValidateService(&Service{ + Selector: map[string]string{ + "foo": "bar", + }, + }) + if len(errs) != 1 { + t.Errorf("Unexpected error list: %#v", errs) + } + + errs = ValidateService(&Service{ + JSONBase: JSONBase{ID: "foo"}, + }) + if len(errs) != 1 { + t.Errorf("Unexpected error list: %#v", errs) + } + + errs = ValidateService(&Service{}) + if len(errs) != 2 { + t.Errorf("Unexpected error list: %#v", errs) + } +} diff --git a/pkg/registry/service_registry.go b/pkg/registry/service_registry.go index 89455a85c50..6d4e0d8fd4d 100644 --- a/pkg/registry/service_registry.go +++ b/pkg/registry/service_registry.go @@ -112,8 +112,9 @@ func (sr *ServiceRegistryStorage) Extract(body []byte) (interface{}, error) { func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, error) { srv := obj.(api.Service) - if srv.ID == "" { - return nil, fmt.Errorf("ID should not be empty: %#v", srv) + errs := api.ValidateService(&srv) + if len(errs) > 0 { + return nil, fmt.Errorf("Validation errors: %v", errs) } return apiserver.MakeAsync(func() (interface{}, error) { // TODO: Consider moving this to a rectification loop, so that we make/remove external load balancers