Skip to content

✨ Add ClusterWithContext #109

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

Merged
merged 3 commits into from
May 14, 2025
Merged
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
11 changes: 11 additions & 0 deletions cmd/cluster-informer-gen/generators/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (g *genericGenerator) GenerateType(c *generator.Context, t *types.Type, w i
"cacheSharedIndexInformer": c.Universe.Type(cacheSharedIndexInformer),
"scopeableCacheSharedIndexInformer": c.Universe.Type(scopeableCacheSharedIndexInformer),
"fmtErrorf": c.Universe.Type(fmtErrorfFunc),
"contextContext": c.Universe.Type(contextContext),
"groups": groups,
"schemeGVs": schemeGVs,
"schemaGroupResource": c.Universe.Type(schemaGroupResource),
Expand All @@ -155,6 +156,7 @@ func (g *genericGenerator) GenerateType(c *generator.Context, t *types.Type, w i
var genericClusterInformer = `
type GenericClusterInformer interface {
Cluster({{.logicalclusterName|raw}}) {{.genericInformer|raw}}
ClusterWithContext({{.contextContext|raw}}, {{.logicalclusterName|raw}}) {{.genericInformer|raw}}
Informer() {{.scopeableCacheSharedIndexInformer|raw}}
Lister() {{.kcpcacheGenericClusterLister|raw}}
}
Expand Down Expand Up @@ -188,6 +190,15 @@ func (i *genericClusterInformer) Cluster(clusterName {{.logicalclusterName|raw}}
lister: i.Lister().ByCluster(clusterName),
}
}

// ClusterWithContext scopes to a GenericInformer and unregisters all
// handles registered through it once the provided context is canceled.
func (i *genericClusterInformer) ClusterWithContext(ctx {{.contextContext|raw}}, clusterName {{.logicalclusterName|raw}}) {{.genericInformer|raw}} {
return &genericInformer{
informer: i.Informer().ClusterWithContext(ctx, clusterName),
lister: i.Lister().ByCluster(clusterName),
}
}
`

var genericInformer = `
Expand Down
9 changes: 9 additions & 0 deletions cmd/cluster-informer-gen/generators/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (g *informerGenerator) GenerateType(c *generator.Context, t *types.Type, w
"clientSetInterface": clientSetInterface,
"clientSetClusterInterface": clientSetClusterInterface,
"contextBackground": c.Universe.Function(contextBackgroundFunc),
"contextContext": c.Universe.Type(contextContext),
"group": namer.IC(g.groupGoName),
"interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}),
"interfacesSharedInformerFactory": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}),
Expand Down Expand Up @@ -151,6 +152,7 @@ var typeClusterInformerInterface = `
// $.type|publicPlural$.
type $.type|public$ClusterInformer interface {
Cluster($.logicalclusterName|raw$) $.informerInterface|raw$
ClusterWithContext($.contextContext|raw$, $.logicalclusterName|raw$) $.informerInterface|raw$
Informer() $.scopeableCacheSharedIndexInformer|raw$
Lister() $.clusterLister|raw$
}
Expand Down Expand Up @@ -227,6 +229,13 @@ func (i *$.type|private$ClusterInformer) Cluster(clusterName $.logicalclusterNam
lister: i.Lister().Cluster(clusterName),
}
}

func (i *$.type|private$ClusterInformer) ClusterWithContext(ctx $.contextContext|raw$, clusterName $.logicalclusterName|raw$) $.informerInterface|raw$ {
return &$.type|private$Informer{
informer: i.Informer().ClusterWithContext(ctx, clusterName),
lister: i.Lister().Cluster(clusterName),
}
}
`

var typeInformer = `
Expand Down
1 change: 1 addition & 0 deletions cmd/cluster-informer-gen/generators/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
kcpcacheNewGenericClusterLister = types.Name{Package: "github.com/kcp-dev/apimachinery/v2/pkg/cache", Name: "NewGenericClusterLister"}
kcpinformersNewSharedIndexInformer = types.Name{Package: "github.com/kcp-dev/apimachinery/v2/third_party/informers", Name: "NewSharedIndexInformer"}
cacheTransformFunc = types.Name{Package: "k8s.io/client-go/tools/cache", Name: "TransformFunc"}
contextContext = types.Name{Package: "context", Name: "Context"}
contextBackgroundFunc = types.Name{Package: "context", Name: "Background"}
fmtErrorfFunc = types.Name{Package: "fmt", Name: "Errorf"}
reflectType = types.Name{Package: "reflect", Name: "Type"}
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.23.7
replace acme.corp/pkg => ./pkg

require (
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba
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/logicalcluster/v3 v3.0.5
k8s.io/apimachinery v0.32.3
Expand Down
8 changes: 4 additions & 4 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba h1:Ar/8wsCQWrZgRiBF2B5xCqCXEA/oiiuDI0yEsUtrxRs=
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba/go.mod h1:pYqnaSG3NlF/pVwfnYCAdGvrA7FpALFmd5S9X4XXf1Q=
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/logicalcluster/v3 v3.0.5 h1:JbYakokb+5Uinz09oTXomSUJVQsqfxEvU4RyHUYxHOU=
Expand Down Expand Up @@ -72,8 +72,8 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down

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

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

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

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

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

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

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

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

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

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

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

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

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

Loading