2017-11-09 17:32:02 +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/>.
|
|
|
|
|
2017-11-13 21:54:11 +08:00
|
|
|
// 当前包处理集群节点的管理
|
2017-11-09 17:32:02 +08:00
|
|
|
package masterserver
|
|
|
|
|
|
|
|
import (
|
2017-11-13 21:54:11 +08:00
|
|
|
"bytes"
|
2017-11-09 17:32:02 +08:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
|
|
|
|
|
|
|
"github.com/coreos/etcd/mvcc/mvccpb"
|
|
|
|
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/pkg/api/v1"
|
|
|
|
|
|
|
|
client "github.com/coreos/etcd/clientv3"
|
|
|
|
"github.com/goodrain/rainbond/cmd/node/option"
|
|
|
|
"github.com/goodrain/rainbond/pkg/node/api/model"
|
|
|
|
"github.com/goodrain/rainbond/pkg/node/core/store"
|
2017-11-13 21:54:11 +08:00
|
|
|
"github.com/goodrain/rainbond/pkg/util"
|
2017-11-09 17:32:02 +08:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
|
|
|
)
|
|
|
|
|
|
|
|
//NodeCluster 节点管理器
|
|
|
|
type NodeCluster struct {
|
2017-11-13 21:54:11 +08:00
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
nodes map[string]*model.HostNode
|
|
|
|
lock sync.Mutex
|
|
|
|
client *store.Client
|
|
|
|
k8sClient *kubernetes.Clientset
|
2017-12-01 09:51:22 +08:00
|
|
|
currentNode *model.HostNode
|
2017-11-13 21:54:11 +08:00
|
|
|
checkInstall chan *model.HostNode
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//CreateNodeCluster 创建节点管理器
|
2017-12-01 09:51:22 +08:00
|
|
|
func CreateNodeCluster(k8sClient *kubernetes.Clientset, node *model.HostNode) (*NodeCluster, error) {
|
2017-11-09 17:32:02 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
nc := NodeCluster{
|
2017-11-13 21:54:11 +08:00
|
|
|
ctx: ctx,
|
|
|
|
cancel: cancel,
|
|
|
|
nodes: make(map[string]*model.HostNode, 5),
|
|
|
|
client: store.DefalutClient,
|
|
|
|
k8sClient: k8sClient,
|
2017-11-27 14:02:18 +08:00
|
|
|
currentNode: node,
|
2017-11-13 21:54:11 +08:00
|
|
|
checkInstall: make(chan *model.HostNode, 4),
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
|
|
|
if err := nc.loadNodes(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &nc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Start 启动
|
|
|
|
func (n *NodeCluster) Start() {
|
|
|
|
go n.watchNodes()
|
|
|
|
go n.watchK8sNodes()
|
2017-11-13 21:54:11 +08:00
|
|
|
go n.worker()
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Stop 停止
|
|
|
|
func (n *NodeCluster) Stop(i interface{}) {
|
|
|
|
n.cancel()
|
|
|
|
}
|
|
|
|
func (n *NodeCluster) loadNodes() error {
|
|
|
|
//加载节点信息
|
|
|
|
res, err := n.client.Get(option.Config.NodePath, client.WithPrefix())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("load cluster nodes error:%s", err.Error())
|
|
|
|
}
|
|
|
|
for _, kv := range res.Kvs {
|
|
|
|
if node := n.getNodeFromKV(kv); node != nil {
|
2017-11-16 16:32:27 +08:00
|
|
|
n.CacheNode(node)
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//加载rainbond节点在线信息
|
|
|
|
res, err = n.client.Get(option.Config.OnlineNodePath, client.WithPrefix())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("load cluster nodes error:%s", err.Error())
|
|
|
|
}
|
|
|
|
for _, kv := range res.Kvs {
|
|
|
|
if node := n.getNodeFromKey(string(kv.Key)); node != nil {
|
|
|
|
if !node.Alived {
|
|
|
|
node.Alived = true
|
|
|
|
node.UpTime = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//加载k8s节点信息
|
2017-11-16 11:31:09 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
list, err := n.k8sClient.Core().Nodes().List(metav1.ListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("load k8s nodes from k8s api error:%s", err.Error())
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, node := range list.Items {
|
|
|
|
if cn, ok := n.nodes[node.Name]; ok {
|
|
|
|
cn.NodeStatus = &node.Status
|
|
|
|
cn.UpdataK8sCondition(node.Status.Conditions)
|
|
|
|
n.UpdateNode(cn)
|
|
|
|
} else {
|
|
|
|
logrus.Warningf("k8s node %s can not exist in rainbond cluster.", node.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
2017-11-16 11:31:09 +08:00
|
|
|
}()
|
2017-11-09 17:32:02 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-13 21:54:11 +08:00
|
|
|
func (n *NodeCluster) worker() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case newNode := <-n.checkInstall:
|
|
|
|
go n.checkNodeInstall(newNode)
|
|
|
|
//其他异步任务
|
|
|
|
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:32:02 +08:00
|
|
|
//UpdateNode 更新节点信息
|
|
|
|
func (n *NodeCluster) UpdateNode(node *model.HostNode) {
|
2017-11-16 11:31:09 +08:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
n.nodes[node.ID] = node
|
2017-11-09 17:32:02 +08:00
|
|
|
n.client.Put(option.Config.NodePath+"/"+node.ID, node.String())
|
|
|
|
}
|
|
|
|
func (n *NodeCluster) getNodeFromKV(kv *mvccpb.KeyValue) *model.HostNode {
|
|
|
|
var node model.HostNode
|
|
|
|
if err := ffjson.Unmarshal(kv.Value, &node); err != nil {
|
|
|
|
logrus.Error("parse node info error:", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &node
|
|
|
|
}
|
|
|
|
func (n *NodeCluster) getNodeFromKey(key string) *model.HostNode {
|
|
|
|
index := strings.LastIndex(key, "/")
|
|
|
|
if index < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
id := key[index+1:]
|
|
|
|
return n.GetNode(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetNode 从缓存获取节点信息
|
|
|
|
func (n *NodeCluster) GetNode(id string) *model.HostNode {
|
2017-11-16 11:31:09 +08:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
2017-11-09 17:32:02 +08:00
|
|
|
if node, ok := n.nodes[id]; ok {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (n *NodeCluster) watchNodes() {
|
|
|
|
ch := n.client.Watch(option.Config.NodePath, client.WithPrefix())
|
|
|
|
onlineCh := n.client.Watch(option.Config.OnlineNodePath, client.WithPrefix())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
return
|
|
|
|
case event := <-ch:
|
|
|
|
for _, ev := range event.Events {
|
|
|
|
switch {
|
|
|
|
case ev.IsCreate(), ev.IsModify():
|
|
|
|
if node := n.getNodeFromKV(ev.Kv); node != nil {
|
2017-11-16 16:32:27 +08:00
|
|
|
n.CacheNode(node)
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
|
|
|
case ev.Type == client.EventTypeDelete:
|
|
|
|
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
|
|
|
|
n.RemoveNode(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case event := <-onlineCh:
|
|
|
|
for _, ev := range event.Events {
|
|
|
|
switch {
|
|
|
|
case ev.IsCreate(), ev.IsModify():
|
|
|
|
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
|
|
|
|
node.Alived = true
|
|
|
|
node.UpTime = time.Now()
|
|
|
|
n.UpdateNode(node)
|
|
|
|
}
|
|
|
|
case ev.Type == client.EventTypeDelete:
|
|
|
|
if node := n.getNodeFromKey(string(ev.Kv.Key)); node != nil {
|
|
|
|
node.Alived = false
|
|
|
|
node.DownTime = time.Now()
|
|
|
|
n.UpdateNode(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *NodeCluster) watchK8sNodes() {
|
|
|
|
for {
|
|
|
|
wc, err := n.k8sClient.Core().Nodes().Watch(metav1.ListOptions{})
|
|
|
|
if err != nil {
|
2017-11-16 11:31:09 +08:00
|
|
|
logrus.Warningf("watch k8s node error.", err.Error())
|
2017-11-09 17:32:02 +08:00
|
|
|
time.Sleep(time.Second * 5)
|
2017-11-16 11:31:09 +08:00
|
|
|
continue
|
2017-11-09 17:32:02 +08:00
|
|
|
}
|
2017-11-16 11:31:09 +08:00
|
|
|
defer func() {
|
|
|
|
if wc != nil {
|
|
|
|
wc.Stop()
|
|
|
|
}
|
|
|
|
}()
|
2017-11-13 21:54:11 +08:00
|
|
|
loop:
|
2017-11-09 17:32:02 +08:00
|
|
|
for {
|
|
|
|
select {
|
2017-11-13 21:54:11 +08:00
|
|
|
case event, ok := <-wc.ResultChan():
|
|
|
|
if !ok {
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
break loop
|
|
|
|
}
|
2017-11-09 17:32:02 +08:00
|
|
|
switch {
|
|
|
|
case event.Type == watch.Added, event.Type == watch.Modified:
|
|
|
|
if node, ok := event.Object.(*v1.Node); ok {
|
2017-11-16 11:31:09 +08:00
|
|
|
//k8s node name is rainbond node id
|
2017-11-09 17:32:02 +08:00
|
|
|
if rbnode := n.GetNode(node.Name); rbnode != nil {
|
|
|
|
rbnode.NodeStatus = &node.Status
|
2017-11-23 15:36:32 +08:00
|
|
|
rbnode.NodeStatus.Images = nil
|
2017-11-09 17:32:02 +08:00
|
|
|
rbnode.UpdataK8sCondition(node.Status.Conditions)
|
2017-12-01 09:51:22 +08:00
|
|
|
if rbnode.AvailableCPU == 0 {
|
|
|
|
rbnode.AvailableCPU, _ = node.Status.Allocatable.Cpu().AsInt64()
|
|
|
|
}
|
|
|
|
if rbnode.AvailableMemory == 0 {
|
|
|
|
rbnode.AvailableMemory, _ = node.Status.Allocatable.Memory().AsInt64()
|
|
|
|
}
|
|
|
|
rbnode.Unschedulable = node.Spec.Unschedulable
|
2017-11-09 17:32:02 +08:00
|
|
|
n.UpdateNode(rbnode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case event.Type == watch.Deleted:
|
|
|
|
if node, ok := event.Object.(*v1.Node); ok {
|
|
|
|
if rbnode := n.GetNode(node.Name); rbnode != nil {
|
|
|
|
rbnode.NodeStatus = nil
|
|
|
|
n.UpdateNode(rbnode)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
logrus.Warning("don't know the kube api watch event type when watch node.")
|
|
|
|
}
|
|
|
|
case <-n.ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//InstallNode 安装节点
|
|
|
|
func (n *NodeCluster) InstallNode() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-13 21:54:11 +08:00
|
|
|
//CheckNodeInstall 简称节点是否安装 rainbond node
|
|
|
|
//如果未安装,尝试安装
|
|
|
|
func (n *NodeCluster) CheckNodeInstall(node *model.HostNode) {
|
|
|
|
n.checkInstall <- node
|
|
|
|
}
|
|
|
|
func (n *NodeCluster) checkNodeInstall(node *model.HostNode) {
|
|
|
|
initCondition := model.NodeCondition{
|
|
|
|
Type: model.NodeInit,
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
node.UpdataCondition(initCondition)
|
|
|
|
n.UpdateNode(node)
|
|
|
|
}()
|
2017-12-11 11:51:28 +08:00
|
|
|
node.Status="init"
|
2017-11-13 21:54:11 +08:00
|
|
|
errorCondition := func(reason string, err error) {
|
|
|
|
initCondition.Status = model.ConditionFalse
|
|
|
|
initCondition.LastTransitionTime = time.Now()
|
|
|
|
initCondition.LastHeartbeatTime = time.Now()
|
|
|
|
initCondition.Reason = reason
|
|
|
|
if err != nil {
|
|
|
|
initCondition.Message = err.Error()
|
|
|
|
}
|
2017-12-01 09:51:22 +08:00
|
|
|
node.Conditions = append(node.Conditions, initCondition)
|
2017-12-12 11:52:43 +08:00
|
|
|
node.Status="init_failed"
|
2017-11-13 21:54:11 +08:00
|
|
|
}
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
2017-12-11 11:51:28 +08:00
|
|
|
if node.Role==nil {
|
|
|
|
node.Role=[]string{"compute"}
|
|
|
|
}
|
2017-12-01 09:51:22 +08:00
|
|
|
role := node.Role[0]
|
2017-11-27 17:42:10 +08:00
|
|
|
|
2017-12-01 09:51:22 +08:00
|
|
|
etcd := n.currentNode.InternalIP
|
|
|
|
cmd := "bash -c \"set " + node.ID + " " + etcd + " " + role + ";$(curl -s repo.goodrain.com/gaops/jobs/install/prepare/init.sh)\""
|
|
|
|
logrus.Infof("init endpoint node cmd is %s", cmd)
|
2017-11-27 13:39:22 +08:00
|
|
|
client := util.NewSSHClient(node.InternalIP, "root", node.RootPass, cmd, 22, &stdout, &stderr)
|
2017-11-13 21:54:11 +08:00
|
|
|
if err := client.Connection(); err != nil {
|
|
|
|
logrus.Error("init endpoint node error:", err.Error())
|
|
|
|
errorCondition("SSH登陆初始化目标节点失败", err)
|
|
|
|
return
|
|
|
|
}
|
2017-11-28 15:35:29 +08:00
|
|
|
|
2017-11-13 21:54:11 +08:00
|
|
|
//TODO:
|
|
|
|
//处理安装结果
|
2017-11-27 17:06:17 +08:00
|
|
|
fmt.Println("初始化节点成功")
|
2017-11-13 21:54:11 +08:00
|
|
|
logrus.Info(stdout.String())
|
|
|
|
}
|
|
|
|
|
2017-11-09 17:32:02 +08:00
|
|
|
//GetAllNode 获取全部节点
|
|
|
|
func (n *NodeCluster) GetAllNode() (nodes []*model.HostNode) {
|
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
for _, node := range n.nodes {
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-16 16:32:27 +08:00
|
|
|
//CacheNode 添加节点到缓存
|
|
|
|
func (n *NodeCluster) CacheNode(node *model.HostNode) {
|
2017-11-09 17:32:02 +08:00
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
logrus.Debugf("add or update a rainbon node id:%s hostname:%s ip:%s", node.ID, node.HostName, node.InternalIP)
|
|
|
|
n.nodes[node.ID] = node
|
|
|
|
}
|
|
|
|
|
|
|
|
//RemoveNode 从缓存移除节点
|
|
|
|
func (n *NodeCluster) RemoveNode(node *model.HostNode) {
|
|
|
|
n.lock.Lock()
|
|
|
|
defer n.lock.Unlock()
|
|
|
|
if _, ok := n.nodes[node.ID]; ok {
|
|
|
|
delete(n.nodes, node.ID)
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 21:54:11 +08:00
|
|
|
|
2017-11-14 16:21:59 +08:00
|
|
|
//UpdateNodeCondition 更新节点状态
|
|
|
|
func (n *NodeCluster) UpdateNodeCondition(nodeID, ctype, cvalue string) {
|
|
|
|
node := n.GetNode(nodeID)
|
2017-11-14 18:57:56 +08:00
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-11-14 16:21:59 +08:00
|
|
|
node.UpdataCondition(model.NodeCondition{
|
|
|
|
Type: model.NodeConditionType(ctype),
|
|
|
|
Status: model.ConditionStatus(cvalue),
|
|
|
|
LastHeartbeatTime: time.Now(),
|
|
|
|
LastTransitionTime: time.Now(),
|
|
|
|
Message: "",
|
|
|
|
Reason: "",
|
|
|
|
})
|
2017-11-16 11:31:09 +08:00
|
|
|
n.UpdateNode(node)
|
2017-11-13 21:54:11 +08:00
|
|
|
}
|
2017-11-17 18:19:54 +08:00
|
|
|
|
|
|
|
//GetLabelsNode 返回匹配labels的节点ID
|
|
|
|
func (n *NodeCluster) GetLabelsNode(labels map[string]string) []string {
|
|
|
|
var nodes []string
|
|
|
|
for _, node := range n.nodes {
|
|
|
|
if checkLables(node, labels) {
|
|
|
|
nodes = append(nodes, node.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkLables(node *model.HostNode, labels map[string]string) bool {
|
|
|
|
for k, v := range labels {
|
|
|
|
if nodev := node.Labels[k]; nodev != v {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|