2016-05-22 03:15:58 +00:00
|
|
|
package config
|
|
|
|
|
2016-05-26 20:31:32 +00:00
|
|
|
import (
|
2017-05-16 19:19:16 +00:00
|
|
|
"net/http"
|
2016-06-17 05:51:06 +00:00
|
|
|
"strings"
|
2016-05-26 20:31:32 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
consul "github.com/hashicorp/consul/api"
|
2017-01-18 23:55:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2016-05-26 20:31:32 +00:00
|
|
|
)
|
|
|
|
|
2016-05-22 03:15:58 +00:00
|
|
|
// ConsulConfig contains the configuration information necessary to
|
|
|
|
// communicate with a Consul Agent in order to:
|
|
|
|
//
|
2016-06-10 02:55:01 +00:00
|
|
|
// - Register services and their checks with Consul
|
2016-05-22 03:15:58 +00:00
|
|
|
//
|
|
|
|
// - Bootstrap this Nomad Client with the list of Nomad Servers registered
|
|
|
|
// with Consul
|
2016-05-23 18:09:31 +00:00
|
|
|
//
|
|
|
|
// Both the Agent and the executor need to be able to import ConsulConfig.
|
2016-05-22 03:15:58 +00:00
|
|
|
type ConsulConfig struct {
|
|
|
|
// ServerServiceName is the name of the service that Nomad uses to register
|
|
|
|
// servers with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ServerServiceName string `hcl:"server_service_name"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
2018-03-20 15:03:58 +00:00
|
|
|
// ServerHTTPCheckName is the name of the health check that Nomad uses
|
2018-03-19 18:37:56 +00:00
|
|
|
// to register the server HTTP health check with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ServerHTTPCheckName string `hcl:"server_http_check_name"`
|
2018-03-19 18:37:56 +00:00
|
|
|
|
2018-03-20 15:03:58 +00:00
|
|
|
// ServerSerfCheckName is the name of the health check that Nomad uses
|
2018-03-19 18:37:56 +00:00
|
|
|
// to register the server Serf health check with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ServerSerfCheckName string `hcl:"server_serf_check_name"`
|
2018-03-19 18:37:56 +00:00
|
|
|
|
2018-03-20 15:03:58 +00:00
|
|
|
// ServerRPCCheckName is the name of the health check that Nomad uses
|
2018-03-19 18:37:56 +00:00
|
|
|
// to register the server RPC health check with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ServerRPCCheckName string `hcl:"server_rpc_check_name"`
|
2018-03-19 18:37:56 +00:00
|
|
|
|
2016-05-22 03:15:58 +00:00
|
|
|
// ClientServiceName is the name of the service that Nomad uses to register
|
|
|
|
// clients with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ClientServiceName string `hcl:"client_service_name"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
2018-03-20 15:03:58 +00:00
|
|
|
// ClientHTTPCheckName is the name of the health check that Nomad uses
|
2018-03-19 18:37:56 +00:00
|
|
|
// to register the client HTTP health check with Consul
|
2019-04-04 18:16:08 +00:00
|
|
|
ClientHTTPCheckName string `hcl:"client_http_check_name"`
|
2018-03-19 18:37:56 +00:00
|
|
|
|
2019-01-09 20:38:39 +00:00
|
|
|
// Tags are optional service tags that get registered with the service
|
|
|
|
// in Consul
|
|
|
|
Tags []string `mapstructure:"tags"`
|
|
|
|
|
2016-06-14 19:11:38 +00:00
|
|
|
// AutoAdvertise determines if this Nomad Agent will advertise its
|
|
|
|
// services via Consul. When true, Nomad Agent will register
|
|
|
|
// services with Consul.
|
2019-04-04 18:16:08 +00:00
|
|
|
AutoAdvertise *bool `hcl:"auto_advertise"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
2016-10-26 19:58:36 +00:00
|
|
|
// ChecksUseAdvertise specifies that Consul checks should use advertise
|
|
|
|
// address instead of bind address
|
2019-04-04 18:16:08 +00:00
|
|
|
ChecksUseAdvertise *bool `hcl:"checks_use_advertise"`
|
2016-10-26 19:58:36 +00:00
|
|
|
|
2016-05-22 03:15:58 +00:00
|
|
|
// Addr is the address of the local Consul agent
|
2019-04-04 18:16:08 +00:00
|
|
|
Addr string `hcl:"address"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
2016-05-26 20:31:32 +00:00
|
|
|
// Timeout is used by Consul HTTP Client
|
2019-04-04 18:16:08 +00:00
|
|
|
Timeout time.Duration
|
2019-04-25 18:54:20 +00:00
|
|
|
TimeoutHCL string `hcl:"timeout" json:"-"`
|
2016-05-26 20:31:32 +00:00
|
|
|
|
2016-06-14 22:21:57 +00:00
|
|
|
// Token is used to provide a per-request ACL token. This options overrides
|
2016-05-22 03:15:58 +00:00
|
|
|
// the agent's default token
|
2019-04-04 18:16:08 +00:00
|
|
|
Token string `hcl:"token"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// Auth is the information to use for http access to Consul agent
|
2019-04-04 18:16:08 +00:00
|
|
|
Auth string `hcl:"auth"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// EnableSSL sets the transport scheme to talk to the Consul agent as https
|
2019-04-04 18:16:08 +00:00
|
|
|
EnableSSL *bool `hcl:"ssl"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// VerifySSL enables or disables SSL verification when the transport scheme
|
|
|
|
// for the consul api client is https
|
2019-04-04 18:16:08 +00:00
|
|
|
VerifySSL *bool `hcl:"verify_ssl"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// CAFile is the path to the ca certificate used for Consul communication
|
2019-04-04 18:16:08 +00:00
|
|
|
CAFile string `hcl:"ca_file"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// CertFile is the path to the certificate for Consul communication
|
2019-04-04 18:16:08 +00:00
|
|
|
CertFile string `hcl:"cert_file"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// KeyFile is the path to the private key for Consul communication
|
2019-04-04 18:16:08 +00:00
|
|
|
KeyFile string `hcl:"key_file"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// ServerAutoJoin enables Nomad servers to find peers by querying Consul and
|
|
|
|
// joining them
|
2019-04-04 18:16:08 +00:00
|
|
|
ServerAutoJoin *bool `hcl:"server_auto_join"`
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
// ClientAutoJoin enables Nomad servers to find addresses of Nomad servers
|
|
|
|
// and register with them
|
2019-04-04 18:16:08 +00:00
|
|
|
ClientAutoJoin *bool `hcl:"client_auto_join"`
|
|
|
|
|
|
|
|
// ExtraKeysHCL is used by hcl to surface unexpected keys
|
2019-04-25 18:54:20 +00:00
|
|
|
ExtraKeysHCL []string `hcl:",unusedKeys" json:"-"`
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 03:41:05 +00:00
|
|
|
// DefaultConsulConfig() returns the canonical defaults for the Nomad
|
|
|
|
// `consul` configuration.
|
|
|
|
func DefaultConsulConfig() *ConsulConfig {
|
|
|
|
return &ConsulConfig{
|
2018-03-20 15:03:58 +00:00
|
|
|
ServerServiceName: "nomad",
|
|
|
|
ServerHTTPCheckName: "Nomad Server HTTP Check",
|
|
|
|
ServerSerfCheckName: "Nomad Server Serf Check",
|
|
|
|
ServerRPCCheckName: "Nomad Server RPC Check",
|
|
|
|
ClientServiceName: "nomad-client",
|
|
|
|
ClientHTTPCheckName: "Nomad Client HTTP Check",
|
|
|
|
AutoAdvertise: helper.BoolToPtr(true),
|
|
|
|
ChecksUseAdvertise: helper.BoolToPtr(false),
|
|
|
|
EnableSSL: helper.BoolToPtr(false),
|
|
|
|
VerifySSL: helper.BoolToPtr(true),
|
|
|
|
ServerAutoJoin: helper.BoolToPtr(true),
|
|
|
|
ClientAutoJoin: helper.BoolToPtr(true),
|
|
|
|
Timeout: 5 * time.Second,
|
2016-06-17 03:41:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 03:15:58 +00:00
|
|
|
// Merge merges two Consul Configurations together.
|
|
|
|
func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
|
2017-02-02 19:12:07 +00:00
|
|
|
result := a.Copy()
|
2016-05-22 03:15:58 +00:00
|
|
|
|
|
|
|
if b.ServerServiceName != "" {
|
|
|
|
result.ServerServiceName = b.ServerServiceName
|
|
|
|
}
|
2018-03-20 15:03:58 +00:00
|
|
|
if b.ServerHTTPCheckName != "" {
|
|
|
|
result.ServerHTTPCheckName = b.ServerHTTPCheckName
|
2018-03-19 18:37:56 +00:00
|
|
|
}
|
2018-03-20 15:03:58 +00:00
|
|
|
if b.ServerSerfCheckName != "" {
|
|
|
|
result.ServerSerfCheckName = b.ServerSerfCheckName
|
2018-03-19 18:37:56 +00:00
|
|
|
}
|
2018-03-20 15:03:58 +00:00
|
|
|
if b.ServerRPCCheckName != "" {
|
|
|
|
result.ServerRPCCheckName = b.ServerRPCCheckName
|
2018-03-19 18:37:56 +00:00
|
|
|
}
|
2016-05-22 03:15:58 +00:00
|
|
|
if b.ClientServiceName != "" {
|
|
|
|
result.ClientServiceName = b.ClientServiceName
|
|
|
|
}
|
2018-03-20 15:03:58 +00:00
|
|
|
if b.ClientHTTPCheckName != "" {
|
|
|
|
result.ClientHTTPCheckName = b.ClientHTTPCheckName
|
2018-03-19 18:37:56 +00:00
|
|
|
}
|
2019-01-09 20:38:39 +00:00
|
|
|
result.Tags = append(result.Tags, b.Tags...)
|
2017-01-18 23:55:14 +00:00
|
|
|
if b.AutoAdvertise != nil {
|
2017-02-02 19:12:07 +00:00
|
|
|
result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise)
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
|
|
|
if b.Addr != "" {
|
|
|
|
result.Addr = b.Addr
|
|
|
|
}
|
2016-05-26 20:31:32 +00:00
|
|
|
if b.Timeout != 0 {
|
|
|
|
result.Timeout = b.Timeout
|
|
|
|
}
|
2016-05-22 03:15:58 +00:00
|
|
|
if b.Token != "" {
|
|
|
|
result.Token = b.Token
|
|
|
|
}
|
|
|
|
if b.Auth != "" {
|
|
|
|
result.Auth = b.Auth
|
|
|
|
}
|
2017-01-18 23:55:14 +00:00
|
|
|
if b.EnableSSL != nil {
|
2017-02-02 19:12:07 +00:00
|
|
|
result.EnableSSL = helper.BoolToPtr(*b.EnableSSL)
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
2017-01-18 23:55:14 +00:00
|
|
|
if b.VerifySSL != nil {
|
2017-02-02 19:12:07 +00:00
|
|
|
result.VerifySSL = helper.BoolToPtr(*b.VerifySSL)
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
|
|
|
if b.CAFile != "" {
|
|
|
|
result.CAFile = b.CAFile
|
|
|
|
}
|
|
|
|
if b.CertFile != "" {
|
|
|
|
result.CertFile = b.CertFile
|
|
|
|
}
|
|
|
|
if b.KeyFile != "" {
|
|
|
|
result.KeyFile = b.KeyFile
|
|
|
|
}
|
2017-01-18 23:55:14 +00:00
|
|
|
if b.ServerAutoJoin != nil {
|
2017-02-02 19:12:07 +00:00
|
|
|
result.ServerAutoJoin = helper.BoolToPtr(*b.ServerAutoJoin)
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
2017-01-18 23:55:14 +00:00
|
|
|
if b.ClientAutoJoin != nil {
|
2017-02-02 19:12:07 +00:00
|
|
|
result.ClientAutoJoin = helper.BoolToPtr(*b.ClientAutoJoin)
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
2017-02-02 19:12:07 +00:00
|
|
|
if b.ChecksUseAdvertise != nil {
|
|
|
|
result.ChecksUseAdvertise = helper.BoolToPtr(*b.ChecksUseAdvertise)
|
|
|
|
}
|
|
|
|
return result
|
2016-05-22 03:15:58 +00:00
|
|
|
}
|
2016-05-26 20:31:32 +00:00
|
|
|
|
2017-05-06 00:08:51 +00:00
|
|
|
// ApiConfig returns a usable Consul config that can be passed directly to
|
2016-05-26 20:31:32 +00:00
|
|
|
// hashicorp/consul/api. NOTE: datacenter is not set
|
|
|
|
func (c *ConsulConfig) ApiConfig() (*consul.Config, error) {
|
2017-05-16 19:19:16 +00:00
|
|
|
// Get the default config from consul to reuse things like the default
|
|
|
|
// http.Transport.
|
2016-05-26 20:31:32 +00:00
|
|
|
config := consul.DefaultConfig()
|
|
|
|
if c.Addr != "" {
|
|
|
|
config.Address = c.Addr
|
|
|
|
}
|
|
|
|
if c.Token != "" {
|
|
|
|
config.Token = c.Token
|
|
|
|
}
|
|
|
|
if c.Timeout != 0 {
|
2017-05-30 18:59:02 +00:00
|
|
|
// Create a custom Client to set the timeout
|
2017-05-16 19:19:16 +00:00
|
|
|
if config.HttpClient == nil {
|
|
|
|
config.HttpClient = &http.Client{}
|
|
|
|
}
|
2016-05-26 20:31:32 +00:00
|
|
|
config.HttpClient.Timeout = c.Timeout
|
2017-05-16 19:19:16 +00:00
|
|
|
config.HttpClient.Transport = config.Transport
|
2016-05-26 20:31:32 +00:00
|
|
|
}
|
2016-06-17 05:51:06 +00:00
|
|
|
if c.Auth != "" {
|
|
|
|
var username, password string
|
|
|
|
if strings.Contains(c.Auth, ":") {
|
|
|
|
split := strings.SplitN(c.Auth, ":", 2)
|
|
|
|
username = split[0]
|
|
|
|
password = split[1]
|
|
|
|
} else {
|
|
|
|
username = c.Auth
|
|
|
|
}
|
|
|
|
|
|
|
|
config.HttpAuth = &consul.HttpBasicAuth{
|
|
|
|
Username: username,
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
2017-01-18 23:55:14 +00:00
|
|
|
if c.EnableSSL != nil && *c.EnableSSL {
|
2016-06-17 05:51:06 +00:00
|
|
|
config.Scheme = "https"
|
2017-05-15 22:32:32 +00:00
|
|
|
config.TLSConfig = consul.TLSConfig{
|
2017-01-18 23:55:14 +00:00
|
|
|
Address: config.Address,
|
|
|
|
CAFile: c.CAFile,
|
|
|
|
CertFile: c.CertFile,
|
|
|
|
KeyFile: c.KeyFile,
|
|
|
|
}
|
|
|
|
if c.VerifySSL != nil {
|
2017-05-15 22:32:32 +00:00
|
|
|
config.TLSConfig.InsecureSkipVerify = !*c.VerifySSL
|
2016-06-17 05:51:06 +00:00
|
|
|
}
|
2017-05-16 19:19:16 +00:00
|
|
|
tlsConfig, err := consul.SetupTLSConfig(&config.TLSConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.Transport.TLSClientConfig = tlsConfig
|
2016-06-17 05:51:06 +00:00
|
|
|
}
|
2016-05-26 20:31:32 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
2016-08-09 22:00:50 +00:00
|
|
|
|
|
|
|
// Copy returns a copy of this Consul config.
|
|
|
|
func (c *ConsulConfig) Copy() *ConsulConfig {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nc := new(ConsulConfig)
|
|
|
|
*nc = *c
|
2017-02-02 19:12:07 +00:00
|
|
|
|
|
|
|
// Copy the bools
|
|
|
|
if nc.AutoAdvertise != nil {
|
|
|
|
nc.AutoAdvertise = helper.BoolToPtr(*nc.AutoAdvertise)
|
|
|
|
}
|
|
|
|
if nc.ChecksUseAdvertise != nil {
|
|
|
|
nc.ChecksUseAdvertise = helper.BoolToPtr(*nc.ChecksUseAdvertise)
|
|
|
|
}
|
|
|
|
if nc.EnableSSL != nil {
|
|
|
|
nc.EnableSSL = helper.BoolToPtr(*nc.EnableSSL)
|
|
|
|
}
|
|
|
|
if nc.VerifySSL != nil {
|
|
|
|
nc.VerifySSL = helper.BoolToPtr(*nc.VerifySSL)
|
|
|
|
}
|
|
|
|
if nc.ServerAutoJoin != nil {
|
|
|
|
nc.ServerAutoJoin = helper.BoolToPtr(*nc.ServerAutoJoin)
|
|
|
|
}
|
|
|
|
if nc.ClientAutoJoin != nil {
|
|
|
|
nc.ClientAutoJoin = helper.BoolToPtr(*nc.ClientAutoJoin)
|
|
|
|
}
|
|
|
|
|
2016-08-09 22:00:50 +00:00
|
|
|
return nc
|
|
|
|
}
|