Skip to content

Commit 79f0601

Browse files
authored
Merge pull request #146 from kubernetes-sigs/revert-106-list-options
Revert "Convert client.List to use functional options"
2 parents ad139d6 + 5e1fc31 commit 79f0601

File tree

11 files changed

+210
-299
lines changed

11 files changed

+210
-299
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(), pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
80+
err = a.List(context.TODO(), client.InNamespace(req.Namespace).MatchingLabels(rs.Spec.Template.Labels), pods)
8181
if err != nil {
8282
return reconcile.Result{}, err
8383
}

pkg/cache/cache_test.go

Lines changed: 9 additions & 12 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(), listObj)).To(Succeed())
116+
Expect(informerCache.List(context.Background(), nil, 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,10 +143,8 @@ 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(), &out,
147-
client.InNamespace(testNamespaceTwo),
148-
client.MatchingLabels(map[string]string{"test-label": "test-pod-2"}),
149-
)).To(Succeed())
146+
Expect(informerCache.List(context.Background(), client.InNamespace(testNamespaceTwo).
147+
MatchingLabels(map[string]string{"test-label": "test-pod-2"}), &out)).To(Succeed())
150148

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

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

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

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

327324
By("verifying that the returned pods have correct restart policy")
328325
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, out runtime.Object, opts ...client.ListOptionFunc) error {
60+
func (ip *informerCache) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) 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, out runtime.Object, opts ...c
8787
return err
8888
}
8989

90-
return cache.Reader.List(ctx, out, opts...)
90+
return cache.Reader.List(ctx, opts, out)
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, list runtime.Object, opts ...client.ListOptionFunc) error {
139+
func (c *FakeInformers) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
140140
return nil
141141
}

pkg/cache/internal/cache_reader.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,32 @@ 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, out runtime.Object, opts ...client.ListOptionFunc) error {
89+
func (c *CacheReader) List(ctx context.Context, opts *client.ListOptions, out runtime.Object) error {
9090
var objs []interface{}
9191
var err error
9292

93-
listOpts := client.ListOptions{}
94-
listOpts.ApplyOptions(opts)
95-
96-
if listOpts.FieldSelector != nil {
93+
if opts != nil && opts.FieldSelector != nil {
9794
// TODO(directxman12): support more complicated field selectors by
9895
// combining multiple indicies, GetIndexers, etc
99-
field, val, requiresExact := requiresExactMatch(listOpts.FieldSelector)
96+
field, val, requiresExact := requiresExactMatch(opts.FieldSelector)
10097
if !requiresExact {
10198
return fmt.Errorf("non-exact field matches are not supported by the cache")
10299
}
103100
// list all objects by the field selector. If this is namespaced and we have one, ask for the
104101
// namespaced index key. Otherwise, ask for the non-namespaced variant by using the fake "all namespaces"
105102
// 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)
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)
109106
} else {
110107
objs = c.indexer.List()
111108
}
112109
if err != nil {
113110
return err
114111
}
115112
var labelSel labels.Selector
116-
if listOpts.LabelSelector != nil {
117-
labelSel = listOpts.LabelSelector
113+
if opts != nil && opts.LabelSelector != nil {
114+
labelSel = opts.LabelSelector
118115
}
119116

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

pkg/client/client.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,20 @@ 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, obj runtime.Object, opts ...ListOptionFunc) error {
143+
func (c *client) List(ctx context.Context, opts *ListOptions, obj runtime.Object) error {
144144
r, err := c.cache.getResource(obj)
145145
if err != nil {
146146
return err
147147
}
148-
149-
listOpts := ListOptions{}
150-
listOpts.ApplyOptions(opts)
148+
namespace := ""
149+
if opts != nil {
150+
namespace = opts.Namespace
151+
}
151152
return r.Get().
152-
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
153+
NamespaceIfScoped(namespace, r.isNamespaced()).
153154
Resource(r.resource()).
154155
Body(obj).
155-
VersionedParams(listOpts.AsListOptions(), c.paramCodec).
156+
VersionedParams(opts.AsListOptions(), c.paramCodec).
156157
Do().
157158
Into(obj)
158159
}

0 commit comments

Comments
 (0)