Merge pull request #115 from jelmersnoeck/jelmer/deregister-node-if-checks-fail
ConsulWatcher: deregister if service checks fail.
This commit is contained in:
commit
321358ae1f
@ -79,15 +79,7 @@ func (cw *consulWatcher) serviceHandler(idx uint64, data interface{}) {
|
|||||||
var del bool
|
var del bool
|
||||||
|
|
||||||
for _, check := range e.Checks {
|
for _, check := range e.Checks {
|
||||||
if check.ServiceName != serviceName {
|
// delete the node if the status is critical
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if check.ServiceID != id {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete the node
|
|
||||||
if check.Status == "critical" {
|
if check.Status == "critical" {
|
||||||
del = true
|
del = true
|
||||||
break
|
break
|
||||||
|
85
registry/consul_watcher_test.go
Normal file
85
registry/consul_watcher_test.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package registry
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/consul/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHealthyServiceHandler(t *testing.T) {
|
||||||
|
watcher := newWatcher()
|
||||||
|
serviceEntry := newServiceEntry(
|
||||||
|
"node-name", "node-address", "service-name", "v1.0.0",
|
||||||
|
[]*api.HealthCheck{
|
||||||
|
newHealthCheck("node-name", "service-name", "passing"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
|
||||||
|
|
||||||
|
if len(watcher.services["service-name"][0].Nodes) != 1 {
|
||||||
|
t.Errorf("Expected length of the service nodes to be 1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnhealthyServiceHandler(t *testing.T) {
|
||||||
|
watcher := newWatcher()
|
||||||
|
serviceEntry := newServiceEntry(
|
||||||
|
"node-name", "node-address", "service-name", "v1.0.0",
|
||||||
|
[]*api.HealthCheck{
|
||||||
|
newHealthCheck("node-name", "service-name", "critical"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
|
||||||
|
|
||||||
|
if len(watcher.services["service-name"][0].Nodes) != 0 {
|
||||||
|
t.Errorf("Expected length of the service nodes to be 0")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnhealthyNodeServiceHandler(t *testing.T) {
|
||||||
|
watcher := newWatcher()
|
||||||
|
serviceEntry := newServiceEntry(
|
||||||
|
"node-name", "node-address", "service-name", "v1.0.0",
|
||||||
|
[]*api.HealthCheck{
|
||||||
|
newHealthCheck("node-name", "service-name", "passing"),
|
||||||
|
newHealthCheck("node-name", "serfHealth", "critical"),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
watcher.serviceHandler(1234, []*api.ServiceEntry{serviceEntry})
|
||||||
|
|
||||||
|
if len(watcher.services["service-name"][0].Nodes) != 0 {
|
||||||
|
t.Errorf("Expected length of the service nodes to be 0")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newWatcher() *consulWatcher {
|
||||||
|
return &consulWatcher{
|
||||||
|
exit: make(chan bool),
|
||||||
|
next: make(chan *Result, 10),
|
||||||
|
services: make(map[string][]*Service),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newHealthCheck(node, name, status string) *api.HealthCheck {
|
||||||
|
return &api.HealthCheck{
|
||||||
|
Node: node,
|
||||||
|
Name: name,
|
||||||
|
Status: status,
|
||||||
|
ServiceName: name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newServiceEntry(node, address, name, version string, checks []*api.HealthCheck) *api.ServiceEntry {
|
||||||
|
return &api.ServiceEntry{
|
||||||
|
Node: &api.Node{Node: node, Address: name},
|
||||||
|
Service: &api.AgentService{
|
||||||
|
Service: name,
|
||||||
|
Address: address,
|
||||||
|
Tags: encodeVersion(version),
|
||||||
|
},
|
||||||
|
Checks: checks,
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user