Merge branch 'V3.7' of https://github.com/goodrain/rainbond into V3.7

This commit is contained in:
barnettZQG 2018-07-27 14:55:41 +08:00
commit 1cedf41fec
5 changed files with 114 additions and 82 deletions

View File

@ -26,6 +26,8 @@ import (
"time"
"github.com/goodrain/rainbond/node/nodem/client"
"sync"
"github.com/goodrain/rainbond/node/nodem/healthy/probe"
"errors"
)
//Manager Manager
@ -42,6 +44,7 @@ type Watcher interface {
Watch() <-chan *service.HealthStatus
Close() error
}
type watcher struct {
manager Manager
statusChan chan *service.HealthStatus
@ -93,44 +96,41 @@ func (p *probeManager) Start(hostNode *client.HostNode) (error) {
logrus.Info("health mode start")
go p.HandleStatus()
logrus.Info("services length===>",len(p.services))
for _,v :=range p.services{
logrus.Info("Need to check===>",v.ServiceHealth.Name,v.ServiceHealth.Model)
}
logrus.Info("services length===>", len(p.services))
for _, v := range p.services {
if v.ServiceHealth.Model == "http" {
h := &HttpProbe{
name: v.ServiceHealth.Name,
address: v.ServiceHealth.Address,
ctx: p.ctx,
cancel: p.cancel,
resultsChan: p.statusChan,
h := &probe.HttpProbe{
Name: v.ServiceHealth.Name,
Address: v.ServiceHealth.Address,
Ctx: p.ctx,
Cancel: p.cancel,
ResultsChan: p.statusChan,
TimeInterval: v.ServiceHealth.TimeInterval,
hostNode: hostNode,
HostNode: hostNode,
}
go h.Check()
go h.HttpCheck()
}
if v.ServiceHealth.Model == "tcp" {
t := &TcpProbe{
name: v.ServiceHealth.Name,
address: v.ServiceHealth.Address,
ctx: p.ctx,
cancel: p.cancel,
resultsChan: p.statusChan,
t := &probe.TcpProbe{
Name: v.ServiceHealth.Name,
Address: v.ServiceHealth.Address,
Ctx: p.ctx,
Cancel: p.cancel,
ResultsChan: p.statusChan,
TimeInterval: v.ServiceHealth.TimeInterval,
hostNode: hostNode,
HostNode: hostNode,
}
go t.TcpCheck()
}
if v.ServiceHealth.Model == "cmd" {
s := &ShellProbe{
name: v.ServiceHealth.Name,
address: v.ServiceHealth.Address,
ctx: p.ctx,
cancel: p.cancel,
resultsChan: p.statusChan,
s := &probe.ShellProbe{
Name: v.ServiceHealth.Name,
Address: v.ServiceHealth.Address,
Ctx: p.ctx,
Cancel: p.cancel,
ResultsChan: p.statusChan,
TimeInterval: v.ServiceHealth.TimeInterval,
hostNode: hostNode,
HostNode: hostNode,
}
go s.ShellCheck()
}
@ -219,3 +219,43 @@ func (p *probeManager) WatchServiceHealthy(serviceName string) Watcher {
}
return w
}
func (p *probeManager) GetCurrentServiceHealthy(serviceName string) (*service.HealthStatus, error) {
for _, v := range p.services {
if v.Name == serviceName {
if v.ServiceHealth.Model == "http" {
statusMap := probe.GetHttpHealth(v.ServiceHealth.Address)
result := &service.HealthStatus{
Name: v.Name,
Status: statusMap["status"],
Info: statusMap["info"],
}
return result, nil
}
if v.ServiceHealth.Model == "tcp" {
statusMap := probe.GetTcpHealth(v.ServiceHealth.Address)
result := &service.HealthStatus{
Name: v.Name,
Status: statusMap["status"],
Info: statusMap["info"],
}
return result, nil
}
if v.ServiceHealth.Model == "cmd" {
statusMap := probe.GetShellHealth(v.ServiceHealth.Address)
result := &service.HealthStatus{
Name: v.Name,
Status: statusMap["status"],
Info: statusMap["info"],
}
return result, nil
}
} else {
return nil, errors.New("the service does not exist")
}
}
return nil, errors.New("service list is empty")
}

View File

@ -1,4 +1,4 @@
package healthy
package probe
import (
"context"
@ -9,49 +9,47 @@ import (
"github.com/goodrain/rainbond/node/nodem/client"
)
type Probe interface {
Check()
}
type HttpProbe struct {
name string
address string
resultsChan chan *service.HealthStatus
ctx context.Context
cancel context.CancelFunc
Name string
Address string
ResultsChan chan *service.HealthStatus
Ctx context.Context
Cancel context.CancelFunc
TimeInterval int
hostNode *client.HostNode
HostNode *client.HostNode
}
func (h *HttpProbe) Check() {
func (h *HttpProbe) HttpCheck() {
util.Exec(h.ctx, func() error {
HealthMap := GetHttpHealth(h.address)
util.Exec(h.Ctx, func() error {
HealthMap := GetHttpHealth(h.Address)
result := &service.HealthStatus{
Name: h.name,
Name: h.Name,
Status: HealthMap["status"],
Info: HealthMap["info"],
}
if HealthMap["status"] != service.Stat_healthy {
v := client.NodeCondition{
Type: client.NodeConditionType(h.name),
Type: client.NodeConditionType(h.Name),
Status: client.ConditionFalse,
LastHeartbeatTime: time.Now(),
LastTransitionTime: time.Now(),
Message: result.Info,
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
if HealthMap["status"] == service.Stat_healthy {
v := client.NodeCondition{
Type: client.NodeConditionType(h.name),
Type: client.NodeConditionType(h.Name),
Status: client.ConditionTrue,
LastHeartbeatTime: time.Now(),
LastTransitionTime: time.Now(),
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
h.resultsChan <- result
h.ResultsChan <- result
return nil
}, time.Second*time.Duration(h.TimeInterval))

View File

@ -1,4 +1,4 @@
package healthy
package probe
//
//import (
// "testing"

View File

@ -1,4 +1,4 @@
package healthy
package probe
import (
"context"
@ -11,49 +11,46 @@ import (
"github.com/goodrain/rainbond/node/nodem/client"
)
type SHELLProbe interface {
Check()
}
type ShellProbe struct {
name string
address string
resultsChan chan *service.HealthStatus
ctx context.Context
cancel context.CancelFunc
Name string
Address string
ResultsChan chan *service.HealthStatus
Ctx context.Context
Cancel context.CancelFunc
TimeInterval int
hostNode *client.HostNode
HostNode *client.HostNode
}
func (h *ShellProbe) ShellCheck() {
util.Exec(h.ctx, func() error {
HealthMap := GetShellHealth(h.address)
util.Exec(h.Ctx, func() error {
HealthMap := GetShellHealth(h.Address)
result := &service.HealthStatus{
Name: h.name,
Name: h.Name,
Status: HealthMap["status"],
Info: HealthMap["info"],
}
if HealthMap["status"] != service.Stat_healthy {
v := client.NodeCondition{
Type: client.NodeConditionType(h.name),
Type: client.NodeConditionType(h.Name),
Status: client.ConditionFalse,
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
Message: result.Info,
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
if HealthMap["status"] == service.Stat_healthy{
v := client.NodeCondition{
Type:client.NodeConditionType(h.name),
Type:client.NodeConditionType(h.Name),
Status:client.ConditionTrue,
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
h.resultsChan <- result
h.ResultsChan <- result
return nil
}, time.Second*time.Duration(h.TimeInterval))

View File

@ -1,4 +1,4 @@
package healthy
package probe
import (
"context"
@ -9,49 +9,46 @@ import (
"github.com/goodrain/rainbond/node/nodem/client"
)
type TCPProbe interface {
TcpCheck()
}
type TcpProbe struct {
name string
address string
resultsChan chan *service.HealthStatus
ctx context.Context
cancel context.CancelFunc
Name string
Address string
ResultsChan chan *service.HealthStatus
Ctx context.Context
Cancel context.CancelFunc
TimeInterval int
hostNode *client.HostNode
HostNode *client.HostNode
}
func (h *TcpProbe) TcpCheck() {
util.Exec(h.ctx, func() error {
HealthMap := GetTcpHealth(h.address)
util.Exec(h.Ctx, func() error {
HealthMap := GetTcpHealth(h.Address)
result := &service.HealthStatus{
Name: h.name,
Name: h.Name,
Status: HealthMap["status"],
Info: HealthMap["info"],
}
if HealthMap["status"] != service.Stat_healthy {
v := client.NodeCondition{
Type: client.NodeConditionType(h.name),
Type: client.NodeConditionType(h.Name),
Status: client.ConditionFalse,
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
Message: result.Info,
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
if HealthMap["status"] == service.Stat_healthy{
v := client.NodeCondition{
Type:client.NodeConditionType(h.name),
Type:client.NodeConditionType(h.Name),
Status:client.ConditionTrue,
LastHeartbeatTime:time.Now(),
LastTransitionTime:time.Now(),
}
h.hostNode.UpdataCondition(v)
h.HostNode.UpdataCondition(v)
}
h.resultsChan <- result
h.ResultsChan <- result
return nil
}, time.Second*time.Duration(h.TimeInterval))
}