Skip to content

Commit 4094fb8

Browse files
authored
Merge pull request #112 from ntnn/update-client-go-3
✨ Use .WithCluster and .WithNamespace
2 parents 126a1bc + 93a66f2 commit 4094fb8

File tree

37 files changed

+453
-794
lines changed

37 files changed

+453
-794
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
`

examples/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ replace acme.corp/pkg => ./pkg
88

99
require (
1010
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077
11-
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98
11+
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49
1212
github.com/kcp-dev/logicalcluster/v3 v3.0.5
1313
k8s.io/apimachinery v0.32.3
1414
k8s.io/client-go v0.32.3

examples/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
3838
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
3939
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077 h1:lDi9nZ75ypmRJwDFXUN70Cdu8+HxAjPU1kcnn+l4MvI=
4040
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077/go.mod h1:jnMZxVnCuKlkIXc4J1Qtmy1Lyo171CDF/RQhNAo0tvA=
41-
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98 h1:A1Hc2zVGd9LRSQqlGGqfzin+4skWJVcsNXw2+MjU6z4=
42-
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98/go.mod h1:79pmlxmvE/hohqD/qvhKaaoXmNDF/uhKnnAO6Vf5hZk=
41+
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49 h1:eCqeB/ddeY5Qow7JUto3rzg1PlBnR/kOu0XlK2SMF/w=
42+
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49/go.mod h1:79pmlxmvE/hohqD/qvhKaaoXmNDF/uhKnnAO6Vf5hZk=
4343
github.com/kcp-dev/logicalcluster/v3 v3.0.5 h1:JbYakokb+5Uinz09oTXomSUJVQsqfxEvU4RyHUYxHOU=
4444
github.com/kcp-dev/logicalcluster/v3 v3.0.5/go.mod h1:EWBUBxdr49fUB1cLMO4nOdBWmYifLbP1LfoL20KkXYY=
4545
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=

examples/pkg/kcp/clients/listers/example/v1/clustertesttype.go

Lines changed: 8 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/pkg/kcp/clients/listers/example/v1/testtype.go

Lines changed: 16 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)