diff --git a/command/agent/config.go b/command/agent/config.go index 376ce7b8df6..1bc21bb2969 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -381,19 +381,31 @@ type ServerJoin struct { RetryInterval time.Duration `mapstructure:"retry_interval"` } -func (s *ServerJoin) Merge(b *ServerJoin) { +func (s *ServerJoin) Merge(b *ServerJoin) *ServerJoin { + if s == nil { + return b + } + + result := *s + + if b == nil { + return &result + } + if len(b.StartJoin) != 0 { - s.StartJoin = b.StartJoin + result.StartJoin = b.StartJoin } if len(b.RetryJoin) != 0 { - s.RetryJoin = b.RetryJoin + result.RetryJoin = b.RetryJoin } if b.RetryMaxAttempts != 0 { - s.RetryMaxAttempts = b.RetryMaxAttempts + result.RetryMaxAttempts = b.RetryMaxAttempts } if b.RetryInterval != 0 { - s.RetryInterval = b.RetryInterval + result.RetryInterval = b.RetryInterval } + + return &result } // EncryptBytes returns the encryption key configured. @@ -1104,7 +1116,7 @@ func (a *ServerConfig) Merge(b *ServerConfig) *ServerConfig { result.EncryptKey = b.EncryptKey } if b.ServerJoin != nil { - result.ServerJoin.Merge(b.ServerJoin) + result.ServerJoin = result.ServerJoin.Merge(b.ServerJoin) } // Add the schedulers diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 6c3337d44ba..c85ad2c0384 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -926,11 +926,11 @@ func TestMergeServerJoin(t *testing.T) { } b := &ServerJoin{} - a.Merge(b) - require.Equal(a.RetryJoin, retryJoin) - require.Equal(a.StartJoin, startJoin) - require.Equal(a.RetryMaxAttempts, retryMaxAttempts) - require.Equal(a.RetryInterval, retryInterval) + result := a.Merge(b) + require.Equal(result.RetryJoin, retryJoin) + require.Equal(result.StartJoin, startJoin) + require.Equal(result.RetryMaxAttempts, retryMaxAttempts) + require.Equal(result.RetryInterval, retryInterval) } { retryJoin := []string{"127.0.0.1", "127.0.0.2"} @@ -946,11 +946,51 @@ func TestMergeServerJoin(t *testing.T) { RetryInterval: time.Duration(retryInterval), } - a.Merge(b) - require.Equal(a.RetryJoin, retryJoin) - require.Equal(a.StartJoin, startJoin) - require.Equal(a.RetryMaxAttempts, retryMaxAttempts) - require.Equal(a.RetryInterval, retryInterval) + result := a.Merge(b) + require.Equal(result.RetryJoin, retryJoin) + require.Equal(result.StartJoin, startJoin) + require.Equal(result.RetryMaxAttempts, retryMaxAttempts) + require.Equal(result.RetryInterval, retryInterval) + } + { + retryJoin := []string{"127.0.0.1", "127.0.0.2"} + startJoin := []string{"127.0.0.1", "127.0.0.2"} + retryMaxAttempts := 1 + retryInterval := time.Duration(0) + + var a *ServerJoin + b := &ServerJoin{ + RetryJoin: retryJoin, + StartJoin: startJoin, + RetryMaxAttempts: retryMaxAttempts, + RetryInterval: time.Duration(retryInterval), + } + + result := a.Merge(b) + require.Equal(result.RetryJoin, retryJoin) + require.Equal(result.StartJoin, startJoin) + require.Equal(result.RetryMaxAttempts, retryMaxAttempts) + require.Equal(result.RetryInterval, retryInterval) + } + { + retryJoin := []string{"127.0.0.1", "127.0.0.2"} + startJoin := []string{"127.0.0.1", "127.0.0.2"} + retryMaxAttempts := 1 + retryInterval := time.Duration(0) + + a := &ServerJoin{ + RetryJoin: retryJoin, + StartJoin: startJoin, + RetryMaxAttempts: retryMaxAttempts, + RetryInterval: time.Duration(retryInterval), + } + var b *ServerJoin + + result := a.Merge(b) + require.Equal(result.RetryJoin, retryJoin) + require.Equal(result.StartJoin, startJoin) + require.Equal(result.RetryMaxAttempts, retryMaxAttempts) + require.Equal(result.RetryInterval, retryInterval) } { retryJoin := []string{"127.0.0.1", "127.0.0.2"} @@ -967,10 +1007,10 @@ func TestMergeServerJoin(t *testing.T) { RetryInterval: time.Duration(retryInterval), } - a.Merge(b) - require.Equal(a.RetryJoin, retryJoin) - require.Equal(a.StartJoin, startJoin) - require.Equal(a.RetryMaxAttempts, retryMaxAttempts) - require.Equal(a.RetryInterval, retryInterval) + result := a.Merge(b) + require.Equal(result.RetryJoin, retryJoin) + require.Equal(result.StartJoin, startJoin) + require.Equal(result.RetryMaxAttempts, retryMaxAttempts) + require.Equal(result.RetryInterval, retryInterval) } }