Skip to content

Commit 71e2069

Browse files
authored
Merge pull request #13 from ecordell/fix-registry
Registry factory add/remove should take FactoryKey not RegistryKey
2 parents 37daaef + c98c0ab commit 71e2069

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

typed/registry.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func NewRegistry() *Registry {
7373
}
7474

7575
// MustNewFilteredDynamicSharedInformerFactory creates a new SharedInformerFactory
76-
// and registers it under the given RegistryKey. It panics if there is already
76+
// and registers it under the given FactoryKey. It panics if there is already
7777
// an entry with that key.
7878
func (r *Registry) MustNewFilteredDynamicSharedInformerFactory(key FactoryKey, client dynamic.Interface, defaultResync time.Duration, namespace string, tweakListOptions dynamicinformer.TweakListOptionsFunc) dynamicinformer.DynamicSharedInformerFactory {
7979
factory, err := r.NewFilteredDynamicSharedInformerFactory(key, client, defaultResync, namespace, tweakListOptions)
@@ -84,15 +84,13 @@ func (r *Registry) MustNewFilteredDynamicSharedInformerFactory(key FactoryKey, c
8484
}
8585

8686
// NewFilteredDynamicSharedInformerFactory creates a new SharedInformerFactory
87-
// and registers it under the given RegistryKey
87+
// and registers it under the given FactoryKey
8888
func (r *Registry) NewFilteredDynamicSharedInformerFactory(key FactoryKey, client dynamic.Interface, defaultResync time.Duration, namespace string, tweakListOptions dynamicinformer.TweakListOptionsFunc) (dynamicinformer.DynamicSharedInformerFactory, error) {
89-
r.Lock()
90-
defer r.Unlock()
91-
if _, ok := r.factories[key]; ok {
92-
return nil, fmt.Errorf("cannot register two InformerFactories with the same key: %s", key)
89+
factory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, defaultResync, namespace, tweakListOptions)
90+
if err := r.Add(key, factory); err != nil {
91+
return nil, err
9392
}
94-
r.factories[key] = dynamicinformer.NewFilteredDynamicSharedInformerFactory(client, defaultResync, namespace, tweakListOptions)
95-
return r.factories[key], nil
93+
return factory, nil
9694
}
9795

9896
// ListerFor returns a typed Lister from a Registry
@@ -105,17 +103,21 @@ func IndexerFor[K runtime.Object](r *Registry, key RegistryKey) *Indexer[K] {
105103
return NewIndexer[K](r.InformerFor(key).GetIndexer())
106104
}
107105

108-
// Add adds a factory to the registry under the given RegistryKey
109-
func (r *Registry) Add(key RegistryKey, factory dynamicinformer.DynamicSharedInformerFactory) {
106+
// Add adds a factory to the registry under the given FactoryKey
107+
func (r *Registry) Add(key FactoryKey, factory dynamicinformer.DynamicSharedInformerFactory) error {
110108
r.Lock()
111109
defer r.Unlock()
110+
if _, ok := r.factories[key]; ok {
111+
return fmt.Errorf("cannot register two InformerFactories with the same key: %s", key)
112+
}
112113
r.factories[key] = factory
114+
return nil
113115
}
114116

115117
// Remove removes a factory from the registry. Note that it does not stop any
116118
// informers that were started via the factory; they should be stopped via
117119
// context cancellation.
118-
func (r *Registry) Remove(key RegistryKey, factory dynamicinformer.DynamicSharedInformerFactory) {
120+
func (r *Registry) Remove(key FactoryKey) {
119121
r.Lock()
120122
defer r.Unlock()
121123
delete(r.factories, key)

typed/registry_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package typed
33
import (
44
"context"
55
"fmt"
6+
"testing"
67

78
corev1 "k8s.io/api/core/v1"
89
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -146,3 +147,32 @@ func ExampleIndexerFor() {
146147
fmt.Printf("%T %s/%s", matchingCachedSecrets, matchingCachedSecrets[0].GetNamespace(), matchingCachedSecrets[0].GetName())
147148
// Output: []*v1.Secret example/mysecret
148149
}
150+
151+
func TestRemove(t *testing.T) {
152+
ctx, cancel := context.WithCancel(context.Background())
153+
defer cancel()
154+
155+
secretGVR := corev1.SchemeGroupVersion.WithResource("secrets")
156+
scheme := runtime.NewScheme()
157+
if err := corev1.AddToScheme(scheme); err != nil {
158+
panic(err)
159+
}
160+
client := fake.NewSimpleDynamicClient(scheme)
161+
registry := NewRegistry()
162+
163+
dependentObjectKey := NewFactoryKey("my-controller", "localCluster", "dependentObjects")
164+
informerFactory := registry.MustNewFilteredDynamicSharedInformerFactory(
165+
dependentObjectKey,
166+
client,
167+
0,
168+
metav1.NamespaceAll,
169+
func(options *metav1.ListOptions) {
170+
options.LabelSelector = "my-controller.com/related-to=myobjecttype"
171+
},
172+
)
173+
informerFactory.ForResource(secretGVR)
174+
informerFactory.Start(ctx.Done())
175+
informerFactory.WaitForCacheSync(ctx.Done())
176+
177+
registry.Remove(dependentObjectKey)
178+
}

0 commit comments

Comments
 (0)