From 1eef7d2eb60816ebcb3c818e2a1f4f332e8fed5d Mon Sep 17 00:00:00 2001 From: wuyingjun Date: Thu, 2 Nov 2023 18:18:44 +0800 Subject: [PATCH] modify leaf_resource_manager to singleton Signed-off-by: wuyingjun --- cmd/clustertree/cluster-manager/app/manager.go | 2 +- .../utils/leaf_resource_manager.go | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/clustertree/cluster-manager/app/manager.go b/cmd/clustertree/cluster-manager/app/manager.go index 69fbd0072..a3ca253d2 100644 --- a/cmd/clustertree/cluster-manager/app/manager.go +++ b/cmd/clustertree/cluster-manager/app/manager.go @@ -58,7 +58,7 @@ func NewAgentCommand(ctx context.Context) *cobra.Command { } func run(ctx context.Context, opts *options.Options) error { - globalleafManager := leafUtils.NewLeafResourceManager() + globalleafManager := leafUtils.GetGlobalLeafResourceManager() config, err := clientcmd.BuildConfigFromFlags(opts.KubernetesOptions.Master, opts.KubernetesOptions.KubeConfig) if err != nil { diff --git a/pkg/clustertree/cluster-manager/utils/leaf_resource_manager.go b/pkg/clustertree/cluster-manager/utils/leaf_resource_manager.go index 8f929d2c4..8ccd3b7c5 100644 --- a/pkg/clustertree/cluster-manager/utils/leaf_resource_manager.go +++ b/pkg/clustertree/cluster-manager/utils/leaf_resource_manager.go @@ -9,6 +9,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +var ( + instance LeafResourceManager + once sync.Once +) + type LeafResource struct { Client client.Client DynamicClient dynamic.Interface @@ -74,8 +79,12 @@ func (l *leafResourceManager) ListNodeNames() []string { return keys } -func NewLeafResourceManager() LeafResourceManager { - return &leafResourceManager{ - resourceMap: make(map[string]*LeafResource), - } +func GetGlobalLeafResourceManager() LeafResourceManager { + once.Do(func() { + instance = &leafResourceManager{ + resourceMap: make(map[string]*LeafResource), + } + }) + + return instance }