Skip to content

Commit

Permalink
Check version compatibility with local agent
Browse files Browse the repository at this point in the history
Compatibility with agent is necessary because the ESM client makes
health check update using txn request to the local agent first, which
then is forwarded to the servers.
  • Loading branch information
findkim committed May 18, 2020
1 parent 4f6d875 commit 240c700
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
31 changes: 23 additions & 8 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,35 +390,50 @@ func (a *Agent) watchHealthChecks(nodeListCh chan map[string]bool) {
}
}

// VerifyConsulCompatibility queries Consul for all server versions to verify
// VerifyConsulCompatibility queries Consul for local agent and all server versions to verify
// compatibility with ESM.
func (a *Agent) VerifyConsulCompatibility() error {
if a.client == nil {
return fmt.Errorf("unable to check version compatibility without Consul client initialized")
}

VERIFYCONSULCOMPAT:
// Fetch local agent version
agentInfo, err := a.client.Agent().Self()
if err != nil {
// ESM blocks in NewAgent() until agent is available. At this point
// /agent/self endpoint should be available and an error would not be useful
// to retry the request.
return fmt.Errorf("unable to check version compatibility with Consul agent: %s", err)
}
agentVersionRaw, ok := agentInfo["Config"]["Version"]
if !ok {
return fmt.Errorf("unable to check version compatibility with Consul agent")
}
agentVersion := agentVersionRaw.(string)

VERIFYCONSULSERVER:
select {
case <-a.shutdownCh:
return nil
default:
}

// Fetch server versions
resp, err := a.client.Operator().AutopilotServerHealth(nil)
if err != nil {
if strings.Contains(err.Error(), "429") {
// 429 is a warning that something is unhealthy. This may occur when ESM
// races with Consul servers first starting up.
// races with Consul servers first starting up, so this is safe to retry.
a.logger.Printf("[ERR] Failed to query for Consul server versions (will retry): %v", err)
time.Sleep(retryTime)
goto VERIFYCONSULCOMPAT
goto VERIFYCONSULSERVER
}

return fmt.Errorf("unable to check the version compatibility with Consul: %s", err)
return fmt.Errorf("unable to check version compatibility with Consul servers: %s", err)
}

var versions []string
uniqueVersions := make(map[string]bool)
versions := []string{agentVersion}
uniqueVersions := map[string]bool{agentVersion: true}
for _, s := range resp.Servers {
if !uniqueVersions[s.Version] {
uniqueVersions[s.Version] = true
Expand All @@ -432,6 +447,6 @@ VERIFYCONSULCOMPAT:
return err
}

a.logger.Printf("[DEBUG] All Consul servers are running compatible versions with ESM")
a.logger.Printf("[DEBUG] Consul agent and all servers are running compatible versions with ESM")
return nil
}
7 changes: 3 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,9 @@ func NewConsulVersionError(consulVersions []string) error {
}

const versionErrorTmpl = `
Consul ESM version %s is incompatible with the running versions of your
Consul servers (%s), please refer to the Consul documentation to safely
upgrade the servers or change to a version of ESM that is compatible with your
servers.
Consul ESM version %s is incompatible with the running versions of the local
Consul agent or Consul servers (%s), please refer to the documentation
to safely upgrade Consul or change to a version of ESM that is compatible.
https://www.consul.io/docs/upgrading.html
`

0 comments on commit 240c700

Please sign in to comment.