Skip to content

Commit

Permalink
Merge pull request #447 from adracus/feature.delete-collection
Browse files Browse the repository at this point in the history
✨ Implement delete collection via delete options
  • Loading branch information
k8s-ci-robot committed Jul 26, 2019
2 parents 59b131b + 3b06668 commit bfba246
Show file tree
Hide file tree
Showing 9 changed files with 289 additions and 10 deletions.
9 changes: 9 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,15 @@ func (c *client) Delete(ctx context.Context, obj runtime.Object, opts ...DeleteO
return c.typedClient.Delete(ctx, obj, opts...)
}

// DeleteAllOf implements client.Client
func (c *client) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...DeleteAllOfOption) error {
_, ok := obj.(*unstructured.Unstructured)
if ok {
return c.unstructuredClient.DeleteAllOf(ctx, obj, opts...)
}
return c.typedClient.DeleteAllOf(ctx, obj, opts...)
}

// Patch implements client.Client
func (c *client) Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error {
_, ok := obj.(*unstructured.Unstructured)
Expand Down
110 changes: 109 additions & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var _ = Describe("Client", func() {
BeforeEach(func(done Done) {
atomic.AddUint64(&count, 1)
dep = &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("deployment-name-%v", count), Namespace: ns},
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("deployment-name-%v", count), Namespace: ns, Labels: map[string]string{"app": fmt.Sprintf("bar-%v", count)}},
Spec: appsv1.DeploymentSpec{
Replicas: &replicaCount,
Selector: &metav1.LabelSelector{
Expand Down Expand Up @@ -898,6 +898,37 @@ var _ = Describe("Client", func() {
PIt("should fail if the GVK cannot be mapped to a Resource", func() {

})

It("should delete a collection of objects", func(done Done) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("initially creating two Deployments")

dep2 := dep.DeepCopy()
dep2.Name = dep2.Name + "-2"

dep, err = clientset.AppsV1().Deployments(ns).Create(dep)
Expect(err).NotTo(HaveOccurred())
dep2, err = clientset.AppsV1().Deployments(ns).Create(dep2)
Expect(err).NotTo(HaveOccurred())

depName := dep.Name
dep2Name := dep2.Name

By("deleting Deployments")
err = cl.DeleteAllOf(context.TODO(), dep, client.InNamespace(ns), client.MatchingLabels(dep.ObjectMeta.Labels))
Expect(err).NotTo(HaveOccurred())

By("validating the Deployment no longer exists")
_, err = clientset.AppsV1().Deployments(ns).Get(depName, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
_, err = clientset.AppsV1().Deployments(ns).Get(dep2Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())

close(done)
})
})
Context("with unstructured objects", func() {
It("should delete an existing object from a go struct", func(done Done) {
Expand Down Expand Up @@ -974,6 +1005,44 @@ var _ = Describe("Client", func() {

close(done)
})

It("should delete a collection of object", func(done Done) {
cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())
Expect(cl).NotTo(BeNil())

By("initially creating two Deployments")

dep2 := dep.DeepCopy()
dep2.Name = dep2.Name + "-2"

dep, err = clientset.AppsV1().Deployments(ns).Create(dep)
Expect(err).NotTo(HaveOccurred())
dep2, err = clientset.AppsV1().Deployments(ns).Create(dep2)
Expect(err).NotTo(HaveOccurred())

depName := dep.Name
dep2Name := dep2.Name

By("deleting Deployments")
u := &unstructured.Unstructured{}
Expect(scheme.Convert(dep, u, nil)).To(Succeed())
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "apps",
Kind: "Deployment",
Version: "v1",
})
err = cl.DeleteAllOf(context.TODO(), u, client.InNamespace(ns), client.MatchingLabels(dep.ObjectMeta.Labels))
Expect(err).NotTo(HaveOccurred())

By("validating the Deployment no longer exists")
_, err = clientset.AppsV1().Deployments(ns).Get(depName, metav1.GetOptions{})
Expect(err).To(HaveOccurred())
_, err = clientset.AppsV1().Deployments(ns).Get(dep2Name, metav1.GetOptions{})
Expect(err).To(HaveOccurred())

close(done)
})
})
})

Expand Down Expand Up @@ -1994,6 +2063,10 @@ var _ = Describe("Client", func() {
PIt("should fail if the object doesn't have meta", func() {

})

PIt("should filter results by namespace selector", func() {

})
})
})

Expand Down Expand Up @@ -2072,6 +2145,34 @@ var _ = Describe("Client", func() {
})
})

