/
manager.go
431 lines (408 loc) · 16.3 KB
/
manager.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// Copyright (C) 2nilfmt.Errorf("a")4-2nilfmt.Errorf("a")8 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// 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 handle
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/Sirupsen/logrus"
"github.com/eapache/channels"
"github.com/goodrain/rainbond/cmd/worker/option"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/worker/appm/controller"
"github.com/goodrain/rainbond/worker/appm/conversion"
"github.com/goodrain/rainbond/worker/appm/store"
v1 "github.com/goodrain/rainbond/worker/appm/types/v1"
"github.com/goodrain/rainbond/worker/discover/model"
)
//Manager manager
type Manager struct {
ctx context.Context
c option.Config
store store.Storer
dbmanager db.Manager
controllerManager *controller.Manager
startCh *channels.RingChannel
}
//NewManager now handle
func NewManager(ctx context.Context,
config option.Config,
store store.Storer,
controllerManager *controller.Manager,
startCh *channels.RingChannel) *Manager {
return &Manager{
ctx: ctx,
c: config,
dbmanager: db.GetManager(),
store: store,
controllerManager: controllerManager,
startCh: startCh,
}
}
//ErrCallback do not handle this task
var ErrCallback = fmt.Errorf("callback task to mq")
func (m *Manager) checkCount() bool {
if m.controllerManager.GetControllerSize() > m.c.MaxTasks {
return true
}
return false
}
//AnalystToExec analyst exec
func (m *Manager) AnalystToExec(task *model.Task) error {
if task == nil {
return nil
}
//max worker count check
if m.checkCount() {
return ErrCallback
}
if !m.store.Ready() {
return ErrCallback
}
switch task.Type {
case "start":
logrus.Info("start a 'start' task worker")
return m.startExec(task)
case "stop":
logrus.Info("start a 'stop' task worker")
return m.stopExec(task)
case "restart":
logrus.Info("start a 'restart' task worker")
return m.restartExec(task)
case "horizontal_scaling":
logrus.Info("start a 'horizontal_scaling' task worker")
return m.horizontalScalingExec(task)
case "vertical_scaling":
logrus.Info("start a 'vertical_scaling' task worker")
return m.verticalScalingExec(task)
case "rolling_upgrade":
logrus.Info("start a 'rolling_upgrade' task worker")
return m.rollingUpgradeExec(task)
case "apply_rule":
logrus.Info("start a 'apply_rule' task worker")
return m.applyRuleExec(task)
case "apply_plugin_config":
logrus.Info("start a 'apply_plugin_config' task worker")
return m.applyPluginConfig(task)
default:
logrus.Warning("task can not execute because no type is identified")
return nil
}
}
//startExec exec start service task
func (m *Manager) startExec(task *model.Task) error {
body, ok := task.Body.(model.StartTaskBody)
if !ok {
logrus.Errorf("start body convert to taskbody error")
return fmt.Errorf("start body convert to taskbody error")
}
logger := event.GetManager().GetLogger(body.EventID)
appService := m.store.GetAppService(body.ServiceID)
if appService != nil && !appService.IsClosed() {
logger.Info("Application is not closed, can not start", controller.GetLastLoggerOption())
event.GetManager().ReleaseLogger(logger)
return nil
}
newAppService, err := conversion.InitAppService(m.dbmanager, body.ServiceID, body.Configs)
if err != nil {
logrus.Errorf("Application init create failure:%s", err.Error())
logger.Error("Application init create failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application init create failure")
}
newAppService.Logger = logger
//regist new app service
m.store.RegistAppService(newAppService)
err = m.controllerManager.StartController(controller.TypeStartController, *newAppService)
if err != nil {
logrus.Errorf("Application run start controller failure:%s", err.Error())
logger.Error("Application run start controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application start failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "start")
return nil
}
func (m *Manager) stopExec(task *model.Task) error {
body, ok := task.Body.(model.StopTaskBody)
if !ok {
logrus.Errorf("stop body convert to taskbody error")
return fmt.Errorf("stop body convert to taskbody error")
}
logger := event.GetManager().GetLogger(body.EventID)
appService := m.store.GetAppService(body.ServiceID)
if appService == nil {
logger.Info("Application is closed, can not stop", controller.GetLastLoggerOption())
event.GetManager().ReleaseLogger(logger)
return nil
}
appService.Logger = logger
for k, v := range body.Configs {
appService.ExtensionSet[k] = v
}
err := m.controllerManager.StartController(controller.TypeStopController, *appService)
if err != nil {
logrus.Errorf("Application run stop controller failure:%s", err.Error())
logger.Info("Application run stop controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application stop failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "stop")
return nil
}
func (m *Manager) restartExec(task *model.Task) error {
body, ok := task.Body.(model.RestartTaskBody)
if !ok {
logrus.Errorf("stop body convert to taskbody error")
return fmt.Errorf("stop body convert to taskbody error")
}
logger := event.GetManager().GetLogger(body.EventID)
appService := m.store.GetAppService(body.ServiceID)
if appService == nil {
logger.Info("Application is closed, can not stop", controller.GetLastLoggerOption())
event.GetManager().ReleaseLogger(logger)
return nil
}
appService.Logger = logger
for k, v := range body.Configs {
appService.ExtensionSet[k] = v
}
//first stop app
err := m.controllerManager.StartController(controller.TypeRestartController, *appService)
if err != nil {
logrus.Errorf("Application run restart controller failure:%s", err.Error())
logger.Info("Application run restart controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application restart failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "restart")
return nil
}
func (m *Manager) horizontalScalingExec(task *model.Task) error {
body, ok := task.Body.(model.HorizontalScalingTaskBody)
if !ok {
logrus.Errorf("horizontal_scaling body convert to taskbody error")
return fmt.Errorf("a")
}
logger := event.GetManager().GetLogger(body.EventID)
service, err := db.GetManager().TenantServiceDao().GetServiceByID(body.ServiceID)
if err != nil {
logger.Error("Get app base info failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
logrus.Errorf("horizontal_scaling get rc error. %v", err)
return fmt.Errorf("a")
}
appService := m.store.GetAppService(body.ServiceID)
if appService == nil || appService.IsClosed() {
logger.Info("service is closed,no need handle", controller.GetLastLoggerOption())
return nil
}
appService.Logger = logger
appService.Replicas = service.Replicas
err = m.controllerManager.StartController(controller.TypeScalingController, *appService)
if err != nil {
logrus.Errorf("Application run scaling controller failure:%s", err.Error())
logger.Info("Application run scaling controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application scaling failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "scaling")
return nil
}
func (m *Manager) verticalScalingExec(task *model.Task) error {
body, ok := task.Body.(model.VerticalScalingTaskBody)
if !ok {
logrus.Errorf("vertical_scaling body convert to taskbody error")
return fmt.Errorf("vertical_scaling body convert to taskbody error")
}
logger := event.GetManager().GetLogger(body.EventID)
service, err := db.GetManager().TenantServiceDao().GetServiceByID(body.ServiceID)
if err != nil {
logrus.Errorf("vertical_scaling get rc error. %v", err)
logger.Error("Get app base info failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("vertical_scaling get rc error. %v", err)
}
appService := m.store.GetAppService(body.ServiceID)
if appService == nil || appService.IsClosed() {
logger.Info("service is closed,no need handle", controller.GetLastLoggerOption())
event.GetManager().ReleaseLogger(logger)
return nil
}
appService.ContainerCPU = service.ContainerCPU
appService.ContainerMemory = service.ContainerMemory
appService.Logger = logger
newAppService, err := conversion.InitAppService(m.dbmanager, body.ServiceID, nil)
if err != nil {
logrus.Errorf("Application init create failure:%s", err.Error())
logger.Error("Application init create failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application init create failure")
}
newAppService.Logger = logger
appService.SetUpgradePatch(newAppService)
err = m.controllerManager.StartController(controller.TypeUpgradeController, *newAppService)
if err != nil {
logrus.Errorf("Application run vertical scaling(upgrade) controller failure:%s", err.Error())
logger.Info("Application run vertical scaling(upgrade) controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application vertical scaling(upgrade) failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "vertical scaling")
return nil
}
func (m *Manager) rollingUpgradeExec(task *model.Task) error {
body, ok := task.Body.(model.RollingUpgradeTaskBody)
if !ok {
logrus.Error("rolling_upgrade body convert to taskbody error", task.Body)
return fmt.Errorf("rolling_upgrade body convert to taskbody error")
}
logger := event.GetManager().GetLogger(body.EventID)
newAppService, err := conversion.InitAppService(m.dbmanager, body.ServiceID, body.Configs)
if err != nil {
logrus.Errorf("Application init create failure:%s", err.Error())
logger.Error("Application init create failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application init create failure")
}
newAppService.Logger = logger
oldAppService := m.store.GetAppService(body.ServiceID)
// if service not deploy,start it
if oldAppService == nil || oldAppService.IsClosed() {
//regist new app service
m.store.RegistAppService(newAppService)
err = m.controllerManager.StartController(controller.TypeStartController, *newAppService)
if err != nil {
logrus.Errorf("Application run start controller failure:%s", err.Error())
logger.Info("Application run start controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application start failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "start")
return nil
}
if err := oldAppService.SetUpgradePatch(newAppService); err != nil {
if err.Error() == "no upgrade" {
logger.Info("Application no change no need upgrade.", controller.GetLastLoggerOption())
return nil
}
logrus.Errorf("Application get upgrade info error:%s", err.Error())
logger.Error(fmt.Sprintf("Application get upgrade info error:%s", err.Error()), controller.GetCallbackLoggerOption())
return nil
}
//if service already deploy,upgrade it:
err = m.controllerManager.StartController(controller.TypeUpgradeController, *newAppService)
if err != nil {
logrus.Errorf("Application run upgrade controller failure:%s", err.Error())
logger.Info("Application run upgrade controller failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application upgrade failure")
}
logrus.Infof("service(%s) %s working is running.", body.ServiceID, "upgrade")
return nil
}
func (m *Manager) applyRuleExec(task *model.Task) error {
body, ok := task.Body.(*model.ApplyRuleTaskBody)
if !ok {
logrus.Errorf("Can't convert %s to *model.ApplyRuleTaskBody", reflect.TypeOf(task.Body))
return fmt.Errorf("Can't convert %s to *model.ApplyRuleTaskBody", reflect.TypeOf(task.Body))
}
svc, err := db.GetManager().TenantServiceDao().GetServiceByID(body.ServiceID)
if err != nil {
logrus.Errorf("error get TenantServices: %v", err)
return fmt.Errorf("error get TenantServices: %v", err)
}
logger := event.GetManager().GetLogger(body.EventID)
oldAppService := m.store.GetAppService(body.ServiceID)
logrus.Debugf("body action: %s", body.Action)
if svc.Kind != dbmodel.ServiceKindThirdParty.String() && !strings.HasPrefix(body.Action, "port") {
if oldAppService == nil || oldAppService.IsClosed() {
logrus.Debugf("service is closed, no need handle")
logger.Info("service is closed,no need handle", controller.GetLastLoggerOption())
event.GetManager().ReleaseLogger(logger)
return nil
}
}
var newAppService *v1.AppService
if svc.Kind == dbmodel.ServiceKindThirdParty.String() {
newAppService, err = conversion.InitAppService(m.dbmanager, body.ServiceID, nil,
"ServiceSource", "TenantServiceBase", "TenantServiceRegist")
} else {
newAppService, err = conversion.InitAppService(m.dbmanager, body.ServiceID, nil)
}
if err != nil {
logrus.Errorf("Application init create failure:%s", err.Error())
logger.Error("Application init create failure", controller.GetCallbackLoggerOption())
event.GetManager().ReleaseLogger(logger)
return fmt.Errorf("Application init create failure")
}
newAppService.Logger = logger
newAppService.SetDeletedResources(oldAppService)
// update k8s resources
err = m.controllerManager.StartController(controller.TypeApplyRuleController, *newAppService)
if err != nil {
logrus.Errorf("Application apply rule controller failure:%s", err.Error())
return fmt.Errorf("Application apply rule controller failure:%s", err.Error())
}
if svc.Kind == dbmodel.ServiceKindThirdParty.String() && strings.HasPrefix(body.Action, "port") {
if oldAppService == nil {
m.store.RegistAppService(newAppService)
}
if body.Action == "port-open" {
m.startCh.In() <- &v1.Event{
Type: v1.StartEvent,
Sid: body.ServiceID,
Port: body.Port,
IsInner: body.IsInner,
}
}
if body.Action == "port-close" {
if !db.GetManager().TenantServicesPortDao().HasOpenPort(body.ServiceID) {
m.startCh.In() <- &v1.Event{
Type: v1.StopEvent,
Sid: body.ServiceID,
}
}
}
}
return nil
}
//applyPluginConfig apply service plugin config
func (m *Manager) applyPluginConfig(task *model.Task) error {
body, ok := task.Body.(*model.ApplyPluginConfigTaskBody)
if !ok {
logrus.Errorf("Can't convert %s to *model.ApplyPluginConfigTaskBody", reflect.TypeOf(task.Body))
return fmt.Errorf("Can't convert %s to *model.ApplyPluginConfigTaskBody", reflect.TypeOf(task.Body))
}
oldAppService := m.store.GetAppService(body.ServiceID)
if oldAppService == nil || oldAppService.IsClosed() {
logrus.Debugf("service is closed,no need handle")
return nil
}
newApp, err := conversion.InitAppService(m.dbmanager, body.ServiceID, nil, "ServiceSource", "TenantServiceBase", "TenantServicePlugin")
if err != nil {
logrus.Errorf("Application apply plugin config controller failure:%s", err.Error())
return err
}
err = m.controllerManager.StartController(controller.TypeApplyConfigController, *newApp)
if err != nil {
logrus.Errorf("Application apply plugin config controller failure:%s", err.Error())
return fmt.Errorf("Application apply plugin config controller failure:%s", err.Error())
}
return nil
}