github.com/manicqin/nomad@v0.9.5/command/agent/plugins.go (about)

     1  package agent
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/nomad/helper/pluginutils/catalog"
     7  	"github.com/hashicorp/nomad/helper/pluginutils/loader"
     8  	"github.com/hashicorp/nomad/helper/pluginutils/singleton"
     9  )
    10  
    11  // setupPlugins is used to setup the plugin loaders.
    12  func (a *Agent) setupPlugins() error {
    13  	// Get our internal plugins
    14  	internal, err := a.internalPluginConfigs()
    15  	if err != nil {
    16  		return err
    17  	}
    18  
    19  	// Build the plugin loader
    20  	config := &loader.PluginLoaderConfig{
    21  		Logger:            a.logger,
    22  		PluginDir:         a.config.PluginDir,
    23  		Configs:           a.config.Plugins,
    24  		InternalPlugins:   internal,
    25  		SupportedVersions: loader.AgentSupportedApiVersions,
    26  	}
    27  	l, err := loader.NewPluginLoader(config)
    28  	if err != nil {
    29  		return fmt.Errorf("failed to create plugin loader: %v", err)
    30  	}
    31  	a.pluginLoader = l
    32  
    33  	// Wrap the loader to get our singleton loader
    34  	a.pluginSingletonLoader = singleton.NewSingletonLoader(a.logger, l)
    35  
    36  	for k, plugins := range a.pluginLoader.Catalog() {
    37  		for _, p := range plugins {
    38  			a.logger.Info("detected plugin", "name", p.Name, "type", k, "plugin_version", p.PluginVersion)
    39  		}
    40  	}
    41  
    42  	return nil
    43  }
    44  
    45  func (a *Agent) internalPluginConfigs() (map[loader.PluginID]*loader.InternalPluginConfig, error) {
    46  	// Get the registered plugins
    47  	catalog := catalog.Catalog()
    48  
    49  	// Create our map of plugins
    50  	internal := make(map[loader.PluginID]*loader.InternalPluginConfig, len(catalog))
    51  
    52  	// Grab the client options map if we can
    53  	var options map[string]string
    54  	if a.config != nil && a.config.Client != nil {
    55  		options = a.config.Client.Options
    56  	}
    57  
    58  	for id, reg := range catalog {
    59  		if reg.Config == nil {
    60  			a.logger.Error("skipping loading internal plugin because it is missing its configuration", "plugin", id)
    61  			continue
    62  		}
    63  
    64  		pluginConfig := reg.Config.Config
    65  		if reg.ConfigLoader != nil {
    66  			pc, err := reg.ConfigLoader(options)
    67  			if err != nil {
    68  				return nil, fmt.Errorf("failed to retrieve config for internal plugin %v: %v", id, err)
    69  			}
    70  
    71  			pluginConfig = pc
    72  
    73  			// TODO We should log the config to warn users about upgrade pathing
    74  		}
    75  
    76  		internal[id] = &loader.InternalPluginConfig{
    77  			Factory: reg.Config.Factory,
    78  			Config:  pluginConfig,
    79  		}
    80  	}
    81  
    82  	return internal, nil
    83  }