Create a `nomad/structs/config` to break an import cycle.

Flattening and normalizing the various Consul config structures and
services has led to an import cycle.  Break this by creating a new package
that is intended to be terminal in the import DAG.
This commit is contained in:
Sean Chittenden 2016-05-21 20:15:58 -07:00
parent 6d162e1e03
commit b509da2d0c
No known key found for this signature in database
GPG Key ID: 4EBC9DC16C2E5E16
6 changed files with 122 additions and 108 deletions

View File

@ -10,6 +10,7 @@ import (
"time"
"github.com/hashicorp/nomad/nomad"
cconfig "github.com/hashicorp/nomad/nomad/structs/config"
)
var nextPort uint32 = 17000
@ -42,7 +43,7 @@ func makeAgent(t testing.TB, cb func(*Config)) (string, *Agent) {
Serf: getPort(),
}
conf.NodeName = fmt.Sprintf("Node %d", conf.Ports.RPC)
conf.Consul = &ConsulConfig{}
conf.Consul = &cconfig.ConsulConfig{}
// Tighten the Serf timing
config.SerfConfig.MemberlistConfig.SuspicionMult = 2

View File

@ -14,6 +14,7 @@ import (
client "github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/nomad"
"github.com/hashicorp/nomad/nomad/structs/config"
)
// Config is the configuration for the Nomad agent.
@ -85,7 +86,7 @@ type Config struct {
// Consul contains the configuration for the Consul Agent and
// parameters necessary to register services, their checks, and
// discover the current Nomad servers.
Consul *ConsulConfig `mapstructure:"consul"`
Consul *config.ConsulConfig `mapstructure:"consul"`
// NomadConfig is used to override the default config.
// This is largly used for testing purposes.
@ -129,62 +130,6 @@ type AtlasConfig struct {
Endpoint string `mapstructure:"endpoint"`
}
// ConsulConfig contains the configuration information necessary to
// communicate with a Consul Agent in order to:
//
// - Register services and checks with Consul
//
// - Bootstrap this Nomad Client with the list of Nomad Servers registered
// with Consul
type ConsulConfig struct {
// ServerServiceName is the name of the service that Nomad uses to register
// servers with Consul
ServerServiceName string `mapstructure:"server_service_name"`
// ClientServiceName is the name of the service that Nomad uses to register
// clients with Consul
ClientServiceName string `mapstructure:"client_service_name"`
// AutoRegister determines if Nomad will register the Nomad client and
// server agents with Consul
AutoRegister bool `mapstructure:"auto_register"`
// Addr is the address of the local Consul agent
Addr string `mapstructure:"addr"`
// Token is used to provide a per-request ACL token.This options overrides
// the agent's default token
Token string `mapstructure:"token"`
// Auth is the information to use for http access to Consul agent
Auth string `mapstructure:"auth"`
// EnableSSL sets the transport scheme to talk to the Consul agent as https
EnableSSL bool `mapstructure:"ssl"`
// VerifySSL enables or disables SSL verification when the transport scheme
// for the consul api client is https
VerifySSL bool `mapstructure:"verify_ssl"`
// CAFile is the path to the ca certificate used for Consul communication
CAFile string `mapstructure:"ca_file"`
// CertFile is the path to the certificate for Consul communication
CertFile string `mapstructure:"cert_file"`
// KeyFile is the path to the private key for Consul communication
KeyFile string `mapstructure:"key_file"`
// ServerAutoJoin enables Nomad servers to find peers by querying Consul and
// joining them
ServerAutoJoin bool `mapstructure:"server_auto_join"`
// ClientAutoJoin enables Nomad servers to find addresses of Nomad servers
// and register with them
ClientAutoJoin bool `mapstructure:"client_auto_join"`
}
// StatsConfig determines behavior of resource usage stats collections
type StatsConfig struct {
@ -445,9 +390,10 @@ func DefaultConfig() *Config {
Addresses: &Addresses{},
AdvertiseAddrs: &AdvertiseAddrs{},
Atlas: &AtlasConfig{},
Consul: &ConsulConfig{
Consul: &config.ConsulConfig{
ServerServiceName: "nomad-server",
ClientServiceName: "nomad-client",
AutoRegister: true,
},
Client: &ClientConfig{
Enabled: false,
@ -815,52 +761,6 @@ func (a *AtlasConfig) Merge(b *AtlasConfig) *AtlasConfig {
return &result
}
// Merge merges two Consul Configurations together.
func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result := *a
if b.ServerServiceName != "" {
result.ServerServiceName = b.ServerServiceName
}
if b.ClientServiceName != "" {
result.ClientServiceName = b.ClientServiceName
}
if b.AutoRegister {
result.AutoRegister = true
}
if b.Addr != "" {
result.Addr = b.Addr
}
if b.Token != "" {
result.Token = b.Token
}
if b.Auth != "" {
result.Auth = b.Auth
}
if b.EnableSSL {
result.EnableSSL = true
}
if b.VerifySSL {
result.VerifySSL = true
}
if b.CAFile != "" {
result.CAFile = b.CAFile
}
if b.CertFile != "" {
result.CertFile = b.CertFile
}
if b.KeyFile != "" {
result.KeyFile = b.KeyFile
}
if b.ServerAutoJoin {
result.ServerAutoJoin = true
}
if b.ClientAutoJoin {
result.ClientAutoJoin = true
}
return &result
}
func (r *Resources) Merge(b *Resources) *Resources {
result := *r
if b.CPU != 0 {

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/mitchellh/mapstructure"
)
@ -586,7 +587,7 @@ func parseAtlas(result **AtlasConfig, list *ast.ObjectList) error {
return nil
}
func parseConsulConfig(result **ConsulConfig, list *ast.ObjectList) error {
func parseConsulConfig(result **config.ConsulConfig, list *ast.ObjectList) error {
list = list.Elem()
if len(list.Items) > 1 {
return fmt.Errorf("only one 'consul' block allowed")
@ -621,7 +622,7 @@ func parseConsulConfig(result **ConsulConfig, list *ast.ObjectList) error {
return err
}
var consulConfig ConsulConfig
var consulConfig config.ConsulConfig
if err := mapstructure.WeakDecode(m, &consulConfig); err != nil {
return err
}

View File

@ -4,6 +4,8 @@ import (
"path/filepath"
"reflect"
"testing"
"github.com/hashicorp/nomad/nomad/structs/config"
)
func TestConfig_Parse(t *testing.T) {
@ -100,7 +102,7 @@ func TestConfig_Parse(t *testing.T) {
Join: true,
Endpoint: "127.0.0.1:1234",
},
Consul: &ConsulConfig{
Consul: &config.ConsulConfig{
ServerServiceName: "nomad-server",
ClientServiceName: "nomad-client",
Addr: "127.0.0.1:9500",

View File

@ -0,0 +1,7 @@
# Overview
`nomad/structs/config` is a package for configuration `struct`s that are
shared among packages that needs the same `struct` definitions, but can't
import each other without creating a cyle. This `config` package must be
terminal in the import graph (or very close to terminal in the dependency
graph).

View File

@ -0,0 +1,103 @@
package config
// ConsulConfig contains the configuration information necessary to
// communicate with a Consul Agent in order to:
//
// - Register services and checks with Consul
//
// - Bootstrap this Nomad Client with the list of Nomad Servers registered
// with Consul
type ConsulConfig struct {
// ServerServiceName is the name of the service that Nomad uses to register
// servers with Consul
ServerServiceName string `mapstructure:"server_service_name"`
// ClientServiceName is the name of the service that Nomad uses to register
// clients with Consul
ClientServiceName string `mapstructure:"client_service_name"`
// AutoRegister determines if Nomad will register the Nomad client and
// server agents with Consul
AutoRegister bool `mapstructure:"auto_register"`
// Addr is the address of the local Consul agent
Addr string `mapstructure:"addr"`
// Token is used to provide a per-request ACL token.This options overrides
// the agent's default token
Token string `mapstructure:"token"`
// Auth is the information to use for http access to Consul agent
Auth string `mapstructure:"auth"`
// EnableSSL sets the transport scheme to talk to the Consul agent as https
EnableSSL bool `mapstructure:"ssl"`
// VerifySSL enables or disables SSL verification when the transport scheme
// for the consul api client is https
VerifySSL bool `mapstructure:"verify_ssl"`
// CAFile is the path to the ca certificate used for Consul communication
CAFile string `mapstructure:"ca_file"`
// CertFile is the path to the certificate for Consul communication
CertFile string `mapstructure:"cert_file"`
// KeyFile is the path to the private key for Consul communication
KeyFile string `mapstructure:"key_file"`
// ServerAutoJoin enables Nomad servers to find peers by querying Consul and
// joining them
ServerAutoJoin bool `mapstructure:"server_auto_join"`
// ClientAutoJoin enables Nomad servers to find addresses of Nomad servers
// and register with them
ClientAutoJoin bool `mapstructure:"client_auto_join"`
}
// Merge merges two Consul Configurations together.
func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result := *a
if b.ServerServiceName != "" {
result.ServerServiceName = b.ServerServiceName
}
if b.ClientServiceName != "" {
result.ClientServiceName = b.ClientServiceName
}
if b.AutoRegister {
result.AutoRegister = true
}
if b.Addr != "" {
result.Addr = b.Addr
}
if b.Token != "" {
result.Token = b.Token
}
if b.Auth != "" {
result.Auth = b.Auth
}
if b.EnableSSL {
result.EnableSSL = true
}
if b.VerifySSL {
result.VerifySSL = true
}
if b.CAFile != "" {
result.CAFile = b.CAFile
}
if b.CertFile != "" {
result.CertFile = b.CertFile
}
if b.KeyFile != "" {
result.KeyFile = b.KeyFile
}
if b.ServerAutoJoin {
result.ServerAutoJoin = true
}
if b.ClientAutoJoin {
result.ClientAutoJoin = true
}
return &result
}