/
initialize.go
228 lines (194 loc) · 5.96 KB
/
initialize.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// 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.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package cmp Core components of multi-cloud management platform
package cmp
import (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/go-redis/redis"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda-infra/base/version"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/cmp/conf"
"github.com/erda-project/erda/modules/cmp/dbclient"
"github.com/erda-project/erda/modules/cmp/endpoints"
"github.com/erda-project/erda/modules/cmp/i18n"
aliyun_resources "github.com/erda-project/erda/modules/cmp/impl/aliyun-resources"
org_resource "github.com/erda-project/erda/modules/cmp/impl/org-resource"
"github.com/erda-project/erda/pkg/database/dbengine"
"github.com/erda-project/erda/pkg/discover"
"github.com/erda-project/erda/pkg/dumpstack"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/http/httpserver"
"github.com/erda-project/erda/pkg/jsonstore"
"github.com/erda-project/erda/pkg/loop"
"github.com/erda-project/erda/pkg/strutil"
"github.com/erda-project/erda/pkg/ucauth"
)
func initialize() error {
conf.Load()
// set log formatter
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000000000",
})
logrus.SetOutput(os.Stdout)
// set the debug level of log
debugLevel := logrus.InfoLevel
if conf.Debug() {
debugLevel = logrus.DebugLevel
}
logrus.SetLevel(debugLevel)
dumpstack.Open()
logrus.Infoln(version.String())
server, err := do()
if err != nil {
return nil
}
return server.ListenAndServe()
}
func do() (*httpserver.Server, error) {
var redisCli *redis.Client
db := dbclient.Open(dbengine.MustOpen())
i18n.InitI18N()
// cache etcd
option := jsonstore.UseCacheEtcdStore(context.Background(), aliyun_resources.CloudResourcePrefix, 100)
cachedJs, err := jsonstore.New(option)
// etcd
js, err := jsonstore.New()
if err != nil {
return nil, err
}
if conf.LocalMode() {
redisCli = redis.NewClient(&redis.Options{
Addr: conf.RedisAddr(),
Password: conf.RedisPwd(),
})
} else {
redisCli = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: conf.RedisMasterName(),
SentinelAddrs: strings.Split(conf.RedisSentinelAddrs(), ","),
Password: conf.RedisPwd(),
})
}
if _, err := redisCli.Ping().Result(); err != nil {
return nil, err
}
// init uc client
uc := ucauth.NewUCClient(discover.UC(), conf.UCClientID(), conf.UCClientSecret())
if conf.OryEnabled() {
uc = ucauth.NewUCClient(conf.OryKratosPrivateAddr(), conf.OryCompatibleClientID(), conf.OryCompatibleClientSecret())
}
// init Bundle
bundleOpts := []bundle.Option{
bundle.WithHTTPClient(
httpclient.New(
httpclient.WithTimeout(time.Second, time.Second*60),
)),
bundle.WithPipeline(),
bundle.WithScheduler(),
bundle.WithMonitor(),
bundle.WithCoreServices(),
bundle.WithOrchestrator(),
bundle.WithDiceHub(),
bundle.WithEventBox(),
bundle.WithClusterManager(),
}
bdl := bundle.New(bundleOpts...)
o := org_resource.New(
org_resource.WithDBClient(db),
org_resource.WithUCClient(uc),
org_resource.WithBundle(bdl),
org_resource.WithRedisClient(redisCli),
)
ep, err := initEndpoints(db, js, cachedJs, bdl, o)
if err != nil {
return nil, err
}
if conf.EnableEss() {
initServices(ep)
}
server := httpserver.New(conf.ListenAddr())
server.RegisterEndpoint(append(ep.Routes()))
logrus.Infof("start the service and listen on address: %s", conf.ListenAddr())
logrus.Info("starting cmp instance")
// init cron job
initCron(ep)
return server, nil
}
func initEndpoints(db *dbclient.DBClient, js, cachedJS jsonstore.JsonStore, bdl *bundle.Bundle, o *org_resource.OrgResource) (*endpoints.Endpoints, error) {
// compose endpoints
ep := endpoints.New(
db,
js,
cachedJS,
endpoints.WithBundle(bdl),
endpoints.WithOrgResource(o),
)
// Sync org resource task status
go func() {
ep.SyncTaskStatus(conf.TaskSyncDuration())
}()
// Clean job/deployment sync
go func() {
ep.TaskClean(conf.TaskCleanDuration())
}()
registerWebHook(bdl)
return ep, nil
}
func initServices(ep *endpoints.Endpoints) {
// run mns service, monitor mns messages & consume them
ep.Mns.Run()
ep.Ess.AutoScale()
}
// 初始化定时任务
func initCron(ep *endpoints.Endpoints) {
// cron job to monitor pipeline created edge clusters
go loop.New(loop.WithInterval(10 * time.Second)).Do(ep.GetCluster().MonitorCloudCluster)
}
func registerWebHook(bdl *bundle.Bundle) {
// register pipeline tasks by webhook
ev := apistructs.CreateHookRequest{
Name: "cmdb_pipeline_tasks",
Events: []string{"pipeline_task", "pipeline_task_runtime"},
URL: strutil.Concat("http://", discover.CMP(), "/api/tasks"),
Active: true,
HookLocation: apistructs.HookLocation{
Org: "-1",
Project: "-1",
Application: "-1",
},
}
if err := bdl.CreateWebhook(ev); err != nil {
logrus.Warnf("failed to register pipeline tasks event, (%v)", err)
}
clusterEv := apistructs.CreateHookRequest{
Name: "cmp-clusterhook",
Events: []string{"cluster"},
URL: fmt.Sprintf("http://%s/api/clusterhook", discover.CMP()),
Active: true,
HookLocation: apistructs.HookLocation{
Org: "-1",
Project: "-1",
Application: "-1",
},
}
if err := bdl.CreateWebhook(clusterEv); err != nil {
logrus.Warnf("failed to register cluster event, (%v)", err)
}
}