diff --git a/examples/examples_test.go b/examples/examples_test.go index 259c8a78833..feb1afdf54d 100644 --- a/examples/examples_test.go +++ b/examples/examples_test.go @@ -41,14 +41,14 @@ func validateObject(obj runtime.Object) (errors []error) { errors = append(errors, validateObject(&t.Items[i])...) } case *api.Service: - api.ValidNamespaceOnCreateOrUpdate(ctx, &t.JSONBase) + api.ValidNamespace(ctx, &t.JSONBase) errors = validation.ValidateService(t) case *api.ServiceList: for i := range t.Items { errors = append(errors, validateObject(&t.Items[i])...) } case *api.Pod: - api.ValidNamespaceOnCreateOrUpdate(ctx, &t.JSONBase) + api.ValidNamespace(ctx, &t.JSONBase) errors = validation.ValidateManifest(&t.DesiredState.Manifest) case *api.PodList: for i := range t.Items { diff --git a/pkg/api/context.go b/pkg/api/context.go index c3f33ba85d3..5fe851bb12a 100644 --- a/pkg/api/context.go +++ b/pkg/api/context.go @@ -63,8 +63,8 @@ func NamespaceFrom(ctx Context) (string, bool) { return namespace, ok } -// ValidNamespaceOnCreateOrUpdate returns false if the namespace on the context differs from the resource. If the resource has no namespace, it is set to the value in the context. -func ValidNamespaceOnCreateOrUpdate(ctx Context, resource *JSONBase) bool { +// ValidNamespace returns false if the namespace on the context differs from the resource. If the resource has no namespace, it is set to the value in the context. +func ValidNamespace(ctx Context, resource *JSONBase) bool { ns, ok := NamespaceFrom(ctx) if len(resource.Namespace) == 0 { resource.Namespace = ns diff --git a/pkg/api/context_test.go b/pkg/api/context_test.go index 1cd9c2b5373..146963cb196 100644 --- a/pkg/api/context_test.go +++ b/pkg/api/context_test.go @@ -32,24 +32,31 @@ func TestNamespaceContext(t *testing.T) { if api.NamespaceDefault != result { t.Errorf("Expected: %v, Actual: %v", api.NamespaceDefault, result) } + + ctx = api.NewContext() + result, ok = api.NamespaceFrom(ctx) + if ok { + t.Errorf("Should not be ok because there is no namespace on the context") + } } -func TestValidNamespaceOnCreateOrUpdate(t *testing.T) { +// TestValidNamespace validates that namespace rules are enforced on a resource prior to create or update +func TestValidNamespace(t *testing.T) { ctx := api.NewDefaultContext() namespace, _ := api.NamespaceFrom(ctx) resource := api.ReplicationController{} - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) { + if !api.ValidNamespace(ctx, &resource.JSONBase) { t.Errorf("expected success") } if namespace != resource.Namespace { t.Errorf("expected resource to have the default namespace assigned during validation") } resource = api.ReplicationController{JSONBase: api.JSONBase{Namespace: "other"}} - if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) { + if api.ValidNamespace(ctx, &resource.JSONBase) { t.Errorf("Expected error that resource and context errors do not match because resource has different namespace") } ctx = api.NewContext() - if api.ValidNamespaceOnCreateOrUpdate(ctx, &resource.JSONBase) { + if api.ValidNamespace(ctx, &resource.JSONBase) { t.Errorf("Expected error that resource and context errors do not match since context has no namespace") } } diff --git a/pkg/registry/controller/rest.go b/pkg/registry/controller/rest.go index 6a3aa7194e1..326144c1f16 100644 --- a/pkg/registry/controller/rest.go +++ b/pkg/registry/controller/rest.go @@ -17,7 +17,6 @@ limitations under the License. package controller import ( - stderrs "errors" "fmt" "time" @@ -60,8 +59,8 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Obje if !ok { return nil, fmt.Errorf("not a replication controller: %#v", obj) } - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &controller.JSONBase) { - return nil, errors.NewConflict("controller", controller.Namespace, stderrs.New("Controller.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &controller.JSONBase) { + return nil, errors.NewConflict("controller", controller.Namespace, fmt.Errorf("Controller.Namespace does not match the provided context")) } if len(controller.ID) == 0 { @@ -133,8 +132,8 @@ func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Obje if !ok { return nil, fmt.Errorf("not a replication controller: %#v", obj) } - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &controller.JSONBase) { - return nil, errors.NewConflict("controller", controller.Namespace, stderrs.New("Controller.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &controller.JSONBase) { + return nil, errors.NewConflict("controller", controller.Namespace, fmt.Errorf("Controller.Namespace does not match the provided context")) } if errs := validation.ValidateReplicationController(controller); len(errs) > 0 { return nil, errors.NewInvalid("replicationController", controller.ID, errs) diff --git a/pkg/registry/pod/rest.go b/pkg/registry/pod/rest.go index 89416130f04..4b45b096db2 100644 --- a/pkg/registry/pod/rest.go +++ b/pkg/registry/pod/rest.go @@ -17,7 +17,6 @@ limitations under the License. package pod import ( - stderrs "errors" "fmt" "sync" "time" @@ -70,8 +69,8 @@ func NewREST(config *RESTConfig) *REST { func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) { pod := obj.(*api.Pod) - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &pod.JSONBase) { - return nil, errors.NewConflict("pod", pod.Namespace, stderrs.New("Pod.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &pod.JSONBase) { + return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context")) } pod.DesiredState.Manifest.UUID = uuid.NewUUID().String() if len(pod.ID) == 0 { @@ -162,8 +161,8 @@ func (*REST) New() runtime.Object { func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) { pod := obj.(*api.Pod) - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &pod.JSONBase) { - return nil, errors.NewConflict("pod", pod.Namespace, stderrs.New("Pod.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &pod.JSONBase) { + return nil, errors.NewConflict("pod", pod.Namespace, fmt.Errorf("Pod.Namespace does not match the provided context")) } if errs := validation.ValidatePod(pod); len(errs) > 0 { return nil, errors.NewInvalid("pod", pod.ID, errs) diff --git a/pkg/registry/service/rest.go b/pkg/registry/service/rest.go index 2f430435f39..702c3c48db0 100644 --- a/pkg/registry/service/rest.go +++ b/pkg/registry/service/rest.go @@ -17,7 +17,6 @@ limitations under the License. package service import ( - stderrs "errors" "fmt" "math/rand" "strconv" @@ -53,8 +52,8 @@ func NewREST(registry Registry, cloud cloudprovider.Interface, machines minion.R func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) { srv := obj.(*api.Service) - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &srv.JSONBase) { - return nil, errors.NewConflict("service", srv.Namespace, stderrs.New("Service.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &srv.JSONBase) { + return nil, errors.NewConflict("service", srv.Namespace, fmt.Errorf("Service.Namespace does not match the provided context")) } if errs := validation.ValidateService(srv); len(errs) > 0 { return nil, errors.NewInvalid("service", srv.ID, errs) @@ -169,8 +168,8 @@ func GetServiceEnvironmentVariables(ctx api.Context, registry Registry, machine func (rs *REST) Update(ctx api.Context, obj runtime.Object) (<-chan runtime.Object, error) { srv := obj.(*api.Service) - if !api.ValidNamespaceOnCreateOrUpdate(ctx, &srv.JSONBase) { - return nil, errors.NewConflict("service", srv.Namespace, stderrs.New("Service.Namespace does not match the provided context")) + if !api.ValidNamespace(ctx, &srv.JSONBase) { + return nil, errors.NewConflict("service", srv.Namespace, fmt.Errorf("Service.Namespace does not match the provided context")) } if errs := validation.ValidateService(srv); len(errs) > 0 { return nil, errors.NewInvalid("service", srv.ID, errs)