[REV] update code style

This commit is contained in:
barnett 2018-04-09 23:08:51 +08:00
parent c4a607a941
commit 4f32a94da8
5 changed files with 93 additions and 190 deletions

View File

@ -46,13 +46,14 @@ type AppRuntimeSync struct {
}
//Start start if have master right
//start grpc server
func (a *AppRuntimeSync) Start() error {
a.srss.Start()
go a.startAppRuntimeSync()
return a.registServer()
}
//Stop 停止
//Stop stop app runtime sync server
func (a *AppRuntimeSync) Stop() error {
a.srss.Stop()
if a.keepalive != nil {

View File

@ -35,4 +35,4 @@ Client code demo:
}
*/
package appruntimesync
package appruntimesync //import

View File

@ -72,19 +72,19 @@ func (n *NodeService) AddNode(node *model.APIHostNode) *utils.APIHandleError {
if _, err := rbnode.Update(); err != nil {
return utils.CreateAPIHandleErrorFromDBError("save node", err)
}
//判断是否需要安装
//Determine if the node needs to be installed.
n.nodecluster.CheckNodeInstall(rbnode)
return nil
}
//DeleteNode 删除节点信息
//只有节点状态属于(离线状态)才能删除
//DeleteNode delete node
//only node status is offline and node can be deleted
func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError {
node := n.nodecluster.GetNode(nodeID)
if node.Alived {
return utils.CreateAPIHandleError(400, fmt.Errorf("node is online, can not delete"))
}
//TODO:计算节点,判断节点是否下线
//TODO:compute node check node is offline
if node.Role.HasRule(model.ComputeNode) {
if node.NodeStatus != nil {
return utils.CreateAPIHandleError(400, fmt.Errorf("node is k8s compute node, can not delete"))
@ -97,7 +97,7 @@ func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError {
return nil
}
//GetNode 获取node
//GetNode get node info
func (n *NodeService) GetNode(nodeID string) (*model.HostNode, *utils.APIHandleError) {
node := n.nodecluster.GetNode(nodeID)
if node == nil {
@ -117,7 +117,7 @@ func (n *NodeService) GetAllNode() ([]*model.HostNode, *utils.APIHandleError) {
return nodes, nil
}
//CordonNode 设置节点不可调度熟悉
//CordonNode set node is unscheduler
func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHandleError {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
@ -126,16 +126,15 @@ func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHa
if !hostNode.Role.HasRule(model.ComputeNode) {
return utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api"))
}
//更新节点状态
//update k8s node unshcedulable status
hostNode.Unschedulable = unschedulable
//k8s节点存在
//update node status
if unschedulable {
hostNode.Status = "unschedulable"
} else {
hostNode.Status = "running"
}
if hostNode.NodeStatus != nil {
//true表示drain不可调度
node, err := k8s.CordonOrUnCordon(hostNode.ID, unschedulable)
if err != nil {
return utils.CreateAPIHandleError(500, fmt.Errorf("set node schedulable info error,%s", err.Error()))
@ -146,7 +145,7 @@ func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHa
return nil
}
//PutNodeLabel 更新node label
//PutNodeLabel update node label
func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) *utils.APIHandleError {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
@ -164,7 +163,7 @@ func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) *uti
return nil
}
//DownNode 节点下线
//DownNode down node
func (n *NodeService) DownNode(nodeID string) (*model.HostNode, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
@ -183,7 +182,7 @@ func (n *NodeService) DownNode(nodeID string) (*model.HostNode, *utils.APIHandle
return hostNode, nil
}
//UpNode 节点上线
//UpNode up node
func (n *NodeService) UpNode(nodeID string) (*model.HostNode, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID)
if apierr != nil {
@ -201,6 +200,8 @@ func (n *NodeService) UpNode(nodeID string) (*model.HostNode, *utils.APIHandleEr
n.nodecluster.UpdateNode(hostNode)
return hostNode, nil
}
//InstallNode install a node
func (n *NodeService) InstallNode(nodeID string) *utils.APIHandleError {
time.Sleep(3 * time.Second)
node, err := n.GetNode(nodeID)
@ -228,6 +229,8 @@ func (n *NodeService) InstallNode(nodeID string) *utils.APIHandleError {
n.nodecluster.UpdateNode(node)
return nil
}
//InitStatus node init status
func (n *NodeService) InitStatus(nodeIP string) (*model.InitStatus, *utils.APIHandleError) {
var hostnode model.HostNode
gotNode := false
@ -245,34 +248,28 @@ func (n *NodeService) InitStatus(nodeIP string) (*model.InitStatus, *utils.APIHa
break
}
}
if i > 0 {
time.Sleep(time.Second)
}
i++
}
if i != 10 {
return nil, utils.CreateAPIHandleError(400, fmt.Errorf("can't find node with given ip %s", nodeIP))
}
nodeUID := hostnode.ID
node, err := n.GetNode(nodeUID)
if err != nil {
return nil, err
}
var status model.InitStatus
for _, val := range node.Conditions {
if node.Alived || (val.Type == model.NodeInit && val.Status == model.ConditionTrue) {
//初始化成功
status.Status = 0
status.StatusCN = "初始化成功"
status.HostID = node.ID
} else if val.Type == model.NodeInit && val.Status == model.ConditionFalse {
status.Status = 1
status.StatusCN = fmt.Sprintf("初始化失<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,%s", val.Message)
status.StatusCN = fmt.Sprintf("初始化失,%s", val.Message)
} else {
status.Status = 2
status.StatusCN = "初始化中"
@ -285,12 +282,12 @@ func (n *NodeService) InitStatus(nodeIP string) (*model.InitStatus, *utils.APIHa
return &status, nil
}
//GetNodeResource get node resource
func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *utils.APIHandleError) {
node, err := n.GetNode(nodeUID)
if err != nil {
return nil, err
}
if !node.Role.HasRule("compute") {
}
@ -298,8 +295,8 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *
if error != nil {
return nil, utils.CreateAPIHandleError(404, err)
}
var cpuTotal int64 = node.AvailableCPU
var memTotal int64 = node.AvailableMemory
var cpuTotal = node.AvailableCPU
var memTotal = node.AvailableMemory
var cpuLimit int64
var cpuRequest int64
var memLimit int64
@ -308,9 +305,7 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *
lc := v.Spec.Containers[0].Resources.Limits.Cpu().MilliValue()
cpuLimit += lc
lm := v.Spec.Containers[0].Resources.Limits.Memory().Value()
memLimit += lm
//logrus.Infof("pod %s limit cpu is %s",v.Name,v.Spec.Containers[0].Resources.Limits.Cpu().MilliValue())
rc := v.Spec.Containers[0].Resources.Requests.Cpu().MilliValue()
cpuRequest += rc
@ -318,7 +313,6 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *
memRequest += rm
}
var res model.NodePodResource
res.CPULimits = cpuLimit
//logrus.Infof("node %s cpu limit is %v",cpuLimit)
res.CPURequests = cpuRequest
@ -332,6 +326,8 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *
res.MemoryRequestsR = strconv.FormatFloat(float64(res.MemoryRequests*100)/float64(res.MemR), 'f', 2, 64)
return &res, nil
}
//CheckNode check node install status
func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.APIHandleError) {
descMap := make(map[string]string)
descMap["check_compute_services"] = "检测计算节点所需服务"
@ -371,8 +367,8 @@ func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.AP
var final model.InstallStatus
var result []*model.ExecedTask
var installStatus int = 1 //0 success 1 ing 2 failed
var statusCN string = "安装中" //0 success 1 ing 2 failed
var installStatus = 1 //0 success 1 ing 2 failed
var statusCN = "安装中" //0 success 1 ing 2 failed
successCount := 0
for _, v := range tasks {
var task model.ExecedTask
@ -382,7 +378,7 @@ func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.AP
task.Status = strings.ToLower(taskStatus.Status)
task.CompleteStatus = taskStatus.CompleStatus
if strings.ToLower(task.Status) == "complete" && strings.ToLower(task.CompleteStatus) == "success" {
successCount += 1
successCount++
}
if strings.ToLower(task.Status) == "parse task output error" {
task.Status = "failure"

View File

@ -19,144 +19,47 @@
package service
import (
"fmt"
"time"
"github.com/goodrain/rainbond/cmd/node/option"
"github.com/goodrain/rainbond/pkg/node/api/model"
"github.com/goodrain/rainbond/pkg/node/masterserver"
"github.com/goodrain/rainbond/pkg/node/utils"
"github.com/twinj/uuid"
)
//TaskService 处理taskAPI
//PrometheusService prometheus service
type PrometheusService struct {
prometheusAPI *model.PrometheusAPI
conf *option.Conf
ms *masterserver.MasterServer
conf *option.Conf
ms *masterserver.MasterServer
}
var prometheusService *PrometheusService
//CreateTaskService 创建Task service
//CreatePrometheusService create prometheus service
func CreatePrometheusService(c *option.Conf, ms *masterserver.MasterServer) *PrometheusService {
if prometheusService == nil {
prometheusService = &PrometheusService{
prometheusAPI: &model.PrometheusAPI{API:c.PrometheusAPI},
conf: c,
ms: ms,
prometheusAPI: &model.PrometheusAPI{API: c.PrometheusAPI},
conf: c,
ms: ms,
}
}
return prometheusService
}
func (ts *PrometheusService)getTasksByCheck(checkTasks []string,nodeID string) ([]*model.Task, *utils.APIHandleError) {
var result []*model.Task
var nextTask []string
for _,v:=range checkTasks{
checkTask,err:=taskService.GetTask(v)
if err != nil {
return nil,err
}
for _,out:=range checkTask.OutPut{
if out.NodeID == nodeID {
for _,status:=range out.Status{
for _,v:=range status.NextTask{
nextTask=append(nextTask,v)
}
}
}
}
}
//tids:=[]string{"do_rbd_images","install_acp_plugins","install_base_plugins","install_db",
// "install_docker","install_k8s","install_manage_ready","install_network","install_plugins","install_storage","install_webcli","update_dns","update_entrance_services","create_host_id_list"}
for _,v:=range nextTask{
task,err:=taskService.GetTask(v)
if err != nil {
return nil,err
}
result=append(result, task)
}
return result,nil
}
func (ts *PrometheusService)GetTasksByNode(n *model.HostNode)([]*model.Task,*utils.APIHandleError) {
if n.Role.HasRule("compute") &&len(n.Role)==1{
checkTask:=[]string{"check_compute_services"}
//tids:=[]string{"install_compute_ready","update_dns_compute","install_storage_client","install_network_compute","install_plugins_compute","install_docker_compute","install_kubelet"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
if err != nil {
return nil,err
}
return result,nil
}else if n.Role.HasRule("manage") &&len(n.Role)==1{
//checkTask:=[]string{"check_manage_base_services","check_manage_services"}
checkTask:=[]string{"check_manage_services"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
if err != nil {
return nil,err
}
return result,nil
}else {
//checkTask:=[]string{"check_manage_base_services","check_manage_services","check_compute_services"}
checkTask:=[]string{"check_manage_services","check_compute_services"}
//tids:=[]string{"do_rbd_images","install_acp_plugins","install_base_plugins","install_db","install_docker","install_k8s","install_manage_ready","install_network","install_plugins","install_storage","install_webcli","update_dns","update_entrance_services","create_host_id_list","install_kubelet_manage","install_compute_ready_manage"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
if err != nil {
return nil,err
}
return result,nil
}
}
//AddTask add task
func (ts *PrometheusService) AddTask(t *model.Task) *utils.APIHandleError {
if t.ID == "" {
t.ID = uuid.NewV4().String()
}
if len(t.Nodes) < 1 {
return utils.CreateAPIHandleError(400, fmt.Errorf("task exec nodes can not be empty"))
}
if t.TempID == "" && t.Temp == nil {
return utils.CreateAPIHandleError(400, fmt.Errorf("task temp can not be empty"))
}
if t.TempID != "" {
//TODO:确定是否应该在执行时获取最新TEMP
temp, err := taskTempService.GetTaskTemp(t.TempID)
if err != nil {
return err
}
t.Temp = temp
}
if t.Temp == nil {
return utils.CreateAPIHandleError(400, fmt.Errorf("task temp can not be empty"))
}
t.Status = map[string]model.TaskStatus{}
for _, n := range t.Nodes {
t.Status[n] = model.TaskStatus{
Status: "create",
}
}
t.CreateTime = time.Now()
err := ts.ms.TaskEngine.AddTask(t)
if err != nil {
return utils.CreateAPIHandleErrorFromDBError("save task", err)
}
return nil
}
//AddTask add task
func (ts *PrometheusService) Exec(expr string) (*model.Prome,*utils.APIHandleError) {
//Exec exec prometheus query
func (ts *PrometheusService) Exec(expr string) (*model.Prome, *utils.APIHandleError) {
resp, err := ts.prometheusAPI.Query(expr)
if err != nil {
return nil,err
return nil, err
}
return resp,nil
return resp, nil
}
func (ts *PrometheusService) ExecRange(expr,start,end,step string) (*model.Prome,*utils.APIHandleError) {
resp, err := ts.prometheusAPI.QueryRange(expr,start,end,step)
//ExecRange exec prometheus query range
func (ts *PrometheusService) ExecRange(expr, start, end, step string) (*model.Prome, *utils.APIHandleError) {
resp, err := ts.prometheusAPI.QueryRange(expr, start, end, step)
if err != nil {
return nil,err
return nil, err
}
return resp,nil
}
return resp, nil
}

View File

@ -36,7 +36,7 @@ import (
"github.com/twinj/uuid"
)
//TaskService 处理taskAPI
//TaskService task services
type TaskService struct {
SavePath string
conf *option.Conf
@ -45,7 +45,7 @@ type TaskService struct {
var taskService *TaskService
//CreateTaskService 创建Task service
//CreateTaskService create task service
func CreateTaskService(c *option.Conf, ms *masterserver.MasterServer) *TaskService {
if taskService == nil {
taskService = &TaskService{
@ -56,19 +56,19 @@ func CreateTaskService(c *option.Conf, ms *masterserver.MasterServer) *TaskServi
}
return taskService
}
func (ts *TaskService)getTasksByCheck(checkTasks []string,nodeID string) ([]*model.Task, *utils.APIHandleError) {
func (ts *TaskService) getTasksByCheck(checkTasks []string, nodeID string) ([]*model.Task, *utils.APIHandleError) {
var result []*model.Task
var nextTask []string
for _,v:=range checkTasks{
checkTask,err:=taskService.GetTask(v)
for _, v := range checkTasks {
checkTask, err := taskService.GetTask(v)
if err != nil {
return nil,err
return nil, err
}
for _,out:=range checkTask.OutPut{
for _, out := range checkTask.OutPut {
if out.NodeID == nodeID {
for _,status:=range out.Status{
for _,v:=range status.NextTask{
nextTask=append(nextTask,v)
for _, status := range out.Status {
for _, v := range status.NextTask {
nextTask = append(nextTask, v)
}
}
}
@ -77,43 +77,46 @@ func (ts *TaskService)getTasksByCheck(checkTasks []string,nodeID string) ([]*mod
}
//tids:=[]string{"do_rbd_images","install_acp_plugins","install_base_plugins","install_db",
// "install_docker","install_k8s","install_manage_ready","install_network","install_plugins","install_storage","install_webcli","update_dns","update_entrance_services","create_host_id_list"}
for _,v:=range nextTask{
task,err:=taskService.GetTask(v)
for _, v := range nextTask {
task, err := taskService.GetTask(v)
if err != nil {
return nil,err
return nil, err
}
result=append(result, task)
result = append(result, task)
}
return result,nil
return result, nil
}
func (ts *TaskService)GetTasksByNode(n *model.HostNode)([]*model.Task,*utils.APIHandleError) {
if n.Role.HasRule("compute") &&len(n.Role)==1{
checkTask:=[]string{"check_compute_services"}
//GetTasksByNode get tasks by node
func (ts *TaskService) GetTasksByNode(n *model.HostNode) ([]*model.Task, *utils.APIHandleError) {
if n.Role.HasRule("compute") && len(n.Role) == 1 {
checkTask := []string{"check_compute_services"}
//tids:=[]string{"install_compute_ready","update_dns_compute","install_storage_client","install_network_compute","install_plugins_compute","install_docker_compute","install_kubelet"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
result, err := ts.getTasksByCheck(checkTask, n.ID)
if err != nil {
return nil,err
return nil, err
}
return result,nil
}else if n.Role.HasRule("manage") &&len(n.Role)==1{
return result, nil
} else if n.Role.HasRule("manage") && len(n.Role) == 1 {
//checkTask:=[]string{"check_manage_base_services","check_manage_services"}
checkTask:=[]string{"check_manage_services"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
checkTask := []string{"check_manage_services"}
result, err := ts.getTasksByCheck(checkTask, n.ID)
if err != nil {
return nil,err
return nil, err
}
return result,nil
}else {
return result, nil
} else {
//checkTask:=[]string{"check_manage_base_services","check_manage_services","check_compute_services"}
checkTask:=[]string{"check_manage_services","check_compute_services"}
checkTask := []string{"check_manage_services", "check_compute_services"}
//tids:=[]string{"do_rbd_images","install_acp_plugins","install_base_plugins","install_db","install_docker","install_k8s","install_manage_ready","install_network","install_plugins","install_storage","install_webcli","update_dns","update_entrance_services","create_host_id_list","install_kubelet_manage","install_compute_ready_manage"}
result,err:=ts.getTasksByCheck(checkTask,n.ID)
result, err := ts.getTasksByCheck(checkTask, n.ID)
if err != nil {
return nil,err
return nil, err
}
return result,nil
return result, nil
}
}
//AddTask add task
func (ts *TaskService) AddTask(t *model.Task) *utils.APIHandleError {
if t.ID == "" {
@ -181,7 +184,7 @@ func (ts *TaskService) GetTasks() ([]*model.Task, *utils.APIHandleError) {
return result, nil
}
//GetTask 获取Task
//GetTask get task by taskID
func (ts *TaskService) GetTask(taskID string) (*model.Task, *utils.APIHandleError) {
var task *model.Task
task = ts.ms.TaskEngine.GetTask(taskID)
@ -191,7 +194,7 @@ func (ts *TaskService) GetTask(taskID string) (*model.Task, *utils.APIHandleErro
return task, nil
}
//DeleteTask 删除Task
//DeleteTask delete task by taskID
func (ts *TaskService) DeleteTask(taskID string) *utils.APIHandleError {
task, err := ts.GetTask(taskID)
if err != nil {
@ -219,7 +222,7 @@ func (ts *TaskService) DeleteTask(taskID string) *utils.APIHandleError {
return nil
}
//ExecTask 执行任务API处理
//ExecTask exec a task in nodes
func (ts *TaskService) ExecTask(taskID string, nodes []string) *utils.APIHandleError {
t, err := ts.GetTask(taskID)
if err != nil {
@ -260,7 +263,7 @@ func (ts *TaskService) ReloadStaticTasks() {
ts.ms.TaskEngine.LoadStaticTask()
}
//TaskTempService 任务模版
//TaskTempService task temp service
type TaskTempService struct {
SavePath string
conf *option.Conf
@ -268,7 +271,7 @@ type TaskTempService struct {
var taskTempService *TaskTempService
//CreateTaskTempService 创建Task service
//CreateTaskTempService create task temp service
func CreateTaskTempService(c *option.Conf) *TaskTempService {
if taskTempService == nil {
taskTempService = &TaskTempService{
@ -279,7 +282,7 @@ func CreateTaskTempService(c *option.Conf) *TaskTempService {
return taskTempService
}
//SaveTaskTemp add task
//SaveTaskTemp add task temp
func (ts *TaskTempService) SaveTaskTemp(t *model.TaskTemp) *utils.APIHandleError {
if t.ID == "" {
t.ID = uuid.NewV4().String()
@ -294,7 +297,7 @@ func (ts *TaskTempService) SaveTaskTemp(t *model.TaskTemp) *utils.APIHandleError
return nil
}
//GetTaskTemp add task
//GetTaskTemp get task temp
func (ts *TaskTempService) GetTaskTemp(tempID string) (*model.TaskTemp, *utils.APIHandleError) {
res, err := store.DefalutClient.Get(ts.SavePath + "/" + tempID)
if err != nil {
@ -314,7 +317,7 @@ func (ts *TaskTempService) GetTaskTemp(tempID string) (*model.TaskTemp, *utils.A
return &task, nil
}
//DeleteTaskTemp 删除任务模版
//DeleteTaskTemp delete task temp
func (ts *TaskTempService) DeleteTaskTemp(tempID string) *utils.APIHandleError {
_, err := ts.GetTaskTemp(tempID)
if err != nil {
@ -327,7 +330,7 @@ func (ts *TaskTempService) DeleteTaskTemp(tempID string) *utils.APIHandleError {
return nil
}
//TaskGroupService 任务组
//TaskGroupService task group
type TaskGroupService struct {
SavePath string
conf *option.Conf
@ -336,7 +339,7 @@ type TaskGroupService struct {
var taskGroupService *TaskGroupService
//CreateTaskGroupService 创建Task group service
//CreateTaskGroupService create Task group service
func CreateTaskGroupService(c *option.Conf, ms *masterserver.MasterServer) *TaskGroupService {
if taskGroupService == nil {
taskGroupService = &TaskGroupService{
@ -393,7 +396,7 @@ func (ts *TaskGroupService) GetTaskGroups() ([]*model.TaskGroup, *utils.APIHandl
return tasks, nil
}
//GetTaskGroup 获取Task
//GetTaskGroup get Task group
func (ts *TaskGroupService) GetTaskGroup(taskGroupID string) (*model.TaskGroup, *utils.APIHandleError) {
res, err := store.DefalutClient.Get(ts.SavePath + "/" + taskGroupID)
if err != nil {
@ -413,8 +416,8 @@ func (ts *TaskGroupService) GetTaskGroup(taskGroupID string) (*model.TaskGroup,
return &task, nil
}
//DeleteTaskGroup 删除TaskGroup
//删除Group不删除包含的Task
//DeleteTaskGroup delete TaskGroup
//delete group but do not delete task in this group
func (ts *TaskGroupService) DeleteTaskGroup(taskGroupID string) *utils.APIHandleError {
taskGroup, err := ts.GetTaskGroup(taskGroupID)
if err != nil {
@ -430,7 +433,7 @@ func (ts *TaskGroupService) DeleteTaskGroup(taskGroupID string) *utils.APIHandle
return nil
}
//ExecTaskGroup 执行组任务API处理
//ExecTaskGroup exec group task
func (ts *TaskGroupService) ExecTaskGroup(taskGroupID string, nodes []string) *utils.APIHandleError {
t, err := ts.GetTaskGroup(taskGroupID)
if err != nil {