Rainbond/node/nodem/node_manager.go

236 lines
6.1 KiB
Go
Raw Normal View History

2018-07-13 10:11:10 +08:00
// RAINBOND, Application Management Platform
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package nodem
import (
"context"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/Sirupsen/logrus"
2018-07-16 11:56:48 +08:00
"github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/version"
2018-07-13 10:11:10 +08:00
"github.com/goodrain/rainbond/cmd/node/option"
2018-07-16 11:56:48 +08:00
"github.com/goodrain/rainbond/node/api"
2018-07-13 10:11:10 +08:00
"github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/node/nodem/controller"
"github.com/goodrain/rainbond/node/nodem/healthy"
"github.com/goodrain/rainbond/node/nodem/info"
"github.com/goodrain/rainbond/node/nodem/monitor"
"github.com/goodrain/rainbond/node/nodem/taskrun"
"github.com/goodrain/rainbond/util"
)
//NodeManager node manager
type NodeManager struct {
client.HostNode
ctx context.Context
cancel context.CancelFunc
cluster client.ClusterClient
2018-07-13 10:30:06 +08:00
monitor monitor.Manager
healthy healthy.Manager
controller controller.Manager
taskrun taskrun.Manager
2018-07-13 10:11:10 +08:00
cfg *option.Conf
2018-07-16 11:56:48 +08:00
apim *api.Manager
2018-07-13 10:11:10 +08:00
}
//NewNodeManager new a node manager
2018-07-16 11:56:48 +08:00
func NewNodeManager(conf *option.Conf) (*NodeManager, error) {
etcdcli, err := clientv3.New(conf.Etcd)
if err != nil {
return nil, err
}
taskrun, err := taskrun.Newmanager(conf, etcdcli)
if err != nil {
return nil, err
}
cluster := client.NewClusterClient(conf, etcdcli)
monitor, err := monitor.CreateManager(conf)
if err != nil {
return nil, err
}
2018-07-13 10:30:06 +08:00
ctx, cancel := context.WithCancel(context.Background())
nodem := &NodeManager{
2018-07-16 11:56:48 +08:00
cfg: conf,
ctx: ctx,
cancel: cancel,
taskrun: taskrun,
cluster: cluster,
monitor: monitor,
}
return nodem, nil
}
//AddAPIManager AddApiManager
func (n *NodeManager) AddAPIManager(apim *api.Manager) error {
n.apim = apim
return n.monitor.SetAPIRoute(apim)
2018-07-13 10:11:10 +08:00
}
//Start start
2018-07-13 17:58:06 +08:00
func (n *NodeManager) Start(errchan chan error) error {
2018-07-13 10:11:10 +08:00
if err := n.init(); err != nil {
2018-07-13 17:58:06 +08:00
return err
2018-07-13 10:11:10 +08:00
}
2018-07-16 11:56:48 +08:00
// if err := n.controller.Start(); err != nil {
// return fmt.Errorf("start node controller error,%s", err.Error())
// }
// services, err := n.controller.GetAllService()
// if err != nil {
// return fmt.Errorf("get all services error,%s", err.Error())
// }
// if err := n.healthy.AddServices(services); err != nil {
// return fmt.Errorf("get all services error,%s", err.Error())
// }
// if err := n.healthy.Start(); err != nil {
// return fmt.Errorf("node healty start error,%s", err.Error())
// }
2018-07-13 10:30:06 +08:00
go n.monitor.Start(errchan)
go n.taskrun.Start(errchan)
2018-07-13 17:58:06 +08:00
go n.heartbeat()
return nil
2018-07-13 10:30:06 +08:00
}
2018-07-13 10:11:10 +08:00
2018-07-13 10:30:06 +08:00
//Stop Stop
func (n *NodeManager) Stop() {
n.cancel()
2018-07-16 11:56:48 +08:00
n.cluster.DownNode(&n.HostNode)
2018-07-13 10:30:06 +08:00
if n.taskrun != nil {
n.taskrun.Stop()
}
if n.controller != nil {
n.controller.Stop()
}
if n.monitor != nil {
n.monitor.Stop()
}
if n.healthy != nil {
n.healthy.Stop()
}
2018-07-13 10:11:10 +08:00
}
//checkNodeHealthy check current node healthy.
//only healthy can controller other service start
func (n *NodeManager) checkNodeHealthy() error {
return nil
}
func (n *NodeManager) heartbeat() {
util.Exec(n.ctx, func() error {
if err := n.cluster.UpdateStatus(&n.HostNode); err != nil {
logrus.Errorf("update node status error %s", err.Error())
}
return nil
}, time.Second*time.Duration(n.cfg.TTL))
}
//init node init
func (n *NodeManager) init() error {
uid, err := util.ReadHostID(n.cfg.HostIDFile)
if err != nil {
return fmt.Errorf("Get host id error:%s", err.Error())
}
node, err := n.cluster.GetNode(uid)
if err != nil {
return err
}
if node == nil {
node, err = n.getCurrentNode(uid)
if err != nil {
return err
}
}
node.NodeStatus.NodeInfo = info.GetSystemInfo()
node.Role = strings.Split(n.cfg.NodeRule, ",")
if node.Labels == nil || len(node.Labels) < 1 {
node.Labels = map[string]string{}
}
for _, rule := range node.Role {
node.Labels["rainbond_node_rule_"+rule] = "true"
}
if node.HostName == "" {
hostname, _ := os.Hostname()
node.HostName = hostname
}
if node.ClusterNode.PID == "" {
node.ClusterNode.PID = strconv.Itoa(os.Getpid())
}
node.Labels["rainbond_node_hostname"] = node.HostName
node.Labels["rainbond_node_ip"] = node.InternalIP
node.UpdataCondition(client.NodeCondition{
Type: client.NodeInit,
Status: client.ConditionTrue,
LastHeartbeatTime: time.Now(),
LastTransitionTime: time.Now(),
})
node.Mode = n.cfg.RunMode
n.HostNode = *node
if node.AvailableMemory == 0 {
node.AvailableMemory = int64(node.NodeStatus.NodeInfo.MemorySize)
}
if node.AvailableCPU == 0 {
2018-07-13 17:58:06 +08:00
node.AvailableCPU = int64(runtime.NumCPU())
2018-07-13 10:11:10 +08:00
}
2018-07-16 11:56:48 +08:00
node.Version = version.Version
2018-07-13 10:11:10 +08:00
return nil
}
//UpdateNodeStatus UpdateNodeStatus
func (n *NodeManager) UpdateNodeStatus() error {
return n.cluster.UpdateStatus(&n.HostNode)
}
//getCurrentNode get current node info
func (n *NodeManager) getCurrentNode(uid string) (*client.HostNode, error) {
if n.cfg.HostIP == "" {
ip, err := util.LocalIP()
if err != nil {
return nil, err
}
n.cfg.HostIP = ip.String()
}
node := CreateNode(uid, n.cfg.HostIP)
return &node, nil
}
2018-07-13 17:58:06 +08:00
//GetCurrentNode get current node
func (n *NodeManager) GetCurrentNode() *client.HostNode {
return &n.HostNode
}
2018-07-13 10:11:10 +08:00
//CreateNode new node
func CreateNode(nodeID, ip string) client.HostNode {
HostNode := client.HostNode{
ID: nodeID,
ClusterNode: client.ClusterNode{
PID: strconv.Itoa(os.Getpid()),
},
InternalIP: ip,
ExternalIP: ip,
CreateTime: time.Now(),
NodeStatus: &client.NodeStatus{},
}
return HostNode
}