From 267ef26b0f6117b69bcc53ce7d1e3ea16a76637f Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Thu, 26 Mar 2015 15:59:09 -0400 Subject: [PATCH] Do not block admission if namespace already exists --- pkg/client/fake_namespaces.go | 2 +- .../namespace/autoprovision/admission.go | 3 ++- .../namespace/autoprovision/admission_test.go | 25 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/client/fake_namespaces.go b/pkg/client/fake_namespaces.go index 055bd2a2403..afa7f6e2eca 100644 --- a/pkg/client/fake_namespaces.go +++ b/pkg/client/fake_namespaces.go @@ -46,7 +46,7 @@ func (c *FakeNamespaces) Delete(name string) error { func (c *FakeNamespaces) Create(namespace *api.Namespace) (*api.Namespace, error) { c.Fake.Actions = append(c.Fake.Actions, FakeAction{Action: "create-namespace"}) - return &api.Namespace{}, nil + return &api.Namespace{}, c.Fake.Err } func (c *FakeNamespaces) Update(namespace *api.Namespace) (*api.Namespace, error) { diff --git a/plugin/pkg/admission/namespace/autoprovision/admission.go b/plugin/pkg/admission/namespace/autoprovision/admission.go index c54b819265c..86747ed513b 100644 --- a/plugin/pkg/admission/namespace/autoprovision/admission.go +++ b/plugin/pkg/admission/namespace/autoprovision/admission.go @@ -21,6 +21,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/admission" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest" "github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" @@ -76,7 +77,7 @@ func (p *provision) Admit(a admission.Attributes) (err error) { return nil } _, err = p.client.Namespaces().Create(namespace) - if err != nil { + if err != nil && !errors.IsAlreadyExists(err) { return err } return nil diff --git a/plugin/pkg/admission/namespace/autoprovision/admission_test.go b/plugin/pkg/admission/namespace/autoprovision/admission_test.go index b6f6bb26974..91b29b735d2 100644 --- a/plugin/pkg/admission/namespace/autoprovision/admission_test.go +++ b/plugin/pkg/admission/namespace/autoprovision/admission_test.go @@ -21,6 +21,7 @@ import ( "github.com/GoogleCloudPlatform/kubernetes/pkg/admission" "github.com/GoogleCloudPlatform/kubernetes/pkg/api" + "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors" "github.com/GoogleCloudPlatform/kubernetes/pkg/client" "github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache" ) @@ -103,3 +104,27 @@ func TestIgnoreAdmission(t *testing.T) { t.Errorf("No client request should have been made") } } + +// TestAdmissionNamespaceExistsUnknownToHandler +func TestAdmissionNamespaceExistsUnknownToHandler(t *testing.T) { + namespace := "test" + mockClient := &client.Fake{ + Err: errors.NewAlreadyExists("namespaces", namespace), + } + store := cache.NewStore(cache.MetaNamespaceKeyFunc) + handler := &provision{ + client: mockClient, + store: store, + } + pod := api.Pod{ + ObjectMeta: api.ObjectMeta{Name: "123", Namespace: namespace}, + Spec: api.PodSpec{ + Volumes: []api.Volume{{Name: "vol"}}, + Containers: []api.Container{{Name: "ctr", Image: "image"}}, + }, + } + err := handler.Admit(admission.NewAttributesRecord(&pod, namespace, "pods", "CREATE")) + if err != nil { + t.Errorf("Unexpected error returned from admission handler") + } +}