Skip to content

Commit 5f0aee6

Browse files
authored
Merge pull request #625 from charith-elastic/avoid-extra-deepcopy
✨ Avoid extra deep copy in CacheReader.List
2 parents 801e12a + 9655019 commit 5f0aee6

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
4545
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
4646
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c // indirect
47-
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 // indirect
47+
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5
4848
sigs.k8s.io/testing_frameworks v0.1.1
4949
sigs.k8s.io/yaml v1.1.0
5050
)

pkg/cache/internal/cache_reader.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"k8s.io/apimachinery/pkg/selection"
3131
"k8s.io/client-go/tools/cache"
3232
"sigs.k8s.io/controller-runtime/pkg/client"
33-
"sigs.k8s.io/controller-runtime/pkg/internal/objectutil"
3433
)
3534

3635
// CacheReader is a CacheReader
@@ -125,15 +124,22 @@ func (c *CacheReader) List(_ context.Context, out runtime.Object, opts ...client
125124
if !isObj {
126125
return fmt.Errorf("cache contained %T, which is not an Object", obj)
127126
}
127+
meta, err := apimeta.Accessor(obj)
128+
if err != nil {
129+
return err
130+
}
131+
if labelSel != nil {
132+
lbls := labels.Set(meta.GetLabels())
133+
if !labelSel.Matches(lbls) {
134+
continue
135+
}
136+
}
137+
128138
outObj := obj.DeepCopyObject()
129139
outObj.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
130140
runtimeObjs = append(runtimeObjs, outObj)
131141
}
132-
filteredItems, err := objectutil.FilterWithLabels(runtimeObjs, labelSel)
133-
if err != nil {
134-
return err
135-
}
136-
return apimeta.SetList(out, filteredItems)
142+
return apimeta.SetList(out, runtimeObjs)
137143
}
138144

139145
// objectKeyToStorageKey converts an object key to store key.

0 commit comments

Comments
 (0)