Do not block admission if namespace already exists

This commit is contained in:
derekwaynecarr 2015-03-26 15:59:09 -04:00
parent 3b0db99692
commit 267ef26b0f
3 changed files with 28 additions and 2 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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")
}
}