From f062cff5a37b65edc51d74ab3bc065f2af4a2006 Mon Sep 17 00:00:00 2001 From: Michelle Noorali Date: Tue, 6 Sep 2016 13:11:37 -0600 Subject: [PATCH] fix(kube): delete should skip resources not found --- pkg/kube/client.go | 20 ++++++++++++++------ pkg/kube/client_test.go | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 7055ec074..0a0f459ba 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -215,25 +215,33 @@ func (c *Client) Update(namespace string, currentReader, targetReader io.Reader) func (c *Client) Delete(namespace string, reader io.Reader) error { return perform(c, namespace, reader, func(info *resource.Info) error { log.Printf("Starting delete for %s", info.Name) + reaper, err := c.Reaper(info.Mapping) if err != nil { // If there is no reaper for this resources, delete it. if kubectl.IsNoSuchReaperError(err) { - return resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name) + err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name) + return skipIfNotFound(err) } return err } + log.Printf("Using reaper for deleting %s", info.Name) err = reaper.Stop(info.Namespace, info.Name, 0, nil) - if err != nil && errors.IsNotFound(err) { - log.Printf("%v", err) - return nil - } - return err + return skipIfNotFound(err) }) } +func skipIfNotFound(err error) error { + if err != nil && errors.IsNotFound(err) { + log.Printf("%v", err) + return nil + } + + return err +} + // WatchUntilReady watches the resource given in the reader, and waits until it is ready. // // This function is mainly for hook implementations. It watches for a resource to diff --git a/pkg/kube/client_test.go b/pkg/kube/client_test.go index c59cc37b0..bfa953481 100644 --- a/pkg/kube/client_test.go +++ b/pkg/kube/client_test.go @@ -126,11 +126,48 @@ func TestReal(t *testing.T) { t.Fatal(err) } - if err := New(nil).Delete("test", strings.NewReader(guestbookManifest)); err != nil { + testSvcEndpointManifest := testServiceManifest + "\n---\n" + testEndpointManifest + c := New(nil) + if err := c.Create("test-delete", strings.NewReader(testSvcEndpointManifest)); err != nil { + t.Fatal(err) + } + + if err := c.Delete("test-delete", strings.NewReader(testEndpointManifest)); err != nil { + t.Fatal(err) + } + + // ensures that delete does not fail if a resource is not found + if err := c.Delete("test-delete", strings.NewReader(testSvcEndpointManifest)); err != nil { t.Fatal(err) } } +const testServiceManifest = ` +kind: Service +apiVersion: v1 +metadata: + name: my-service +spec: + selector: + app: myapp + ports: + - port: 80 + protocol: TCP + targetPort: 9376 +` + +const testEndpointManifest = ` +kind: Endpoints +apiVersion: v1 +metadata: + name: my-service +subsets: + - addresses: + - ip: "1.2.3.4" + ports: + - port: 9376 +` + const guestbookManifest = ` apiVersion: v1 kind: Service