Skip to content

Commit

Permalink
Merge pull request #2278 from hashicorp/b-consul-merge
Browse files Browse the repository at this point in the history
Fix Consul Config Merging/Copying
  • Loading branch information
dadgar committed Feb 2, 2017
2 parents d838cb0 + d212d40 commit acf9e58
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 37 deletions.
3 changes: 1 addition & 2 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -662,8 +662,7 @@ func (c *Config) Merge(b *Config) *Config {

// Apply the Consul Configuration
if result.Consul == nil && b.Consul != nil {
consulConfig := *b.Consul
result.Consul = &consulConfig
result.Consul = b.Consul.Copy()
} else if b.Consul != nil {
result.Consul = result.Consul.Merge(b.Consul)
}
Expand Down
58 changes: 30 additions & 28 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,21 @@ func TestConfig_Merge(t *testing.T) {
TLSServerName: "1",
},
Consul: &config.ConsulConfig{
ServerServiceName: "1",
ClientServiceName: "1",
AutoAdvertise: &falseValue,
Addr: "1",
Timeout: 1 * time.Second,
Token: "1",
Auth: "1",
EnableSSL: &falseValue,
VerifySSL: &falseValue,
CAFile: "1",
CertFile: "1",
KeyFile: "1",
ServerAutoJoin: &falseValue,
ClientAutoJoin: &falseValue,
ServerServiceName: "1",
ClientServiceName: "1",
AutoAdvertise: &falseValue,
Addr: "1",
Timeout: 1 * time.Second,
Token: "1",
Auth: "1",
EnableSSL: &falseValue,
VerifySSL: &falseValue,
CAFile: "1",
CertFile: "1",
KeyFile: "1",
ServerAutoJoin: &falseValue,
ClientAutoJoin: &falseValue,
ChecksUseAdvertise: &falseValue,
},
}

Expand Down Expand Up @@ -249,20 +250,21 @@ func TestConfig_Merge(t *testing.T) {
TLSServerName: "2",
},
Consul: &config.ConsulConfig{
ServerServiceName: "2",
ClientServiceName: "2",
AutoAdvertise: &trueValue,
Addr: "2",
Timeout: 2 * time.Second,
Token: "2",
Auth: "2",
EnableSSL: &trueValue,
VerifySSL: &trueValue,
CAFile: "2",
CertFile: "2",
KeyFile: "2",
ServerAutoJoin: &trueValue,
ClientAutoJoin: &trueValue,
ServerServiceName: "2",
ClientServiceName: "2",
AutoAdvertise: &trueValue,
Addr: "2",
Timeout: 2 * time.Second,
Token: "2",
Auth: "2",
EnableSSL: &trueValue,
VerifySSL: &trueValue,
CAFile: "2",
CertFile: "2",
KeyFile: "2",
ServerAutoJoin: &trueValue,
ClientAutoJoin: &trueValue,
ChecksUseAdvertise: &trueValue,
},
}

Expand Down
38 changes: 31 additions & 7 deletions nomad/structs/config/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func DefaultConsulConfig() *ConsulConfig {

// Merge merges two Consul Configurations together.
func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result := *a
result := a.Copy()

if b.ServerServiceName != "" {
result.ServerServiceName = b.ServerServiceName
Expand All @@ -102,7 +102,7 @@ func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result.ClientServiceName = b.ClientServiceName
}
if b.AutoAdvertise != nil {
result.AutoAdvertise = b.AutoAdvertise
result.AutoAdvertise = helper.BoolToPtr(*b.AutoAdvertise)
}
if b.Addr != "" {
result.Addr = b.Addr
Expand All @@ -117,10 +117,10 @@ func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result.Auth = b.Auth
}
if b.EnableSSL != nil {
result.EnableSSL = b.EnableSSL
result.EnableSSL = helper.BoolToPtr(*b.EnableSSL)
}
if b.VerifySSL != nil {
result.VerifySSL = b.VerifySSL
result.VerifySSL = helper.BoolToPtr(*b.VerifySSL)
}
if b.CAFile != "" {
result.CAFile = b.CAFile
Expand All @@ -132,12 +132,15 @@ func (a *ConsulConfig) Merge(b *ConsulConfig) *ConsulConfig {
result.KeyFile = b.KeyFile
}
if b.ServerAutoJoin != nil {
result.ServerAutoJoin = b.ServerAutoJoin
result.ServerAutoJoin = helper.BoolToPtr(*b.ServerAutoJoin)
}
if b.ClientAutoJoin != nil {
result.ClientAutoJoin = b.ServerAutoJoin
result.ClientAutoJoin = helper.BoolToPtr(*b.ClientAutoJoin)
}
return &result
if b.ChecksUseAdvertise != nil {
result.ChecksUseAdvertise = helper.BoolToPtr(*b.ChecksUseAdvertise)
}
return result
}

// ApiConfig() returns a usable Consul config that can be passed directly to
Expand Down Expand Up @@ -200,5 +203,26 @@ func (c *ConsulConfig) Copy() *ConsulConfig {

nc := new(ConsulConfig)
*nc = *c

// 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)
}

return nc
}

0 comments on commit acf9e58

Please sign in to comment.