From ce3871966ecfab711d771292d30f34a727ffd118 Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Tue, 4 May 2021 14:40:20 +0800 Subject: [PATCH] services/introspection: support to show introspection grpc service Signed-off-by: Iceber Gu --- plugin/context.go | 12 +++++++++++- plugin/plugin.go | 19 +++++++++++++++++++ services/introspection/local.go | 26 +++++++++++++++----------- services/introspection/service.go | 9 ++------- 4 files changed, 47 insertions(+), 19 deletions(-) diff --git a/plugin/context.go b/plugin/context.go index 263fa9b8f..dcb533c8a 100644 --- a/plugin/context.go +++ b/plugin/context.go @@ -132,9 +132,19 @@ func (ps *Set) Get(t Type) (interface{}, error) { return nil, fmt.Errorf("no plugins registered for %s: %w", t, errdefs.ErrNotFound) } +// GetAll returns all initialized plugins +func (ps *Set) GetAll() []*Plugin { + return ps.ordered +} + +// Plugins returns plugin set +func (i *InitContext) Plugins() *Set { + return i.plugins +} + // GetAll plugins in the set func (i *InitContext) GetAll() []*Plugin { - return i.plugins.ordered + return i.plugins.GetAll() } // GetByID returns the plugin of the given type and ID diff --git a/plugin/plugin.go b/plugin/plugin.go index eb38c1271..02c3d197d 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -177,6 +177,25 @@ func checkUnique(r *Registration) error { return nil } +// AreRegisteredPluginsInitialized returns all registered plugins are initialized +func AreRegisteredPluginsInitialized(plugins *Set) bool { + if len(register.r) != len(plugins.ordered) { + return false + } + for _, reg := range register.r { + byID, typeok := plugins.byTypeAndID[reg.Type] + if !typeok { + return false + } + + if _, ok := byID[reg.ID]; !ok { + return false + } + } + + return true +} + // DisableFilter filters out disabled plugins type DisableFilter func(r *Registration) bool diff --git a/services/introspection/local.go b/services/introspection/local.go index a409e1cac..110327f76 100644 --- a/services/introspection/local.go +++ b/services/introspection/local.go @@ -41,12 +41,9 @@ func init() { ID: services.IntrospectionService, Requires: []plugin.Type{}, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - // this service works by using the plugin context up till the point - // this service is initialized. Since we require this service last, - // it should provide the full set of plugins. - pluginsPB := pluginsToPB(ic.GetAll()) + // this service fetches all plugins through the plugin set of the plugin context return &Local{ - plugins: pluginsPB, + plugins: ic.Plugins(), root: ic.Root, }, nil }, @@ -55,19 +52,19 @@ func init() { // Local is a local implementation of the introspection service type Local struct { - mu sync.Mutex - plugins []api.Plugin - root string + mu sync.Mutex + root string + plugins *plugin.Set + pluginsPB []api.Plugin } var _ = (api.IntrospectionClient)(&Local{}) // UpdateLocal updates the local introspection service -func (l *Local) UpdateLocal(root string, plugins []api.Plugin) { +func (l *Local) UpdateLocal(root string) { l.mu.Lock() defer l.mu.Unlock() l.root = root - l.plugins = plugins } // Plugins returns the locally defined plugins @@ -93,9 +90,16 @@ func (l *Local) Plugins(ctx context.Context, req *api.PluginsRequest, _ ...grpc. } func (l *Local) getPlugins() []api.Plugin { + if !plugin.AreRegisteredPluginsInitialized(l.plugins) { + return pluginsToPB(l.plugins.GetAll()) + } + l.mu.Lock() defer l.mu.Unlock() - return l.plugins + if l.pluginsPB == nil { + l.pluginsPB = pluginsToPB(l.plugins.GetAll()) + } + return l.pluginsPB } // Server returns the local server information diff --git a/services/introspection/service.go b/services/introspection/service.go index f161e3525..c11b8dc1c 100644 --- a/services/introspection/service.go +++ b/services/introspection/service.go @@ -31,11 +31,8 @@ func init() { plugin.Register(&plugin.Registration{ Type: plugin.GRPCPlugin, ID: "introspection", - Requires: []plugin.Type{"*"}, + Requires: []plugin.Type{plugin.ServicePlugin}, InitFn: func(ic *plugin.InitContext) (interface{}, error) { - // this service works by using the plugin context up till the point - // this service is initialized. Since we require this service last, - // it should provide the full set of plugins. plugins, err := ic.GetByType(plugin.ServicePlugin) if err != nil { return nil, err @@ -50,13 +47,11 @@ func init() { return nil, err } - allPluginsPB := pluginsToPB(ic.GetAll()) - localClient, ok := i.(*Local) if !ok { return nil, errors.New("Could not create a local client for introspection service") } - localClient.UpdateLocal(ic.Root, allPluginsPB) + localClient.UpdateLocal(ic.Root) return &server{ local: localClient,