github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/helper/pluginutils/catalog/catalog.go (about)

     1  // Package catalog is used to register internal plugins such that they can be
     2  // loaded.
     3  package catalog
     4  
     5  import (
     6  	"sync"
     7  
     8  	"github.com/hashicorp/nomad/helper/pluginutils/loader"
     9  )
    10  
    11  var (
    12  	// catalog is the set of registered internal plugins
    13  	catalog = map[loader.PluginID]*Registration{}
    14  	mu      sync.Mutex
    15  )
    16  
    17  // Registration is the registration of an internal plugin
    18  type Registration struct {
    19  	Config       *loader.InternalPluginConfig
    20  	ConfigLoader ConfigFromOptions
    21  }
    22  
    23  // ConfigFromOptions is used to retrieve a plugin config when passed a node's
    24  // option map. This allows upgrade pathing from the old configuration format to
    25  // the new config format.
    26  type ConfigFromOptions func(options map[string]string) (config map[string]interface{}, err error)
    27  
    28  // Register is used to register an internal plugin.
    29  func Register(id loader.PluginID, config *loader.InternalPluginConfig) {
    30  	mu.Lock()
    31  	defer mu.Unlock()
    32  	catalog[id] = &Registration{
    33  		Config: config,
    34  	}
    35  }
    36  
    37  // RegisterDeferredConfig is used to register an internal plugin that sets its
    38  // config using the client's option map.
    39  func RegisterDeferredConfig(id loader.PluginID, config *loader.InternalPluginConfig, configLoader ConfigFromOptions) {
    40  	mu.Lock()
    41  	defer mu.Unlock()
    42  	catalog[id] = &Registration{
    43  		Config:       config,
    44  		ConfigLoader: configLoader,
    45  	}
    46  }
    47  
    48  // Catalog returns the catalog of internal plugins
    49  func Catalog() map[loader.PluginID]*Registration {
    50  	mu.Lock()
    51  	defer mu.Unlock()
    52  	return catalog
    53  }