Skip to content

✨ Use .WithCluster and .WithNamespace #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: kcp-1.32.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 17 additions & 32 deletions cmd/cluster-lister-gen/generators/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ func (g *listerGenerator) Imports(_ *generator.Context) (imports []string) {
`kcplisters "github.com/kcp-dev/client-go/third_party/k8s.io/client-go/listers"`,
`"k8s.io/apimachinery/pkg/labels"`,
`"k8s.io/client-go/tools/cache"`,
`"k8s.io/client-go/listers"`,
)
return
}
Expand Down Expand Up @@ -264,8 +263,6 @@ func (g *listerGenerator) GenerateType(c *generator.Context, t *types.Type, w io
if g.singleClusterListersPkg == "" {
sw.Do(namespaceListerInterface, m)
}

sw.Do(namespaceListerConstructor, m)
}

sw.Do(scopedLister, m)
Expand All @@ -289,7 +286,6 @@ type $.type|public$ClusterLister interface {
// $.type|private$ClusterLister implements the $.type|public$ClusterLister interface.
type $.type|private$ClusterLister struct {
kcplisters.ResourceClusterIndexer[*$.type|raw$]
indexer cache.Indexer
}

var _ $.type|public$ClusterLister = new($.type|private$ClusterLister)
Expand All @@ -302,19 +298,16 @@ var _ $.type|public$ClusterLister = new($.type|private$ClusterLister)
$if .namespaced -$
// - has the kcpcache.ClusterAndNamespaceIndex as an index
$end -$
func New$.type|public$ClusterLister(indexer cache.Indexer) *$.type|private$ClusterLister {
func New$.type|public$ClusterLister(indexer cache.Indexer) $.type|public$ClusterLister {
return &$.type|private$ClusterLister{
kcplisters.NewCluster[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
indexer,
}
}

// Cluster scopes the lister to one workspace, allowing users to list and get $.type|publicPlural$.
func (l *$.type|private$ClusterLister) Cluster(clusterName logicalcluster.Name) $.listerInterface|raw$ {
return &$.type|private$Lister{
kcplisters.New[*$.type|raw$](l.indexer, clusterName, $.Resource|raw$("$.type|lowercaseSingular$")),
l.indexer,
clusterName,
l.ResourceClusterIndexer.WithCluster(clusterName),
}
}
`
Expand Down Expand Up @@ -347,8 +340,6 @@ var typeListerStruct = `
// or scope down to a $.namespaceListerInterface|raw$ for one namespace.
type $.type|private$Lister struct {
kcplisters.ResourceIndexer[*$.type|raw$]
indexer cache.Indexer
clusterName logicalcluster.Name
}

var _ $.listerInterface|raw$ = new($.type|private$Lister)
Expand All @@ -357,7 +348,9 @@ var _ $.listerInterface|raw$ = new($.type|private$Lister)
var typeListerNamespaceLister = `
// $.type|publicPlural$ returns an object that can list and get $.type|publicPlural$ in one namespace.
func (l *$.type|private$Lister) $.type|publicPlural$(namespace string) $.namespaceListerInterface|raw$ {
return new$.type|public$NamespaceLister(l.ResourceIndexer, namespace)
return &$.type|private$NamespaceLister{
l.ResourceIndexer.WithNamespace(namespace),
}
}
`

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

var namespaceListerConstructor = `
// new$.type|public$NamespaceLister returns a new $.namespaceListerInterface|raw$.
func new$.type|public$NamespaceLister(indexer kcplisters.ResourceIndexer[*$.type|raw$], namespace string) $.namespaceListerInterface|raw$ {
return &$.type|private$NamespaceLister{
kcplisters.NewNamespaced(indexer, namespace),
}
}
`

var scopedLister = `
// New$.type|public$Lister returns a new $.listerInterface|raw$.
// New$.type|public$Lister returns a new $.type|public$Lister.
// We assume that the indexer:
// - is fed by a workspace-scoped LIST+WATCH
// - uses cache.MetaNamespaceKeyFunc as the key function
// - is fed by a cross-workspace LIST+WATCH
// - uses kcpcache.MetaClusterNamespaceKeyFunc as the key function
// - has the kcpcache.ClusterIndex as an index
$if .namespaced -$
// - has the cache.NamespaceIndex as an index
// - has the kcpcache.ClusterAndNamespaceIndex as an index
$end -$
func New$.type|public$Lister(indexer cache.Indexer) $.listerInterface|raw$ {
return &$.type|private$ScopedLister{
listers.New[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
indexer,
return &$.type|private$Lister{
kcplisters.New[*$.type|raw$](indexer, $.Resource|raw$("$.type|lowercaseSingular$")),
}
}

// $.type|private$ScopedLister can list all $.type|publicPlural$ inside a workspace
// or scope down to a $.namespaceListerInterface|raw$$if .namespaced$ for one namespace$end$.
type $.type|private$ScopedLister struct {
listers.ResourceIndexer[*$.type|raw$]
indexer cache.Indexer
kcplisters.ResourceIndexer[*$.type|raw$]
}

$if .namespaced -$
// $.type|publicPlural$ returns an object that can list and get $.type|publicPlural$ in one namespace.
func (l *$.type|private$ScopedLister) $.type|publicPlural$(namespace string) $.namespaceListerInterface|raw$ {
return listers.NewNamespaced(l.ResourceIndexer, namespace)
func (l *$.type|private$ScopedLister) $.type|publicPlural$(namespace string) $.listerInterface|raw$ {
return &$.type|private$Lister{
l.ResourceIndexer.WithNamespace(namespace),
}
}
$end -$
`
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ replace acme.corp/pkg => ./pkg

require (
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49
github.com/kcp-dev/logicalcluster/v3 v3.0.5
k8s.io/apimachinery v0.32.3
k8s.io/client-go v0.32.3
Expand Down
4 changes: 2 additions & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077 h1:lDi9nZ75ypmRJwDFXUN70Cdu8+HxAjPU1kcnn+l4MvI=
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077/go.mod h1:jnMZxVnCuKlkIXc4J1Qtmy1Lyo171CDF/RQhNAo0tvA=
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98 h1:A1Hc2zVGd9LRSQqlGGqfzin+4skWJVcsNXw2+MjU6z4=
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98/go.mod h1:79pmlxmvE/hohqD/qvhKaaoXmNDF/uhKnnAO6Vf5hZk=
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49 h1:eCqeB/ddeY5Qow7JUto3rzg1PlBnR/kOu0XlK2SMF/w=
github.com/kcp-dev/client-go v0.0.0-20250706115143-de95389a5f49/go.mod h1:79pmlxmvE/hohqD/qvhKaaoXmNDF/uhKnnAO6Vf5hZk=
github.com/kcp-dev/logicalcluster/v3 v3.0.5 h1:JbYakokb+5Uinz09oTXomSUJVQsqfxEvU4RyHUYxHOU=
github.com/kcp-dev/logicalcluster/v3 v3.0.5/go.mod h1:EWBUBxdr49fUB1cLMO4nOdBWmYifLbP1LfoL20KkXYY=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
24 changes: 8 additions & 16 deletions examples/pkg/kcp/clients/listers/example/v1/clustertesttype.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 16 additions & 27 deletions examples/pkg/kcp/clients/listers/example/v1/testtype.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading