Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c3c11a5

Browse files
committedApr 28, 2025·
Add ClusterWithContext
Signed-off-by: Nelo-T. Wallus <n.wallus@sap.com>
1 parent 54aa151 commit c3c11a5

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed
 

‎pkg/internal/clientgen/clientset.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import (
5858
"github.com/kcp-dev/logicalcluster/v3"
5959
6060
client "{{.singleClusterClientPackagePath}}"
61-
61+
6262
"k8s.io/client-go/discovery"
6363
"k8s.io/client-go/rest"
6464
"k8s.io/client-go/util/flowcontrol"
@@ -91,7 +91,7 @@ func (c *ClusterClientset) Discovery() discovery.DiscoveryInterface {
9191
}
9292
9393
{{range .groups}}
94-
// {{.GroupGoName}}{{.Version}} retrieves the {{.GroupGoName}}{{.Version}}ClusterClient.
94+
// {{.GroupGoName}}{{.Version}} retrieves the {{.GroupGoName}}{{.Version}}ClusterClient.
9595
func (c *ClusterClientset) {{.GroupGoName}}{{.Version}}() {{.GoPackageAlias}}.{{.GroupGoName}}{{.Version}}ClusterInterface {
9696
return c.{{.GroupGoNameLower}}{{.Version}}
9797
}
@@ -106,7 +106,7 @@ func (c *ClusterClientset) Cluster(clusterPath logicalcluster.Path) client.Inter
106106
}
107107
108108
// NewForConfig creates a new ClusterClientset for the given config.
109-
// If config's RateLimiter is not set and QPS and Burst are acceptable,
109+
// If config's RateLimiter is not set and QPS and Burst are acceptable,
110110
// NewForConfig will generate a rate-limiter in configShallowCopy.
111111
// NewForConfig is equivalent to NewForConfigAndClient(c, httpClient),
112112
// where httpClient was generated with rest.HTTPClientFor(c).

‎pkg/internal/informergen/factory.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ import (
8282
"k8s.io/client-go/tools/cache"
8383
8484
clientset "{{.clientsetPackagePath}}"
85-
{{if not .useUpstreamInterfaces -}}
85+
{{if not .useUpstreamInterfaces -}}
8686
scopedclientset "{{.singleClusterClientPackagePath}}"
8787
{{end -}}
8888
{{if .useUpstreamInterfaces -}}
@@ -265,7 +265,7 @@ func (f *sharedInformerFactory) InformerFor(obj runtime.Object, newFunc internal
265265
type ScopedDynamicSharedInformerFactory interface {
266266
// ForResource gives generic access to a shared informer of the matching type.
267267
ForResource(resource schema.GroupVersionResource) ({{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer, error)
268-
268+
269269
// Start initializes all requested informers. They are handled in goroutines
270270
// which run until the stop channel gets closed.
271271
Start(stopCh <-chan struct{})
@@ -353,7 +353,7 @@ func (f *scopedDynamicSharedInformerFactory) ForResource(resource schema.GroupVe
353353
if err != nil {
354354
return nil, err
355355
}
356-
return clusterInformer.Cluster(f.clusterName), nil
356+
return clusterInformer.Cluster(f.clusterName), nil
357357
}
358358
359359
func (f *scopedDynamicSharedInformerFactory) Start(stopCh <-chan struct{}) {

‎pkg/internal/informergen/generic.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ var genericInformer = `
6363
package informers
6464
6565
import (
66+
"context"
6667
"fmt"
6768
6869
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -81,6 +82,7 @@ import (
8182
8283
type GenericClusterInformer interface {
8384
Cluster(logicalcluster.Name) {{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer
85+
ClusterWithContext(context.Context, logicalcluster.Name) {{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer
8486
Informer() kcpcache.ScopeableSharedIndexInformer
8587
Lister() kcpcache.GenericClusterLister
8688
}
@@ -115,6 +117,15 @@ func (f *genericClusterInformer) Cluster(clusterName logicalcluster.Name) {{if .
115117
}
116118
}
117119
120+
// ClusterWithContext scopes to a GenericInformer and unregisters all
121+
// handles registered through it once the provided context is canceled.
122+
func (f *genericClusterInformer) ClusterWithContext(ctx context.Context, clusterName logicalcluster.Name) {{if .useUpstreamInterfaces}}upstreaminformers.{{end}}GenericInformer {
123+
return &genericInformer{
124+
informer: f.Informer().ClusterWithContext(ctx, clusterName),
125+
lister: f.Lister().ByCluster(clusterName),
126+
}
127+
}
128+
118129
type genericInformer struct {
119130
informer cache.SharedIndexInformer
120131
lister cache.GenericLister

‎pkg/internal/informergen/informer.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import (
108108
{{end -}}
109109
110110
clientset "{{.clientsetPackagePath}}"
111-
{{if not .useUpstreamInterfaces -}}
111+
{{if not .useUpstreamInterfaces -}}
112112
scopedclientset "{{.singleClusterClientPackagePath}}"
113113
{{end -}}
114114
@@ -119,6 +119,7 @@ import (
119119
// {{.kind.Plural}}.
120120
type {{.kind}}ClusterInformer interface {
121121
Cluster(logicalcluster.Name) {{if .useUpstreamInterfaces}}upstream{{.group.GoPackageAlias}}informers.{{end}}{{.kind}}Informer
122+
ClusterWithContext(context.Context, logicalcluster.Name) {{if .useUpstreamInterfaces}}upstream{{.group.GoPackageAlias}}informers.{{end}}{{.kind}}Informer
122123
Informer() kcpcache.ScopeableSharedIndexInformer
123124
Lister() {{.group.GoPackageAlias}}listers.{{.kind}}ClusterLister
124125
}
@@ -164,7 +165,7 @@ func (f *{{.kind.String|lowerFirst}}ClusterInformer) defaultInformer(client clie
164165
return NewFiltered{{.kind}}ClusterInformer(client, resyncPeriod, cache.Indexers{
165166
kcpcache.ClusterIndexName: kcpcache.ClusterIndexFunc,
166167
{{if .kind.IsNamespaced}}kcpcache.ClusterAndNamespaceIndexName: kcpcache.ClusterAndNamespaceIndexFunc,{{end -}}
167-
},
168+
},
168169
f.tweakListOptions,
169170
)
170171
}
@@ -193,6 +194,13 @@ func (f *{{.kind.String|lowerFirst}}ClusterInformer) Cluster(clusterName logical
193194
}
194195
}
195196
197+
func (f *{{.kind.String|lowerFirst}}ClusterInformer) ClusterWithContext(ctx context.Context, clusterName logicalcluster.Name) {{if .useUpstreamInterfaces}}upstream{{.group.GoPackageAlias}}informers.{{end}}{{.kind}}Informer {
198+
return &{{.kind.String|lowerFirst}}Informer{
199+
informer: f.Informer().ClusterWithContext(ctx, clusterName),
200+
lister: f.Lister().Cluster(clusterName),
201+
}
202+
}
203+
196204
type {{.kind.String|lowerFirst}}Informer struct {
197205
informer cache.SharedIndexInformer
198206
lister {{if .useUpstreamInterfaces}}upstream{{end}}{{.group.GoPackageAlias}}listers.{{.kind.String}}Lister

‎pkg/internal/listergen/lister.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ var lister = `
4646
package {{.group.Version.PackageName}}
4747
4848
import (
49-
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
49+
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
5050
"github.com/kcp-dev/logicalcluster/v3"
51-
51+
5252
"k8s.io/client-go/tools/cache"
5353
"k8s.io/apimachinery/pkg/labels"
5454
"k8s.io/apimachinery/pkg/api/errors"

0 commit comments

Comments
 (0)
Please sign in to comment.