-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
consul: correctly interpret missing consul checks as unhealthy #15822
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dcc19d4
consul: correctly understand missing consul checks as unhealthy
shoenig d103467
consul: minor CR refactor using maps not sets
shoenig e16c489
consul: observe transition from healthy to unhealthy checks
shoenig 2be222b
consul: spell healthy correctly
shoenig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:bug | ||
consul: correctly interpret missing consul checks as unhealthy | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
|
||
"github.com/hashicorp/consul/api" | ||
"github.com/hashicorp/go-hclog" | ||
"github.com/hashicorp/go-set" | ||
"github.com/hashicorp/nomad/client/serviceregistration" | ||
"github.com/hashicorp/nomad/client/serviceregistration/checks/checkstore" | ||
cstructs "github.com/hashicorp/nomad/client/structs" | ||
|
@@ -257,10 +258,9 @@ func (t *Tracker) setTaskHealth(healthy, terminal bool) { | |
// setCheckHealth is used to mark the checks as either healthy or unhealthy. | ||
// returns true if health is propagated and no more health monitoring is needed | ||
// | ||
// todo: this is currently being shared by watchConsulEvents and watchNomadEvents, | ||
// | ||
// and must be split up if/when we support registering services (and thus checks) | ||
// of different providers. | ||
// todo: this is currently being shared by watchConsulEvents and watchNomadEvents | ||
// and must be split up if/when we support registering services (and thus checks) | ||
// of different providers. | ||
func (t *Tracker) setCheckHealth(healthy bool) bool { | ||
t.lock.Lock() | ||
defer t.lock.Unlock() | ||
|
@@ -437,6 +437,7 @@ func (h *healthyFuture) C() <-chan time.Time { | |
// | ||
// Does not watch Nomad service checks; see watchNomadEvents for those. | ||
func (t *Tracker) watchConsulEvents() { | ||
|
||
// checkTicker is the ticker that triggers us to look at the checks in Consul | ||
checkTicker := time.NewTicker(t.checkLookupInterval) | ||
defer checkTicker.Stop() | ||
|
@@ -502,30 +503,9 @@ OUTER: | |
// Detect if all the checks are passing | ||
passed := true | ||
|
||
CHECKS: | ||
for _, treg := range allocReg.Tasks { | ||
for _, sreg := range treg.Services { | ||
for _, check := range sreg.Checks { | ||
onUpdate := sreg.CheckOnUpdate[check.CheckID] | ||
switch check.Status { | ||
case api.HealthPassing: | ||
continue | ||
case api.HealthWarning: | ||
if onUpdate == structs.OnUpdateIgnoreWarn || onUpdate == structs.OnUpdateIgnore { | ||
continue | ||
} | ||
case api.HealthCritical: | ||
if onUpdate == structs.OnUpdateIgnore { | ||
continue | ||
} | ||
default: | ||
} | ||
|
||
passed = false | ||
t.setCheckHealth(false) | ||
break CHECKS | ||
} | ||
} | ||
// scan for missing or unhealthy consul checks | ||
if !evaluateConsulChecks(t.tg, allocReg) { | ||
passed = false | ||
} | ||
|
||
if !passed { | ||
|
@@ -543,6 +523,59 @@ OUTER: | |
} | ||
} | ||
|
||
func evaluateConsulChecks(tg *structs.TaskGroup, registrations *serviceregistration.AllocRegistration) bool { | ||
// First, identify any case where a check definition is missing or outdated | ||
// on the Consul side. Note that because check names are not unique, we must | ||
// also keep track of the counts on each side and make sure those also match. | ||
services := tg.ConsulServices() | ||
expChecks := set.New[string](10) | ||
expCount := 0 | ||
regChecks := set.New[string](10) | ||
regCount := 0 | ||
for _, service := range services { | ||
for _, check := range service.Checks { | ||
expChecks.Insert(check.Name) | ||
expCount++ | ||
} | ||
} | ||
for _, task := range registrations.Tasks { | ||
for _, service := range task.Services { | ||
for _, check := range service.Checks { | ||
regChecks.Insert(check.Name) | ||
regCount++ | ||
} | ||
} | ||
} | ||
if expCount != regCount { | ||
return false | ||
} | ||
if !expChecks.Equal(regChecks) { | ||
return false | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit-picking, but do we need a set and counter here? I think using a |
||
|
||
// Now we can simply scan the status of each Check reported by Consul. | ||
for _, task := range registrations.Tasks { | ||
for _, service := range task.Services { | ||
for _, check := range service.Checks { | ||
onUpdate := service.CheckOnUpdate[check.CheckID] | ||
switch check.Status { | ||
case api.HealthWarning: | ||
if onUpdate != structs.OnUpdateIgnoreWarn && onUpdate != structs.OnUpdateIgnore { | ||
return false | ||
} | ||
case api.HealthCritical: | ||
if onUpdate != structs.OnUpdateIgnore { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// All checks are present and healthy. | ||
return true | ||
} | ||
|
||
// watchNomadEvents is a watcher for the health of the allocation's Nomad checks. | ||
// If all checks report healthy the watcher will exit after the MinHealthyTime has | ||
// been reached, otherwise the watcher will continue to check unhealthy checks until | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it meaningful that
t.setCheckHealth(false)
is no longer called alongside this bool being set? I see that it is called elsewhere, but haven't chased down the potential implications of its removal here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good catch, and I believe this is important in the edge case where
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this back in, and also added
TestTracker_ConsulChecks_HealthToUnhealthy
to cover the case where health checks transition from healthy to unhealthy during the minimum health period.