Skip to content

Commit da648d0

Browse files
listergen: use helpers for listing by index
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
1 parent 0c7dc03 commit da648d0

3 files changed

Lines changed: 9 additions & 46 deletions

File tree

examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
replace acme.corp/pkg => ./pkg
66

77
require (
8-
github.com/kcp-dev/apimachinery v0.0.0-20221019133255-9e1e13940519
8+
github.com/kcp-dev/apimachinery v0.0.0-20221102195355-d65878bc16be
99
github.com/kcp-dev/client-go v0.0.0-20221013125607-cac9fbfe7455
1010
github.com/kcp-dev/logicalcluster/v2 v2.0.0-alpha.3
1111
k8s.io/apimachinery v0.25.2

examples/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
139139
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
140140
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
141141
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
142-
github.com/kcp-dev/apimachinery v0.0.0-20221019133255-9e1e13940519 h1:eU1HvmmP8TbzS2pB9IX2Spky20n6V79/KgX4ssiG/A8=
143-
github.com/kcp-dev/apimachinery v0.0.0-20221019133255-9e1e13940519/go.mod h1:qnvUHkdxOrNzX17yX+z8r81CZEBuFdveNzWqFlwZ55w=
142+
github.com/kcp-dev/apimachinery v0.0.0-20221102195355-d65878bc16be h1:2uDzJ896+ojtzgr9HJL8+tZEoqhq8blwymGinWFrQ6E=
143+
github.com/kcp-dev/apimachinery v0.0.0-20221102195355-d65878bc16be/go.mod h1:qnvUHkdxOrNzX17yX+z8r81CZEBuFdveNzWqFlwZ55w=
144144
github.com/kcp-dev/client-go v0.0.0-20221013125607-cac9fbfe7455 h1:4tYJ1Pkjwsv9x+TKRyFizyUTJL4SfbNahPckRyIMD4A=
145145
github.com/kcp-dev/client-go v0.0.0-20221013125607-cac9fbfe7455/go.mod h1:E/pWNs9gXdW4QbDJhDl6yVWv22AHU+EhSv54EagP96I=
146146
github.com/kcp-dev/logicalcluster/v2 v2.0.0-alpha.3 h1:+DwIG/loh2nDB9c/FqNvLzFFq/YtBliLxAfw/uWNzyE=

pkg/internal/listergen/lister.go

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@ import (
5757
"k8s.io/client-go/tools/cache"
5858
"k8s.io/apimachinery/pkg/labels"
5959
"k8s.io/apimachinery/pkg/api/errors"
60-
{{if .kind.IsNamespaced }}
61-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
62-
{{end -}}
6360
6461
{{.group.PackageAlias}} "{{.apiPackagePath}}/{{.group.Group.PackageName}}/{{.group.Version.PackageName}}"
6562
{{if .useUpstreamInterfaces -}}
@@ -126,24 +123,9 @@ type {{.kind.String | lowerFirst}}Lister struct {
126123
127124
// List lists all {{.kind.Plural}} in the indexer for a workspace.
128125
func (s *{{.kind.String | lowerFirst}}Lister) List(selector labels.Selector) (ret []*{{.group.PackageAlias}}.{{.kind.String}}, err error) {
129-
selectAll := selector == nil || selector.Empty()
130-
131-
list, err := s.indexer.ByIndex(kcpcache.ClusterIndexName, kcpcache.ClusterIndexKey(s.cluster))
132-
if err != nil {
133-
return nil, err
134-
}
135-
136-
for i := range list {
137-
obj := list[i].(*{{.group.PackageAlias}}.{{.kind.String}})
138-
if selectAll {
139-
ret = append(ret, obj)
140-
} else {
141-
if selector.Matches(labels.Set(obj.GetLabels())) {
142-
ret = append(ret, obj)
143-
}
144-
}
145-
}
146-
126+
err = kcpcache.ListAllByCluster(s.indexer, s.cluster, selector, func(i interface{}) {
127+
ret = append(ret, i.(*{{.group.PackageAlias}}.{{.kind.String}}))
128+
})
147129
return ret, err
148130
}
149131
@@ -192,28 +174,9 @@ type {{.kind.String | lowerFirst}}NamespaceLister struct {
192174
193175
// List lists all {{.kind.Plural}} in the indexer for a given workspace and namespace.
194176
func (s *{{.kind.String | lowerFirst}}NamespaceLister) List(selector labels.Selector) (ret []*{{.group.PackageAlias}}.{{.kind.String}}, err error) {
195-
selectAll := selector == nil || selector.Empty()
196-
197-
var list []interface{}
198-
if s.namespace == metav1.NamespaceAll {
199-
list, err = s.indexer.ByIndex(kcpcache.ClusterIndexName, kcpcache.ClusterIndexKey(s.cluster))
200-
} else {
201-
list, err = s.indexer.ByIndex(kcpcache.ClusterAndNamespaceIndexName, kcpcache.ClusterAndNamespaceIndexKey(s.cluster, s.namespace))
202-
}
203-
if err != nil {
204-
return nil, err
205-
}
206-
207-
for i := range list {
208-
obj := list[i].(*{{.group.PackageAlias}}.{{.kind.String}})
209-
if selectAll {
210-
ret = append(ret, obj)
211-
} else {
212-
if selector.Matches(labels.Set(obj.GetLabels())) {
213-
ret = append(ret, obj)
214-
}
215-
}
216-
}
177+
err = kcpcache.ListAllByClusterAndNamespace(s.indexer, s.cluster, s.namespace, selector, func(i interface{}) {
178+
ret = append(ret, i.(*{{.group.PackageAlias}}.{{.kind.String}}))
179+
})
217180
return ret, err
218181
}
219182

0 commit comments

Comments
 (0)