Skip to content

Commit 778d809

Browse files
committed
Update List implementations to use new signature
The fake client now uses similar GVK detection code as the other implementations and no longer relies on the Raw object having a TypeMeta.
1 parent 5c314c2 commit 778d809

File tree

6 files changed

+50
-51
lines changed

6 files changed

+50
-51
lines changed

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/fake/client.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package fake
1919
import (
2020
"context"
2121
"encoding/json"
22+
"fmt"
2223
"os"
24+
"strings"
2325

2426
"k8s.io/apimachinery/pkg/api/meta"
2527
"k8s.io/apimachinery/pkg/runtime"
@@ -77,10 +79,23 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.
7779
return err
7880
}
7981

80-
func (c *fakeClient) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
81-
gvk := opts.Raw.TypeMeta.GroupVersionKind()
82+
func (c *fakeClient) List(ctx context.Context, obj runtime.Object, opts ...client.ListOptionFunc) error {
83+
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
84+
if err != nil {
85+
return err
86+
}
87+
88+
if !strings.HasSuffix(gvk.Kind, "List") {
89+
return fmt.Errorf("non-list type %T (kind %q) passed as output", obj, gvk)
90+
}
91+
// we need the non-list GVK, so chop off the "List" from the end of the kind
92+
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
93+
94+
listOpts := client.ListOptions{}
95+
listOpts.ApplyOptions(opts)
96+
8297
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
83-
o, err := c.tracker.List(gvr, gvk, opts.Namespace)
98+
o, err := c.tracker.List(gvr, gvk, listOpts.Namespace)
8499
if err != nil {
85100
return err
86101
}
@@ -89,7 +104,7 @@ func (c *fakeClient) List(ctx context.Context, opts *client.ListOptions, list ru
89104
return err
90105
}
91106
decoder := scheme.Codecs.UniversalDecoder()
92-
_, _, err = decoder.Decode(j, nil, list)
107+
_, _, err = decoder.Decode(j, nil, obj)
93108
return err
94109
}
95110

pkg/client/fake/client_test.go

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ limitations under the License.
1717
package fake
1818

1919
import (
20-
"encoding/json"
21-
2220
. "github.com/onsi/ginkgo"
2321
. "github.com/onsi/gomega"
2422

2523
appsv1 "k8s.io/api/apps/v1"
2624
corev1 "k8s.io/api/core/v1"
2725
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28-
"k8s.io/apimachinery/pkg/runtime"
2926
"k8s.io/apimachinery/pkg/types"
3027
"sigs.k8s.io/controller-runtime/pkg/client"
3128
)
@@ -69,22 +66,11 @@ var _ = Describe("Fake client", func() {
6966

7067
It("should be able to List", func() {
7168
By("Listing all deployments in a namespace")
72-
list := &metav1.List{}
73-
err := cl.List(nil, &client.ListOptions{
74-
Namespace: "ns1",
75-
Raw: &metav1.ListOptions{
76-
TypeMeta: metav1.TypeMeta{
77-
APIVersion: "apps/v1",
78-
Kind: "Deployment",
79-
},
80-
},
81-
}, list)
69+
list := &appsv1.DeploymentList{}
70+
err := cl.List(nil, list, client.InNamespace("ns1"))
8271
Expect(err).To(BeNil())
8372
Expect(list.Items).To(HaveLen(1))
84-
j, err := json.Marshal(dep)
85-
Expect(err).To(BeNil())
86-
expectedDep := runtime.RawExtension{Raw: j}
87-
Expect(list.Items).To(ConsistOf(expectedDep))
73+
Expect(list.Items).To(ConsistOf(*dep))
8874
})
8975

9076
It("should be able to Create", func() {
@@ -140,16 +126,8 @@ var _ = Describe("Fake client", func() {
140126
Expect(err).To(BeNil())
141127

142128
By("Listing all deployments in the namespace")
143-
list := &metav1.List{}
144-
err = cl.List(nil, &client.ListOptions{
145-
Namespace: "ns1",
146-
Raw: &metav1.ListOptions{
147-
TypeMeta: metav1.TypeMeta{
148-
APIVersion: "apps/v1",
149-
Kind: "Deployment",
150-
},
151-
},
152-
}, list)
129+
list := &appsv1.DeploymentList{}
130+
err = cl.List(nil, list, client.InNamespace("ns1"))
153131
Expect(err).To(BeNil())
154132
Expect(list.Items).To(HaveLen(0))
155133
})

0 commit comments

Comments
 (0)