Skip to content

Commit

Permalink
Reduce sharedInformer memory usage
Browse files Browse the repository at this point in the history
Trim ManagedFields to reduce shared informer memory usage.
This requires informers generated with  newer version of k8s/client-go [0.29]
so currently it is done for following informers:
 - k8s
 - submarinerconfig

Rest informers should be updated once WithTransform() is available.

Refer: kubernetes/kubernetes#118455

Signed-off-by: Vishal Thapar <5137689+vthapar@users.noreply.github.com>
  • Loading branch information
vthapar committed Feb 15, 2024
1 parent c87aca4 commit fb30e71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 17 additions & 3 deletions pkg/hub/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
apiextensionsinformers "k8s.io/apiextensions-apiserver/pkg/client/informers/externalversions"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/dynamic"
Expand Down Expand Up @@ -133,11 +134,24 @@ func (o *AddOnOptions) RunControllerManager(ctx context.Context, controllerConte
return err
}

// Informer transform to trim ManagedFields for memory efficiency.
// TODO: Apply trim to all informers when they support WithTransform.
trim := func(obj interface{}) (interface{}, error) {
if accessor, err := meta.Accessor(obj); err == nil {
accessor.SetManagedFields(nil)
}

return obj, nil
}

clusterInformers := clusterinformers.NewSharedInformerFactory(clusterClient, 10*time.Minute)
workInformers := workinformers.NewSharedInformerFactory(workClient, 10*time.Minute)
kubeInformers := kubeinformers.NewSharedInformerFactory(kubeClient, 10*time.Minute)
configInformers := configinformers.NewSharedInformerFactory(configClient, 10*time.Minute)
apiExtensionsInformers := apiextensionsinformers.NewSharedInformerFactory(apiExtensionClient, 10*time.Minute)
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(
kubeClient, 10*time.Minute, kubeinformers.WithTransform(trim))
configInformers := configinformers.NewSharedInformerFactoryWithOptions(
configClient, 10*time.Minute, configinformers.WithTransform(trim))
apiExtensionsInformers := apiextensionsinformers.NewSharedInformerFactoryWithOptions(
apiExtensionClient, 10*time.Minute, apiextensionsinformers.WithTransform(trim))
addOnInformers := addoninformers.NewSharedInformerFactoryWithOptions(addOnClient, 10*time.Minute)

submarinerBrokerCRDsController := submarinerbroker.NewCRDsController(
Expand Down
11 changes: 10 additions & 1 deletion pkg/spoke/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,22 @@ func (o *AgentOptions) RunAgent(ctx context.Context, controllerContext *controll
return err
}

// Informer transform to trim ManagedFields for memory efficiency.
trim := func(obj interface{}) (interface{}, error) {
if accessor, err := meta.Accessor(obj); err == nil {
accessor.SetManagedFields(nil)
}

return obj, nil
}

addOnInformers := addoninformers.NewSharedInformerFactoryWithOptions(addOnHubKubeClient, 10*time.Minute,
addoninformers.WithNamespace(o.ClusterName))
configInformers := configinformers.NewSharedInformerFactoryWithOptions(configHubKubeClient, 10*time.Minute,
configinformers.WithNamespace(o.ClusterName))

spokeKubeInformers := informers.NewSharedInformerFactoryWithOptions(spokeKubeClient, 10*time.Minute,
informers.WithNamespace(o.InstallationNamespace))
informers.WithNamespace(o.InstallationNamespace), informers.WithTransform(trim))
// TODO if submariner provides the informer in future, we will use it instead of dynamic informer
dynamicInformers := dynamicinformer.NewFilteredDynamicSharedInformerFactory(spokeDynamicClient, 10*time.Minute, o.InstallationNamespace,
nil)
Expand Down

0 comments on commit fb30e71

Please sign in to comment.