github.com/hernad/nomad@v1.6.112/helper/pluginutils/catalog/catalog.go (about)

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