Describe("DeleteCollectionOptions", func() {
It("should be convertable to list options", func() {
gp := int64(1)
do := &client.DeleteAllOfOptions{}
do.ApplyOptions([]client.DeleteAllOfOption{
client.GracePeriodSeconds(gp),
client.MatchingLabels{"foo": "bar"},
})

listOpts := do.AsListOptions()
Expect(listOpts).NotTo(BeNil())
Expect(listOpts.LabelSelector).To(Equal("foo=bar"))
})

It("should be convertable to delete options", func() {
gp := int64(1)
do := &client.DeleteAllOfOptions{}
do.ApplyOptions([]client.DeleteAllOfOption{
client.GracePeriodSeconds(gp),
client.MatchingLabels{"foo": "bar"},
})

deleteOpts := do.AsDeleteOptions()
Expect(deleteOpts).NotTo(BeNil())
Expect(deleteOpts.GracePeriodSeconds).To(Equal(&gp))
})
})

Describe("ListOptions", func() {
It("should be convertable to metav1.ListOptions", func() {
lo := (&client.ListOptions{}).ApplyOptions([]client.ListOption{
Expand Down Expand Up @@ -2105,6 +2206,13 @@ var _ = Describe("Client", func() {
Expect(lo).NotTo(BeNil())
Expect(lo.Namespace).To(Equal("test"))
})

It("should produce empty metav1.ListOptions if nil", func() {
var do *client.ListOptions
Expect(do.AsListOptions()).To(Equal(&metav1.ListOptions{}))
do = &client.ListOptions{}
Expect(do.AsListOptions()).To(Equal(&metav1.ListOptions{}))
})
})

Describe("UpdateOptions", func() {
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,22 @@ func ExampleClient_delete() {
_ = c.Delete(context.Background(), u)
}

// This example shows how to use the client with typed and unstrucurted objects to delete collections of objects.
func ExampleClient_deleteAllOf() {
// Using a typed object.
// c is a created client.
_ = c.DeleteAllOf(context.Background(), &corev1.Pod{}, client.InNamespace("foo"), client.MatchingLabels{"app": "foo"})

// Using an unstructured Object
u := &unstructured.UnstructuredList{}
u.SetGroupVersionKind(schema.GroupVersionKind{
Group: "apps",
Kind: "Deployment",
Version: "v1",
})
_ = c.DeleteAllOf(context.Background(), u, client.InNamespace("foo"), client.MatchingLabels{"app": "foo"})
}

// This example shows how to set up and consume a field selector over a pod's volumes' secretName field.
func ExampleFieldIndexer_secretName() {
// someIndexer is a FieldIndexer over a Cache
Expand Down
39 changes: 39 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,49 @@ func (c *fakeClient) Delete(ctx context.Context, obj runtime.Object, opts ...cli
if err != nil {
return err
}
delOptions := client.DeleteOptions{}
delOptions.ApplyOptions(opts)

//TODO: implement propagation
return c.tracker.Delete(gvr, accessor.GetNamespace(), accessor.GetName())
}

func (c *fakeClient) DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...client.DeleteAllOfOption) error {
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
if err != nil {
return err
}

dcOptions := client.DeleteAllOfOptions{}
dcOptions.ApplyOptions(opts)

gvr, _ := meta.UnsafeGuessKindToResource(gvk)
o, err := c.tracker.List(gvr, gvk, dcOptions.Namespace)
if err != nil {
return err
}

objs, err := meta.ExtractList(o)
if err != nil {
return err
}
filteredObjs, err := objectutil.FilterWithLabels(objs, dcOptions.LabelSelector)
if err != nil {
return err
}
for _, o := range filteredObjs {
accessor, err := meta.Accessor(o)
if err != nil {
return err
}
err = c.tracker.Delete(gvr, accessor.GetNamespace(), accessor.GetName())
if err != nil {
return err
}
}
return nil
}

func (c *fakeClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOption) error {
updateOptions := &client.UpdateOptions{}
updateOptions.ApplyOptions(opts)
Expand Down
12 changes: 12 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should be able to Delete a Collection", func() {
By("Deleting a deploymentList")
err := cl.DeleteAllOf(nil, &appsv1.Deployment{}, client.InNamespace("ns1"))
Expect(err).To(BeNil())

By("Listing all deployments in the namespace")
list := &appsv1.DeploymentList{}
err = cl.List(nil, list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(BeEmpty())
})

Context("with the DryRun option", func() {
It("should not create a new object", func() {
By("Creating a new configmap with DryRun")
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Writer interface {
// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
Patch(ctx context.Context, obj runtime.Object, patch Patch, opts ...PatchOption) error

// DeleteAllOf deletes all objects of the given type matching the given options.
DeleteAllOf(ctx context.Context, obj runtime.Object, opts ...DeleteAllOfOption) error
}

// StatusClient knows how to create a client which can update status subresource
Expand Down
Loading

0 comments on commit bfba246

Please sign in to comment.