Skip to content

Commit 33e72a5

Browse files
authored
feat: memory optimization (hub cache transformation #2) (#246)
1 parent 2fddc1c commit 33e72a5

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

pkg/utils/informer/informermanager.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/client-go/dynamic/dynamicinformer"
2828
"k8s.io/client-go/tools/cache"
2929
"k8s.io/klog/v2"
30+
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
3031
)
3132

3233
// InformerManager manages dynamic shared informer for all resources, include Kubernetes resource and
@@ -130,7 +131,17 @@ func (s *informerManagerImpl) AddDynamicResources(dynResources []APIResourceMeta
130131
s.apiResources[newRes.GroupVersionKind] = &newRes
131132
// TODO (rzhang): remember the ResourceEventHandlerRegistration and remove it when the resource is deleted
132133
// TODO: handle error which only happens if the informer is stopped
133-
_, _ = s.informerFactory.ForResource(newRes.GroupVersionResource).Informer().AddEventHandler(handler)
134+
informer := s.informerFactory.ForResource(newRes.GroupVersionResource).Informer()
135+
// Strip away the ManagedFields info from objects to save memory.
136+
//
137+
// TO-DO (chenyu1): evaluate if there are other fields, e.g., owner refs, status, that can also be stripped
138+
// away to save memory.
139+
if err := informer.SetTransform(ctrlcache.TransformStripManagedFields()); err != nil {
140+
// The SetTransform func would only fail if the informer has already started. In this case,
141+
// no further action is needed.
142+
klog.ErrorS(err, "Failed to set transform func for informer", "gvr", newRes.GroupVersionResource)
143+
}
144+
_, _ = informer.AddEventHandler(handler)
134145
klog.InfoS("Added an informer for a new resource", "res", newRes)
135146
} else if !dynRes.isPresent {
136147
// we just mark it as enabled as we should not add another eventhandler to the informer as it's still

0 commit comments

Comments
 (0)