Skip to content

Commit

Permalink
modify leaf_resource_manager to singleton
Browse files Browse the repository at this point in the history
Signed-off-by: wuyingjun <[email protected]>
  • Loading branch information
wuyingjun-lucky committed Nov 2, 2023
1 parent 344d998 commit 1eef7d2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/clustertree/cluster-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions pkg/clustertree/cluster-manager/utils/leaf_resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 1eef7d2

Please sign in to comment.