Skip to content

Commit 28ba1f2

Browse files
authored
Merge pull request #106 from grantr/list-options
Convert client.List to use functional options
2 parents 8d93cc7 + bf0795d commit 28ba1f2

File tree

11 files changed

+299
-210
lines changed

11 files changed

+299
-210
lines changed

pkg/builder/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (a *ReplicaSetReconciler) Reconcile(req reconcile.Request) (reconcile.Resul
7777

7878
// List the Pods matching the PodTemplate Labels
7979
pods := &corev1.PodList{}
80-
err = a.List(context.TODO(), client.InNamespace(req.Namespace).MatchingLabels(rs.Spec.Template.Labels), pods)
80+
err = a.List(context.TODO(), pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
8181
if err != nil {
8282
return reconcile.Result{}, err
8383
}

pkg/cache/cache_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ var _ = Describe("Informer Cache", func() {
113113
It("should be able to list objects that haven't been watched previously", func() {
114114
By("listing all services in the cluster")
115115
listObj := &kcorev1.ServiceList{}
116-
Expect(informerCache.List(context.Background(), nil, listObj)).To(Succeed())
116+
Expect(informerCache.List(context.Background(), listObj)).To(Succeed())
117117

118118
By("verifying that the returned list contains the Kubernetes service")
119119
// NB: kubernetes default service is automatically created in testenv.
@@ -143,8 +143,10 @@ var _ = Describe("Informer Cache", func() {
143143
By("listing pods with a particular label")
144144
// NB: each pod has a "test-label": <pod-name>
145145
out := kcorev1.PodList{}
146-
Expect(informerCache.List(context.Background(), client.InNamespace(testNamespaceTwo).
147-
MatchingLabels(map[string]string{"test-label": "test-pod-2"}), &out)).To(Succeed())
146+
Expect(informerCache.List(context.Background(), &out,
147+
client.InNamespace(testNamespaceTwo),
148+
client.MatchingLabels(map[string]string{"test-label": "test-pod-2"}),
149+
)).To(Succeed())
148150

149151
By("verifying the returned pods have the correct label")
150152
Expect(out.Items).NotTo(BeEmpty())
@@ -161,8 +163,9 @@ var _ = Describe("Informer Cache", func() {
161163
// NB: each pod has a "test-label": <pod-name>
162164
out := kcorev1.PodList{}
163165
labels := map[string]string{"test-label": "test-pod-2"}
164-
Expect(informerCache.List(context.Background(),
165-
client.MatchingLabels(labels), &out)).To(Succeed())
166+
Expect(informerCache.List(context.Background(), &out,
167+
client.MatchingLabels(labels),
168+
)).To(Succeed())
166169

167170
By("verifying multiple pods with the same label in different namespaces are returned")
168171
Expect(out.Items).NotTo(BeEmpty())
@@ -177,9 +180,9 @@ var _ = Describe("Informer Cache", func() {
177180
It("should be able to list objects by namespace", func() {
178181
By("listing pods in test-namespace-1")
179182
listObj := &kcorev1.PodList{}
180-
Expect(informerCache.List(context.Background(),
183+
Expect(informerCache.List(context.Background(), listObj,
181184
client.InNamespace(testNamespaceOne),
182-
listObj)).To(Succeed())
185+
)).To(Succeed())
183186

184187
By("verifying that the returned pods are in test-namespace-1")
185188
Expect(listObj.Items).NotTo(BeEmpty())
@@ -317,9 +320,9 @@ var _ = Describe("Informer Cache", func() {
317320

318321
By("listing Pods with restartPolicyOnFailure")
319322
listObj := &kcorev1.PodList{}
320-
Expect(informer.List(context.Background(),
323+
Expect(informer.List(context.Background(), listObj,
321324
client.MatchingField("spec.restartPolicy", "OnFailure"),
322-
listObj)).To(Succeed())
325+
)).To(Succeed())
323326

324327
By("verifying that the returned pods have correct restart policy")
325328
Expect(listObj.Items).NotTo(BeEmpty())

pkg/cache/informer_cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runt
5757
}
5858

5959
// List implements Reader
60-
func (ip *informerCache) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
60+
func (ip *informerCache) List(ctx context.Context, out runtime.Object, opts ...client.ListOptionFunc) error {
6161
itemsPtr, err := apimeta.GetItemsPtr(out)
6262
if err != nil {
6363
return nil
@@ -87,7 +87,7 @@ func (ip *informerCache) List(ctx context.Context, opts *client.ListOptions, out
8787
return err
8888
}
8989

90-
return cache.Reader.List(ctx, opts, out)
90+
return cache.Reader.List(ctx, out, opts...)
9191
}
9292

9393
// GetInformerForKind returns the informer for the GroupVersionKind

pkg/cache/informertest/fake_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ func (c *FakeInformers) Get(ctx context.Context, key client.ObjectKey, obj runti
136136
}
137137

138138
// List implements Cache
139-
func (c *FakeInformers) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
139+
func (c *FakeInformers) List(ctx context.Context, list runtime.Object, opts ...client.ListOptionFunc) error {
140140
return nil
141141
}

pkg/cache/internal/cache_reader.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,32 +86,35 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.O
8686
}
8787

8888
// List lists items out of the indexer and writes them to out
89-
func (c *CacheReader) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
89+
func (c *CacheReader) List(ctx context.Context, out runtime.Object, opts ...client.ListOptionFunc) error {
9090
var objs []interface{}
9191
var err error
9292

93-
if opts != nil && opts.FieldSelector != nil {
93+
listOpts := client.ListOptions{}
94+
listOpts.ApplyOptions(opts)
95+
96+
if listOpts.FieldSelector != nil {
9497
// TODO(directxman12): support more complicated field selectors by
9598
// combining multiple indicies, GetIndexers, etc
96-
field, val, requiresExact := requiresExactMatch(opts.FieldSelector)
99+
field, val, requiresExact := requiresExactMatch(listOpts.FieldSelector)
97100
if !requiresExact {
98101
return fmt.Errorf("non-exact field matches are not supported by the cache")
99102
}
100103
// list all objects by the field selector. If this is namespaced and we have one, ask for the
101104
// namespaced index key. Otherwise, ask for the non-namespaced variant by using the fake "all namespaces"
102105
// namespace.
103-
objs, err = c.indexer.ByIndex(FieldIndexName(field), KeyToNamespacedKey(opts.Namespace, val))
104-
} else if opts != nil && opts.Namespace != "" {
105-
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, opts.Namespace)
106+
objs, err = c.indexer.ByIndex(FieldIndexName(field), KeyToNamespacedKey(listOpts.Namespace, val))
107+
} else if listOpts.Namespace != "" {
108+
objs, err = c.indexer.ByIndex(cache.NamespaceIndex, listOpts.Namespace)
106109
} else {
107110
objs = c.indexer.List()
108111
}
109112
if err != nil {
110113
return err
111114
}
112115
var labelSel labels.Selector
113-
if opts != nil && opts.LabelSelector != nil {
114-
labelSel = opts.LabelSelector
116+
if listOpts.LabelSelector != nil {
117+
labelSel = listOpts.LabelSelector
115118
}
116119

117120
outItems, err := c.getListItems(objs, labelSel)

pkg/client/client.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,19 @@ func (c *client) Get(ctx context.Context, key ObjectKey, obj runtime.Object) err
140140
}
141141

142142
// List implements client.Client
143-
func (c *client) List(ctx context.Context, opts *ListOptions, obj runtime.Object) error {
143+
func (c *client) List(ctx context.Context, obj runtime.Object, opts ...ListOptionFunc) error {
144144
r, err := c.cache.getResource(obj)
145145
if err != nil {
146146
return err
147147
}
148-
namespace := ""
149-
if opts != nil {
150-
namespace = opts.Namespace
151-
}
148+
149+
listOpts := ListOptions{}
150+
listOpts.ApplyOptions(opts)
152151
return r.Get().
153-
NamespaceIfScoped(namespace, r.isNamespaced()).
152+
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
154153
Resource(r.resource()).
155154
Body(obj).
156-
VersionedParams(opts.AsListOptions(), c.paramCodec).
155+
VersionedParams(listOpts.AsListOptions(), c.paramCodec).
157156
Do().
158157
Into(obj)
159158
}

0 commit comments

Comments
 (0)