Skip to content

🐛 Fakeclient: Support WithWatch #1510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
utilrand "k8s.io/apimachinery/pkg/util/rand"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/testing"

Expand All @@ -49,7 +50,7 @@ type fakeClient struct {
scheme *runtime.Scheme
}

var _ client.Client = &fakeClient{}
var _ client.WithWatch = &fakeClient{}

const (
maxNameLength = 63
Expand All @@ -61,7 +62,7 @@ const (
// You can choose to initialize it with a slice of runtime.Object.
//
// Deprecated: Please use NewClientBuilder instead.
func NewFakeClient(initObjs ...runtime.Object) client.Client {
func NewFakeClient(initObjs ...runtime.Object) client.WithWatch {
return NewClientBuilder().WithRuntimeObjects(initObjs...).Build()
}

Expand All @@ -70,7 +71,7 @@ func NewFakeClient(initObjs ...runtime.Object) client.Client {
// You can choose to initialize it with a slice of runtime.Object.
//
// Deprecated: Please use NewClientBuilder instead.
func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...runtime.Object) client.Client {
func NewFakeClientWithScheme(clientScheme *runtime.Scheme, initObjs ...runtime.Object) client.WithWatch {
return NewClientBuilder().WithScheme(clientScheme).WithRuntimeObjects(initObjs...).Build()
}

Expand Down Expand Up @@ -113,7 +114,7 @@ func (f *ClientBuilder) WithRuntimeObjects(initRuntimeObjs ...runtime.Object) *C
}

// Build builds and returns a new fake client.
func (f *ClientBuilder) Build() client.Client {
func (f *ClientBuilder) Build() client.WithWatch {
if f.scheme == nil {
f.scheme = scheme.Scheme
}
Expand Down Expand Up @@ -284,6 +285,23 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.O
return err
}

func (c *fakeClient) Watch(ctx context.Context, list client.ObjectList, opts ...client.ListOption) (watch.Interface, error) {
gvk, err := apiutil.GVKForObject(list, c.scheme)
if err != nil {
return nil, err
}

if strings.HasSuffix(gvk.Kind, "List") {
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}

listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)

gvr, _ := meta.UnsafeGuessKindToResource(gvk)
return c.tracker.Watch(gvr, listOpts.Namespace)
}

func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...client.ListOption) error {
gvk, err := apiutil.GVKForObject(obj, c.scheme)
if err != nil {
Expand Down
30 changes: 29 additions & 1 deletion pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -33,14 +34,15 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Fake client", func() {
var dep *appsv1.Deployment
var dep2 *appsv1.Deployment
var cm *corev1.ConfigMap
var cl client.Client
var cl client.WithWatch

BeforeEach(func() {
dep = &appsv1.Deployment{
Expand Down Expand Up @@ -541,6 +543,32 @@ var _ = Describe("Fake client", func() {
}
})

It("should be able to watch", func() {
By("Creating a watch")
objWatch, err := cl.Watch(context.Background(), &corev1.ServiceList{})
Expect(err).NotTo(HaveOccurred())

defer objWatch.Stop()

go func() {
defer GinkgoRecover()
// It is likely starting a new goroutine is slower than progressing
// in the outer routine, sleep to make sure this is always true
time.Sleep(100 * time.Millisecond)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a channel or semaphore to enforce it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we are waiting for is that the main routine starts to read, how would you sync that with a channel or semaphore?


err := cl.Create(context.Background(), &corev1.Service{ObjectMeta: metav1.ObjectMeta{Namespace: "default", Name: "for-watch"}})
Expect(err).ToNot(HaveOccurred())
}()

event, ok := <-objWatch.ResultChan()
Expect(ok).To(BeTrue())
Expect(event.Type).To(Equal(watch.Added))

service, ok := event.Object.(*corev1.Service)
Expect(ok).To(BeTrue())
Expect(service.Name).To(Equal("for-watch"))
})

Context("with the DryRun option", func() {
It("should not create a new object", func() {
By("Creating a new configmap with DryRun")
Expand Down