Skip to content

Commit

Permalink
fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
halfcrazy committed Oct 9, 2023
1 parent 9727ea2 commit 2dae0b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
22 changes: 11 additions & 11 deletions pkg/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1975,10 +1975,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
Expect(err).NotTo(HaveOccurred())

pod := &corev1.Pod{}
By("indexing pods with namespace before starting")
fieldName1 := "indexByNamespace"
By("indexing pods with label before starting")
fieldName1 := "indexByLabel"
indexFunc1 := func(obj client.Object) []string {
return []string{obj.(*corev1.Pod).Namespace}
return []string{obj.(*corev1.Pod).Labels["common-label"]}
}
Expect(informer.IndexField(context.TODO(), pod, fieldName1, indexFunc1)).To(Succeed())
By("indexing pods with restart policy before starting")
Expand All @@ -1995,23 +1995,23 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
}()
Expect(informer.WaitForCacheSync(informerCacheCtx)).To(BeTrue())

By("listing pods with namespace index")
By("listing pods with label index")
listObj := &corev1.PodList{}
Expect(informer.List(context.Background(), listObj,
client.MatchingFields{fieldName1: testNamespaceTwo})).To(Succeed())
Expect(listObj.Items).To(HaveLen(3))
client.MatchingFields{fieldName1: "common"})).To(Succeed())
Expect(listObj.Items).To(HaveLen(2))

By("listing pods with restart policy index")
listObj = &corev1.PodList{}
Expect(informer.List(context.Background(), listObj,
client.MatchingFields{fieldName2: string(corev1.RestartPolicyAlways)})).To(Succeed())
Expect(listObj.Items).To(HaveLen(2))
client.MatchingFields{fieldName2: string(corev1.RestartPolicyNever)})).To(Succeed())
Expect(listObj.Items).To(HaveLen(3))

By("listing Namespaces with both fixed indexers 1 and 2")
By("listing pods with both fixed indexers 1 and 2")
listObj = &corev1.PodList{}
Expect(informer.List(context.Background(), listObj,
client.MatchingFields{fieldName1: testNamespaceTwo, fieldName2: string(corev1.RestartPolicyAlways)})).To(Succeed())
Expect(listObj.Items).To(HaveLen(2))
client.MatchingFields{fieldName1: "common", fieldName2: string(corev1.RestartPolicyNever)})).To(Succeed())
Expect(listObj.Items).To(HaveLen(1))
})
})
Context("with unstructured objects", func() {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cache/internal/cache_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,23 @@ func byIndexes(indexer cache.Indexer, requires fields.Requirements, namespace st
if !exist {
return nil, fmt.Errorf("index with name %s does not exist", indexName)
}
tObjs := make([]interface{}, 0, len(objs))
filteredObjects := make([]interface{}, 0, len(objs))
for _, obj := range objs {
vals, err = fn(obj)
if err != nil {
return nil, err
}
for _, val := range vals {
if val == indexedValue {
tObjs = append(tObjs, obj)
filteredObjects = append(filteredObjects, obj)
break
}
}
}
if len(tObjs) == 0 {
if len(filteredObjects) == 0 {
return nil, nil
}
objs = tObjs
objs = filteredObjects
}
return objs, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,15 +604,15 @@ func (c *fakeClient) filterWithFields(list []runtime.Object, gvk schema.GroupVer

filteredList := make([]runtime.Object, 0, len(list))
for _, obj := range list {
ok := true
matches := true
for _, req := range fs.Requirements() {
indexExtractor := indexes[req.Field]
if !c.objMatchesFieldSelector(obj, indexExtractor, req.Value) {
ok = false
matches = false
break
}
}
if ok {
if matches {
filteredList = append(filteredList, obj)
}
}
Expand Down

0 comments on commit 2dae0b0

Please sign in to comment.