Skip to content

Commit

Permalink
Do not block admission if namespace already exists
Browse files Browse the repository at this point in the history
  • Loading branch information
derekwaynecarr committed Mar 26, 2015
1 parent 3b0db99 commit 267ef26
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/client/fake_namespaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion plugin/pkg/admission/namespace/autoprovision/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions plugin/pkg/admission/namespace/autoprovision/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 267ef26

Please sign in to comment.