Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
britaniar committed Mar 20, 2024
1 parent 1956673 commit 0e9ca75
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 367 deletions.
18 changes: 4 additions & 14 deletions pkg/utils/informer/informermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func NewInformerManager(client dynamic.Interface, scheme *runtime.Scheme, defaul
cfg := config.GetConfigOrDie()
c, err := Cache.New(cfg, Cache.Options{Scheme: scheme, SyncPeriod: &defaultResync})
if err != nil {
//handle error
klog.V(2).ErrorS(err, "Failed to create informer cache")
}
return &informerManagerImpl{
Expand Down Expand Up @@ -127,18 +126,13 @@ func (s *informerManagerImpl) AddDynamicResources(dynResources []APIResourceMeta
addInformerFunc := func(newRes APIResourceMeta) {
_, exist := s.apiResources[newRes.GroupVersionKind]
if !exist {
c, err := Cache.New(config.GetConfigOrDie(), Cache.Options{Scheme: s.scheme})
if err != nil {
//handle error
klog.ErrorS(err, "Failed to create informer cache")
}
s.cache = c
// TODO: how to add GVK to scheme?
informer, err := s.cache.GetInformerForKind(s.ctx, newRes.GroupVersionKind)
if err != nil {
// Log the error instead of panicking
klog.ErrorS(err, "Failed to create informer for resource", "gvk", newRes.GroupVersionKind, "err", err)
return
}

// if AddEventHandler returns an error, it is because the informer has stopped and cannot be restarted
if newRes.Registration, err = informer.AddEventHandler(handler); err != nil {
if s.ctx.Err() != nil {
Expand Down Expand Up @@ -173,8 +167,8 @@ func (s *informerManagerImpl) AddDynamicResources(dynResources []APIResourceMeta
if !newGVKs[gvk] && !dynRes.isStaticResource {
informer, err := s.cache.GetInformerForKind(s.ctx, dynRes.GroupVersionKind)
if err != nil {
//handle error
panic(err)
klog.ErrorS(err, "Failed to get informer for resource", "gvk", dynRes.GroupVersionKind, "err", err)
return
}
if err := informer.RemoveEventHandler(dynRes.Registration); err != nil {
if s.ctx.Err() == nil {
Expand All @@ -185,7 +179,6 @@ func (s *informerManagerImpl) AddDynamicResources(dynResources []APIResourceMeta
}
obj, err := s.scheme.New(gvk)
if err != nil {
// handle error
klog.V(2).ErrorS(err, "Could not get object of a disappeared resource", "res", dynRes)
}
err = s.cache.RemoveInformer(s.ctx, obj.(client.Object))
Expand Down Expand Up @@ -215,12 +208,10 @@ func (s *informerManagerImpl) AddStaticResource(resource APIResourceMeta, handle
s.apiResources[resource.GroupVersionKind] = &resource
informer, err := s.cache.GetInformerForKind(s.ctx, resource.GroupVersionKind)
if err != nil {
//handle error
klog.V(2).ErrorS(err, "Failed to create informer for resource", "gvk", resource.GroupVersionKind)
}
resource.Registration, err = informer.AddEventHandler(handler)
if err != nil {
//handle error
klog.V(2).ErrorS(err, "Failed to add event handler for resource", "gvk", resource.GroupVersionKind)
}
}
Expand All @@ -229,7 +220,6 @@ func (s *informerManagerImpl) IsInformerSynced(resource schema.GroupVersionKind)
// TODO: use a lazy initialized sync map to reduce the number of informer sync look ups
informer, err := s.cache.GetInformerForKind(s.ctx, resource)
if err != nil {
//handle error
klog.V(2).ErrorS(err, "Failed to get informer for resource", "gvk", resource)
}
return informer.HasSynced()
Expand Down
277 changes: 0 additions & 277 deletions pkg/utils/informer/informermanager_test.go

This file was deleted.

Loading

0 comments on commit 0e9ca75

Please sign in to comment.