github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/plugins/base/base.go (about)

     1  package base
     2  
     3  import (
     4  	"github.com/hashicorp/nomad/plugins/base/proto"
     5  	"github.com/hashicorp/nomad/plugins/shared/hclspec"
     6  )
     7  
     8  // BasePlugin is the interface that all Nomad plugins must support.
     9  type BasePlugin interface {
    10  	// PluginInfo describes the type and version of a plugin.
    11  	PluginInfo() (*PluginInfoResponse, error)
    12  
    13  	// ConfigSchema returns the schema for parsing the plugins configuration.
    14  	ConfigSchema() (*hclspec.Spec, error)
    15  
    16  	// SetConfig is used to set the configuration by passing a MessagePack
    17  	// encoding of it.
    18  	SetConfig(c *Config) error
    19  }
    20  
    21  // PluginInfoResponse returns basic information about the plugin such that Nomad
    22  // can decide whether to load the plugin or not.
    23  type PluginInfoResponse struct {
    24  	// Type returns the plugins type
    25  	Type string
    26  
    27  	// PluginApiVersions returns the versions of the Nomad plugin API that the
    28  	// plugin supports.
    29  	PluginApiVersions []string
    30  
    31  	// PluginVersion is the version of the plugin.
    32  	PluginVersion string
    33  
    34  	// Name is the plugins name.
    35  	Name string
    36  }
    37  
    38  // Config contains the configuration for the plugin.
    39  type Config struct {
    40  	// ApiVersion is the negotiated plugin API version to use.
    41  	ApiVersion string
    42  
    43  	// PluginConfig is the MessagePack encoding of the plugins user
    44  	// configuration.
    45  	PluginConfig []byte
    46  
    47  	// AgentConfig is the Nomad agents configuration as applicable to plugins
    48  	AgentConfig *AgentConfig
    49  }
    50  
    51  // AgentConfig is the Nomad agent's configuration sent to all plugins
    52  type AgentConfig struct {
    53  	Driver *ClientDriverConfig
    54  }
    55  
    56  // ClientDriverConfig is the driver specific configuration for all driver plugins
    57  type ClientDriverConfig struct {
    58  	// ClientMaxPort is the upper range of the ports that the client uses for
    59  	// communicating with plugin subsystems over loopback
    60  	ClientMaxPort uint
    61  
    62  	// ClientMinPort is the lower range of the ports that the client uses for
    63  	// communicating with plugin subsystems over loopback
    64  	ClientMinPort uint
    65  }
    66  
    67  func (c *AgentConfig) toProto() *proto.NomadConfig {
    68  	if c == nil {
    69  		return nil
    70  	}
    71  
    72  	cfg := &proto.NomadConfig{}
    73  	if c.Driver != nil {
    74  
    75  		cfg.Driver = &proto.NomadDriverConfig{
    76  			ClientMaxPort: uint32(c.Driver.ClientMaxPort),
    77  			ClientMinPort: uint32(c.Driver.ClientMinPort),
    78  		}
    79  	}
    80  
    81  	return cfg
    82  }
    83  
    84  func nomadConfigFromProto(pb *proto.NomadConfig) *AgentConfig {
    85  	if pb == nil {
    86  		return nil
    87  	}
    88  
    89  	cfg := &AgentConfig{}
    90  	if pb.Driver != nil {
    91  		cfg.Driver = &ClientDriverConfig{
    92  			ClientMaxPort: uint(pb.Driver.ClientMaxPort),
    93  			ClientMinPort: uint(pb.Driver.ClientMinPort),
    94  		}
    95  	}
    96  
    97  	return cfg
    98  }