Skip to content

Commit e6e67c1

Browse files
committed
Use .WithCluster and .WithNamespace
Signed-off-by: Nelo-T. Wallus <[email protected]>
1 parent dbac55a commit e6e67c1

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

cmd/cluster-lister-gen/generators/lister.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ func (g *listerGenerator) Imports(_ *generator.Context) (imports []string) {
221221
`kcplisters "github.com/kcp-dev/client-go/third_party/k8s.io/client-go/listers"`,
222222
`"k8s.io/apimachinery/pkg/labels"`,
223223
`"k8s.io/client-go/tools/cache"`,
224-
`"k8s.io/client-go/listers"`,
225224
)
226225
return
227226
}
@@ -264,8 +263,6 @@ func (g *listerGenerator) GenerateType(c *generator.Context, t *types.Type, w io
264263
if g.singleClusterListersPkg == "" {
265264
sw.Do(namespaceListerInterface, m)
266265
}
267-
268-
sw.Do(namespaceListerConstructor, m)
269266
}
270267

271268
sw.Do(scopedLister, m)
@@ -289,7 +286,6 @@ type $.type|public$ClusterLister interface {
289286
// $.type|private$ClusterLister implements the $.type|public$ClusterLister interface.
290287
type $.type|private$ClusterLister struct {
291288
kcplisters.ResourceClusterIndexer[*$.type|raw$]
292-
indexer cache.Indexer
293289
}
294290
295291
var _ $.type|public$ClusterLister = new($.type|private$ClusterLister)
@@ -302,19 +298,16 @@ var _ $.type|public$ClusterLister = new($.type|private$ClusterLister)
302298
$if .namespaced -$
303299
// - has the kcpcache.ClusterAndNamespaceIndex as an index
304300
$end -$
305-
func New$.type|public$ClusterLister(indexer cache.Indexer) *$.type|private$ClusterLister {
301+
func New$.type|public$ClusterLister(indexer cache.Indexer) $.type|public$ClusterLister {
306302
return &$.type|private$ClusterLister{
307303
kcplisters.NewCluster[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
308-
indexer,
309304
}
310305
}
311306
312307
// Cluster scopes the lister to one workspace, allowing users to list and get $.type|publicPlural$.
313308
func (l *$.type|private$ClusterLister) Cluster(clusterName logicalcluster.Name) $.listerInterface|raw$ {
314309
return &$.type|private$Lister{
315-
kcplisters.New[*$.type|raw$](l.indexer, clusterName, $.Resource|raw$("$.type|lowercaseSingular$")),
316-
l.indexer,
317-
clusterName,
310+
l.ResourceClusterIndexer.WithCluster(clusterName),
318311
}
319312
}
320313
`
@@ -347,8 +340,6 @@ var typeListerStruct = `
347340
// or scope down to a $.namespaceListerInterface|raw$ for one namespace.
348341
type $.type|private$Lister struct {
349342
kcplisters.ResourceIndexer[*$.type|raw$]
350-
indexer cache.Indexer
351-
clusterName logicalcluster.Name
352343
}
353344
354345
var _ $.listerInterface|raw$ = new($.type|private$Lister)
@@ -357,7 +348,9 @@ var _ $.listerInterface|raw$ = new($.type|private$Lister)
357348
var typeListerNamespaceLister = `
358349
// $.type|publicPlural$ returns an object that can list and get $.type|publicPlural$ in one namespace.
359350
func (l *$.type|private$Lister) $.type|publicPlural$(namespace string) $.namespaceListerInterface|raw$ {
360-
return new$.type|public$NamespaceLister(l.ResourceIndexer, namespace)
351+
return &$.type|private$NamespaceLister{
352+
l.ResourceIndexer.WithNamespace(namespace),
353+
}
361354
}
362355
`
363356

@@ -385,41 +378,33 @@ type $.type|private$NamespaceLister struct {
385378
var _ $.namespaceListerInterface|raw$ = new($.type|private$NamespaceLister)
386379
`
387380

388-
var namespaceListerConstructor = `
389-
// new$.type|public$NamespaceLister returns a new $.namespaceListerInterface|raw$.
390-
func new$.type|public$NamespaceLister(indexer kcplisters.ResourceIndexer[*$.type|raw$], namespace string) $.namespaceListerInterface|raw$ {
391-
return &$.type|private$NamespaceLister{
392-
kcplisters.NewNamespaced(indexer, namespace),
393-
}
394-
}
395-
`
396-
397381
var scopedLister = `
398-
// New$.type|public$Lister returns a new $.listerInterface|raw$.
382+
// New$.type|public$Lister returns a new $.type|public$Lister.
399383
// We assume that the indexer:
400-
// - is fed by a workspace-scoped LIST+WATCH
401-
// - uses cache.MetaNamespaceKeyFunc as the key function
384+
// - is fed by a cross-workspace LIST+WATCH
385+
// - uses kcpcache.MetaClusterNamespaceKeyFunc as the key function
386+
// - has the kcpcache.ClusterIndex as an index
402387
$if .namespaced -$
403-
// - has the cache.NamespaceIndex as an index
388+
// - has the kcpcache.ClusterAndNamespaceIndex as an index
404389
$end -$
405390
func New$.type|public$Lister(indexer cache.Indexer) $.listerInterface|raw$ {
406-
return &$.type|private$ScopedLister{
407-
listers.New[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
408-
indexer,
391+
return &$.type|private$Lister{
392+
kcplisters.New[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
409393
}
410394
}
411395
412396
// $.type|private$ScopedLister can list all $.type|publicPlural$ inside a workspace
413397
// or scope down to a $.namespaceListerInterface|raw$$if .namespaced$ for one namespace$end$.
414398
type $.type|private$ScopedLister struct {
415-
listers.ResourceIndexer[*$.type|raw$]
416-
indexer cache.Indexer
399+
kcplisters.ResourceIndexer[*$.type|raw$]
417400
}
418401
419402
$if .namespaced -$
420403
// $.type|publicPlural$ returns an object that can list and get $.type|publicPlural$ in one namespace.
421-
func (l *$.type|private$ScopedLister) $.type|publicPlural$(namespace string) $.namespaceListerInterface|raw$ {
422-
return listers.NewNamespaced(l.ResourceIndexer, namespace)
404+
func (l *$.type|private$ScopedLister) $.type|publicPlural$(namespace string) $.listerInterface|raw$ {
405+
return &$.type|private$Lister{
406+
l.ResourceIndexer.WithNamespace(namespace),
407+
}
423408
}
424409
$end -$
425410
`

0 commit comments

Comments
 (0)