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