Skip to content

Commit

Permalink
Merge pull request #625 from charith-elastic/avoid-extra-deepcopy
Browse files Browse the repository at this point in the history
✨ Avoid extra deep copy in CacheReader.List
  • Loading branch information
k8s-ci-robot committed Oct 7, 2019
2 parents 801e12a + 9655019 commit 5f0aee6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c // indirect
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5 // indirect
k8s.io/utils v0.0.0-20190506122338-8fab8cb257d5
sigs.k8s.io/testing_frameworks v0.1.1
sigs.k8s.io/yaml v1.1.0
)
18 changes: 12 additions & 6 deletions pkg/cache/internal/cache_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/tools/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/internal/objectutil"
)

// CacheReader is a CacheReader
Expand Down Expand Up @@ -125,15 +124,22 @@ func (c *CacheReader) List(_ context.Context, out runtime.Object, opts ...client
if !isObj {
return fmt.Errorf("cache contained %T, which is not an Object", obj)
}
meta, err := apimeta.Accessor(obj)
if err != nil {
return err
}
if labelSel != nil {
lbls := labels.Set(meta.GetLabels())
if !labelSel.Matches(lbls) {
continue
}
}

outObj := obj.DeepCopyObject()
outObj.GetObjectKind().SetGroupVersionKind(c.groupVersionKind)
runtimeObjs = append(runtimeObjs, outObj)
}
filteredItems, err := objectutil.FilterWithLabels(runtimeObjs, labelSel)
if err != nil {
return err
}
return apimeta.SetList(out, filteredItems)
return apimeta.SetList(out, runtimeObjs)
}

// objectKeyToStorageKey converts an object key to store key.
Expand Down

0 comments on commit 5f0aee6

Please sign in to comment.