Skip to content

Commit e635c8b

Browse files
authored
Merge pull request #109 from ntnn/kcp3350-clusterwithcontext-rebase
✨ Add ClusterWithContext
2 parents 7459bdf + d883fe5 commit e635c8b

File tree

39 files changed

+304
-5
lines changed

39 files changed

+304
-5
lines changed

cmd/cluster-informer-gen/generators/generic.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (g *genericGenerator) GenerateType(c *generator.Context, t *types.Type, w i
132132
"cacheSharedIndexInformer": c.Universe.Type(cacheSharedIndexInformer),
133133
"scopeableCacheSharedIndexInformer": c.Universe.Type(scopeableCacheSharedIndexInformer),
134134
"fmtErrorf": c.Universe.Type(fmtErrorfFunc),
135+
"contextContext": c.Universe.Type(contextContext),
135136
"groups": groups,
136137
"schemeGVs": schemeGVs,
137138
"schemaGroupResource": c.Universe.Type(schemaGroupResource),
@@ -155,6 +156,7 @@ func (g *genericGenerator) GenerateType(c *generator.Context, t *types.Type, w i
155156
var genericClusterInformer = `
156157
type GenericClusterInformer interface {
157158
Cluster({{.logicalclusterName|raw}}) {{.genericInformer|raw}}
159+
ClusterWithContext({{.contextContext|raw}}, {{.logicalclusterName|raw}}) {{.genericInformer|raw}}
158160
Informer() {{.scopeableCacheSharedIndexInformer|raw}}
159161
Lister() {{.kcpcacheGenericClusterLister|raw}}
160162
}
@@ -188,6 +190,15 @@ func (i *genericClusterInformer) Cluster(clusterName {{.logicalclusterName|raw}}
188190
lister: i.Lister().ByCluster(clusterName),
189191
}
190192
}
193+
194+
// ClusterWithContext scopes to a GenericInformer and unregisters all
195+
// handles registered through it once the provided context is canceled.
196+
func (i *genericClusterInformer) ClusterWithContext(ctx {{.contextContext|raw}}, clusterName {{.logicalclusterName|raw}}) {{.genericInformer|raw}} {
197+
return &genericInformer{
198+
informer: i.Informer().ClusterWithContext(ctx, clusterName),
199+
lister: i.Lister().ByCluster(clusterName),
200+
}
201+
}
191202
`
192203

193204
var genericInformer = `

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (g *informerGenerator) GenerateType(c *generator.Context, t *types.Type, w
107107
"clientSetInterface": clientSetInterface,
108108
"clientSetClusterInterface": clientSetClusterInterface,
109109
"contextBackground": c.Universe.Function(contextBackgroundFunc),
110+
"contextContext": c.Universe.Type(contextContext),
110111
"group": namer.IC(g.groupGoName),
111112
"interfacesTweakListOptionsFunc": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "TweakListOptionsFunc"}),
112113
"interfacesSharedInformerFactory": c.Universe.Type(types.Name{Package: g.internalInterfacesPackage, Name: "SharedInformerFactory"}),
@@ -151,6 +152,7 @@ var typeClusterInformerInterface = `
151152
// $.type|publicPlural$.
152153
type $.type|public$ClusterInformer interface {
153154
Cluster($.logicalclusterName|raw$) $.informerInterface|raw$
155+
ClusterWithContext($.contextContext|raw$, $.logicalclusterName|raw$) $.informerInterface|raw$
154156
Informer() $.scopeableCacheSharedIndexInformer|raw$
155157
Lister() $.clusterLister|raw$
156158
}
@@ -227,6 +229,13 @@ func (i *$.type|private$ClusterInformer) Cluster(clusterName $.logicalclusterNam
227229
lister: i.Lister().Cluster(clusterName),
228230
}
229231
}
232+
233+
func (i *$.type|private$ClusterInformer) ClusterWithContext(ctx $.contextContext|raw$, clusterName $.logicalclusterName|raw$) $.informerInterface|raw$ {
234+
return &$.type|private$Informer{
235+
informer: i.Informer().ClusterWithContext(ctx, clusterName),
236+
lister: i.Lister().Cluster(clusterName),
237+
}
238+
}
230239
`
231240

232241
var typeInformer = `

cmd/cluster-informer-gen/generators/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var (
3333
kcpcacheNewGenericClusterLister = types.Name{Package: "github.com/kcp-dev/apimachinery/v2/pkg/cache", Name: "NewGenericClusterLister"}
3434
kcpinformersNewSharedIndexInformer = types.Name{Package: "github.com/kcp-dev/apimachinery/v2/third_party/informers", Name: "NewSharedIndexInformer"}
3535
cacheTransformFunc = types.Name{Package: "k8s.io/client-go/tools/cache", Name: "TransformFunc"}
36+
contextContext = types.Name{Package: "context", Name: "Context"}
3637
contextBackgroundFunc = types.Name{Package: "context", Name: "Background"}
3738
fmtErrorfFunc = types.Name{Package: "fmt", Name: "Errorf"}
3839
reflectType = types.Name{Package: "reflect", Name: "Type"}

examples/go.mod

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

99
require (
10-
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba
10+
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077
1111
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98
1212
github.com/kcp-dev/logicalcluster/v3 v3.0.5
1313
k8s.io/apimachinery v0.32.3

examples/go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
3636
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
3737
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
3838
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
39-
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba h1:Ar/8wsCQWrZgRiBF2B5xCqCXEA/oiiuDI0yEsUtrxRs=
40-
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250425065633-635c2a0fbaba/go.mod h1:pYqnaSG3NlF/pVwfnYCAdGvrA7FpALFmd5S9X4XXf1Q=
39+
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077 h1:lDi9nZ75ypmRJwDFXUN70Cdu8+HxAjPU1kcnn+l4MvI=
40+
github.com/kcp-dev/apimachinery/v2 v2.0.1-0.20250512171935-ebb573a40077/go.mod h1:jnMZxVnCuKlkIXc4J1Qtmy1Lyo171CDF/RQhNAo0tvA=
4141
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98 h1:A1Hc2zVGd9LRSQqlGGqfzin+4skWJVcsNXw2+MjU6z4=
4242
github.com/kcp-dev/client-go v0.0.0-20250512170835-5457a0f4bd98/go.mod h1:79pmlxmvE/hohqD/qvhKaaoXmNDF/uhKnnAO6Vf5hZk=
4343
github.com/kcp-dev/logicalcluster/v3 v3.0.5 h1:JbYakokb+5Uinz09oTXomSUJVQsqfxEvU4RyHUYxHOU=
@@ -72,8 +72,8 @@ github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
7272
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
7373
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7474
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
75-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
76-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
75+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
76+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
7777
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
7878
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
7979
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

examples/pkg/kcp/clients/informers/externalversions/example/v1/clustertesttype.go

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

examples/pkg/kcp/clients/informers/externalversions/example/v1/testtype.go

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

examples/pkg/kcp/clients/informers/externalversions/example/v1alpha1/clustertesttype.go

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

examples/pkg/kcp/clients/informers/externalversions/example/v1alpha1/testtype.go

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

examples/pkg/kcp/clients/informers/externalversions/example/v1beta1/clustertesttype.go

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

0 commit comments

Comments
 (0)