diff --git a/pkg/kube/client.go b/pkg/kube/client.go index cd5227dd7..673b06f4b 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -100,6 +100,12 @@ func (c *Client) Create(namespace string, reader io.Reader, timeout int64, shoul if buildErr != nil { return buildErr } + + c.Log("checking %d resources for existing conflicts", len(infos)) + if err := c.existingResourceConflict(infos); err != nil { + return err + } + c.Log("creating %d resource(s)", len(infos)) if err := perform(infos, createResource); err != nil { return err @@ -110,6 +116,26 @@ func (c *Client) Create(namespace string, reader io.Reader, timeout int64, shoul return nil } +func (s *Client) existingResourceConflict(infos Result) error { + err := infos.Visit(func(info *resource.Info, err error) error { + if err != nil { + return err + } + + helper := resource.NewHelper(info.Client, info.Mapping) + if _, err := helper.Get(info.Namespace, info.Name, info.Export); err != nil { + if errors.IsNotFound(err) { + return nil + } + + return fmt.Errorf("Could not get information about the resource: %s", err) + } + + return fmt.Errorf("Existing resource conflict: %s", info.Name) + }) + return err +} + func (c *Client) newBuilder(namespace string, reader io.Reader) *resource.Result { return c.NewBuilder(). Internal(). diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index 36c334a23..8c26ffa25 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -355,8 +355,7 @@ func TestCreate(t *testing.T) { existingPod := strings.NewReader(fmt.Sprintf(testNewPodFormatString, existingResourceName)) err := c.Create("default", existingPod, 3, false) if err == nil { - t.Skip("This test is pending. still need to implement exit on existing resource.") - t.Errorf("creating a resource which already exists should with proper error, got: %q", err) + t.Errorf("creating a resource which already exists should fail with error, got: %q", err) } }