Skip to content

Commit c49e843

Browse files
committed
:fakeclient: Don't return items on invalid selector
Currently, if a List call to the fake client references an invalid fieldSelector, we will return an error and put all items into the passed List. Avoid putting anything into the passed list if the selector is invalid.
1 parent b88f351 commit c49e843

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

pkg/client/fake/client.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,21 +599,22 @@ func (c *fakeClient) List(ctx context.Context, obj client.ObjectList, opts ...cl
599599
return err
600600
}
601601
zero(obj)
602-
if err := json.Unmarshal(j, obj); err != nil {
602+
objCopy := obj.DeepCopyObject().(client.ObjectList)
603+
if err := json.Unmarshal(j, objCopy); err != nil {
604+
return err
605+
}
606+
607+
objs, err := meta.ExtractList(objCopy)
608+
if err != nil {
603609
return err
604610
}
605611

606612
if listOpts.LabelSelector == nil && listOpts.FieldSelector == nil {
607-
return nil
613+
return meta.SetList(obj, objs)
608614
}
609615

610616
// If we're here, either a label or field selector are specified (or both), so before we return
611617
// the list we must filter it. If both selectors are set, they are ANDed.
612-
objs, err := meta.ExtractList(obj)
613-
if err != nil {
614-
return err
615-
}
616-
617618
filteredList, err := c.filterList(objs, gvk, listOpts.LabelSelector, listOpts.FieldSelector)
618619
if err != nil {
619620
return err

pkg/client/fake/client_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,16 +1319,20 @@ var _ = Describe("Fake client", func() {
13191319
listOpts := &client.ListOptions{
13201320
FieldSelector: fields.OneTermEqualSelector("key", "val"),
13211321
}
1322-
err := cl.List(context.Background(), &corev1.ConfigMapList{}, listOpts)
1322+
list := &corev1.ConfigMapList{}
1323+
err := cl.List(context.Background(), list, listOpts)
13231324
Expect(err).To(HaveOccurred())
1325+
Expect(list.Items).To(BeEmpty())
13241326
})
13251327

13261328
It("errors when there's no Index matching the field name", func() {
13271329
listOpts := &client.ListOptions{
13281330
FieldSelector: fields.OneTermEqualSelector("spec.paused", "false"),
13291331
}
1330-
err := cl.List(context.Background(), &appsv1.DeploymentList{}, listOpts)
1332+
list := &appsv1.DeploymentList{}
1333+
err := cl.List(context.Background(), list, listOpts)
13311334
Expect(err).To(HaveOccurred())
1335+
Expect(list.Items).To(BeEmpty())
13321336
})
13331337

13341338
It("errors when field selector uses two requirements", func() {
@@ -1337,8 +1341,10 @@ var _ = Describe("Fake client", func() {
13371341
fields.OneTermEqualSelector("spec.replicas", "1"),
13381342
fields.OneTermEqualSelector("spec.strategy.type", string(appsv1.RecreateDeploymentStrategyType)),
13391343
)}
1340-
err := cl.List(context.Background(), &appsv1.DeploymentList{}, listOpts)
1344+
list := &appsv1.DeploymentList{}
1345+
err := cl.List(context.Background(), list, listOpts)
13411346
Expect(err).To(HaveOccurred())
1347+
Expect(list.Items).To(BeEmpty())
13421348
})
13431349

13441350
It("returns two deployments that match the only field selector requirement", func() {

0 commit comments

Comments
 (0)