forked from goodrain/rainbond
/
rolling_upgrade_task.go
137 lines (122 loc) · 5.13 KB
/
rolling_upgrade_task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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 task
import (
"github.com/goodrain/rainbond/pkg/db"
dbmodel "github.com/goodrain/rainbond/pkg/db/model"
"github.com/goodrain/rainbond/pkg/event"
"github.com/goodrain/rainbond/pkg/util"
"github.com/goodrain/rainbond/pkg/worker/appm"
"github.com/goodrain/rainbond/pkg/worker/discover/model"
"fmt"
"github.com/goodrain/rainbond/pkg/status"
"github.com/Sirupsen/logrus"
)
type rollingUpgradeTask struct {
modelTask model.RollingUpgradeTaskBody
taskID string
logger event.Logger
taskManager *TaskManager
serviceType string
oldVersion string
stopChan chan struct{}
}
func (s *rollingUpgradeTask) RunSuccess() {
//设置应用状态为运行中
s.taskManager.statusManager.SetStatus(s.modelTask.ServiceID, status.RUNNING)
s.logger.Info("应用滚动升级任务完成", map[string]string{"step": "last", "status": "success"})
}
//RunError 如果有错误发生,回滚移除可能创建的资源
func (s *rollingUpgradeTask) RunError(e error) {
if e == appm.ErrTimeOut {
//TODO:
//应用启动超时,怎么处理?
//是否关闭应用?
//暂时不自动关闭
s.logger.Error("滚动更新超时,应用关闭或启动缓慢,状态将由后台处理", map[string]string{"step": "callback", "status": "failure"})
return
}
//TODO:
//是否还原到原版本
if e.Error() == "应用容器重启" {
s.logger.Error("滚动升级失败,应用启动失败,请查询应用日志", map[string]string{"step": "callback", "status": "failure"})
} else if e.Error() != "dont't support" {
s.logger.Error("滚动升级失败,请重试", map[string]string{"step": "callback", "status": "failure"})
}
s.taskManager.statusManager.CheckStatus(s.modelTask.ServiceID)
}
func (s *rollingUpgradeTask) BeforeRun() {
label, err := db.GetManager().TenantServiceLabelDao().GetTenantServiceTypeLabel(s.modelTask.ServiceID)
if err == nil && label != nil {
if label.LabelValue == util.StatefulServiceType {
s.serviceType = dbmodel.TypeStatefulSet
}
if label.LabelValue == util.StatelessServiceType {
//s.serviceType = dbmodel.TypeDeployment
s.serviceType = dbmodel.TypeReplicationController
}
}
s.taskManager.statusManager.SetStatus(s.modelTask.ServiceID, status.UPGRADE)
}
func (s *rollingUpgradeTask) AfterRun() {
s.logger.Info("应用滚动升级任务工作器退出", map[string]string{"step": "worker-executor", "status": "success"})
event.GetManager().ReleaseLogger(s.logger)
logrus.Info("rolling upgrade worker run complete.")
}
func (s *rollingUpgradeTask) Stop() {
s.logger.Info("应用滚动升级任务工作器退出", map[string]string{"step": "worker-executor", "status": "success"})
//发送停止信号
close(s.stopChan)
}
func (s *rollingUpgradeTask) RollBack() {
//不支持
}
func (s *rollingUpgradeTask) Run() error {
s.logger.Info("应用滚动升级任务开始执行", map[string]string{"step": "worker-executor", "status": "starting"})
switch s.serviceType {
case dbmodel.TypeStatefulSet:
_, err := s.taskManager.appm.RollingUpgradeStatefulSet(s.modelTask.ServiceID, s.logger)
if err != nil && err.Error() != appm.ErrTimeOut.Error() {
logrus.Error(err.Error())
s.logger.Info("应用升级发生错误", map[string]string{"step": "worker-executor", "status": "failure"})
return err
}
break
case dbmodel.TypeDeployment:
s.logger.Error("版本构建成功,当前类型不支持滚动升级,请手动重启", map[string]string{"step": "callback", "status": "failure"})
return fmt.Errorf("dont't support")
case dbmodel.TypeReplicationController:
rc, err := s.taskManager.appm.RollingUpgradeReplicationController(s.modelTask.ServiceID, s.stopChan, s.logger)
if err != nil && err.Error() != appm.ErrTimeOut.Error() {
logrus.Error(err.Error())
s.logger.Info("应用滚动升级发生错误", map[string]string{"step": "worker-executor", "status": "failure"})
return err
}
err = s.taskManager.appm.UpdateService(s.modelTask.ServiceID, s.logger, rc.Name, dbmodel.TypeReplicationController)
if err != nil {
logrus.Error(err.Error())
s.logger.Info("应用Service升级发生错误", map[string]string{"step": "worker-executor", "status": "failure"})
return err
}
break
}
return nil
}
func (s *rollingUpgradeTask) TaskID() string {
return s.taskID
}
func (s *rollingUpgradeTask) Logger() event.Logger {
return s.logger
}