github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/nomad/structs/config/consul.go (about)

     1  package config
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  	"time"
     7  
     8  	consul "github.com/hashicorp/consul/api"
     9  	"github.com/hashicorp/nomad/helper"
    10  )
    11  
    12  // ConsulConfig contains the configuration information necessary to
    13  // communicate with a Consul Agent in order to:
    14  //
    15  // - Register services and their checks with Consul
    16  //
    17  // - Bootstrap this Nomad Client with the list of Nomad Servers registered
    18  //   with Consul
    19  //
    20  // Both the Agent and the executor need to be able to import ConsulConfig.
    21  type ConsulConfig struct {
    22  	// ServerServiceName is the name of the service that Nomad uses to register
    23  	// servers with Consul
    24  	ServerServiceName string `mapstructure:"server_service_name"`
    25  
    26  	// ClientServiceName is the name of the service that Nomad uses to register
    27  	// clients with Consul
    28  	ClientServiceName string `mapstructure:"client_service_name"`
    29  
    30  	// AutoAdvertise determines if this Nomad Agent will advertise its
    31  	// services via Consul.  When true, Nomad Agent will register
    32  	// services with Consul.
    33  	AutoAdvertise *bool `mapstructure:"auto_advertise"`
    34  
    35  	// ChecksUseAdvertise specifies that Consul checks should use advertise
    36  	// address instead of bind address
    37  	ChecksUseAdvertise *bool `mapstructure:"checks_use_advertise"`
    38  
    39  	// Addr is the address of the local Consul agent
    40  	Addr string `mapstructure:"address"`
    41  
    42  	// Timeout is used by Consul HTTP Client
    43  	Timeout time.Duration `mapstructure:"timeout"`
    44  
    45  	// Token is used to provide a per-request ACL token. This options overrides
    46  	// the agent's default token
    47  	Token string `mapstructure:"token"`
    48  
    49  	// Auth is the information to use for http access to Consul agent
    50  	Auth string `mapstructure:"auth"`
    51  
    52  	// EnableSSL sets the transport scheme to talk to the Consul agent as https
    53  	EnableSSL *bool `mapstructure:"ssl"`
    54  
    55  	// VerifySSL enables or disables SSL verification when the transport scheme
    56  	// for the consul api client is https
    57  	VerifySSL *bool `mapstructure:"verify_ssl"`
    58  
    59  	// CAFile is the path to the ca certificate used for Consul communication
    60  	CAFile string `mapstructure:"ca_file"`
    61  
    62  	// CertFile is the path to the certificate for Consul communication
    63  	CertFile string `mapstructure:"cert_file"`
    64  
    65  	// KeyFile is the path to the private key for Consul communication
    66  	KeyFile string `mapstructure:"key_file"`
    67  
    68  	// ServerAutoJoin enables Nomad servers to find peers by querying Consul and
    69  	// joining them
    70  	ServerAutoJoin *bool `mapstructure:"server_auto_join"`
    71  
    72  	// ClientAutoJoin enables Nomad servers to find addresses of Nomad servers
    73  	// and register with them
    74  	ClientAutoJoin *bool `mapstructure:"client_auto_join"`
    75  }
    76  
    77  // DefaultConsulConfig() returns the canonical defaults for the Nomad
    78  // `consul` configuration.
    79  func DefaultConsulConfig() *ConsulConfig {
    80  	return &ConsulConfig{
    81  		ServerServiceName:  "nomad",
    82  		ClientServiceName:  "nomad-client",
    83  		AutoAdvertise:      helper.BoolToPtr(true),
    84  		ChecksUseAdvertise: helper.BoolToPtr(false),
    85  		EnableSSL:          helper.BoolToPtr(false),
    86  		VerifySSL:          helper.BoolToPtr(true),
    87  		ServerAutoJoin:     helper.BoolToPtr(true),
    88  		ClientAutoJoin:     helper.BoolToPtr(true),
    89  		Timeout:            5 * time.Second,
    90  	}
    91  }
    92  
    93  // Merge merges two Consul Configurations together.
    94  func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
    95  	result := a.Copy()
    96  
    97  	if b.ServerServiceName != "" {
    98  		result.ServerServiceName = b.ServerServiceName
    99  	}
   100  	if b.ClientServiceName != "" {
   101  		result.ClientServiceName = b.ClientServiceName
   102  	}
   103  	if b.AutoAdvertise != nil {
   104  		result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise)
   105  	}
   106  	if b.Addr != "" {
   107  		result.Addr = b.Addr
   108  	}
   109  	if b.Timeout != 0 {
   110  		result.Timeout = b.Timeout
   111  	}
   112  	if b.Token != "" {
   113  		result.Token = b.Token
   114  	}
   115  	if b.Auth != "" {
   116  		result.Auth = b.Auth
   117  	}
   118  	if b.EnableSSL != nil {
   119  		result.EnableSSL = helper.BoolToPtr(*b.EnableSSL)
   120  	}
   121  	if b.VerifySSL != nil {
   122  		result.VerifySSL = helper.BoolToPtr(*b.VerifySSL)
   123  	}
   124  	if b.CAFile != "" {
   125  		result.CAFile = b.CAFile
   126  	}
   127  	if b.CertFile != "" {
   128  		result.CertFile = b.CertFile
   129  	}
   130  	if b.KeyFile != "" {
   131  		result.KeyFile = b.KeyFile
   132  	}
   133  	if b.ServerAutoJoin != nil {
   134  		result.ServerAutoJoin = helper.BoolToPtr(*b.ServerAutoJoin)
   135  	}
   136  	if b.ClientAutoJoin != nil {
   137  		result.ClientAutoJoin = helper.BoolToPtr(*b.ClientAutoJoin)
   138  	}
   139  	if b.ChecksUseAdvertise != nil {
   140  		result.ChecksUseAdvertise = helper.BoolToPtr(*b.ChecksUseAdvertise)
   141  	}
   142  	return result
   143  }
   144  
   145  // ApiConfig returns a usable Consul config that can be passed directly to
   146  // hashicorp/consul/api.  NOTE: datacenter is not set
   147  func (c *ConsulConfig) ApiConfig() (*consul.Config, error) {
   148  	// Get the default config from consul to reuse things like the default
   149  	// http.Transport.
   150  	config := consul.DefaultConfig()
   151  	if c.Addr != "" {
   152  		config.Address = c.Addr
   153  	}
   154  	if c.Token != "" {
   155  		config.Token = c.Token
   156  	}
   157  	if c.Timeout != 0 {
   158  		// Create a custom Client to set the timeout
   159  		if config.HttpClient == nil {
   160  			config.HttpClient = &http.Client{}
   161  		}
   162  		config.HttpClient.Timeout = c.Timeout
   163  		config.HttpClient.Transport = config.Transport
   164  	}
   165  	if c.Auth != "" {
   166  		var username, password string
   167  		if strings.Contains(c.Auth, ":") {
   168  			split := strings.SplitN(c.Auth, ":", 2)
   169  			username = split[0]
   170  			password = split[1]
   171  		} else {
   172  			username = c.Auth
   173  		}
   174  
   175  		config.HttpAuth = &consul.HttpBasicAuth{
   176  			Username: username,
   177  			Password: password,
   178  		}
   179  	}
   180  	if c.EnableSSL != nil && *c.EnableSSL {
   181  		config.Scheme = "https"
   182  		config.TLSConfig = consul.TLSConfig{
   183  			Address:  config.Address,
   184  			CAFile:   c.CAFile,
   185  			CertFile: c.CertFile,
   186  			KeyFile:  c.KeyFile,
   187  		}
   188  		if c.VerifySSL != nil {
   189  			config.TLSConfig.InsecureSkipVerify = !*c.VerifySSL
   190  		}
   191  		tlsConfig, err := consul.SetupTLSConfig(&config.TLSConfig)
   192  		if err != nil {
   193  			return nil, err
   194  		}
   195  		config.Transport.TLSClientConfig = tlsConfig
   196  	}
   197  
   198  	return config, nil
   199  }
   200  
   201  // Copy returns a copy of this Consul config.
   202  func (c *ConsulConfig) Copy() *ConsulConfig {
   203  	if c == nil {
   204  		return nil
   205  	}
   206  
   207  	nc := new(ConsulConfig)
   208  	*nc = *c
   209  
   210  	// Copy the bools
   211  	if nc.AutoAdvertise != nil {
   212  		nc.AutoAdvertise = helper.BoolToPtr(*nc.AutoAdvertise)
   213  	}
   214  	if nc.ChecksUseAdvertise != nil {
   215  		nc.ChecksUseAdvertise = helper.BoolToPtr(*nc.ChecksUseAdvertise)
   216  	}
   217  	if nc.EnableSSL != nil {
   218  		nc.EnableSSL = helper.BoolToPtr(*nc.EnableSSL)
   219  	}
   220  	if nc.VerifySSL != nil {
   221  		nc.VerifySSL = helper.BoolToPtr(*nc.VerifySSL)
   222  	}
   223  	if nc.ServerAutoJoin != nil {
   224  		nc.ServerAutoJoin = helper.BoolToPtr(*nc.ServerAutoJoin)
   225  	}
   226  	if nc.ClientAutoJoin != nil {
   227  		nc.ClientAutoJoin = helper.BoolToPtr(*nc.ClientAutoJoin)
   228  	}
   229  
   230  	return nc
   231  }