-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛 Add cross-version compatibility with client-go 1.27 #2223
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,42 +305,63 @@ type multiNamespaceInformer struct { | |
namespaceToInformer map[string]Informer | ||
} | ||
|
||
type handlerRegistration struct { | ||
handles map[string]toolscache.ResourceEventHandlerRegistration | ||
} | ||
|
||
type syncer interface { | ||
HasSynced() bool | ||
} | ||
|
||
// HasSynced asserts that the handler has been called for the full initial state of the informer. | ||
// This uses syncer to be compatible between client-go 1.27+ and older versions when the interface changed. | ||
func (h handlerRegistration) HasSynced() bool { | ||
for _, reg := range h.handles { | ||
if s, ok := reg.(syncer); ok { | ||
if !s.HasSynced() { | ||
return false | ||
} | ||
} | ||
} | ||
return true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like this will give a problematic answer with the old client? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would expect an old client can never call this since they would see a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, gotcha |
||
} | ||
|
||
var _ Informer = &multiNamespaceInformer{} | ||
|
||
// AddEventHandler adds the handler to each namespaced informer. | ||
func (i *multiNamespaceInformer) AddEventHandler(handler toolscache.ResourceEventHandler) (toolscache.ResourceEventHandlerRegistration, error) { | ||
handles := make(map[string]toolscache.ResourceEventHandlerRegistration, len(i.namespaceToInformer)) | ||
handles := handlerRegistration{handles: make(map[string]toolscache.ResourceEventHandlerRegistration, len(i.namespaceToInformer))} | ||
for ns, informer := range i.namespaceToInformer { | ||
registration, err := informer.AddEventHandler(handler) | ||
if err != nil { | ||
return nil, err | ||
} | ||
handles[ns] = registration | ||
handles.handles[ns] = registration | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. won't the multiple informers collide and overwrite the prior one's handle? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait, there's a different informer per namespace? ok nvm, sorry. |
||
} | ||
return handles, nil | ||
} | ||
|
||
// AddEventHandlerWithResyncPeriod adds the handler with a resync period to each namespaced informer. | ||
func (i *multiNamespaceInformer) AddEventHandlerWithResyncPeriod(handler toolscache.ResourceEventHandler, resyncPeriod time.Duration) (toolscache.ResourceEventHandlerRegistration, error) { | ||
handles := make(map[string]toolscache.ResourceEventHandlerRegistration, len(i.namespaceToInformer)) | ||
handles := handlerRegistration{handles: make(map[string]toolscache.ResourceEventHandlerRegistration, len(i.namespaceToInformer))} | ||
for ns, informer := range i.namespaceToInformer { | ||
registration, err := informer.AddEventHandlerWithResyncPeriod(handler, resyncPeriod) | ||
if err != nil { | ||
return nil, err | ||
} | ||
handles[ns] = registration | ||
handles.handles[ns] = registration | ||
} | ||
return handles, nil | ||
} | ||
|
||
// RemoveEventHandler removes a formerly added event handler given by its registration handle. | ||
func (i *multiNamespaceInformer) RemoveEventHandler(h toolscache.ResourceEventHandlerRegistration) error { | ||
handles, ok := h.(map[string]toolscache.ResourceEventHandlerRegistration) | ||
handles, ok := h.(handlerRegistration) | ||
if !ok { | ||
return fmt.Errorf("it is not the registration returned by multiNamespaceInformer") | ||
} | ||
for ns, informer := range i.namespaceToInformer { | ||
registration, ok := handles[ns] | ||
registration, ok := handles.handles[ns] | ||
if !ok { | ||
continue | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,49 @@ type FakeInformer struct { | |
// RunCount is incremented each time RunInformersAndControllers is called | ||
RunCount int | ||
|
||
handlers []cache.ResourceEventHandler | ||
handlers []eventHandlerWrapper | ||
} | ||
|
||
type modernResourceEventHandler interface { | ||
OnAdd(obj interface{}, isInInitialList bool) | ||
OnUpdate(oldObj, newObj interface{}) | ||
OnDelete(obj interface{}) | ||
} | ||
|
||
type legacyResourceEventHandler interface { | ||
OnAdd(obj interface{}) | ||
OnUpdate(oldObj, newObj interface{}) | ||
OnDelete(obj interface{}) | ||
} | ||
|
||
// eventHandlerWrapper wraps a ResourceEventHandler in a manner that is compatible with client-go 1.27+ and older. | ||
// The interface was changed in these versions. | ||
type eventHandlerWrapper struct { | ||
handler any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use generics to type this to one of the two interfaces? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think so, or at least do not know how to. See golang/go#49054 |
||
} | ||
|
||
func (e eventHandlerWrapper) OnAdd(obj interface{}) { | ||
if m, ok := e.handler.(modernResourceEventHandler); ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clever, lol |
||
m.OnAdd(obj, false) | ||
return | ||
} | ||
e.handler.(legacyResourceEventHandler).OnAdd(obj) | ||
} | ||
|
||
func (e eventHandlerWrapper) OnUpdate(oldObj, newObj interface{}) { | ||
if m, ok := e.handler.(modernResourceEventHandler); ok { | ||
m.OnUpdate(oldObj, newObj) | ||
return | ||
} | ||
e.handler.(legacyResourceEventHandler).OnUpdate(oldObj, newObj) | ||
} | ||
|
||
func (e eventHandlerWrapper) OnDelete(obj interface{}) { | ||
if m, ok := e.handler.(modernResourceEventHandler); ok { | ||
m.OnDelete(obj) | ||
return | ||
} | ||
e.handler.(legacyResourceEventHandler).OnDelete(obj) | ||
} | ||
|
||
// AddIndexers does nothing. TODO(community): Implement this. | ||
|
@@ -58,7 +100,7 @@ func (f *FakeInformer) HasSynced() bool { | |
|
||
// AddEventHandler implements the Informer interface. Adds an EventHandler to the fake Informers. TODO(community): Implement Registration. | ||
func (f *FakeInformer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) { | ||
f.handlers = append(f.handlers, handler) | ||
f.handlers = append(f.handlers, eventHandlerWrapper{handler}) | ||
return nil, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I am blind, where do we call this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't call this, if allows the interface to be implemented on 0.27
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since they added this function