From 891541cd40fac59bd37bb4f2566e348ade964604 Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Fri, 15 Oct 2021 14:34:09 -0700 Subject: [PATCH] Create cache in manager and set into plugin if available --- internal/plugin/manager.go | 7 ++++++- internal/plugin/plugin.go | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/internal/plugin/manager.go b/internal/plugin/manager.go index 3963d7af2..0a3c32d83 100644 --- a/internal/plugin/manager.go +++ b/internal/plugin/manager.go @@ -13,10 +13,11 @@ import ( "github.com/hashicorp/go-hclog" "github.com/hashicorp/go-multierror" "github.com/hashicorp/go-plugin" - "github.com/hashicorp/vagrant/internal/serverclient" "github.com/hashicorp/vagrant-plugin-sdk/component" "github.com/hashicorp/vagrant-plugin-sdk/helper/path" + "github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cacher" + "github.com/hashicorp/vagrant/internal/serverclient" ) type PluginRegistration func(hclog.Logger) (*Plugin, error) @@ -33,6 +34,7 @@ type Manager struct { logger hclog.Logger m sync.Mutex parent *Manager + cache cacher.Cache } // Create a new plugin manager @@ -42,6 +44,7 @@ func NewManager(ctx context.Context, l hclog.Logger) *Manager { builtins: NewBuiltins(ctx, l), ctx: ctx, logger: l, + cache: cacher.New(), } } @@ -58,6 +61,7 @@ func (m *Manager) Sub(name string) *Manager { legacyLoaded: true, logger: m.logger.Named(name), parent: m, + cache: m.cache, } m.closer(func() error { return s.Close() }) @@ -336,6 +340,7 @@ func (m *Manager) register( if err != nil { return } + plg.Cache = m.cache for _, t := range plg.Types { m.logger.Info("registering plugin", diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go index 4f8afe6cf..faf58f6e3 100644 --- a/internal/plugin/plugin.go +++ b/internal/plugin/plugin.go @@ -11,6 +11,7 @@ import ( sdk "github.com/hashicorp/vagrant-plugin-sdk" "github.com/hashicorp/vagrant-plugin-sdk/component" + "github.com/hashicorp/vagrant-plugin-sdk/internal-shared/cacher" "github.com/hashicorp/vagrant/builtin/myplugin" "github.com/hashicorp/vagrant/builtin/otherplugin" ) @@ -38,6 +39,7 @@ type Plugin struct { Location string // Location of the plugin (generally path to binary) Name string // Name of the plugin Types []component.Type // Component types supported by this plugin + Cache cacher.Cache closers []func() error components map[component.Type]*Instance @@ -144,6 +146,12 @@ func (p *Plugin) InstanceOf( return nil, fmt.Errorf("unable to extract broker from plugin client") } + if c, ok := raw.(interface { + SetCache(cacher.Cache) + }); ok { + c.SetCache(p.Cache) + } + i = &Instance{ Component: raw, Broker: b.GRPCBroker(),