/
upgrader.go
203 lines (177 loc) · 5.6 KB
/
upgrader.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
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// Package upgrader xxx
package upgrader
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
"github.com/Tencent/bk-bcs/bcs-common/common/encrypt"
"github.com/Tencent/bk-bcs/bcs-common/common/http/httpserver"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers"
"github.com/Tencent/bk-bcs/bcs-common/pkg/odm/drivers/mongo"
"github.com/Tencent/bk-bcs/bcs-services/bcs-upgrader/app/options"
restful "github.com/emicklei/go-restful"
)
// Upgrader is a data struct of bcs upgrader server
type Upgrader struct {
ctx context.Context
ctxCancelFunc context.CancelFunc
stopCh chan struct{}
opt *options.UpgraderOptions
httpServer *httpserver.HttpServer
db drivers.DB
upgradeHelper *Helper
}
// NewUpgrader create upgrader server object
func NewUpgrader(op *options.UpgraderOptions) (*Upgrader, error) {
ctx, cancel := context.WithCancel(context.Background())
s := &Upgrader{
ctx: ctx,
ctxCancelFunc: cancel,
stopCh: make(chan struct{}),
opt: op,
}
// Http server
s.httpServer = httpserver.NewHttpServer(s.opt.Port, s.opt.Address, "")
if s.opt.ServerCert.IsSSL {
s.httpServer.SetSsl(
s.opt.ServerCert.CAFile,
s.opt.ServerCert.CertFile,
s.opt.ServerCert.KeyFile,
s.opt.ServerCert.CertPwd)
}
return s, nil
}
// initHTTPService init all http service
func (u *Upgrader) initHTTPService() error {
u.initUpgraderService()
if u.opt.DebugMode {
u.initPprofService()
}
return nil
}
// initUpgraderService init the upgrader service
func (u *Upgrader) initUpgraderService() {
actions := []*httpserver.Action{
httpserver.NewAction("POST", "/upgrade", nil, u.Upgrade),
}
u.httpServer.RegisterWebServer("/upgrader/v1", nil, actions)
}
// initPprofService init the pprof service
func (u *Upgrader) initPprofService() {
action := []*httpserver.Action{
httpserver.NewAction("GET", "/debug/pprof/", nil, getRouteFunc(pprof.Index)),
httpserver.NewAction("GET", "/debug/pprof/{uri:*}", nil, getRouteFunc(pprof.Index)),
httpserver.NewAction("GET", "/debug/pprof/cmdline", nil, getRouteFunc(pprof.Cmdline)),
httpserver.NewAction("GET", "/debug/pprof/profile", nil, getRouteFunc(pprof.Profile)),
httpserver.NewAction("GET", "/debug/pprof/symbol", nil, getRouteFunc(pprof.Symbol)),
httpserver.NewAction("GET", "/debug/pprof/trace", nil, getRouteFunc(pprof.Trace)),
}
u.httpServer.RegisterWebServer("", nil, action)
}
// initMongoClient xxx
// init mongo client
func (u *Upgrader) initMongoClient() error {
if len(u.opt.MongoAddress) == 0 {
return fmt.Errorf("mongo address cannot be empty")
}
if len(u.opt.MongoDatabase) == 0 {
return fmt.Errorf("mongo database cannot be empty")
}
password := u.opt.MongoPassword
if password != "" {
realPwd, _ := encrypt.DesDecryptFromBase([]byte(password))
password = string(realPwd)
}
mongoOptions := &mongo.Options{
AuthMechanism: u.opt.MongoAuthMechanism,
Hosts: strings.Split(u.opt.MongoAddress, ","),
ConnectTimeoutSeconds: int(u.opt.MongoConnectTimeout),
Database: u.opt.MongoDatabase,
Username: u.opt.MongoUsername,
Password: password,
MaxPoolSize: uint64(u.opt.MongoMaxPoolSize),
MinPoolSize: uint64(u.opt.MongoMinPoolSize),
}
mongoDB, err := mongo.NewDB(mongoOptions)
if err != nil {
blog.Errorf("init mongo client failed, err: %s", err)
return err
}
if err = mongoDB.Ping(); err != nil {
blog.Errorf("ping mongodb failed, err: %s", err)
return err
}
u.db = mongoDB
blog.Infof("init mongo client successfully")
return nil
}
func (u *Upgrader) initSigalHandler() {
// listen system signal
// to run in the container, should not trap SIGTERM
interrupt := make(chan os.Signal, 10)
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case e := <-interrupt:
blog.Infof("receive interrupt %s, do close", e.String())
u.stopCh <- struct{}{}
u.close()
}
}()
}
func (u *Upgrader) initUpgradeHelper() {
opt := &HelperOpt{
DB: u.db,
config: u.opt.HttpCliConfig,
}
u.upgradeHelper = NewUpgradeHelper(opt)
}
func (u *Upgrader) close() {
u.ctxCancelFunc()
}
// Start to run upgrader server
func (u *Upgrader) Start() error {
// init system signale handler
u.initSigalHandler()
if err := u.initMongoClient(); err != nil {
blog.Errorf("init mongo clinet failed, err: %s", err)
return err
}
u.initUpgradeHelper()
if err := u.initHTTPService(); err != nil {
blog.Errorf("init http service failed, err: %s", err)
return err
}
go func() {
err := u.httpServer.ListenAndServe()
blog.Errorf("http listen and service failed, err: %s", err)
u.stopCh <- struct{}{}
}()
select {
case <-u.stopCh:
return nil
}
}
func getRouteFunc(f http.HandlerFunc) restful.RouteFunction {
return func(req *restful.Request, resp *restful.Response) {
f(resp, req.Request)
}
}