Add formatting to autopilot init messages

This commit is contained in:
Kyle Havlovitz 2017-04-12 18:38:36 -07:00
parent e467da5d3e
commit 196dd81cc5
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
4 changed files with 79 additions and 9 deletions

View File

@ -49,7 +49,7 @@ func (s *Server) autopilotLoop() {
case <-s.autopilotShutdownCh:
return
case <-ticker.C:
autopilotConfig, ok := s.getAutopilotConfig()
autopilotConfig, ok := s.getOrCreateAutopilotConfig()
if !ok {
continue
}
@ -62,7 +62,7 @@ func (s *Server) autopilotLoop() {
s.logger.Printf("[ERR] autopilot: error checking for dead servers to remove: %s", err)
}
case <-s.autopilotRemoveDeadCh:
autopilotConfig, ok := s.getAutopilotConfig()
autopilotConfig, ok := s.getOrCreateAutopilotConfig()
if !ok {
continue
}

View File

@ -154,7 +154,7 @@ func (s *Server) establishLeadership() error {
}
// Setup autopilot config if we need to
s.getAutopilotConfig()
s.getOrCreateAutopilotConfig()
s.startAutopilot()
@ -246,12 +246,12 @@ func (s *Server) initializeACL() error {
return nil
}
// getAutopilotConfig is used to get the autopilot config, initializing it if necessary
func (s *Server) getAutopilotConfig() (*structs.AutopilotConfig, bool) {
// getOrCreateAutopilotConfig is used to get the autopilot config, initializing it if necessary
func (s *Server) getOrCreateAutopilotConfig() (*structs.AutopilotConfig, bool) {
state := s.fsm.State()
_, config, err := state.AutopilotConfig()
if err != nil {
s.logger.Printf("failed to get autopilot config: %v", err)
s.logger.Printf("[ERR] autopilot: failed to get config: %v", err)
return nil, false
}
if config != nil {
@ -259,14 +259,14 @@ func (s *Server) getAutopilotConfig() (*structs.AutopilotConfig, bool) {
}
if !ServersMeetMinimumVersion(s.LANMembers(), minAutopilotVersion) {
s.logger.Printf("can't initialize autopilot until all servers are >= %s", minAutopilotVersion.String())
s.logger.Printf("[ERR] autopilot: can't initialize until all servers are >= %s", minAutopilotVersion.String())
return nil, false
}
config = s.config.AutopilotConfig
req := structs.AutopilotSetConfigRequest{Config: *config}
if _, err = s.raftApply(structs.AutopilotRequestType, req); err != nil {
s.logger.Printf("failed to initialize autopilot config")
s.logger.Printf("[ERR] autopilot: failed to initialize config: %v", err)
return nil, false
}

View File

@ -310,4 +310,4 @@ func ServersMeetMinimumVersion(members []serf.Member, minVersion *version.Versio
}
return true
}
}

View File

@ -7,6 +7,7 @@ import (
"regexp"
"testing"
"github.com/hashicorp/go-version"
"github.com/hashicorp/serf/serf"
)
@ -325,3 +326,72 @@ func TestGetPublicIPv6(t *testing.T) {
}
}
}
func TestServersMeetMinimumVersion(t *testing.T) {
makeMember := func(version string) serf.Member {
return serf.Member{
Name: "foo",
Addr: net.IP([]byte{127, 0, 0, 1}),
Tags: map[string]string{
"role": "consul",
"id": "asdf",
"dc": "east-aws",
"port": "10000",
"build": version,
"wan_join_port": "1234",
"vsn": "1",
"expect": "3",
"raft_vsn": "3",
},
Status: serf.StatusAlive,
}
}
cases := []struct {
members []serf.Member
ver *version.Version
expected bool
}{
// One server, meets reqs
{
members: []serf.Member{
makeMember("0.7.5"),
},
ver: version.Must(version.NewVersion("0.7.5")),
expected: true,
},
// One server, doesn't meet reqs
{
members: []serf.Member{
makeMember("0.7.5"),
},
ver: version.Must(version.NewVersion("0.8.0")),
expected: false,
},
// Multiple servers, meets req version
{
members: []serf.Member{
makeMember("0.7.5"),
makeMember("0.8.0"),
},
ver: version.Must(version.NewVersion("0.7.5")),
expected: true,
},
// Multiple servers, doesn't meet req version
{
members: []serf.Member{
makeMember("0.7.5"),
makeMember("0.8.0"),
},
ver: version.Must(version.NewVersion("0.8.0")),
expected: false,
},
}
for _, tc := range cases {
result := ServersMeetMinimumVersion(tc.members, tc.ver)
if result != tc.expected {
t.Fatalf("bad: %v, %v, %v", result, tc.ver.String(), tc)
}
}
}