From 2a4e3b219db78caca17468a266dc3830f9837cc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=80=86=E6=B5=81=E8=80=8C=E4=B8=8A?= <62740231+DokiDoki1103@users.noreply.github.com> Date: Wed, 10 Jan 2024 13:36:35 +0800 Subject: [PATCH] perf: Reduce API component dependency on ETCD (#1833) * perf: optimize communication exceptions --- api/controller/manager.go | 12 +- api/controller/websocket.go | 63 +++--- api/daemon/region_api.go | 65 ------ api/db/db.go | 99 ++------- api/handler/LicenseHandler_test.go | 49 ----- api/handler/eventLog_test.go | 183 ---------------- api/handler/handler.go | 86 +++----- api/handler/netRules_test.go | 74 ------- api/handler/proxy.go | 24 +-- api/handler/registry_auth_secret_action.go | 5 +- api/handler/rootFunc_test.go | 19 -- api/handler/service_check.go | 8 +- api/middleware/middleware.go | 20 +- api/server/api.go | 3 +- builder/build/code_build_test.go | 171 --------------- builder/build/netcore_build_test.go | 51 ----- builder/exector/exector_test.go | 110 ---------- cmd/api/main.go | 30 ++- cmd/api/option/option.go | 20 +- cmd/api/server/server.go | 198 ------------------ cmd/builder/option/option.go | 8 +- cmd/builder/server/server.go | 3 +- cmd/eventlog/server/server.go | 10 +- cmd/gateway/option/option.go | 8 +- cmd/monitor/option/option.go | 6 +- cmd/mq/option/option.go | 14 +- cmd/mqcli/main.go | 5 +- cmd/node/option/conf.go | 8 +- cmd/node/server/server.go | 3 +- cmd/webcli/option/option.go | 12 +- cmd/worker/option/option.go | 20 +- cmd/worker/server/server.go | 8 - config/configs/config.go | 27 +++ db/3rd_party_db_test.go | 178 ---------------- db/db.go | 12 +- db/etcd/etcd.go | 1 + event/manager.go | 56 ++--- eventlog/entry/server_test.go | 163 -------------- eventlog/store/manager_test.go | 73 ------- eventlog/test/dockerserver/main.go | 120 ----------- eventlog/test/eventserver/main.go | 101 --------- .../test/monitorserver/monitorserver_test.go | 83 -------- monitor/api/router.go | 16 +- mq/client/client.go | 41 ++-- node/api/router/router.go | 5 +- node/core/service/node_service.go | 47 ++--- node/core/store/client.go | 4 +- pkg/component/README.md | 1 + pkg/component/core.go | 130 ++++++++++++ pkg/component/etcd/etcdComponent.go | 109 ++++++++++ .../component/grpc/grpcComponent.go | 51 +++-- .../hubregistry/registryComponent.go | 78 +++++++ pkg/component/k8s/k8sComponent.go | 123 +++++++++++ .../component/mq/mqComponent.go | 45 ++-- .../component/prom/promComponent.go | 57 +++-- .../gogo/go.go | 43 ++-- .../barrel_test.go => pkg/gogo/option.go | 18 +- pkg/interceptors/http.go | 94 +++++++++ pkg/rainbond/component.go | 56 +++++ pkg/rainbond/registry.go | 104 +++++++++ test/shell/echo1.sh | 1 - test/shell/echo2.sh | 1 - util/comman_test.go | 6 - worker/client/client.go | 72 ++----- worker/client/client_test.go | 63 ------ worker/discover/manager.go | 25 +-- 66 files changed, 1177 insertions(+), 2222 deletions(-) delete mode 100644 api/daemon/region_api.go delete mode 100644 api/handler/LicenseHandler_test.go delete mode 100644 api/handler/eventLog_test.go delete mode 100644 api/handler/netRules_test.go delete mode 100644 api/handler/rootFunc_test.go delete mode 100644 builder/build/code_build_test.go delete mode 100644 builder/build/netcore_build_test.go delete mode 100644 builder/exector/exector_test.go delete mode 100644 cmd/api/server/server.go create mode 100644 config/configs/config.go delete mode 100644 db/3rd_party_db_test.go delete mode 100644 eventlog/entry/server_test.go delete mode 100644 eventlog/store/manager_test.go delete mode 100644 eventlog/test/dockerserver/main.go delete mode 100644 eventlog/test/eventserver/main.go delete mode 100644 eventlog/test/monitorserver/monitorserver_test.go create mode 100644 pkg/component/README.md create mode 100644 pkg/component/core.go create mode 100644 pkg/component/etcd/etcdComponent.go rename worker/appm/componentdefinition/componentdefinition_test.go => pkg/component/grpc/grpcComponent.go (54%) create mode 100644 pkg/component/hubregistry/registryComponent.go create mode 100644 pkg/component/k8s/k8sComponent.go rename builder/build/dockerfile_build_test.go => pkg/component/mq/mqComponent.go (55%) rename event/manager_test.go => pkg/component/prom/promComponent.go (51%) rename eventlog/store/new_monitor_message_store_test.go => pkg/gogo/go.go (62%) rename eventlog/store/barrel_test.go => pkg/gogo/option.go (77%) create mode 100644 pkg/interceptors/http.go create mode 100644 pkg/rainbond/component.go create mode 100644 pkg/rainbond/registry.go delete mode 100644 test/shell/echo1.sh delete mode 100644 test/shell/echo2.sh delete mode 100644 worker/client/client_test.go diff --git a/api/controller/manager.go b/api/controller/manager.go index 96cd6db7c..19d080750 100644 --- a/api/controller/manager.go +++ b/api/controller/manager.go @@ -22,11 +22,9 @@ import ( "net/http" "github.com/goodrain/rainbond/api/api" - "github.com/goodrain/rainbond/api/discover" "github.com/goodrain/rainbond/api/proxy" "github.com/goodrain/rainbond/cmd/api/option" mqclient "github.com/goodrain/rainbond/mq/client" - etcdutil "github.com/goodrain/rainbond/util/etcd" "github.com/goodrain/rainbond/worker/client" ) @@ -69,13 +67,7 @@ func GetManager() V2Manager { // NewManager new manager func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) { - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: conf.EtcdEndpoint, - CaFile: conf.EtcdCaFile, - CertFile: conf.EtcdCertFile, - KeyFile: conf.EtcdKeyFile, - } - mqClient, err := mqclient.NewMqClient(etcdClientArgs, conf.MQAPI) + mqClient, err := mqclient.NewMqClient(conf.MQAPI) if err != nil { return nil, err } @@ -86,7 +78,7 @@ func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2 v2r.GatewayStruct.cfg = &conf v2r.LabelController.optconfig = &conf eventServerProxy := proxy.CreateProxy("eventlog", "http", []string{"local=>rbd-eventlog:6363"}) - discover.GetEndpointDiscover().AddProject("event_log_event_http", eventServerProxy) + //discover.GetEndpointDiscover().AddProject("event_log_event_http", eventServerProxy) v2r.EventLogStruct.EventlogServerProxy = eventServerProxy return &v2r, nil } diff --git a/api/controller/websocket.go b/api/controller/websocket.go index 4818ba6ad..c7c923e43 100644 --- a/api/controller/websocket.go +++ b/api/controller/websocket.go @@ -20,12 +20,12 @@ package controller import ( "context" + "github.com/goodrain/rainbond/config/configs" "net/http" "os" "path" "github.com/go-chi/chi" - "github.com/goodrain/rainbond/api/discover" "github.com/goodrain/rainbond/api/handler" "github.com/goodrain/rainbond/api/proxy" ctxutil "github.com/goodrain/rainbond/api/util/ctx" @@ -33,110 +33,107 @@ import ( "github.com/sirupsen/logrus" ) -//DockerConsole docker console +// DockerConsole docker console type DockerConsole struct { socketproxy proxy.Proxy } -var defaultDockerConsoleEndpoints = []string{"127.0.0.1:7171"} -var defaultEventLogEndpoints = []string{"local=>rbd-eventlog:6363"} - var dockerConsole *DockerConsole -//GetDockerConsole get Docker console +// GetDockerConsole get Docker console func GetDockerConsole() *DockerConsole { if dockerConsole != nil { return dockerConsole } dockerConsole = &DockerConsole{ - socketproxy: proxy.CreateProxy("dockerconsole", "websocket", defaultDockerConsoleEndpoints), + socketproxy: proxy.CreateProxy("dockerconsole", "websocket", configs.Default().APIConfig.DockerConsoleServers), } - discover.GetEndpointDiscover().AddProject("acp_webcli", dockerConsole.socketproxy) + //discover.GetEndpointDiscover().AddProject("acp_webcli", dockerConsole.socketproxy) return dockerConsole } -//Get get +// Get get func (d DockerConsole) Get(w http.ResponseWriter, r *http.Request) { d.socketproxy.Proxy(w, r) } var dockerLog *DockerLog -//DockerLog docker log +// DockerLog docker log type DockerLog struct { socketproxy proxy.Proxy } -//GetDockerLog get docker log +// GetDockerLog get docker log func GetDockerLog() *DockerLog { if dockerLog == nil { dockerLog = &DockerLog{ - socketproxy: proxy.CreateProxy("dockerlog", "websocket", defaultEventLogEndpoints), + socketproxy: proxy.CreateProxy("dockerlog", "websocket", configs.Default().APIConfig.EventLogEndpoints), } - discover.GetEndpointDiscover().AddProject("event_log_event_http", dockerLog.socketproxy) + //discover.GetEndpointDiscover().AddProject("event_log_event_http", dockerLog.socketproxy) } return dockerLog } -//Get get +// Get get func (d DockerLog) Get(w http.ResponseWriter, r *http.Request) { d.socketproxy.Proxy(w, r) } -//MonitorMessage monitor message +// MonitorMessage monitor message type MonitorMessage struct { socketproxy proxy.Proxy } var monitorMessage *MonitorMessage -//GetMonitorMessage get MonitorMessage +// GetMonitorMessage get MonitorMessage func GetMonitorMessage() *MonitorMessage { if monitorMessage == nil { monitorMessage = &MonitorMessage{ - socketproxy: proxy.CreateProxy("monitormessage", "websocket", defaultEventLogEndpoints), + socketproxy: proxy.CreateProxy("monitormessage", "websocket", configs.Default().APIConfig.EventLogEndpoints), } - discover.GetEndpointDiscover().AddProject("event_log_event_http", monitorMessage.socketproxy) + //discover.GetEndpointDiscover().AddProject("event_log_event_http", monitorMessage.socketproxy) } return monitorMessage } -//Get get +// Get get func (d MonitorMessage) Get(w http.ResponseWriter, r *http.Request) { d.socketproxy.Proxy(w, r) } -//EventLog event log +// EventLog event log type EventLog struct { socketproxy proxy.Proxy } var eventLog *EventLog -//GetEventLog get event log +// GetEventLog get event log func GetEventLog() *EventLog { if eventLog == nil { eventLog = &EventLog{ - socketproxy: proxy.CreateProxy("eventlog", "websocket", defaultEventLogEndpoints), + socketproxy: proxy.CreateProxy("eventlog", "websocket", configs.Default().APIConfig.EventLogEndpoints), } - discover.GetEndpointDiscover().AddProject("event_log_event_http", eventLog.socketproxy) + //discover.GetEndpointDiscover().AddProject("event_log_event_http", eventLog.socketproxy) } return eventLog } -//Get get +// Get get func (d EventLog) Get(w http.ResponseWriter, r *http.Request) { d.socketproxy.Proxy(w, r) } -//LogFile log file down server +// LogFile log file down server type LogFile struct { Root string } var logFile *LogFile -//GetLogFile get log file +// GetLogFile get log file func GetLogFile() *LogFile { root := os.Getenv("SERVICE_LOG_ROOT") if root == "" { @@ -151,7 +148,7 @@ func GetLogFile() *LogFile { return logFile } -//Get get +// Get get func (d LogFile) Get(w http.ResponseWriter, r *http.Request) { gid := chi.URLParam(r, "gid") filename := chi.URLParam(r, "filename") @@ -180,23 +177,23 @@ func (d LogFile) GetInstallLog(w http.ResponseWriter, r *http.Request) { var pubSubControll *PubSubControll -//PubSubControll service pub sub +// PubSubControll service pub sub type PubSubControll struct { socketproxy proxy.Proxy } -//GetPubSubControll get service pub sub controller +// GetPubSubControll get service pub sub controller func GetPubSubControll() *PubSubControll { if pubSubControll == nil { pubSubControll = &PubSubControll{ - socketproxy: proxy.CreateProxy("dockerlog", "websocket", defaultEventLogEndpoints), + socketproxy: proxy.CreateProxy("dockerlog", "websocket", configs.Default().APIConfig.EventLogEndpoints), } - discover.GetEndpointDiscover().AddProject("event_log_event_http", pubSubControll.socketproxy) + //discover.GetEndpointDiscover().AddProject("event_log_event_http", pubSubControll.socketproxy) } return pubSubControll } -//Get pubsub controller +// Get pubsub controller func (d PubSubControll) Get(w http.ResponseWriter, r *http.Request) { serviceID := chi.URLParam(r, "serviceID") name, _ := handler.GetEventHandler().GetLogInstance(serviceID) @@ -207,7 +204,7 @@ func (d PubSubControll) Get(w http.ResponseWriter, r *http.Request) { d.socketproxy.Proxy(w, r) } -//GetHistoryLog get service docker logs +// GetHistoryLog get service docker logs func (d PubSubControll) GetHistoryLog(w http.ResponseWriter, r *http.Request) { serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string) name, _ := handler.GetEventHandler().GetLogInstance(serviceID) diff --git a/api/daemon/region_api.go b/api/daemon/region_api.go deleted file mode 100644 index cdeb27a32..000000000 --- a/api/daemon/region_api.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package daemon - -import ( - "github.com/goodrain/rainbond/api/util" - "net/http" - "os" - "os/exec" - "time" - - "github.com/sirupsen/logrus" -) - -//StartRegionAPI 启动 -func StartRegionAPI(ch chan os.Signal) { - logrus.Info("old region api begin start..") - arg := []string{"region_api.wsgi", "-b=127.0.0.1:8887", "--max-requests=5000", "--reload", "--debug", "--workers=4", "--log-file", "-", "--access-logfile", "-", "--error-logfile", "-"} - cmd := exec.Command("gunicorn", arg...) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - go func() { - if err := cmd.Start(); err != nil { - logrus.Error("start region old api error.", err.Error()) - } - tick := time.NewTicker(time.Second * 5) - select { - case si := <-ch: - cmd.Process.Signal(si) - return - case <-tick.C: - monitor() - } - }() - return -} -func monitor() { - response, err := http.Get("http://127.0.0.1:8887/monitor") - if err != nil { - logrus.Error("monitor region old api error.", err.Error()) - return - } - defer util.CloseResponse(response) - if response != nil && response.StatusCode/100 > 2 { - logrus.Errorf("monitor region old api error. response code is %s", response.Status) - } - -} diff --git a/api/db/db.go b/api/db/db.go index 91f38a558..1f453e987 100644 --- a/api/db/db.go +++ b/api/db/db.go @@ -19,37 +19,38 @@ package db import ( - "encoding/json" + "context" + "github.com/goodrain/rainbond/config/configs" "time" tsdbClient "github.com/bluebreezecf/opentsdb-goclient/client" tsdbConfig "github.com/bluebreezecf/opentsdb-goclient/config" - "github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db/config" dbModel "github.com/goodrain/rainbond/db/model" - "github.com/goodrain/rainbond/event" - "github.com/goodrain/rainbond/mq/api/grpc/pb" - "github.com/goodrain/rainbond/mq/client" - etcdutil "github.com/goodrain/rainbond/util/etcd" "github.com/goodrain/rainbond/worker/discover/model" "github.com/jinzhu/gorm" "github.com/sirupsen/logrus" ) -//ConDB struct +// ConDB struct type ConDB struct { ConnectionInfo string DBType string } -//CreateDBManager get db manager -//TODO: need to try when happened error, try 4 times -func CreateDBManager(conf option.Config) error { +// Database - +func Database() *ConDB { + return &ConDB{} +} + +// Start - +func (d *ConDB) Start(ctx context.Context, cfg *configs.Config) error { + logrus.Info("start db client...") dbCfg := config.Config{ - MysqlConnectionInfo: conf.DBConnectionInfo, - DBType: conf.DBType, - ShowSQL: conf.ShowSQL, + MysqlConnectionInfo: cfg.APIConfig.DBConnectionInfo, + DBType: cfg.APIConfig.DBType, + ShowSQL: cfg.APIConfig.ShowSQL, } if err := db.CreateManager(dbCfg); err != nil { logrus.Errorf("get db manager failed,%s", err.Error()) @@ -61,65 +62,27 @@ func CreateDBManager(conf option.Config) error { return nil } -//CreateEventManager create event manager -func CreateEventManager(conf option.Config) error { - var tryTime time.Duration - var err error - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: conf.EtcdEndpoint, - CaFile: conf.EtcdCaFile, - CertFile: conf.EtcdCertFile, - KeyFile: conf.EtcdKeyFile, - } - for tryTime < 4 { - tryTime++ - if err = event.NewManager(event.EventConfig{ - EventLogServers: conf.EventLogServers, - DiscoverArgs: etcdClientArgs, - }); err != nil { - logrus.Errorf("get event manager failed, try time is %v,%s", tryTime, err.Error()) - time.Sleep((5 + tryTime*10) * time.Second) - } else { - break - } - } +// CloseHandle - +func (d *ConDB) CloseHandle() { + err := db.CloseManager() if err != nil { - logrus.Errorf("get event manager failed. %v", err.Error()) - return err + logrus.Errorf("close db manager failed,%s", err.Error()) } - logrus.Debugf("init event manager success") - return nil } -//MQManager mq manager -type MQManager struct { - EtcdClientArgs *etcdutil.ClientArgs - DefaultServer string -} - -//NewMQManager new mq manager -func (m *MQManager) NewMQManager() (client.MQClient, error) { - client, err := client.NewMqClient(m.EtcdClientArgs, m.DefaultServer) - if err != nil { - logrus.Errorf("new mq manager error, %v", err) - return client, err - } - return client, nil -} - -//TaskStruct task struct +// TaskStruct task struct type TaskStruct struct { TaskType string TaskBody model.TaskBody User string } -//OpentsdbManager OpentsdbManager +// OpentsdbManager OpentsdbManager type OpentsdbManager struct { Endpoint string } -//NewOpentsdbManager NewOpentsdbManager +// NewOpentsdbManager NewOpentsdbManager func (o *OpentsdbManager) NewOpentsdbManager() (tsdbClient.Client, error) { opentsdbCfg := tsdbConfig.OpenTSDBConfig{ OpentsdbHost: o.Endpoint, @@ -131,25 +94,7 @@ func (o *OpentsdbManager) NewOpentsdbManager() (tsdbClient.Client, error) { return tc, nil } -//BuildTask build task -func BuildTask(t *TaskStruct) (*pb.EnqueueRequest, error) { - var er pb.EnqueueRequest - taskJSON, err := json.Marshal(t.TaskBody) - if err != nil { - logrus.Errorf("tran task json error") - return &er, err - } - er.Topic = "worker" - er.Message = &pb.TaskMessage{ - TaskType: t.TaskType, - CreateTime: time.Now().Format(time.RFC3339), - TaskBody: taskJSON, - User: t.User, - } - return &er, nil -} - -//GetBegin get db transaction +// GetBegin get db transaction func GetBegin() *gorm.DB { return db.GetManager().Begin() } diff --git a/api/handler/LicenseHandler_test.go b/api/handler/LicenseHandler_test.go deleted file mode 100644 index b303ac6b8..000000000 --- a/api/handler/LicenseHandler_test.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package handler - -import ( - "fmt" - "github.com/goodrain/rainbond/api/db" - "github.com/goodrain/rainbond/cmd/api/option" - "testing" -) - -func TestLicenseInfo(t *testing.T) { - conf := option.Config{ - DBType: "mysql", - DBConnectionInfo: "admin:admin@tcp(127.0.0.1:3306)/region", - } - //创建db manager - if err := db.CreateDBManager(conf); err != nil { - fmt.Printf("create db manager error, %v", err) - - } - //创建license验证 manager - if err := CreateLicensesInfoManager(); err != nil { - fmt.Printf("create license check manager error, %v", err) - } - lists, err := GetLicensesInfosHandler().ShowInfos() - if err != nil { - fmt.Printf("get list error, %v", err) - } - for _, v := range lists { - fmt.Printf("license value is %v", v) - } -} diff --git a/api/handler/eventLog_test.go b/api/handler/eventLog_test.go deleted file mode 100644 index 93ead3557..000000000 --- a/api/handler/eventLog_test.go +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package handler - -import ( - "fmt" - "os/exec" - "testing" - "time" - - api_db "github.com/goodrain/rainbond/api/db" - api_model "github.com/goodrain/rainbond/api/model" - "github.com/goodrain/rainbond/cmd/api/option" - - "github.com/sirupsen/logrus" -) - -func TestEmessage(t *testing.T) { - fmt.Printf("begin.\n") - conf := option.Config{ - DBType: "mysql", - DBConnectionInfo: "admin:admin@tcp(127.0.0.1:3306)/region", - } - //创建db manager - if err := api_db.CreateDBManager(conf); err != nil { - fmt.Printf("create db manager error, %v", err) - } - getLevelLog("dd09a25eb9744afa9b3ad5f5541013e7", "info") - fmt.Printf("end.\n") -} - -func getLevelLog(eventID string, level string) (*api_model.DataLog, error) { - //messages, err := db.GetManager().EventLogDao().GetEventLogMessages(eventID) - //if err != nil { - // return nil, err - //} - ////var d []api_model.MessageData - //for _, v := range messages { - // log, err := uncompress(v.Message) - // if err != nil { - // return nil, err - // } - // logrus.Debugf("log is %v", log) - // fmt.Printf("log is %v", string(log)) - // - // var mLogs []msgStruct - // if err := ffjson.Unmarshal(log, &mLogs); err != nil { - // return nil, err - // } - // fmt.Printf("jlog %v", mLogs) - // break - //} - return nil, nil -} - -type msgStruct struct { - EventID string `json:"event_id"` - Step string `json:"step"` - Message string `json:"message"` - Level string `json:"level"` - Time string `json:"time"` -} - -func TestLines(t *testing.T) { - filePath := "/Users/pujielan/Downloads/log" - logrus.Debugf("file path is %s", filePath) - n := 1000 - f, err := exec.Command("tail", "-n", fmt.Sprintf("%d", n), filePath).Output() - if err != nil { - fmt.Printf("err if %v", err) - } - fmt.Printf("f is %v", string(f)) -} - -func TestTimes(t *testing.T) { - //toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写 - toBeCharge := "2017-09-29T10:02:44+08:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板走的 修改模板的话也可以不写 - timeLayout := "2006-01-02T15:04:05" //转化所需模板 - loc, _ := time.LoadLocation("Local") //重要:获取时区 - //toBeCharge = strings.Split(toBeCharge, ".")[0] - fmt.Println(toBeCharge) - theTime, err := time.ParseInLocation(timeLayout, toBeCharge, loc) //使用模板在对应时区转化为time.time类型 - fmt.Println(err) - sr := theTime.Unix() //转化为时间戳 类型是int64 - fmt.Println(theTime) //打印输出theTime 2015-01-01 15:15:00 +0800 CST - fmt.Println(sr) -} - -func TestSort(t *testing.T) { - arr := [...]int{3, 41, 24, 76, 11, 45, 3, 3, 64, 21, 69, 19, 36} - fmt.Println(arr) - num := len(arr) - - //循环排序 - for i := 0; i < num; i++ { - for j := i + 1; j < num; j++ { - if arr[i] > arr[j] { - temp := arr[i] - arr[i] = arr[j] - arr[j] = temp - } - } - } - fmt.Println(arr) -} - -func quickSort(array []int, left int, right int) { - if left < right { - key := array[left] - low := left - high := right - for low < high { - for low < high && array[high] > key { - high-- - } - array[low] = array[high] - for low < high && array[low] < key { - low++ - } - array[high] = array[low] - } - array[low] = key - quickSort(array, left, low-1) - quickSort(array, low+1, right) - } -} - -func qsort(array []int, low, high int) { - if low < high { - m := partition(array, low, high) - // fmt.Println(m) - qsort(array, low, m-1) - qsort(array, m+1, high) - } -} - -func partition(array []int, low, high int) int { - key := array[low] - tmpLow := low - tmpHigh := high - for { - //查找小于等于key的元素,该元素的位置一定是tmpLow到high之间,因为array[tmpLow]及左边元素小于等于key,不会越界 - for array[tmpHigh] > key { - tmpHigh-- - } - //找到大于key的元素,该元素的位置一定是low到tmpHigh+1之间。因为array[tmpHigh+1]必定大于key - for array[tmpLow] <= key && tmpLow < tmpHigh { - tmpLow++ - } - - if tmpLow >= tmpHigh { - break - } - // swap(array[tmpLow], array[tmpHigh]) - array[tmpLow], array[tmpHigh] = array[tmpHigh], array[tmpLow] - fmt.Println(array) - } - array[tmpLow], array[low] = array[low], array[tmpLow] - return tmpLow -} - -func TestFastSort(t *testing.T) { - var sortArray = []int{3, 41, 24, 76, 11, 45, 3, 3, 64, 21, 69, 19, 36} - fmt.Println(sortArray) - qsort(sortArray, 0, len(sortArray)-1) - fmt.Println(sortArray) -} diff --git a/api/handler/handler.go b/api/handler/handler.go index 6a32c23c9..966bbcfdf 100644 --- a/api/handler/handler.go +++ b/api/handler/handler.go @@ -19,64 +19,46 @@ package handler import ( - "github.com/coreos/etcd/clientv3" - "github.com/goodrain/rainbond/api/client/prometheus" - api_db "github.com/goodrain/rainbond/api/db" "github.com/goodrain/rainbond/api/handler/group" "github.com/goodrain/rainbond/api/handler/share" - "github.com/goodrain/rainbond/builder/sources/registry" "github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/db" - "github.com/goodrain/rainbond/pkg/generated/clientset/versioned" - etcdutil "github.com/goodrain/rainbond/util/etcd" - "github.com/goodrain/rainbond/worker/client" + "github.com/goodrain/rainbond/pkg/component/etcd" + "github.com/goodrain/rainbond/pkg/component/grpc" + "github.com/goodrain/rainbond/pkg/component/hubregistry" + "github.com/goodrain/rainbond/pkg/component/k8s" + "github.com/goodrain/rainbond/pkg/component/mq" + "github.com/goodrain/rainbond/pkg/component/prom" "github.com/sirupsen/logrus" - "k8s.io/apimachinery/pkg/api/meta" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "kubevirt.io/client-go/kubecli" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" - gateway "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1beta1" ) // InitHandle 初始化handle -func InitHandle(conf option.Config, - etcdClientArgs *etcdutil.ClientArgs, - statusCli *client.AppRuntimeSyncClient, - etcdcli *clientv3.Client, - kubeClient *kubernetes.Clientset, - rainbondClient versioned.Interface, - k8sClient k8sclient.Client, - config *rest.Config, - mapper meta.RESTMapper, - dynamicClient dynamic.Interface, - gatewayClient *gateway.GatewayV1beta1Client, - kubevirtCli kubecli.KubevirtClient, - registryCli *registry.Registry, -) error { - mq := api_db.MQManager{ - EtcdClientArgs: etcdClientArgs, - DefaultServer: conf.MQAPI, - } - mqClient, errMQ := mq.NewMQManager() - if errMQ != nil { - logrus.Errorf("new MQ manager failed, %v", errMQ) - return errMQ - } - prometheusCli, err := prometheus.NewPrometheus(&prometheus.Options{ - Endpoint: conf.PrometheusEndpoint, - }) - if err != nil { - logrus.Errorf("new prometheus client failure, %v", err) - return err - } +func InitHandle(conf option.Config) error { + + // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可 + // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可 + // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可 + + etcdcli := etcd.Default().EtcdClient + statusCli := grpc.Default().StatusClient + clientset := k8s.Default().Clientset + rainbondClient := k8s.Default().RainbondClient + k8sClient := k8s.Default().K8sClient + restconfig := k8s.Default().RestConfig + dynamicClient := k8s.Default().DynamicClient + gatewayClient := k8s.Default().GatewayClient + kubevirtCli := k8s.Default().KubevirtCli + mapper := k8s.Default().Mapper + registryCli := hubregistry.Default().RegistryCli + mqClient := mq.Default().MqClient + prometheusCli := prom.Default().PrometheusCli + dbmanager := db.GetManager() - defaultServieHandler = CreateManager(conf, mqClient, etcdcli, statusCli, prometheusCli, rainbondClient, kubeClient, kubevirtCli, dbmanager, registryCli) + defaultServieHandler = CreateManager(conf, mqClient, etcdcli, statusCli, prometheusCli, rainbondClient, clientset, kubevirtCli, dbmanager, registryCli) defaultPluginHandler = CreatePluginManager(mqClient) defaultAppHandler = CreateAppManager(mqClient) - defaultTenantHandler = CreateTenManager(mqClient, statusCli, &conf, kubeClient, prometheusCli, k8sClient) - defaultHelmHandler = CreateHelmManager(kubeClient, rainbondClient, config, mapper) + defaultTenantHandler = CreateTenManager(mqClient, statusCli, &conf, clientset, prometheusCli, k8sClient) + defaultHelmHandler = CreateHelmManager(clientset, rainbondClient, restconfig, mapper) defaultNetRulesHandler = CreateNetRulesManager(etcdcli) defaultCloudHandler = CreateCloudManager(conf) defaultAPPBackupHandler = group.CreateBackupHandle(mqClient, statusCli, etcdcli) @@ -87,20 +69,20 @@ func InitHandle(conf option.Config, logrus.Errorf("create token identification mannager error, %v", err) return err } - defaultGatewayHandler = CreateGatewayManager(dbmanager, mqClient, etcdcli, gatewayClient, kubeClient) + defaultGatewayHandler = CreateGatewayManager(dbmanager, mqClient, etcdcli, gatewayClient, clientset) def3rdPartySvcHandler = Create3rdPartySvcHandler(dbmanager, statusCli) operationHandler = CreateOperationHandler(mqClient) batchOperationHandler = CreateBatchOperationHandler(mqClient, statusCli, operationHandler) defaultAppRestoreHandler = NewAppRestoreHandler() defPodHandler = NewPodHandler(statusCli) - defClusterHandler = NewClusterHandler(kubeClient, conf.RbdNamespace, conf.GrctlImage, config, mapper, prometheusCli, rainbondClient, statusCli, dynamicClient, gatewayClient, mqClient) + defClusterHandler = NewClusterHandler(clientset, conf.RbdNamespace, conf.GrctlImage, restconfig, mapper, prometheusCli, rainbondClient, statusCli, dynamicClient, gatewayClient, mqClient) defaultVolumeTypeHandler = CreateVolumeTypeManger(statusCli) defaultEtcdHandler = NewEtcdHandler(etcdcli) defaultmonitorHandler = NewMonitorHandler(prometheusCli) defServiceEventHandler = NewServiceEventHandler() - defApplicationHandler = NewApplicationHandler(statusCli, prometheusCli, rainbondClient, kubeClient, dynamicClient) - defRegistryAuthSecretHandler = CreateRegistryAuthSecretManager(dbmanager, mqClient, etcdcli) - defNodesHandler = NewNodesHandler(kubeClient, conf.RbdNamespace, config, mapper, prometheusCli) + defApplicationHandler = NewApplicationHandler(statusCli, prometheusCli, rainbondClient, clientset, dynamicClient) + defRegistryAuthSecretHandler = CreateRegistryAuthSecretManager(dbmanager, mqClient) + defNodesHandler = NewNodesHandler(clientset, conf.RbdNamespace, restconfig, mapper, prometheusCli) return nil } diff --git a/api/handler/netRules_test.go b/api/handler/netRules_test.go deleted file mode 100644 index 35aa41a8c..000000000 --- a/api/handler/netRules_test.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package handler - -import ( - "context" - "fmt" - "time" - - api_model "github.com/goodrain/rainbond/api/model" - - "testing" -) - -func TestStoreETCD(t *testing.T) { - cli, err := clientv3.New(clientv3.Config{ - Endpoints: []string{"127.0.0.1:2379"}, - DialTimeout: 10 * time.Second, - }) - if err != nil { - t.Error(err) - } - nra := &NetRulesAction{ - etcdCli: cli, - } - rules := &api_model.NetDownStreamRules{ - Limit: 1024, - //Header: "E1:V1,E2:V2", - //Domain: "test.redis.com", - //Prefix: "/redis", - } - - srs := &api_model.SetNetDownStreamRuleStruct{ - TenantName: "123", - ServiceAlias: "grtest12", - } - srs.Body.DestService = "redis" - srs.Body.DestServiceAlias = "grtest34" - srs.Body.Port = 6379 - srs.Body.Protocol = "tcp" - srs.Body.Rules = rules - - tenantID := "tenantid1b50sfadfadfafadfadfadf" - - if err := nra.CreateDownStreamNetRules(tenantID, srs); err != nil { - t.Error(err) - } - - k := fmt.Sprintf("/netRules/%s/%s/downstream/%s/%v", - tenantID, srs.ServiceAlias, srs.Body.DestServiceAlias, srs.Body.Port) - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - v, err := cli.Get(ctx, k) - cancel() - if err != nil { - t.Error(err) - } - fmt.Printf("v is %v\n", string(v.Kvs[0].Value)) -} diff --git a/api/handler/proxy.go b/api/handler/proxy.go index 68ac6193e..bf042dd3f 100644 --- a/api/handler/proxy.go +++ b/api/handler/proxy.go @@ -19,7 +19,6 @@ package handler import ( - "github.com/goodrain/rainbond/api/discover" "github.com/goodrain/rainbond/api/proxy" "github.com/goodrain/rainbond/cmd/api/option" ) @@ -30,11 +29,10 @@ var prometheusProxy proxy.Proxy var monitorProxy proxy.Proxy var kubernetesDashboard proxy.Proxy -//InitProxy 初始化 +// InitProxy 初始化 func InitProxy(conf option.Config) { if nodeProxy == nil { nodeProxy = proxy.CreateProxy("acp_node", "http", conf.NodeAPI) - discover.GetEndpointDiscover().AddProject("acp_node", nodeProxy) } if builderProxy == nil { builderProxy = proxy.CreateProxy("builder", "http", conf.BuilderAPI) @@ -42,34 +40,34 @@ func InitProxy(conf option.Config) { if prometheusProxy == nil { prometheusProxy = proxy.CreateProxy("prometheus", "http", []string{conf.PrometheusEndpoint}) } - if monitorProxy == nil { - monitorProxy = proxy.CreateProxy("monitor", "http", []string{"127.0.0.1:3329"}) - discover.GetEndpointDiscover().AddProject("monitor", monitorProxy) - } + //if monitorProxy == nil { + // monitorProxy = proxy.CreateProxy("monitor", "http", []string{"127.0.0.1:3329"}) + // discover.GetEndpointDiscover().AddProject("monitor", monitorProxy) + //} if kubernetesDashboard == nil { kubernetesDashboard = proxy.CreateProxy("kubernetesdashboard", "http", []string{conf.KuberentesDashboardAPI}) } } -//GetNodeProxy GetNodeProxy +// GetNodeProxy GetNodeProxy func GetNodeProxy() proxy.Proxy { return nodeProxy } -//GetBuilderProxy GetNodeProxy +// GetBuilderProxy GetNodeProxy func GetBuilderProxy() proxy.Proxy { return builderProxy } -//GetPrometheusProxy GetPrometheusProxy +// GetPrometheusProxy GetPrometheusProxy func GetPrometheusProxy() proxy.Proxy { return prometheusProxy } //GetMonitorProxy GetMonitorProxy -func GetMonitorProxy() proxy.Proxy { - return monitorProxy -} +//func GetMonitorProxy() proxy.Proxy { +// return monitorProxy +//} // GetKubernetesDashboardProxy returns the kubernetes dashboard proxy. func GetKubernetesDashboardProxy() proxy.Proxy { diff --git a/api/handler/registry_auth_secret_action.go b/api/handler/registry_auth_secret_action.go index 74a43cb59..cd08902ba 100644 --- a/api/handler/registry_auth_secret_action.go +++ b/api/handler/registry_auth_secret_action.go @@ -21,7 +21,6 @@ package handler import ( "fmt" - "github.com/coreos/etcd/clientv3" apimodel "github.com/goodrain/rainbond/api/model" "github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/mq/client" @@ -31,15 +30,13 @@ import ( type RegistryAuthSecretAction struct { dbmanager db.Manager mqclient client.MQClient - etcdCli *clientv3.Client } // CreateRegistryAuthSecretManager creates registry auth secret manager -func CreateRegistryAuthSecretManager(dbmanager db.Manager, mqclient client.MQClient, etcdCli *clientv3.Client) *RegistryAuthSecretAction { +func CreateRegistryAuthSecretManager(dbmanager db.Manager, mqclient client.MQClient) *RegistryAuthSecretAction { return &RegistryAuthSecretAction{ dbmanager: dbmanager, mqclient: mqclient, - etcdCli: etcdCli, } } diff --git a/api/handler/rootFunc_test.go b/api/handler/rootFunc_test.go deleted file mode 100644 index 6a7dc9e2c..000000000 --- a/api/handler/rootFunc_test.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package handler diff --git a/api/handler/service_check.go b/api/handler/service_check.go index f4e3c65a4..95c7c9df2 100644 --- a/api/handler/service_check.go +++ b/api/handler/service_check.go @@ -34,7 +34,7 @@ import ( "github.com/twinj/uuid" ) -//ServiceCheck check service build source +// ServiceCheck check service build source func (s *ServiceAction) ServiceCheck(scs *api_model.ServiceCheckStruct) (string, string, *util.APIHandleError) { checkUUID := uuid.NewV4().String() scs.Body.CheckUUID = checkUUID @@ -76,16 +76,16 @@ func maybeIsWindowsContainerImage(source string) bool { } -//GetServiceCheckInfo get application source detection information +// GetServiceCheckInfo get application source detection information func (s *ServiceAction) GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError) { k := fmt.Sprintf("/servicecheck/%s", uuid) var si exector.ServiceCheckResult - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() resp, err := s.EtcdCli.Get(ctx, k) if err != nil { logrus.Errorf("get etcd k %s error, %v", k, err) - return nil, util.CreateAPIHandleError(500, err) + return nil, util.CreateAPIHandleError(503, err) } if resp.Count == 0 { return &si, nil diff --git a/api/middleware/middleware.go b/api/middleware/middleware.go index 0a8aea5cd..b458100cc 100644 --- a/api/middleware/middleware.go +++ b/api/middleware/middleware.go @@ -46,7 +46,7 @@ func init() { } } -//InitTenant 实现中间件 +// InitTenant 实现中间件 func InitTenant(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { debugRequestBody(r) @@ -75,7 +75,7 @@ func InitTenant(next http.Handler) http.Handler { return http.HandlerFunc(fn) } -//InitService 实现serviceinit中间件 +// InitService 实现serviceinit中间件 func InitService(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { serviceAlias := chi.URLParam(r, "service_alias") @@ -120,7 +120,7 @@ func InitApplication(next http.Handler) http.Handler { return http.HandlerFunc(fn) } -//InitPlugin 实现plugin init中间件 +// InitPlugin 实现plugin init中间件 func InitPlugin(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { debugRequestBody(r) @@ -147,7 +147,7 @@ func InitPlugin(next http.Handler) http.Handler { return http.HandlerFunc(fn) } -//SetLog SetLog +// SetLog SetLog func SetLog(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { eventID := chi.URLParam(r, "event_id") @@ -160,7 +160,7 @@ func SetLog(next http.Handler) http.Handler { return http.HandlerFunc(fn) } -//Proxy 反向代理中间件 +// Proxy 反向代理中间件 func Proxy(next http.Handler) http.Handler { fn := func(w http.ResponseWriter, r *http.Request) { if strings.HasPrefix(r.RequestURI, "/v2/nodes") { @@ -191,10 +191,10 @@ func Proxy(next http.Handler) http.Handler { handler.GetNodeProxy().Proxy(w, r) return } - if strings.HasPrefix(r.RequestURI, "/v2/rules") { - handler.GetMonitorProxy().Proxy(w, r) - return - } + //if strings.HasPrefix(r.RequestURI, "/v2/rules") { + // handler.GetMonitorProxy().Proxy(w, r) + // return + //} if strings.HasPrefix(r.RequestURI, "/kubernetes/dashboard") { proxy := handler.GetKubernetesDashboardProxy() r.URL.Path = strings.Replace(r.URL.Path, "/kubernetes/dashboard", "", 1) @@ -242,7 +242,7 @@ func (w *resWriter) WriteHeader(statusCode int) { func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var ( - serviceKind string + serviceKind string ) serviceObj := r.Context().Value(ctxutil.ContextKey("service")) if serviceObj != nil { diff --git a/api/server/api.go b/api/server/api.go index c05ab5574..4825a4e50 100644 --- a/api/server/api.go +++ b/api/server/api.go @@ -23,6 +23,7 @@ import ( "crypto/tls" "crypto/x509" "github.com/coreos/etcd/clientv3" + "github.com/goodrain/rainbond/pkg/interceptors" "io/ioutil" "log" "net/http" @@ -105,7 +106,7 @@ func (m *Manager) SetMiddleware() { r.Use(middleware.DefaultLogger) } //Gracefully absorb panics and prints the stack trace - r.Use(middleware.Recoverer) + r.Use(interceptors.Recoverer) //request time out r.Use(middleware.Timeout(time.Second * 5)) //simple authz diff --git a/builder/build/code_build_test.go b/builder/build/code_build_test.go deleted file mode 100644 index b79235939..000000000 --- a/builder/build/code_build_test.go +++ /dev/null @@ -1,171 +0,0 @@ -package build - -import ( - "archive/tar" - "compress/gzip" - "context" - "fmt" - "io" - "os" - "strings" - "testing" - "time" - - "github.com/docker/docker/api/types" - "github.com/docker/docker/client" - jobc "github.com/goodrain/rainbond/builder/job" - "github.com/goodrain/rainbond/builder/parser/code" - "github.com/goodrain/rainbond/builder/sources" - "github.com/goodrain/rainbond/cmd/builder/option" - "github.com/goodrain/rainbond/event" - - etcdutil "github.com/goodrain/rainbond/util/etcd" - k8sutil "github.com/goodrain/rainbond/util/k8s" - - "k8s.io/client-go/kubernetes" -) - -func TestCreateJob(t *testing.T) { - conf := option.Config{ - EtcdEndPoints: []string{"192.168.2.203:2379"}, - MQAPI: "192.168.2.203:6300", - EventLogServers: []string{"192.168.2.203:6366"}, - RbdRepoName: "rbd-dns", - RbdNamespace: "rbd-system", - MysqlConnectionInfo: "EeM2oc:lee7OhQu@tcp(192.168.2.203:3306)/region", - } - event.NewManager(event.EventConfig{ - EventLogServers: conf.EventLogServers, - DiscoverArgs: &etcdutil.ClientArgs{Endpoints: conf.EtcdEndPoints}, - }) - restConfig, err := k8sutil.NewRestConfig("/Users/fanyangyang/Documents/company/goodrain/remote/192.168.2.206/admin.kubeconfig") - if err != nil { - t.Fatal(err) - } - clientset, err := kubernetes.NewForConfig(restConfig) - if err != nil { - t.Fatal(err) - } - dockerClient, err := client.NewEnvClient() - if err != nil { - t.Fatal("new docker error: ", err.Error()) - } - logger := event.GetManager().GetLogger("0000") - req := Request{ - ServerType: "git", - DockerClient: dockerClient, - KubeClient: clientset, - ServiceID: "d9b8d718510dc53118af1e1219e36d3a", - DeployVersion: "123", - TenantID: "7c89455140284fd7b263038b44dc65bc", - Lang: code.JavaMaven, - Runtime: "1.8", - Logger: logger, - } - req.BuildEnvs = map[string]string{ - "PROCFILE": "web: java $JAVA_OPTS -jar target/java-maven-demo-0.0.1.jar", - "PROC_ENV": `{"procfile": "", "dependencies": {}, "language": "Java-maven", "runtimes": "1.8"}`, - "RUNTIME": "1.8", - } - req.CacheDir = fmt.Sprintf("/cache/build/%s/cache/%s", req.TenantID, req.ServiceID) - req.TGZDir = fmt.Sprintf("/grdata/build/tenant/%s/slug/%s", req.TenantID, req.ServiceID) - req.SourceDir = fmt.Sprintf("/cache/source/build/%s/%s", req.TenantID, req.ServiceID) - sb := slugBuild{tgzDir: "string"} - if err := sb.runBuildJob(&req); err != nil { - t.Fatal(err) - } - fmt.Println("create job finished") - -} - -func Test1(t *testing.T) { - tarFile := "/opt/rainbond/pkg/rainbond-pkg-V5.2-dev.tgz" - srcFile, err := os.Open(tarFile) - if err != nil { - t.Fatal(err) - } - defer srcFile.Close() - gr, err := gzip.NewReader(srcFile) //handle gzip feature - if err != nil { - t.Fatal(err) - } - defer gr.Close() - tr := tar.NewReader(gr) // tar reader - now := time.Now() - for hdr, err := tr.Next(); err != io.EOF; hdr, err = tr.Next() { // next range tar info - if err != nil { - t.Fatal(err) - } - // 读取文件信息 - fi := hdr.FileInfo() - - if !strings.HasPrefix(fi.Name(), "._") && strings.HasSuffix(fi.Name(), ".tgz") { - t.Logf("name: %s, size: %d", fi.Name(), fi.Size()) - - } - } - t.Logf("cost: %d", time.Since(now)) -} - -func TestDockerClient(t *testing.T) { - dockerClient, err := client.NewEnvClient() - if err != nil { - t.Fatal("new docker error: ", err.Error()) - } - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - containers, err := dockerClient.ContainerList(ctx, types.ContainerListOptions{}) - if err != nil { - t.Fatal(err) - } - for _, container := range containers { - t.Log("container id : ", container.ID) - } - // images, err := dockerClient.ImageList(ctx, types.ImageListOptions{}) - // for _, image := range images { - // t.Log("image is : ", image.ID) - // } -} - -func TestBuildFromOSS(t *testing.T) { - restConfig, err := k8sutil.NewRestConfig("/Users/barnett/.kube/config") - if err != nil { - t.Fatal(err) - } - os.Setenv("IMAGE_PULL_SECRET", "rbd-hub-credentials") - clientset, err := kubernetes.NewForConfig(restConfig) - if err != nil { - t.Fatal(err) - } - stop := make(chan struct{}) - if err := jobc.InitJobController("rbd-system", stop, clientset); err != nil { - t.Fatal(err) - } - logger := event.GetTestLogger() - req := &Request{ - ServerType: "oss", - RepositoryURL: "http://8081.gr021644.64q1jlfb.17f4cc.grapps.cn/artifactory/dev/java-war-demo-master.zip", - CodeSouceInfo: sources.CodeSourceInfo{ - User: "demo", - Password: "gr123465!", - }, - KubeClient: clientset, - Ctx: context.Background(), - ServiceID: "d9b8d718510dc53118af1e1219e36d3a", - DeployVersion: "123asdadsadsasdasd1", - TenantID: "7c89455140284fd7b263038b44dc65bc", - Lang: code.OSS, - Logger: logger, - GRDataPVCName: "rbd-cpt-grdata", - CachePVCName: "rbd-chaos-cache", - } - build, err := GetBuild(code.OSS) - if err != nil { - t.Fatal(err) - } - res, err := build.Build(req) - if err != nil { - t.Fatal(err) - } - t.Log(res.MediumPath) -} diff --git a/builder/build/netcore_build_test.go b/builder/build/netcore_build_test.go deleted file mode 100644 index 116bc7172..000000000 --- a/builder/build/netcore_build_test.go +++ /dev/null @@ -1,51 +0,0 @@ -// 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 . - -package build - -import ( - "testing" - - "github.com/docker/docker/client" - "github.com/goodrain/rainbond/builder/parser/code" - "github.com/goodrain/rainbond/event" -) - -func TestBuildNetCore(t *testing.T) { - build, err := netcoreBuilder() - if err != nil { - t.Fatal(err) - } - dockerCli, _ := client.NewEnvClient() - req := &Request{ - SourceDir: "/Users/qingguo/goodrain/dotnet-docker/samples/aspnetapp/test", - CacheDir: "/Users/qingguo/goodrain/dotnet-docker/samples/aspnetapp/test/cache", - RepositoryURL: "https://github.com/dotnet/dotnet-docker.git", - ServiceAlias: "gr123456", - DeployVersion: "666666", - Commit: Commit{User: "barnett"}, - Lang: code.NetCore, - Logger: event.GetTestLogger(), - DockerClient: dockerCli, - } - res, err := build.Build(req) - if err != nil { - t.Fatal(err) - } - t.Log(*res) -} diff --git a/builder/exector/exector_test.go b/builder/exector/exector_test.go deleted file mode 100644 index 358c84b1e..000000000 --- a/builder/exector/exector_test.go +++ /dev/null @@ -1,110 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package exector - -import ( - "context" - "encoding/json" - "runtime" - "testing" - "time" - - "github.com/docker/docker/client" - "k8s.io/client-go/kubernetes" - - "github.com/goodrain/rainbond/builder/parser/code" - "github.com/goodrain/rainbond/cmd/builder/option" - "github.com/goodrain/rainbond/event" - "github.com/goodrain/rainbond/mq/api/grpc/pb" - - mqclient "github.com/goodrain/rainbond/mq/client" - etcdutil "github.com/goodrain/rainbond/util/etcd" - k8sutil "github.com/goodrain/rainbond/util/k8s" -) - -func Test_exectorManager_buildFromSourceCode(t *testing.T) { - conf := option.Config{ - EtcdEndPoints: []string{"192.168.2.203:2379"}, - MQAPI: "192.168.2.203:6300", - EventLogServers: []string{"192.168.2.203:6366"}, - RbdRepoName: "rbd-dns", - RbdNamespace: "rbd-system", - MysqlConnectionInfo: "EeM2oc:lee7OhQu@tcp(192.168.2.203:3306)/region", - } - etcdArgs := etcdutil.ClientArgs{Endpoints: conf.EtcdEndPoints} - event.NewManager(event.EventConfig{ - EventLogServers: conf.EventLogServers, - DiscoverArgs: &etcdArgs, - }) - restConfig, err := k8sutil.NewRestConfig("/Users/fanyangyang/Documents/company/goodrain/admin.kubeconfig") - if err != nil { - t.Fatal(err) - } - kubeClient, err := kubernetes.NewForConfig(restConfig) - dockerClient, err := client.NewEnvClient() - if err != nil { - t.Fatal(err) - } - etcdCli, err := clientv3.New(clientv3.Config{ - Endpoints: conf.EtcdEndPoints, - DialTimeout: 10 * time.Second, - }) - var maxConcurrentTask int - if conf.MaxTasks == 0 { - maxConcurrentTask = runtime.NumCPU() * 2 - } else { - maxConcurrentTask = conf.MaxTasks - } - mqClient, err := mqclient.NewMqClient(&etcdArgs, conf.MQAPI) - if err != nil { - t.Fatal(err) - } - ctx, cancel := context.WithCancel(context.Background()) - e := &exectorManager{ - DockerClient: dockerClient, - KubeClient: kubeClient, - EtcdCli: etcdCli, - tasks: make(chan *pb.TaskMessage, maxConcurrentTask), - maxConcurrentTask: maxConcurrentTask, - mqClient: mqClient, - ctx: ctx, - cancel: cancel, - cfg: conf, - } - taskBodym := make(map[string]interface{}) - taskBodym["repo_url"] = "https://github.com/goodrain/java-maven-demo.git" - taskBodym["branch"] = "master" - taskBodym["tenant_id"] = "5d7bd886e6dc4425bb6c2ac5fc9fa593" - taskBodym["service_id"] = "4eaa41ccf145b8e43a6aeb1a5efeab53" - taskBodym["deploy_version"] = "20200115193617" - taskBodym["lang"] = code.JavaMaven - taskBodym["event_id"] = "0000" - taskBodym["envs"] = map[string]string{} - - taskBody, _ := json.Marshal(taskBodym) - task := pb.TaskMessage{ - TaskType: "build_from_source_code", - TaskBody: taskBody, - } - i := NewSouceCodeBuildItem(task.TaskBody) - if err := i.Run(30 * time.Second); err != nil { - t.Fatal(err) - } - e.buildFromSourceCode(&task) -} diff --git a/cmd/api/main.go b/cmd/api/main.go index 57b0d6532..ec976a971 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -20,12 +20,15 @@ package main import ( - "fmt" + "context" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/pkg/component" + "github.com/goodrain/rainbond/pkg/rainbond" + "github.com/sirupsen/logrus" "os" "github.com/goodrain/rainbond/cmd" "github.com/goodrain/rainbond/cmd/api/option" - "github.com/goodrain/rainbond/cmd/api/server" "github.com/spf13/pflag" ) @@ -38,8 +41,25 @@ func main() { s.AddFlags(pflag.CommandLine) pflag.Parse() s.SetLog() - if err := server.Run(s); err != nil { - fmt.Fprintf(os.Stderr, "error: %v\n", err) - os.Exit(1) + + configs.SetDefault(&configs.Config{ + AppName: "rbd-api", + APIConfig: s.Config, + }) + // 启动 rbd-api + err := rainbond.New(context.Background(), configs.Default()).Registry(component.Database()). + Registry(component.Grpc()). + Registry(component.Event()). + Registry(component.K8sClient()). + Registry(component.HubRegistry()). + Registry(component.Proxy()). + Registry(component.Etcd()). + Registry(component.MQ()). + Registry(component.Prometheus()). + Registry(component.Handler()). + Registry(component.Router()). + Start() + if err != nil { + logrus.Errorf("start rbd-api error %s", err.Error()) } } diff --git a/cmd/api/option/option.go b/cmd/api/option/option.go index 20eb23f17..45a25c45b 100644 --- a/cmd/api/option/option.go +++ b/cmd/api/option/option.go @@ -33,6 +33,8 @@ type Config struct { APIAddrSSL string DBConnectionInfo string EventLogServers []string + DockerConsoleServers []string + EventLogEndpoints []string NodeAPI []string BuilderAPI []string V1API string @@ -64,6 +66,8 @@ type Config struct { RbdNamespace string ShowSQL bool GrctlImage string + RbdHub string + RbdWorker string } // APIServer apiserver server @@ -95,12 +99,8 @@ func (a *APIServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.WebsocketCertFile, "ws-ssl-certfile", "/etc/ssl/goodrain.com/goodrain.com.crt", "websocket and fileserver ssl cert file") fs.StringVar(&a.WebsocketKeyFile, "ws-ssl-keyfile", "/etc/ssl/goodrain.com/goodrain.com.key", "websocket and fileserver ssl key file") fs.StringVar(&a.V1API, "v1-api", "127.0.0.1:8887", "the region v1 api") - fs.StringSliceVar(&a.NodeAPI, "node-api", []string{"127.0.0.1:6100"}, "the node server api") fs.StringSliceVar(&a.BuilderAPI, "builder-api", []string{"rbd-chaos:3228"}, "the builder api") - fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"127.0.0.1:6366"}, "event log server address. simple lb") - fs.StringVar(&a.MQAPI, "mq-api", "127.0.0.1:6300", "acp_mq api") fs.BoolVar(&a.StartRegionAPI, "start", false, "Whether to start region old api") - fs.StringSliceVar(&a.EtcdEndpoint, "etcd", []string{"http://127.0.0.1:2379"}, "etcd server or proxy address") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "verify etcd certificates of TLS-enabled secure servers using this CA bundle") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "identify secure etcd client using this TLS certificate file") fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "identify secure etcd client using this TLS key file") @@ -115,10 +115,20 @@ func (a *APIServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogPath, "log-path", "/grdata/logs", "Where Docker log files and event log files are stored.") fs.StringVar(&a.KubeConfigPath, "kube-config", "", "kube config file path, No setup is required to run in a cluster.") fs.StringVar(&a.KuberentesDashboardAPI, "k8s-dashboard-api", "kubernetes-dashboard.rbd-system:443", "The service DNS name of Kubernetes dashboard. Default to kubernetes-dashboard.kubernetes-dashboard") - fs.StringVar(&a.PrometheusEndpoint, "prom-api", "rbd-monitor:9999", "The service DNS name of Prometheus api. Default to rbd-monitor:9999") fs.StringVar(&a.RbdNamespace, "rbd-namespace", "rbd-system", "rbd component namespace") fs.BoolVar(&a.ShowSQL, "show-sql", false, "The trigger for showing sql.") fs.StringVar(&a.GrctlImage, "shell-image", "registry.cn-hangzhou.aliyuncs.com/goodrain/rbd-shell:v5.13.0-release", "use shell image") + + fs.StringSliceVar(&a.EtcdEndpoint, "etcd", []string{"http://rbd-etcd:2379"}, "etcd server or proxy address") + fs.StringSliceVar(&a.DockerConsoleServers, "docker-console", []string{"rbd-webcli:7171"}, "docker console address") + fs.StringVar(&a.PrometheusEndpoint, "prom-api", "rbd-monitor:9999", "The service DNS name of Prometheus api. Default to rbd-monitor:9999") + fs.StringVar(&a.RbdHub, "hub-api", "http://rbd-hub:5000", "the rbd-hub server api") + fs.StringSliceVar(&a.NodeAPI, "node-api", []string{"rbd-node:6100"}, "the rbd-node server api") + fs.StringVar(&a.MQAPI, "mq-api", "rbd-mq:6300", "the rbd-mq server api") + fs.StringVar(&a.RbdWorker, "worker-api", "rbd-worker:6535", "the rbd-worker server api") + fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"rbd-eventlog:6366"}, "event log server address") + fs.StringSliceVar(&a.EventLogEndpoints, "event-log", []string{"local=>rbd-eventlog:6363"}, "event log websocket address") + } // SetLog 设置log diff --git a/cmd/api/server/server.go b/cmd/api/server/server.go deleted file mode 100644 index 589216c99..000000000 --- a/cmd/api/server/server.go +++ /dev/null @@ -1,198 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package server - -import ( - "context" - rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" - "github.com/goodrain/rainbond/api/controller" - "github.com/goodrain/rainbond/api/db" - "github.com/goodrain/rainbond/api/discover" - "github.com/goodrain/rainbond/api/handler" - "github.com/goodrain/rainbond/api/server" - registry "github.com/goodrain/rainbond/builder/sources/registry" - "github.com/goodrain/rainbond/cmd/api/option" - "github.com/goodrain/rainbond/event" - "github.com/goodrain/rainbond/grctl/clients" - "github.com/goodrain/rainbond/pkg/generated/clientset/versioned" - rainbondscheme "github.com/goodrain/rainbond/pkg/generated/clientset/versioned/scheme" - etcdutil "github.com/goodrain/rainbond/util/etcd" - k8sutil "github.com/goodrain/rainbond/util/k8s" - "github.com/goodrain/rainbond/worker/client" - "github.com/pkg/errors" - "github.com/sirupsen/logrus" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - "k8s.io/client-go/dynamic" - "k8s.io/client-go/kubernetes" - clientgoscheme "k8s.io/client-go/kubernetes/scheme" - "k8s.io/client-go/restmapper" - "kubevirt.io/client-go/kubecli" - "os" - "os/signal" - k8sclient "sigs.k8s.io/controller-runtime/pkg/client" - gateway "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1beta1" - "syscall" -) - -// Run start run -func Run(s *option.APIServer) error { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - errChan := make(chan error) - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: s.Config.EtcdEndpoint, - CaFile: s.Config.EtcdCaFile, - CertFile: s.Config.EtcdCertFile, - KeyFile: s.Config.EtcdKeyFile, - } - //启动服务发现 - if _, err := discover.CreateEndpointDiscover(etcdClientArgs); err != nil { - return err - } - //创建db manager - if err := db.CreateDBManager(s.Config); err != nil { - logrus.Debugf("create db manager error, %v", err) - return err - } - //创建event manager - if err := db.CreateEventManager(s.Config); err != nil { - logrus.Debugf("create event manager error, %v", err) - } - - config, err := k8sutil.NewRestConfig(s.KubeConfigPath) - if err != nil { - return err - } - clientset, err := kubernetes.NewForConfig(config) - if err != nil { - return err - } - gatewayClient, err := gateway.NewForConfig(config) - if err != nil { - return err - } - dynamicClient, err := dynamic.NewForConfig(config) - if err != nil { - return err - } - - rainbondClient := versioned.NewForConfigOrDie(config) - - // k8s runtime client - scheme := runtime.NewScheme() - clientgoscheme.AddToScheme(scheme) - rainbondscheme.AddToScheme(scheme) - k8sClient, err := k8sclient.New(config, k8sclient.Options{ - Scheme: scheme, - }) - if err != nil { - return errors.WithMessage(err, "create k8s client") - } - // rest mapper - gr, err := restmapper.GetAPIGroupResources(clientset) - if err != nil { - return err - } - mapper := restmapper.NewDiscoveryRESTMapper(gr) - - if err := event.NewManager(event.EventConfig{ - EventLogServers: s.Config.EventLogServers, - DiscoverArgs: etcdClientArgs, - }); err != nil { - return err - } - defer event.CloseManager() - //create app status client - cli, err := client.NewClient(ctx, client.AppRuntimeSyncClientConf{ - EtcdEndpoints: s.Config.EtcdEndpoint, - EtcdCaFile: s.Config.EtcdCaFile, - EtcdCertFile: s.Config.EtcdCertFile, - EtcdKeyFile: s.Config.EtcdKeyFile, - NonBlock: s.Config.Debug, - }) - if err != nil { - logrus.Errorf("create app status client error, %v", err) - return err - } - - etcdcli, err := etcdutil.NewClient(ctx, etcdClientArgs) - if err != nil { - logrus.Errorf("create etcd client v3 error, %v", err) - return err - } - - kubevirtCli, err := kubecli.GetKubevirtClientFromRESTConfig(config) - if err != nil { - logrus.Errorf("create kubevirt cli failure: %v", err) - return err - } - - var cluster rainbondv1alpha1.RainbondCluster - err = clients.K8SClientInitClient(clientset, config) - if err != nil { - logrus.Errorf("k8s client init rainbondClient failure: %v", err) - return err - } - if err := clients.RainbondKubeClient.Get(context.Background(), types.NamespacedName{Namespace: "rbd-system", Name: "rainbondcluster"}, &cluster); err != nil { - return errors.Wrap(err, "get configuration from rainbond cluster") - } - - registryConfig := cluster.Spec.ImageHub - if registryConfig.Domain == "goodrain.me" { - registryConfig.Domain = "http://rbd-hub:5000" - } - - registryCli, err := registry.NewInsecure(registryConfig.Domain, registryConfig.Username, registryConfig.Password) - if err != nil { - return errors.WithMessage(err, "create registry cleaner") - } - - //初始化 middleware - handler.InitProxy(s.Config) - //创建handle - if err := handler.InitHandle(s.Config, etcdClientArgs, cli, etcdcli, clientset, rainbondClient, k8sClient, config, mapper, dynamicClient, gatewayClient, kubevirtCli, registryCli); err != nil { - logrus.Errorf("init all handle error, %v", err) - return err - } - //创建v2Router manager - if err := controller.CreateV2RouterManager(s.Config, cli); err != nil { - logrus.Errorf("create v2 route manager error, %v", err) - } - // 启动api - apiManager := server.NewManager(s.Config, etcdcli) - if err := apiManager.Start(); err != nil { - return err - } - defer apiManager.Stop() - logrus.Info("api router is running...") - - //step finally: listen Signal - term := make(chan os.Signal) - signal.Notify(term, os.Interrupt, syscall.SIGTERM) - select { - case s := <-term: - logrus.Infof("Received a Signal %s, exiting gracefully...", s.String()) - case err := <-errChan: - logrus.Errorf("Received a error %s, exiting gracefully...", err.Error()) - } - logrus.Info("See you next time!") - return nil -} diff --git a/cmd/builder/option/option.go b/cmd/builder/option/option.go index 2c57dbca8..e6f349c7b 100644 --- a/cmd/builder/option/option.go +++ b/cmd/builder/option/option.go @@ -80,7 +80,6 @@ func NewBuilder() *Builder { // AddFlags config func (a *Builder) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogLevel, "log-level", "info", "the builder log level") - fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://127.0.0.1:2379"}, "etcd v3 cluster endpoints.") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "") fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "") @@ -90,11 +89,9 @@ func (a *Builder) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.BuildKitImage, "buildkit-image", "registry.cn-hangzhou.aliyuncs.com/goodrain/buildkit:v0.12.0", "buildkit image version") fs.StringVar(&a.DBType, "db-type", "mysql", "db type mysql or etcd") fs.StringVar(&a.MysqlConnectionInfo, "mysql", "root:admin@tcp(127.0.0.1:3306)/region", "mysql db connection info") - fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"127.0.0.1:6366"}, "event log server address. simple lb") fs.StringVar(&a.KubeConfig, "kube-config", "", "kubernetes api server config file") fs.IntVar(&a.MaxTasks, "max-tasks", 50, "Maximum number of simultaneous build tasks") fs.IntVar(&a.APIPort, "api-port", 3228, "the port for api server") - fs.StringVar(&a.MQAPI, "mq-api", "127.0.0.1:6300", "acp_mq api") fs.StringVar(&a.RunMode, "run", "sync", "sync data when worker start") fs.StringVar(&a.DockerEndpoint, "dockerd", "127.0.0.1:2376", "dockerd endpoint") fs.StringVar(&a.HostIP, "hostIP", "", "Current node Intranet IP") @@ -114,6 +111,11 @@ func (a *Builder) AddFlags(fs *pflag.FlagSet) { fs.IntVar(&a.KeepCount, "keep-count", 5, "default number of reserved copies for images") fs.IntVar(&a.CleanInterval, "clean-interval", 60, "clean image interval,default 60 minute") fs.StringVar(&a.BRVersion, "br-version", "v5.16.0-release", "builder and runner version") + + fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"rbd-eventlog:6366"}, "event log server address. simple lb") + fs.StringVar(&a.MQAPI, "mq-api", "rbd-mq:6300", "acp_mq api") + fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://rbd-etcd:2379"}, "etcd v3 cluster endpoints.") + } // SetLog 设置log diff --git a/cmd/builder/server/server.go b/cmd/builder/server/server.go index 96c95bae5..cd87f7a39 100644 --- a/cmd/builder/server/server.go +++ b/cmd/builder/server/server.go @@ -67,12 +67,11 @@ func Run(s *option.Builder) error { } if err := event.NewManager(event.EventConfig{ EventLogServers: s.Config.EventLogServers, - DiscoverArgs: etcdClientArgs, }); err != nil { return err } defer event.CloseManager() - mqClient, err := client.NewMqClient(etcdClientArgs, s.Config.MQAPI) + mqClient, err := client.NewMqClient(s.Config.MQAPI) if err != nil { logrus.Errorf("new Mq mqClient error, %v", err) return err diff --git a/cmd/eventlog/server/server.go b/cmd/eventlog/server/server.go index d71d09951..7c07a49fd 100644 --- a/cmd/eventlog/server/server.go +++ b/cmd/eventlog/server/server.go @@ -56,7 +56,7 @@ func NewLogServer() *LogServer { } } -//AddFlags 添加参数 +// AddFlags 添加参数 func (s *LogServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&s.Conf.Entry.EventLogServer.BindIP, "eventlog.bind.ip", "0.0.0.0", "Collect the log service to listen the IP") fs.IntVar(&s.Conf.Entry.EventLogServer.BindPort, "eventlog.bind.port", 6366, "Collect the log service to listen the Port") @@ -71,7 +71,7 @@ func (s *LogServer) AddFlags(fs *pflag.FlagSet) { fs.BoolVar(&s.Conf.ClusterMode, "cluster", true, "Whether open cluster mode") fs.StringVar(&s.Conf.Cluster.Discover.InstanceIP, "cluster.instance.ip", "", "The current instance IP in the cluster can be communications.") fs.StringVar(&s.Conf.Cluster.Discover.Type, "discover.type", "etcd", "the instance in cluster auto discover way.") - fs.StringSliceVar(&s.Conf.Cluster.Discover.EtcdAddr, "discover.etcd.addr", []string{"http://127.0.0.1:2379"}, "set all etcd server addr in cluster for message instence auto discover.") + fs.StringSliceVar(&s.Conf.Cluster.Discover.EtcdAddr, "discover.etcd.addr", []string{"http://rbd-etcd:2379"}, "set all etcd server addr in cluster for message instence auto discover.") fs.StringVar(&s.Conf.Cluster.Discover.EtcdCaFile, "discover.etcd.ca", "", "verify etcd certificates of TLS-enabled secure servers using this CA bundle") fs.StringVar(&s.Conf.Cluster.Discover.EtcdCertFile, "discover.etcd.cert", "", "identify secure etcd client using this TLS certificate file") fs.StringVar(&s.Conf.Cluster.Discover.EtcdKeyFile, "discover.etcd.key", "", "identify secure etcd client using this TLS key file") @@ -115,7 +115,7 @@ func (s *LogServer) AddFlags(fs *pflag.FlagSet) { fs.DurationVar(&s.Conf.Cluster.PubSub.PollingTimeout, "zmq4-polling-timeout", 200*time.Millisecond, "The timeout determines the time-out on the polling of sockets") } -//InitLog 初始化log +// InitLog 初始化log func (s *LogServer) InitLog() { log := logrus.New() if l, err := logrus.ParseLevel(s.Conf.Log.LogLevel); err == nil { @@ -155,7 +155,7 @@ func (s *LogServer) InitLog() { s.Logger = log } -//InitConf 初始化配置 +// InitConf 初始化配置 func (s *LogServer) InitConf() { s.Conf.Cluster.Discover.ClusterMode = s.Conf.ClusterMode s.Conf.Cluster.PubSub.ClusterMode = s.Conf.ClusterMode @@ -171,7 +171,7 @@ func (s *LogServer) InitConf() { } } -//Run 执行 +// Run 执行 func (s *LogServer) Run() error { s.Logger.Debug("Start run server.") log := s.Logger diff --git a/cmd/gateway/option/option.go b/cmd/gateway/option/option.go index 27a9d699c..ad4cedf7a 100644 --- a/cmd/gateway/option/option.go +++ b/cmd/gateway/option/option.go @@ -41,7 +41,7 @@ func NewGWServer() *GWServer { return &GWServer{} } -//Config contains all configuration +// Config contains all configuration type Config struct { K8SConfPath string EtcdEndpoint []string @@ -110,7 +110,6 @@ func (g *GWServer) AddFlags(fs *pflag.FlagSet) { fs.IntVar(&g.KeepaliveTimeout, "keepalive-timeout", 30, "Timeout for keep-alive connections. Server will close connections after this time.") fs.DurationVar(&g.ResyncPeriod, "resync-period", 10*time.Minute, "the default resync period for any handlers added via AddEventHandler and how frequently the listener wants a full resync from the shared informer") // etcd - fs.StringSliceVar(&g.EtcdEndpoint, "etcd-endpoints", []string{"http://127.0.0.1:2379"}, "etcd cluster endpoints.") fs.IntVar(&g.EtcdTimeout, "etcd-timeout", 10, "etcd http timeout seconds") fs.StringVar(&g.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") fs.StringVar(&g.EtcdCertFile, "etcd-cert", "", "etcd tls cert file") @@ -126,6 +125,9 @@ func (g *GWServer) AddFlags(fs *pflag.FlagSet) { fs.Uint64Var(&g.ShareMemory, "max-config-share-memory", 128, "Nginx maximum Shared memory size, which should be increased for larger clusters.") fs.Float32Var(&g.SyncRateLimit, "sync-rate-limit", 0.3, "Define the sync frequency upper limit") fs.StringArrayVar(&g.IgnoreInterface, "ignore-interface", []string{"docker0", "tunl0", "cni0", "kube-ipvs0", "flannel"}, "The network interface name that ignore by gateway") + + fs.StringSliceVar(&g.EtcdEndpoint, "etcd-endpoints", []string{"http://rbd-etcd:2379"}, "etcd cluster endpoints.") + } // SetLog sets log @@ -138,7 +140,7 @@ func (g *GWServer) SetLog() { logrus.SetLevel(level) } -//CheckConfig check config +// CheckConfig check config func (g *GWServer) CheckConfig() error { if g.NodeName == "" { g.NodeName, _ = os.Hostname() diff --git a/cmd/monitor/option/option.go b/cmd/monitor/option/option.go index 38b8af74d..9a4130112 100644 --- a/cmd/monitor/option/option.go +++ b/cmd/monitor/option/option.go @@ -99,7 +99,7 @@ func NewConfig() *Config { host, _ := os.Hostname() config := &Config{ - EtcdEndpointsLine: "http://127.0.0.1:2379", + EtcdEndpointsLine: "http://rbd-etcd:2379", EtcdEndpoints: []string{}, AdvertiseAddr: host + ":9999", BindIP: host, @@ -134,7 +134,7 @@ func NewConfig() *Config { return config } -//AddFlag monitor flag +// AddFlag monitor flag func (c *Config) AddFlag(cmd *pflag.FlagSet) { cmd.StringVar(&c.EtcdEndpointsLine, "etcd-endpoints", c.EtcdEndpointsLine, "etcd endpoints list.") cmd.StringVar(&c.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") @@ -148,7 +148,7 @@ func (c *Config) AddFlag(cmd *pflag.FlagSet) { cmd.StringVar(&c.KubeConfig, "kube-config", "", "kubernetes api server config file") } -//AddPrometheusFlag prometheus flag +// AddPrometheusFlag prometheus flag func (c *Config) AddPrometheusFlag(cmd *pflag.FlagSet) { cmd.StringVar(&c.ConfigFile, "config.file", c.ConfigFile, "Prometheus configuration file path.") diff --git a/cmd/mq/option/option.go b/cmd/mq/option/option.go index 0bfe9270d..98f05921a 100644 --- a/cmd/mq/option/option.go +++ b/cmd/mq/option/option.go @@ -22,7 +22,7 @@ import "github.com/spf13/pflag" import "github.com/sirupsen/logrus" import "fmt" -//Config config server +// Config config server type Config struct { EtcdEndPoints []string EtcdCaFile string @@ -38,21 +38,20 @@ type Config struct { HostName string } -//MQServer lb worker server +// MQServer lb worker server type MQServer struct { Config LogLevel string } -//NewMQServer new server +// NewMQServer new server func NewMQServer() *MQServer { return &MQServer{} } -//AddFlags config +// AddFlags config func (a *MQServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogLevel, "log-level", "info", "the mq log level") - fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://127.0.0.1:2379"}, "etcd v3 cluster endpoints.") fs.IntVar(&a.EtcdTimeout, "etcd-timeout", 10, "etcd http timeout seconds") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "etcd tls cert file") @@ -63,9 +62,12 @@ func (a *MQServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.PrometheusMetricPath, "metric", "/metrics", "prometheus metrics path") fs.StringVar(&a.HostIP, "hostIP", "", "Current node Intranet IP") fs.StringVar(&a.HostName, "hostName", "", "Current node host name") + + fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://rbd-etcd:2379"}, "etcd v3 cluster endpoints.") + } -//SetLog 设置log +// SetLog 设置log func (a *MQServer) SetLog() { level, err := logrus.ParseLevel(a.LogLevel) if err != nil { diff --git a/cmd/mqcli/main.go b/cmd/mqcli/main.go index c9b512de7..c05c426af 100644 --- a/cmd/mqcli/main.go +++ b/cmd/mqcli/main.go @@ -42,7 +42,7 @@ var mode string func main() { AddFlags(pflag.CommandLine) pflag.Parse() - c, err := client.NewMqClient(nil, server) + c, err := client.NewMqClient(server) if err != nil { logrus.Error("new mq client error.", err.Error()) os.Exit(1) @@ -83,8 +83,9 @@ func main() { } +// AddFlags - func AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&server, "server", "127.0.0.1:6300", "mq server") + fs.StringVar(&server, "server", "rbd-mq:6300", "mq server") fs.StringVar(&topic, "topic", "builder", "mq topic") fs.StringVar(&taskbody, "task-body", "", "mq task body") fs.StringVar(&taskfile, "task-file", "", "mq task body file") diff --git a/cmd/node/option/conf.go b/cmd/node/option/conf.go index 6caa54428..87e1d3650 100644 --- a/cmd/node/option/conf.go +++ b/cmd/node/option/conf.go @@ -154,15 +154,12 @@ type UDPMonitorConfig struct { func (a *Conf) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogLevel, "log-level", "info", "the log level") fs.StringVar(&a.LogFile, "log-file", "", "the log file path that log output") - fs.StringVar(&a.PrometheusAPI, "prometheus", "http://rbd-monitor:9999", "the prometheus server address") fs.StringVar(&a.NodePath, "nodePath", "/rainbond/nodes", "the path of node in etcd") fs.StringVar(&a.HostID, "nodeid", "", "the unique ID for this node. Just specify, don't modify") fs.StringVar(&a.HostIP, "hostIP", "", "the host ip you can define. default get ip from eth0") fs.StringVar(&a.PodIP, "podIP", "", "The pod ip of node.") - fs.StringSliceVar(&a.EventLogServer, "event-log-server", []string{"127.0.0.1:6366"}, "host:port slice of event log server") fs.StringVar(&a.ConfigStoragePath, "config-path", "/rainbond/acp_configs", "the path of config to store(new)") fs.StringVar(&a.Service, "servicePath", "/traefik/backends", "the path of service info to store") - fs.StringSliceVar(&a.EtcdEndpoints, "etcd", []string{"http://127.0.0.1:2379"}, "the path of node in etcd") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "verify etcd certificates of TLS-enabled secure servers using this CA bundle") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "identify secure etcd client using this TLS certificate file") fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "identify secure etcd client using this TLS key file") @@ -199,6 +196,11 @@ func (a *Conf) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.HostsFile, "hostsfile", "/newetc/hosts", "/etc/hosts mapped path in the container. eg. /etc/hosts:/tmp/hosts. Do not set hostsfile to /etc/hosts") fs.StringVar(&a.ContainerRuntime, "container-runtime", sources.ContainerRuntimeContainerd, "container runtime, support docker and containerd") fs.StringVar(&a.RuntimeEndpoint, "runtime-endpoint", sources.RuntimeEndpointContainerd, "container runtime endpoint") + + fs.StringSliceVar(&a.EventLogServer, "event-log-server", []string{"rbd-eventlog:6366"}, "host:port slice of event log server") + fs.StringSliceVar(&a.EtcdEndpoints, "etcd", []string{"http://rbd-etcd:2379"}, "the path of node in etcd") + fs.StringVar(&a.PrometheusAPI, "prometheus", "http://rbd-monitor:9999", "the prometheus server address") + } // SetLog 设置log diff --git a/cmd/node/server/server.go b/cmd/node/server/server.go index dfc8921f6..ea9dd3e53 100644 --- a/cmd/node/server/server.go +++ b/cmd/node/server/server.go @@ -44,7 +44,7 @@ import ( "k8s.io/client-go/kubernetes" ) -//Run start run +// Run start run func Run(cfg *option.Conf) error { var stoped = make(chan struct{}) stopfunc := func() error { @@ -88,7 +88,6 @@ func Run(cfg *option.Conf) error { err = eventLog.NewManager(eventLog.EventConfig{ EventLogServers: cfg.EventLogServer, - DiscoverArgs: etcdClientArgs, }) if err != nil { logrus.Errorf("error creating eventlog manager") diff --git a/cmd/webcli/option/option.go b/cmd/webcli/option/option.go index 1929a1dca..43696b0fe 100644 --- a/cmd/webcli/option/option.go +++ b/cmd/webcli/option/option.go @@ -25,7 +25,7 @@ import ( "github.com/spf13/pflag" ) -//Config config server +// Config config server type Config struct { EtcdEndPoints []string EtcdCaFile string @@ -40,21 +40,21 @@ type Config struct { K8SConfPath string } -//WebCliServer container webcli server +// WebCliServer container webcli server type WebCliServer struct { Config LogLevel string } -//NewWebCliServer new server +// NewWebCliServer new server func NewWebCliServer() *WebCliServer { return &WebCliServer{} } -//AddFlags config +// AddFlags config func (a *WebCliServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogLevel, "log-level", "info", "the webcli log level") - fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://127.0.0.1:2379"}, "etcd v3 cluster endpoints.") + fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://rbd-etcd:2379"}, "etcd v3 cluster endpoints.") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "etcd tls cert file") fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "etcd http tls cert key file") @@ -66,7 +66,7 @@ func (a *WebCliServer) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.PrometheusMetricPath, "metric", "/metrics", "prometheus metrics path") } -//SetLog 设置log +// SetLog 设置log func (a *WebCliServer) SetLog() { level, err := logrus.ParseLevel(a.LogLevel) if err != nil { diff --git a/cmd/worker/option/option.go b/cmd/worker/option/option.go index 850c146ad..d3c686196 100644 --- a/cmd/worker/option/option.go +++ b/cmd/worker/option/option.go @@ -28,7 +28,7 @@ import ( "k8s.io/client-go/kubernetes" ) -//Config config server +// Config config server type Config struct { EtcdEndPoints []string EtcdCaFile string @@ -67,22 +67,21 @@ type Helm struct { ChartCache string } -//Worker worker server +// Worker worker server type Worker struct { Config LogLevel string RunMode string //default,sync } -//NewWorker new server +// NewWorker new server func NewWorker() *Worker { return &Worker{} } -//AddFlags config +// AddFlags config func (a *Worker) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.LogLevel, "log-level", "info", "the worker log level") - fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://127.0.0.1:2379"}, "etcd v3 cluster endpoints.") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "") fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "") @@ -92,12 +91,10 @@ func (a *Worker) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.Listen, "listen", ":6369", "prometheus listen host and port") fs.StringVar(&a.DBType, "db-type", "mysql", "db type mysql or etcd") fs.StringVar(&a.MysqlConnectionInfo, "mysql", "root:admin@tcp(127.0.0.1:3306)/region", "mysql db connection info") - fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"127.0.0.1:6366"}, "event log server address. simple lb") fs.StringVar(&a.KubeConfig, "kube-config", "", "kubernetes api server config file") fs.IntVar(&a.KubeAPIQPS, "kube-api-qps", 50, "kube client qps") fs.IntVar(&a.KubeAPIBurst, "kube-api-burst", 10, "kube clint burst") fs.IntVar(&a.MaxTasks, "max-tasks", 50, "the max tasks for per node") - fs.StringVar(&a.MQAPI, "mq-api", "127.0.0.1:6300", "acp_mq api") fs.StringVar(&a.RunMode, "run", "sync", "sync data when worker start") fs.StringVar(&a.NodeName, "node-name", "", "the name of this worker,it must be global unique name") fs.StringVar(&a.HostIP, "host-ip", "", "the ip of this worker,it must be global connected ip") @@ -108,12 +105,17 @@ func (a *Worker) AddFlags(fs *pflag.FlagSet) { fs.StringVar(&a.GrdataPVCName, "grdata-pvc-name", "rbd-cpt-grdata", "The name of grdata persistent volume claim") fs.StringVar(&a.Helm.DataDir, "/grdata/helm", "/grdata/helm", "The data directory of Helm.") fs.StringVar(&a.SharedStorageClass, "shared-storageclass", "", "custom shared storage class.use the specified storageclass to create shared storage, if this parameter is not specified, it will use rainbondsssc by default") + + fs.StringSliceVar(&a.EtcdEndPoints, "etcd-endpoints", []string{"http://rbd-etcd:2379"}, "etcd v3 cluster endpoints.") + fs.StringVar(&a.MQAPI, "mq-api", "rbd-mq:6300", "acp_mq api") + fs.StringSliceVar(&a.EventLogServers, "event-servers", []string{"rbd-eventlog:6366"}, "event log server address. simple lb") + a.Helm.RepoFile = path.Join(a.Helm.DataDir, "repo/repositories.yaml") a.Helm.RepoCache = path.Join(a.Helm.DataDir, "cache") a.Helm.ChartCache = path.Join(a.Helm.DataDir, "chart") } -//SetLog 设置log +// SetLog 设置log func (a *Worker) SetLog() { level, err := logrus.ParseLevel(a.LogLevel) if err != nil { @@ -123,7 +125,7 @@ func (a *Worker) SetLog() { logrus.SetLevel(level) } -//CheckEnv 检测环境变量 +// CheckEnv 检测环境变量 func (a *Worker) CheckEnv() error { if err := os.Setenv("GRDATA_PVC_NAME", a.Config.GrdataPVCName); err != nil { return fmt.Errorf("set env 'GRDATA_PVC_NAME': %v", err) diff --git a/cmd/worker/server/server.go b/cmd/worker/server/server.go index a4c500d6d..aba140531 100644 --- a/cmd/worker/server/server.go +++ b/cmd/worker/server/server.go @@ -32,7 +32,6 @@ import ( "github.com/goodrain/rainbond/event" "github.com/goodrain/rainbond/pkg/common" "github.com/goodrain/rainbond/pkg/generated/clientset/versioned" - etcdutil "github.com/goodrain/rainbond/util/etcd" k8sutil "github.com/goodrain/rainbond/util/k8s" "github.com/goodrain/rainbond/worker/appm/componentdefinition" "github.com/goodrain/rainbond/worker/appm/controller" @@ -62,15 +61,8 @@ func Run(s *option.Worker) error { return err } defer db.CloseManager() - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: s.Config.EtcdEndPoints, - CaFile: s.Config.EtcdCaFile, - CertFile: s.Config.EtcdCertFile, - KeyFile: s.Config.EtcdKeyFile, - } if err := event.NewManager(event.EventConfig{ EventLogServers: s.Config.EventLogServers, - DiscoverArgs: etcdClientArgs, }); err != nil { return err } diff --git a/config/configs/config.go b/config/configs/config.go new file mode 100644 index 000000000..03272fd7e --- /dev/null +++ b/config/configs/config.go @@ -0,0 +1,27 @@ +package configs + +import "github.com/goodrain/rainbond/cmd/api/option" + +// Env - +type Env string + +// Config - +type Config struct { + AppName string + Version string + Env Env + Debug bool + APIConfig option.Config +} + +var defaultConfig *Config + +// Default - +func Default() *Config { + return defaultConfig +} + +// SetDefault - +func SetDefault(cfg *Config) { + defaultConfig = cfg +} diff --git a/db/3rd_party_db_test.go b/db/3rd_party_db_test.go deleted file mode 100644 index 2c265a837..000000000 --- a/db/3rd_party_db_test.go +++ /dev/null @@ -1,178 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package db - -import ( - "context" - "fmt" - dbconfig "github.com/goodrain/rainbond/db/config" - "github.com/goodrain/rainbond/db/model" - "github.com/goodrain/rainbond/util" - "github.com/testcontainers/testcontainers-go" - "testing" - "time" -) - -func TestEndpointDaoImpl_UpdateModel(t *testing.T) { - dbname := "region" - rootpw := "rainbond" - - ctx := context.Background() - req := testcontainers.ContainerRequest{ - Image: "mariadb", - ExposedPorts: []string{"3306/tcp"}, - Env: map[string]string{ - "MYSQL_ROOT_PASSWORD": rootpw, - "MYSQL_DATABASE": dbname, - }, - Cmd: []string{"character-set-server=utf8mb4", "collation-server=utf8mb4_unicode_ci"}, - } - mariadb, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - t.Fatal(err) - } - defer mariadb.Terminate(ctx) - - host, err := mariadb.Host(ctx) - if err != nil { - t.Error(err) - } - port, err := mariadb.MappedPort(ctx, "3306") - if err != nil { - t.Error(err) - } - - connInfo := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "root", - rootpw, host, port.Int(), dbname) - tryTimes := 3 - for { - if err := CreateManager(dbconfig.Config{ - DBType: "mysql", - MysqlConnectionInfo: connInfo, - }); err != nil { - if tryTimes == 0 { - t.Fatalf("Connect info: %s; error creating db manager: %v", connInfo, err) - } else { - tryTimes = tryTimes - 1 - time.Sleep(10 * time.Second) - continue - } - } - break - } - - trueVal := true - falseVal := false - ep := &model.Endpoint{ - UUID: util.NewUUID(), - ServiceID: util.NewUUID(), - IP: "10.10.10.10", - IsOnline: &trueVal, - } - err = GetManager().EndpointsDao().AddModel(ep) - if err != nil { - t.Fatalf("error adding endpoint: %v", err) - } - ep.IsOnline = &falseVal - err = GetManager().EndpointsDao().UpdateModel(ep) - if err != nil { - t.Fatalf("error updating endpoint: %v", err) - } - e, err := GetManager().EndpointsDao().GetByUUID(ep.UUID) - if err != nil { - t.Fatalf("error getting endpoint: %v", err) - } - if *e.IsOnline != false { - t.Errorf("Expected %v for e.IsOnline, but returned %v", false, e.IsOnline) - } -} - -func TestEndpointDaoImpl_AddModel(t *testing.T) { - dbname := "region" - rootpw := "rainbond" - - ctx := context.Background() - req := testcontainers.ContainerRequest{ - Image: "mariadb", - ExposedPorts: []string{"3306/tcp"}, - Env: map[string]string{ - "MYSQL_ROOT_PASSWORD": rootpw, - "MYSQL_DATABASE": dbname, - }, - Cmd: []string{"character-set-server=utf8mb4", "collation-server=utf8mb4_unicode_ci"}, - } - mariadb, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ - ContainerRequest: req, - Started: true, - }) - if err != nil { - t.Fatal(err) - } - defer mariadb.Terminate(ctx) - - host, err := mariadb.Host(ctx) - if err != nil { - t.Fatal(err) - } - port, err := mariadb.MappedPort(ctx, "3306") - if err != nil { - t.Fatal(err) - } - - connInfo := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", "root", - rootpw, host, port.Int(), dbname) - tryTimes := 3 - for { - if err := CreateManager(dbconfig.Config{ - DBType: "mysql", - MysqlConnectionInfo: connInfo, - }); err != nil { - if tryTimes == 0 { - t.Fatalf("Connect info: %s; error creating db manager: %v", connInfo, err) - } else { - tryTimes = tryTimes - 1 - time.Sleep(10 * time.Second) - continue - } - } - break - } - - falseVal := false - ep := &model.Endpoint{ - UUID: util.NewUUID(), - ServiceID: util.NewUUID(), - IP: "10.10.10.10", - IsOnline: &falseVal, - } - err = GetManager().EndpointsDao().AddModel(ep) - if err != nil { - t.Fatalf("error adding endpoint: %v", err) - } - e, err := GetManager().EndpointsDao().GetByUUID(ep.UUID) - if err != nil { - t.Fatalf("error getting endpoint: %v", err) - } - if *e.IsOnline != false { - t.Errorf("Expected %v for e.IsOnline, but returned %v", false, e.IsOnline) - } -} diff --git a/db/db.go b/db/db.go index a104eb761..5719f03f5 100644 --- a/db/db.go +++ b/db/db.go @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2018 Goodrain Co., Ltd. +// Copyright (C) 2014-2024 Goodrain Co., Ltd. // RAINBOND, Application Management Platform // This program is free software: you can redistribute it and/or modify @@ -30,7 +30,7 @@ import ( "github.com/sirupsen/logrus" ) -//Manager db manager +// Manager db manager type Manager interface { CloseManager() error Begin() *gorm.DB @@ -152,11 +152,11 @@ func init() { supportDrivers = map[string]struct{}{ "mysql": {}, "cockroachdb": {}, - "sqlite": {}, + "sqlite": {}, } } -//CreateManager 创建manager +// CreateManager 创建manager func CreateManager(config config.Config) (err error) { if _, ok := supportDrivers[config.DBType]; !ok { return fmt.Errorf("DB drivers: %s not supported", config.DBType) @@ -176,7 +176,7 @@ func CreateManager(config config.Config) (err error) { return } -//CloseManager close db manager +// CloseManager close db manager func CloseManager() error { if defaultManager == nil { return errors.New("default db manager not init") @@ -184,7 +184,7 @@ func CloseManager() error { return defaultManager.CloseManager() } -//GetManager get db manager +// GetManager get db manager func GetManager() Manager { return defaultManager } diff --git a/db/etcd/etcd.go b/db/etcd/etcd.go index 45f859eda..45aff9cd7 100644 --- a/db/etcd/etcd.go +++ b/db/etcd/etcd.go @@ -20,6 +20,7 @@ package etcd import ( "context" + "github.com/coreos/etcd/clientv3" "sync" "github.com/goodrain/rainbond/db/config" diff --git a/event/manager.go b/event/manager.go index 5bb6ced0b..179b392ab 100644 --- a/event/manager.go +++ b/event/manager.go @@ -27,19 +27,17 @@ import ( "sync/atomic" "time" - "github.com/goodrain/rainbond/discover" "github.com/goodrain/rainbond/discover/config" eventclient "github.com/goodrain/rainbond/eventlog/entry/grpc/client" eventpb "github.com/goodrain/rainbond/eventlog/entry/grpc/pb" "github.com/goodrain/rainbond/util" - etcdutil "github.com/goodrain/rainbond/util/etcd" "github.com/pquerna/ffjson/ffjson" "github.com/sirupsen/logrus" "golang.org/x/net/context" ) -//Manager 操作日志,客户端服务 -//客户端负载均衡 +// Manager 操作日志,客户端服务 +// 客户端负载均衡 type Manager interface { GetLogger(eventID string) Logger Start() error @@ -50,7 +48,6 @@ type Manager interface { // EventConfig event config struct type EventConfig struct { EventLogServers []string - DiscoverArgs *etcdutil.ClientArgs } type manager struct { ctx context.Context @@ -62,7 +59,6 @@ type manager struct { lock sync.Mutex eventServer []string abnormalServer map[string]string - dis discover.Discover } var defaultManager Manager @@ -75,15 +71,8 @@ const ( buffersize = 1000 ) -//NewManager 创建manager +// NewManager 创建manager func NewManager(conf EventConfig) error { - dis, err := discover.GetDiscover(config.DiscoverConfig{EtcdClientArgs: conf.DiscoverArgs}) - if err != nil { - logrus.Error("create discover manager error.", err.Error()) - if len(conf.EventLogServers) < 1 { - return err - } - } ctx, cancel := context.WithCancel(context.Background()) defaultManager = &manager{ ctx: ctx, @@ -92,13 +81,12 @@ func NewManager(conf EventConfig) error { loggers: make(map[string]Logger, 1024), handles: make(map[string]handle), eventServer: conf.EventLogServers, - dis: dis, abnormalServer: make(map[string]string), } return defaultManager.Start() } -//GetManager 获取日志服务 +// GetManager 获取日志服务 func GetManager() Manager { return defaultManager } @@ -108,13 +96,14 @@ func NewTestManager(m Manager) { defaultManager = m } -//CloseManager 关闭日志服务 +// CloseManager 关闭日志服务 func CloseManager() { if defaultManager != nil { defaultManager.Close() } } +// Start - func (m *manager) Start() error { m.lock.Lock() defer m.lock.Unlock() @@ -129,13 +118,14 @@ func (m *manager) Start() error { m.handles[m.eventServer[i]] = h go h.HandleLog() } - if m.dis != nil { - m.dis.AddProject("event_log_event_grpc", m) - } + //if m.dis != nil { + // m.dis.AddProject("event_log_event_grpc", m) + //} go m.GC() return nil } +// UpdateEndpoints - func (m *manager) UpdateEndpoints(endpoints ...*config.Endpoint) { m.lock.Lock() defer m.lock.Unlock() @@ -177,17 +167,18 @@ func (m *manager) UpdateEndpoints(endpoints ...*config.Endpoint) { logrus.Debugf("update event handle core success,handle core count:%d, event server count:%d", len(m.handles), len(m.eventServer)) } +// Error - func (m *manager) Error(err error) { } + +// Close - func (m *manager) Close() error { m.cancel() - if m.dis != nil { - m.dis.Stop() - } return nil } +// GC - func (m *manager) GC() { util.IntermittentExec(m.ctx, func() { m.lock.Lock() @@ -208,8 +199,7 @@ func (m *manager) GC() { }, time.Second*20) } -//GetLogger -//使用完成后必须调用ReleaseLogger方法 +// GetLogger 使用完成后必须调用ReleaseLogger方法 func (m *manager) GetLogger(eventID string) Logger { m.lock.Lock() defer m.lock.Unlock() @@ -224,6 +214,7 @@ func (m *manager) GetLogger(eventID string) Logger { return l } +// ReleaseLogger 释放logger func (m *manager) ReleaseLogger(l Logger) { m.lock.Lock() defer m.lock.Unlock() @@ -240,6 +231,7 @@ type handle struct { manager *manager } +// DiscardedLoggerChan - func (m *manager) DiscardedLoggerChan(cacheChan chan []byte) { m.lock.Lock() defer m.lock.Unlock() @@ -285,6 +277,8 @@ func (m *manager) getLBChan() chan []byte { } return nil } + +// RemoveHandle - func (m *manager) RemoveHandle(server string) { m.lock.Lock() defer m.lock.Unlock() @@ -292,6 +286,8 @@ func (m *manager) RemoveHandle(server string) { delete(m.handles, server) } } + +// HandleLog - func (m *handle) HandleLog() error { defer m.manager.RemoveHandle(m.server) return util.Exec(m.ctx, func() error { @@ -332,11 +328,12 @@ func (m *handle) HandleLog() error { }, time.Second*3) } +// Stop - func (m *handle) Stop() { close(m.stop) } -//Logger 日志发送器 +// Logger 日志发送器 type Logger interface { Info(string, map[string]string) Error(string, map[string]string) @@ -363,9 +360,12 @@ type logger struct { createTime time.Time } +// GetChan - func (l *logger) GetChan() chan []byte { return l.sendChan } + +// SetChan - func (l *logger) SetChan(ch chan []byte) { l.sendChan = ch } @@ -406,7 +406,7 @@ func (l *logger) send(message string, info map[string]string) { } } -//LoggerWriter logger writer +// LoggerWriter logger writer type LoggerWriter interface { io.Writer SetFormat(map[string]interface{}) @@ -475,7 +475,7 @@ func (l *loggerWriter) Write(b []byte) (n int, err error) { return len(b), nil } -//GetTestLogger GetTestLogger +// GetTestLogger GetTestLogger func GetTestLogger() Logger { return &testLogger{} } diff --git a/eventlog/entry/server_test.go b/eventlog/entry/server_test.go deleted file mode 100644 index f78d7178c..000000000 --- a/eventlog/entry/server_test.go +++ /dev/null @@ -1,163 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package entry - -import ( - "errors" - "os" - "os/signal" - "syscall" - "testing" - "time" - - "fmt" - - "sync" - - "github.com/pebbe/zmq4" - "github.com/sirupsen/logrus" - "github.com/twinj/uuid" -) - -const ( - REQUEST_TIMEOUT = 1000 * time.Millisecond - MAX_RETRIES = 3 // Before we abandon -) - -func TestServer(t *testing.T) { - t.SkipNow() - request := []string{`{"event_id":"qwertyuiuiosadfkbjasdv","message":"hello word2"}`} - reply := []string{} - var err error - // For one endpoint, we retry N times - for retries := 0; retries < MAX_RETRIES; retries++ { - endpoint := "tcp://127.0.0.1:4320" - reply, err = try_request(endpoint, request) - if err == nil { - break // Successful - } - t.Errorf("W: no response from %s, %s\n", endpoint, err.Error()) - } - - if len(reply) > 0 { - t.Logf("Service is running OK: %q\n", reply) - } - -} - -func TestContinuousServer(t *testing.T) { - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) - coreNum := 30 - var wait sync.WaitGroup - endpoint := "tcp://127.0.0.1:4321" - for i := 0; i < coreNum; i++ { - if i < 15 { - endpoint = "tcp://127.0.0.1:4320" - } else { - endpoint = "tcp://127.0.0.1:4321" - } - wait.Add(1) - go func(en string) { - client, _ := zmq4.NewSocket(zmq4.REQ) - client.Connect(en) - defer client.Close() - id := uuid.NewV4() - Continuous: - for { - request := []string{fmt.Sprintf(`{"event_id":"%s","message":"hello word2","time":"%s"}`, id, time.Now().Format(time.RFC3339))} - _, err := client.SendMessage(request) - if err != nil { - logrus.Error("Send:", err) - } - poller := zmq4.NewPoller() - poller.Add(client, zmq4.POLLIN) - polled, err := poller.Poll(REQUEST_TIMEOUT) - if err != nil { - logrus.Error("Red:", err) - } - reply := []string{} - if len(polled) > 0 { - reply, err = client.RecvMessage(0) - } else { - err = errors.New("Time out") - } - if len(reply) > 0 { - logrus.Info(en, ":", reply[0]) - } - if err != nil { - logrus.Error(err) - break Continuous - } - select { - case <-interrupt: - break Continuous - case <-time.Tick(time.Second): - } - } - wait.Done() - }(endpoint) - } - wait.Wait() -} - -//go test -v -bench=“.” -func BenchmarkServer(b *testing.B) { - for i := 0; i < b.N; i++ { - request := []string{fmt.Sprintf(`{"event_id":"qwertyuiuiosadfkbjasdv","message":"hello word","step":"%d"}`, i)} - reply := []string{} - var err error - // For one endpoint, we retry N times - for retries := 0; retries < MAX_RETRIES; retries++ { - endpoint := "tcp://127.0.0.1:6366" - reply, err = try_request(endpoint, request) - if err == nil { - break // Successful - } - b.Errorf("W: no response from %s, %s\n", endpoint, err.Error()) - } - if len(reply) > 0 { - b.Logf("Service is running OK: %q\n", reply) - } - } -} - -func try_request(endpoint string, request []string) (reply []string, err error) { - logrus.Infof("I: trying echo service at %s...\n", endpoint) - client, _ := zmq4.NewSocket(zmq4.REQ) - client.Connect(endpoint) - defer client.Close() - for i := 0; i < 5; i++ { - // Send request, wait safely for reply - client.SendMessage(request) - poller := zmq4.NewPoller() - poller.Add(client, zmq4.POLLIN) - polled, err := poller.Poll(REQUEST_TIMEOUT) - reply = []string{} - if len(polled) == 1 { - reply, err = client.RecvMessage(0) - } else { - err = errors.New("Time out") - } - if err != nil { - return nil, err - } - } - return nil, nil -} diff --git a/eventlog/store/manager_test.go b/eventlog/store/manager_test.go deleted file mode 100644 index 2361e778d..000000000 --- a/eventlog/store/manager_test.go +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package store - -import ( - "github.com/goodrain/rainbond/eventlog/conf" - "testing" - - "github.com/sirupsen/logrus" -) - -var urlData = ` -2017-05-19 11:33:34 APPS SumTimeByUrl [{"tenant":"o2o","service":"zzcplus","url":"/active/js/wx_share.js","avgtime":"1.453","sumtime":"1.453","counts":"1"}] -` - -func BenchmarkHandleMonitorMessage(b *testing.B) { - manager, err := NewManager(conf.EventStoreConf{ - HandleDockerLogCoreNumber: 10, - DB: conf.DBConf{ - Type: "mysql", - URL: "root:admin@tcp(127.0.0.1:3306)/event", - }, - }, logrus.WithField("MODO", "test")) - if err != nil { - b.Fatal(err) - } - - err = manager.Run() - if err != nil { - b.Fatal(err) - } - //defer manager.Stop() - for i := 0; i < b.N; i++ { - manager.MonitorMessageChan() <- [][]byte{[]byte("xxx"), []byte(`2017-05-19 11:33:32 APPS SumTimeBySql [{"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"update weixin_user set subscribe=? where openid = ?","avgtime":"0.10058000000000006","sumtime":"0.20116000000000012","counts":"2"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"insert into lottery_prize (lottery_type,lottery_no,prize_balls,create_time,prize_time) values (?,?,?,?,?)","avgtime":"0.298413","sumtime":"0.298413","counts":"1"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"select miss_type,miss_data from lottery_miss where lottery_type=? and lottery_no=?","avgtime":"0.326492","sumtime":"0.326492","counts":"1"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"insert into weixin_msg (openid,msg_type,content,msg_id,msg_time,create_time) values (?,?,?,?,?,?)","avgtime":"0.8989751","sumtime":"8.989751","counts":"10"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"select * from lottery_prize where lottery_type = ? and lottery_no = ? limit ?","avgtime":"0.20503315909090927","sumtime":"9.021459000000007","counts":"44"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"select id from news where sourceid = ? and del=? limit ?","avgtime":"0.26174076219512193","sumtime":"42.925484999999995","counts":"164"}, {"tenant_id":"d9621ccfc0b742829a517a2642ba04b7","service_id":"b6e19107cadb14b53a95442cb9120b8d","sql":"select a.id,a.title,a.public_time from news a inner join news_class b on a.id=b.news_id where b.class_id=? and a.del=? and a.public_time. - -package main - -import ( - "fmt" - "io/ioutil" - "net/http" - "os" - "os/signal" - "sync" - "syscall" - "time" - - "github.com/pebbe/zmq4" - "github.com/sirupsen/logrus" - "github.com/spf13/pflag" - "github.com/tidwall/gjson" - "github.com/twinj/uuid" -) - -const ( - REQUEST_TIMEOUT = 1000 * time.Millisecond - MAX_RETRIES = 3 // Before we abandon -) - -var endpoint string -var coreNum int -var t string - -func AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&endpoint, "endpoint", "tcp://127.0.0.1:6363", "docker log server url") - fs.IntVar(&coreNum, "core", 1, "core number") - fs.StringVar(&t, "t", "1s", "时间间隔") -} - -func main() { - AddFlags(pflag.CommandLine) - pflag.Parse() - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) - re, _ := http.NewRequest("GET", "http://127.0.0.1:6363/docker-instance?service_id=asdasdadsasdasdassd", nil) - res, err := http.DefaultClient.Do(re) - if err != nil { - logrus.Error(err) - return - } - body, err := ioutil.ReadAll(res.Body) - if err != nil { - logrus.Error(err) - return - } - status := gjson.Get(string(body), "status") - host := gjson.Get(string(body), "host") - if status.String() == "success" { - endpoint = host.String() - } else { - logrus.Error("获取日志接收节点失败." + gjson.Get(string(body), "host").String()) - return - } - var wait sync.WaitGroup - d, _ := time.ParseDuration(t) - for i := 0; i < coreNum; i++ { - wait.Add(1) - go func(en string) { - client, _ := zmq4.NewSocket(zmq4.PUB) - client.Monitor("inproc://monitor.rep", zmq4.EVENT_ALL) - go monitor() - client.Connect(en) - defer client.Close() - id := uuid.NewV4() - Continuous: - for { - request := fmt.Sprintf(`{"event_id":"%s","message":"hello word2","time":"%s"}`, id, time.Now().Format(time.RFC3339)) - client.Send("servasd223123123123", zmq4.SNDMORE) - _, err := client.Send(request, zmq4.DONTWAIT) - if err != nil { - logrus.Error("Send Error:", err) - } - select { - case <-interrupt: - break Continuous - case <-time.Tick(d): - } - } - wait.Done() - }(endpoint) - } - wait.Wait() -} - -func monitor() { - mo, _ := zmq4.NewSocket(zmq4.PAIR) - mo.Connect("inproc://monitor.rep") - for { - a, b, c, err := mo.RecvEvent(0) - if err != nil { - logrus.Error(err) - return - } - logrus.Infof("A:%s B:%s C:%d", a, b, c) - } - -} diff --git a/eventlog/test/eventserver/main.go b/eventlog/test/eventserver/main.go deleted file mode 100644 index 9e7253d95..000000000 --- a/eventlog/test/eventserver/main.go +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package main - -import ( - "errors" - "fmt" - "os" - "os/signal" - "sync" - "syscall" - "time" - - "github.com/pebbe/zmq4" - "github.com/sirupsen/logrus" - "github.com/spf13/pflag" - "github.com/twinj/uuid" -) - -const ( - REQUEST_TIMEOUT = 1000 * time.Millisecond - MAX_RETRIES = 3 // Before we abandon -) - -var endpoint string -var coreNum int -var t string - -func AddFlags(fs *pflag.FlagSet) { - fs.StringVar(&endpoint, "endpoint", "tcp://127.0.0.1:6366", "event server url") - fs.IntVar(&coreNum, "core", 10, "core number") - fs.StringVar(&t, "t", "1s", "时间间隔") -} - -func main() { - AddFlags(pflag.CommandLine) - pflag.Parse() - interrupt := make(chan os.Signal, 1) - signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) - var wait sync.WaitGroup - d, _ := time.ParseDuration(t) - for i := 0; i < coreNum; i++ { - wait.Add(1) - go func(en string) { - client, _ := zmq4.NewSocket(zmq4.REQ) - client.Connect(en) - defer client.Close() - id := uuid.NewV4() - Continuous: - for { - request := []string{fmt.Sprintf(`{"event_id":"%s","message":"hello word2","time":"%s"}`, id, time.Now().Format(time.RFC3339))} - _, err := client.SendMessage(request) - if err != nil { - logrus.Error("Send:", err) - } - poller := zmq4.NewPoller() - poller.Add(client, zmq4.POLLIN) - polled, err := poller.Poll(REQUEST_TIMEOUT) - if err != nil { - logrus.Error("Red:", err) - } - reply := []string{} - if len(polled) > 0 { - reply, err = client.RecvMessage(0) - } else { - err = errors.New("Time out") - } - if len(reply) > 0 { - logrus.Info(en, ":", reply[0]) - } - if err != nil { - logrus.Error(err) - break Continuous - } - select { - case <-interrupt: - break Continuous - case <-time.Tick(d): - } - } - wait.Done() - }(endpoint) - } - wait.Wait() -} diff --git a/eventlog/test/monitorserver/monitorserver_test.go b/eventlog/test/monitorserver/monitorserver_test.go deleted file mode 100644 index 94432ad30..000000000 --- a/eventlog/test/monitorserver/monitorserver_test.go +++ /dev/null @@ -1,83 +0,0 @@ -// Copyright (C) 2014-2018 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 . - -package monitorserver - -import ( - "testing" - - "github.com/pebbe/zmq4" - "github.com/sirupsen/logrus" -) - -var urlData = ` -2017-05-19 11:33:34 APPS SumTimeByUrl [{"tenant":"o2o","service":"zzcplus","url":"/active/js/wx_share.js","avgtime":"1.453","sumtime":"1.453","counts":"1"}] -` -var newMonitorMessage = ` -[{"ServiceID":"test", - "Port":"5000", - "MessageType":"http", - "Key":"/test", - "CumulativeTime":0.1, - "AverageTime":0.1, - "MaxTime":0.1, - "Count":1, - "AbnormalCount":0} -,{"ServiceID":"test", - "Port":"5000", - "MessageType":"http", - "Key":"/test2", - "CumulativeTime":0.36, - "AverageTime":0.18, - "MaxTime":0.2, - "Count":2, - "AbnormalCount":2} -] -` - -func BenchmarkMonitorServer(t *testing.B) { - client, _ := zmq4.NewSocket(zmq4.PUB) - // client.Monitor("inproc://monitor.rep", zmq4.EVENT_ALL) - // go monitor() - client.Bind("tcp://0.0.0.0:9442") - defer client.Close() - var size int64 - for i := 0; i < t.N; i++ { - client.Send("ceptop", zmq4.SNDMORE) - _, err := client.Send(urlData, zmq4.DONTWAIT) - if err != nil { - logrus.Error("Send Error:", err) - } - size++ - } - logrus.Info(size) -} - -func monitor() { - mo, _ := zmq4.NewSocket(zmq4.PAIR) - mo.Connect("inproc://monitor.rep") - for { - a, b, c, err := mo.RecvEvent(0) - if err != nil { - logrus.Error(err) - return - } - logrus.Infof("A:%s B:%s C:%d", a, b, c) - } - -} diff --git a/monitor/api/router.go b/monitor/api/router.go index 1029f7cdc..fd7b8e594 100644 --- a/monitor/api/router.go +++ b/monitor/api/router.go @@ -28,7 +28,7 @@ import ( httputil "github.com/goodrain/rainbond/util/http" ) -//Server api server +// Server api server func Server(c *controller.RuleControllerManager) *chi.Mux { r := chi.NewRouter() r.Route("/monitor", func(r chi.Router) { @@ -37,13 +37,13 @@ func Server(c *controller.RuleControllerManager) *chi.Mux { httputil.ReturnSuccess(r, w, bean) }) }) - r.Route("/v2/rules", func(r chi.Router) { - r.Post("/", c.AddRules) - r.Put("/{rules_name}", c.RegRules) - r.Delete("/{rules_name}", c.DelRules) - r.Get("/{rules_name}", c.GetRules) - r.Get("/all", c.GetAllRules) - }) + //r.Route("/v2/rules", func(r chi.Router) { + // r.Post("/", c.AddRules) + // r.Put("/{rules_name}", c.RegRules) + // r.Delete("/{rules_name}", c.DelRules) + // r.Get("/{rules_name}", c.GetRules) + // r.Get("/all", c.GetAllRules) + //}) util.ProfilerSetup(r) return r } diff --git a/mq/client/client.go b/mq/client/client.go index 437c04950..04022ede7 100644 --- a/mq/client/client.go +++ b/mq/client/client.go @@ -24,23 +24,21 @@ import ( "time" "github.com/goodrain/rainbond/mq/api/grpc/pb" - etcdutil "github.com/goodrain/rainbond/util/etcd" - grpcutil "github.com/goodrain/rainbond/util/grpc" "github.com/sirupsen/logrus" context "golang.org/x/net/context" grpc "google.golang.org/grpc" ) -//BuilderTopic builder for linux +// BuilderTopic builder for linux var BuilderTopic = "builder" -//WindowsBuilderTopic builder for windows +// WindowsBuilderTopic builder for windows var WindowsBuilderTopic = "windows_builder" -//WorkerTopic worker topic +// WorkerTopic worker topic var WorkerTopic = "worker" -//MQClient mq client +// MQClient mq client type MQClient interface { pb.TaskQueueClient Close() @@ -53,27 +51,12 @@ type mqClient struct { cancel context.CancelFunc } -//NewMqClient new a mq client -func NewMqClient(etcdClientArgs *etcdutil.ClientArgs, defaultserver string) (MQClient, error) { +// NewMqClient new a mq client +func NewMqClient(mqAddr string) (MQClient, error) { ctx, cancel := context.WithCancel(context.Background()) - var conn *grpc.ClientConn - if etcdClientArgs != nil && etcdClientArgs.Endpoints != nil && len(defaultserver) > 1 { - c, err := etcdutil.NewClient(ctx, etcdClientArgs) - if err != nil { - return nil, err - } - r := &grpcutil.GRPCResolver{Client: c} - b := grpc.RoundRobin(r) - conn, err = grpc.DialContext(ctx, "/rainbond/discover/rainbond_mq", grpc.WithBalancer(b), grpc.WithInsecure()) - if err != nil { - return nil, err - } - } else { - var err error - conn, err = grpc.DialContext(ctx, defaultserver, grpc.WithInsecure()) - if err != nil { - return nil, err - } + conn, err := grpc.DialContext(ctx, mqAddr, grpc.WithInsecure()) + if err != nil { + return nil, err } cli := pb.NewTaskQueueClient(conn) client := &mqClient{ @@ -84,12 +67,12 @@ func NewMqClient(etcdClientArgs *etcdutil.ClientArgs, defaultserver string) (MQC return client, nil } -//Close mq grpc client must be closed after uesd +// Close mq grpc client must be closed after uesd func (m *mqClient) Close() { m.cancel() } -//TaskStruct task struct +// TaskStruct task struct type TaskStruct struct { Topic string Arch string @@ -97,7 +80,7 @@ type TaskStruct struct { TaskBody interface{} } -//buildTask build task +// buildTask build task func buildTask(t TaskStruct) (*pb.EnqueueRequest, error) { var er pb.EnqueueRequest taskJSON, err := json.Marshal(t.TaskBody) diff --git a/node/api/router/router.go b/node/api/router/router.go index ddcddea75..f5e34895c 100644 --- a/node/api/router/router.go +++ b/node/api/router/router.go @@ -19,6 +19,7 @@ package router import ( + "github.com/goodrain/rainbond/pkg/interceptors" "time" "github.com/sirupsen/logrus" @@ -30,7 +31,7 @@ import ( "github.com/go-chi/chi/middleware" ) -//Routers 路由 +// Routers 路由 func Routers(mode string) *chi.Mux { r := chi.NewRouter() r.Use(middleware.RequestID) //每个请求的上下文中注册一个id @@ -41,7 +42,7 @@ func Routers(mode string) *chi.Mux { logger.SetLevel(logrus.GetLevel()) r.Use(log.NewStructuredLogger(logger)) //Gracefully absorb panics and prints the stack trace - r.Use(middleware.Recoverer) + r.Use(interceptors.Recoverer) //request time out r.Use(middleware.Timeout(time.Second * 5)) r.Mount("/v1", DisconverRoutes()) diff --git a/node/core/service/node_service.go b/node/core/service/node_service.go index 5acb34158..87d9bff2c 100644 --- a/node/core/service/node_service.go +++ b/node/core/service/node_service.go @@ -35,31 +35,22 @@ import ( "github.com/goodrain/rainbond/node/utils" "github.com/goodrain/rainbond/util" ansibleUtil "github.com/goodrain/rainbond/util/ansible" - etcdutil "github.com/goodrain/rainbond/util/etcd" licutil "github.com/goodrain/rainbond/util/license" "github.com/sirupsen/logrus" "github.com/twinj/uuid" ) -//NodeService node service +// NodeService node service type NodeService struct { c *option.Conf nodecluster *node.Cluster kubecli kubecache.KubeClient } -//CreateNodeService create +// CreateNodeService create func CreateNodeService(c *option.Conf, nodecluster *node.Cluster, kubecli kubecache.KubeClient) *NodeService { - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: c.EtcdEndpoints, - CaFile: c.EtcdCaFile, - CertFile: c.EtcdCertFile, - KeyFile: c.EtcdKeyFile, - DialTimeout: c.EtcdDialTimeout, - } if err := event.NewManager(event.EventConfig{ EventLogServers: c.EventLogServer, - DiscoverArgs: etcdClientArgs, }); err != nil { logrus.Errorf("create event manager faliure") } @@ -70,7 +61,7 @@ func CreateNodeService(c *option.Conf, nodecluster *node.Cluster, kubecli kubeca } } -//AddNode add node +// AddNode add node func (n *NodeService) AddNode(node *client.APIHostNode) (*client.HostNode, *utils.APIHandleError) { if n.nodecluster == nil { return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) @@ -108,7 +99,7 @@ func (n *NodeService) AddNode(node *client.APIHostNode) (*client.HostNode, *util return rbnode, nil } -//InstallNode install node +// InstallNode install node func (n *NodeService) InstallNode(node *client.HostNode) *utils.APIHandleError { node.Status = client.Installing node.NodeStatus.Status = client.Installing @@ -153,7 +144,7 @@ func (n *NodeService) writeHostsFile() error { return nil } -//UpdateNodeStatus update node status +// UpdateNodeStatus update node status func (n *NodeService) UpdateNodeStatus(nodeID, status string) *utils.APIHandleError { node := n.nodecluster.GetNode(nodeID) if node == nil { @@ -168,7 +159,7 @@ func (n *NodeService) UpdateNodeStatus(nodeID, status string) *utils.APIHandleEr return nil } -//AsynchronousInstall AsynchronousInstall +// AsynchronousInstall AsynchronousInstall func (n *NodeService) AsynchronousInstall(node *client.HostNode, eventID string) { // write ansible hosts file err := n.writeHostsFile() @@ -206,8 +197,8 @@ func (n *NodeService) AsynchronousInstall(node *client.HostNode, eventID string) logrus.Infof("Install node %s successful", node.ID) } -//DeleteNode delete node -//only node status is offline and node can be deleted +// 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 == nil { @@ -224,7 +215,7 @@ func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError { return nil } -//GetNode get node info +// GetNode get node info func (n *NodeService) GetNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { node := n.nodecluster.GetNode(nodeID) if node == nil { @@ -233,7 +224,7 @@ func (n *NodeService) GetNode(nodeID string) (*client.HostNode, *utils.APIHandle return node, nil } -//GetAllNode get all node +// GetAllNode get all node func (n *NodeService) GetAllNode() ([]*client.HostNode, *utils.APIHandleError) { if n.nodecluster == nil { return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) @@ -243,7 +234,7 @@ func (n *NodeService) GetAllNode() ([]*client.HostNode, *utils.APIHandleError) { return nodes, nil } -//GetServicesHealthy get service health +// GetServicesHealthy get service health func (n *NodeService) GetServicesHealthy() (map[string][]map[string]string, *utils.APIHandleError) { if n.nodecluster == nil { return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) @@ -266,7 +257,7 @@ func (n *NodeService) GetServicesHealthy() (map[string][]map[string]string, *uti return StatusMap, nil } -//CordonNode set node is unscheduler +// CordonNode set node is unscheduler func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHandleError { hostNode, apierr := n.GetNode(nodeID) if apierr != nil { @@ -302,7 +293,7 @@ func (n *NodeService) GetNodeLabels(nodeID string) (*model.LabelsResp, *utils.AP return labels, nil } -//PutNodeLabel update node label +// PutNodeLabel update node label func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) { hostNode, apierr := n.GetNode(nodeID) if apierr != nil { @@ -327,7 +318,7 @@ func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) (map return hostNode.CustomLabels, nil } -//DeleteNodeLabel delete node label +// DeleteNodeLabel delete node label func (n *NodeService) DeleteNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) { hostNode, apierr := n.GetNode(nodeID) if apierr != nil { @@ -353,7 +344,7 @@ func (n *NodeService) DeleteNodeLabel(nodeID string, labels map[string]string) ( return hostNode.CustomLabels, nil } -//DownNode down node +// DownNode down node func (n *NodeService) DownNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { hostNode, apierr := n.GetNode(nodeID) if apierr != nil { @@ -373,7 +364,7 @@ func (n *NodeService) DownNode(nodeID string) (*client.HostNode, *utils.APIHandl return hostNode, nil } -//UpNode up node +// UpNode up node func (n *NodeService) UpNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { hostNode, apierr := n.GetNode(nodeID) if apierr != nil { @@ -396,7 +387,7 @@ func (n *NodeService) UpNode(nodeID string) (*client.HostNode, *utils.APIHandleE return hostNode, nil } -//GetNodeResource get node resource +// GetNodeResource get node resource func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *utils.APIHandleError) { node, err := n.GetNode(nodeUID) if err != nil { @@ -448,13 +439,13 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, * return &res, nil } -//CheckNode check node install status +// CheckNode check node install status func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.APIHandleError) { return nil, nil } -//DeleteNodeCondition delete node condition +// DeleteNodeCondition delete node condition func (n *NodeService) DeleteNodeCondition(nodeUID string, condition client.NodeConditionType) (*client.HostNode, *utils.APIHandleError) { node, err := n.GetNode(nodeUID) if err != nil { diff --git a/node/core/store/client.go b/node/core/store/client.go index d15159a2b..7d1172114 100644 --- a/node/core/store/client.go +++ b/node/core/store/client.go @@ -35,7 +35,6 @@ import ( ) var ( - //DefalutClient etcd client DefalutClient *Client ) @@ -66,8 +65,7 @@ func NewClient(ctx context.Context, cfg *conf.Conf, etcdClientArgs *etcdutil.Cli // ErrKeyExists key exist error var ErrKeyExists = errors.New("key already exists") -// Post attempts to create the given key, only succeeding if the key did -// not yet exist. +// Post attempts to create the given key, only succeeding if the key did not yet exist. func (c *Client) Post(key, val string, opts ...client.OpOption) (*client.PutResponse, error) { ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout) defer cancel() diff --git a/pkg/component/README.md b/pkg/component/README.md new file mode 100644 index 000000000..3e52f0c71 --- /dev/null +++ b/pkg/component/README.md @@ -0,0 +1 @@ +# 通用的封装的组件 \ No newline at end of file diff --git a/pkg/component/core.go b/pkg/component/core.go new file mode 100644 index 000000000..e30dece6a --- /dev/null +++ b/pkg/component/core.go @@ -0,0 +1,130 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package component + +import ( + "context" + "github.com/goodrain/rainbond/api/controller" + "github.com/goodrain/rainbond/api/db" + "github.com/goodrain/rainbond/api/handler" + "github.com/goodrain/rainbond/api/server" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/event" + "github.com/goodrain/rainbond/pkg/component/etcd" + "github.com/goodrain/rainbond/pkg/component/grpc" + "github.com/goodrain/rainbond/pkg/component/hubregistry" + "github.com/goodrain/rainbond/pkg/component/k8s" + "github.com/goodrain/rainbond/pkg/component/mq" + "github.com/goodrain/rainbond/pkg/component/prom" + "github.com/goodrain/rainbond/pkg/rainbond" + "github.com/sirupsen/logrus" + "time" +) + +// Database - +func Database() rainbond.Component { + return db.Database() +} + +// K8sClient - +func K8sClient() rainbond.Component { + return k8s.Client() +} + +// HubRegistry - +func HubRegistry() rainbond.Component { + return hubregistry.HubRegistry() +} + +// Etcd - +func Etcd() rainbond.Component { + return etcd.Etcd() +} + +// MQ - +func MQ() rainbond.Component { + return mq.MQ() +} + +// Prometheus - +func Prometheus() rainbond.Component { + return prom.Prometheus() +} + +// Grpc - +func Grpc() rainbond.Component { + return grpc.Grpc() +} + +// Event - +func Event() rainbond.FuncComponent { + logrus.Infof("init event...") + return func(ctx context.Context, cfg *configs.Config) error { + var tryTime time.Duration + var err error + for tryTime < 4 { + tryTime++ + if err = event.NewManager(event.EventConfig{ + EventLogServers: cfg.APIConfig.EventLogServers, + }); err != nil { + logrus.Errorf("get event manager failed, try time is %v,%s", tryTime, err.Error()) + time.Sleep((5 + tryTime*10) * time.Second) + } else { + break + } + } + if err != nil { + logrus.Errorf("get event manager failed. %v", err.Error()) + return err + } + logrus.Info("init event manager success") + return nil + } +} + +// Handler - +func Handler() rainbond.FuncComponent { + return func(ctx context.Context, cfg *configs.Config) error { + return handler.InitHandle(cfg.APIConfig) + } +} + +// Router - +func Router() rainbond.FuncComponent { + return func(ctx context.Context, cfg *configs.Config) error { + if err := controller.CreateV2RouterManager(cfg.APIConfig, grpc.Default().StatusClient); err != nil { + logrus.Errorf("create v2 route manager error, %v", err) + } + // 启动api + apiManager := server.NewManager(cfg.APIConfig, etcd.Default().EtcdClient) + if err := apiManager.Start(); err != nil { + return err + } + logrus.Info("api router is running...") + return nil + } +} + +// Proxy - +func Proxy() rainbond.FuncComponent { + return func(ctx context.Context, cfg *configs.Config) error { + handler.InitProxy(cfg.APIConfig) + return nil + } +} diff --git a/pkg/component/etcd/etcdComponent.go b/pkg/component/etcd/etcdComponent.go new file mode 100644 index 000000000..c1cb155e8 --- /dev/null +++ b/pkg/component/etcd/etcdComponent.go @@ -0,0 +1,109 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package etcd + +import ( + "context" + "github.com/coreos/etcd/clientv3" + "github.com/coreos/etcd/pkg/transport" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/pkg/gogo" + etcdutil "github.com/goodrain/rainbond/util/etcd" + "github.com/sirupsen/logrus" + "google.golang.org/grpc" + "time" +) + +var defaultEtcdComponent *Component + +// Component - +type Component struct { + EtcdClient *clientv3.Client +} + +// Etcd - +func Etcd() *Component { + defaultEtcdComponent = &Component{} + return defaultEtcdComponent +} + +// Default - +func Default() *Component { + return defaultEtcdComponent +} + +// Start - +func (e *Component) Start(ctx context.Context, cfg *configs.Config) error { + logrus.Info("start etcd client...") + clientArgs := &etcdutil.ClientArgs{ + Endpoints: cfg.APIConfig.EtcdEndpoint, + CaFile: cfg.APIConfig.EtcdCaFile, + CertFile: cfg.APIConfig.EtcdCertFile, + KeyFile: cfg.APIConfig.EtcdKeyFile, + } + if clientArgs.DialTimeout <= 5 { + clientArgs.DialTimeout = 5 * time.Second + } + if clientArgs.AutoSyncInterval <= 30 { + clientArgs.AutoSyncInterval = 10 * time.Second + } + + config := clientv3.Config{ + Context: ctx, + Endpoints: clientArgs.Endpoints, + DialTimeout: clientArgs.DialTimeout, + DialKeepAliveTime: time.Second * 2, + DialKeepAliveTimeout: time.Second * 6, + AutoSyncInterval: clientArgs.AutoSyncInterval, + } + + if clientArgs.CaFile != "" && clientArgs.CertFile != "" && clientArgs.KeyFile != "" { + // create etcd client with tls + tlsInfo := transport.TLSInfo{ + CertFile: clientArgs.CertFile, + KeyFile: clientArgs.KeyFile, + TrustedCAFile: clientArgs.CaFile, + } + tlsConfig, err := tlsInfo.ClientConfig() + if err != nil { + return err + } + config.TLS = tlsConfig + config.DialOptions = []grpc.DialOption{grpc.WithInsecure()} + + } + gogo.Go(func(ctx context.Context) error { + var err error + for { + e.EtcdClient, err = clientv3.New(config) + if err == nil { + logrus.Infof("create etcd.v3 client success") + return nil + } + logrus.Errorf("create etcd.v3 client failed, try time is %d,%s", 10, err.Error()) + time.Sleep(10 * time.Second) + } + }) + time.Sleep(5 * time.Second) + return nil +} + +// CloseHandle - +func (e *Component) CloseHandle() { +} diff --git a/worker/appm/componentdefinition/componentdefinition_test.go b/pkg/component/grpc/grpcComponent.go similarity index 54% rename from worker/appm/componentdefinition/componentdefinition_test.go rename to pkg/component/grpc/grpcComponent.go index beb4fe95c..e39758dff 100644 --- a/worker/appm/componentdefinition/componentdefinition_test.go +++ b/pkg/component/grpc/grpcComponent.go @@ -1,5 +1,5 @@ // RAINBOND, Application Management Platform -// Copyright (C) 2021-2021 Goodrain Co., Ltd. +// Copyright (C) 2021-2024 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 @@ -16,27 +16,38 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package componentdefinition +package grpc import ( - "encoding/json" - "testing" - - v1 "github.com/goodrain/rainbond/worker/appm/types/v1" + "context" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/worker/client" ) -func TestTemplateContext(t *testing.T) { - ctx := NewTemplateContext(&v1.AppService{AppServiceBase: v1.AppServiceBase{ServiceID: "1234567890", ServiceAlias: "niasdjaj", TenantID: "098765432345678"}}, cueTemplate, map[string]interface{}{ - "kubernetes": map[string]interface{}{ - "name": "service-name", - "namespace": "t-namesapce", - }, - "port": []map[string]interface{}{}, - }) - manifests, err := ctx.GenerateComponentManifests() - if err != nil { - t.Fatal(err) - } - show, _ := json.Marshal(manifests) - t.Log(string(show)) +var defaultGrpcComponent *Component + +// Component - +type Component struct { + StatusClient *client.AppRuntimeSyncClient +} + +// Start - +func (c *Component) Start(ctx context.Context, cfg *configs.Config) (err error) { + c.StatusClient, err = client.NewClient(ctx, cfg.APIConfig.RbdWorker) + return err +} + +// CloseHandle - +func (c *Component) CloseHandle() { +} + +// Grpc - +func Grpc() *Component { + defaultGrpcComponent = &Component{} + return defaultGrpcComponent +} + +// Default - +func Default() *Component { + return defaultGrpcComponent } diff --git a/pkg/component/hubregistry/registryComponent.go b/pkg/component/hubregistry/registryComponent.go new file mode 100644 index 000000000..45c4ec7f1 --- /dev/null +++ b/pkg/component/hubregistry/registryComponent.go @@ -0,0 +1,78 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package hubregistry + +import ( + "context" + rainbondv1alpha1 "github.com/goodrain/rainbond-operator/api/v1alpha1" + "github.com/goodrain/rainbond/builder/sources/registry" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/grctl/clients" + "github.com/goodrain/rainbond/pkg/component/k8s" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "k8s.io/apimachinery/pkg/types" +) + +var defaultRegistryComponent *RegistryComponent + +// RegistryComponent - +type RegistryComponent struct { + RegistryCli *registry.Registry +} + +// HubRegistry - +func HubRegistry() *RegistryComponent { + defaultRegistryComponent = &RegistryComponent{} + return defaultRegistryComponent +} + +// Start - +func (r *RegistryComponent) Start(ctx context.Context, cfg *configs.Config) error { + logrus.Infof("init hub registry...") + var cluster rainbondv1alpha1.RainbondCluster + + err := clients.K8SClientInitClient(k8s.Default().Clientset, k8s.Default().RestConfig) + if err != nil { + logrus.Errorf("k8s client init rainbondClient failure: %v", err) + return err + } + if err := clients.RainbondKubeClient.Get(context.Background(), types.NamespacedName{Namespace: "rbd-system", Name: "rainbondcluster"}, &cluster); err != nil { + return errors.Wrap(err, "get configuration from rainbond cluster") + } + + registryConfig := cluster.Spec.ImageHub + if registryConfig.Domain == "goodrain.me" { + registryConfig.Domain = cfg.APIConfig.RbdHub + } + + r.RegistryCli, err = registry.NewInsecure(registryConfig.Domain, registryConfig.Username, registryConfig.Password) + logrus.Info("init hub registry success") + return err +} + +// CloseHandle - +func (r *RegistryComponent) CloseHandle() { + +} + +// Default - +func Default() *RegistryComponent { + return defaultRegistryComponent +} diff --git a/pkg/component/k8s/k8sComponent.go b/pkg/component/k8s/k8sComponent.go new file mode 100644 index 000000000..08bc19658 --- /dev/null +++ b/pkg/component/k8s/k8sComponent.go @@ -0,0 +1,123 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package k8s + +import ( + "context" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/pkg/generated/clientset/versioned" + rainbondscheme "github.com/goodrain/rainbond/pkg/generated/clientset/versioned/scheme" + k8sutil "github.com/goodrain/rainbond/util/k8s" + "github.com/sirupsen/logrus" + "k8s.io/apimachinery/pkg/api/meta" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/dynamic" + "k8s.io/client-go/kubernetes" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "k8s.io/client-go/rest" + "k8s.io/client-go/restmapper" + "kubevirt.io/client-go/kubecli" + k8sclient "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1beta1" + gateway "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1beta1" +) + +// Component - +type Component struct { + RestConfig *rest.Config + Clientset *kubernetes.Clientset + GatewayClient *v1beta1.GatewayV1beta1Client + DynamicClient *dynamic.DynamicClient + + RainbondClient *versioned.Clientset + K8sClient k8sclient.Client + KubevirtCli kubecli.KubevirtClient + + Mapper meta.RESTMapper +} + +var defaultK8sComponent *Component + +// Client - +func Client() *Component { + defaultK8sComponent = &Component{} + return defaultK8sComponent +} + +// Start - +func (k *Component) Start(ctx context.Context, cfg *configs.Config) error { + logrus.Infof("init k8s client...") + config, err := k8sutil.NewRestConfig(cfg.APIConfig.KubeConfigPath) + k.RestConfig = config + if err != nil { + logrus.Errorf("create k8s config failure: %v", err) + return err + } + k.Clientset, err = kubernetes.NewForConfig(config) + if err != nil { + logrus.Errorf("create k8s client failure: %v", err) + return err + } + k.GatewayClient, err = gateway.NewForConfig(config) + if err != nil { + logrus.Errorf("create gateway client failure: %v", err) + return err + } + k.DynamicClient, err = dynamic.NewForConfig(config) + if err != nil { + logrus.Errorf("create dynamic client failure: %v", err) + return err + } + + k.RainbondClient = versioned.NewForConfigOrDie(config) + + scheme := runtime.NewScheme() + clientgoscheme.AddToScheme(scheme) + rainbondscheme.AddToScheme(scheme) + k.K8sClient, err = k8sclient.New(config, k8sclient.Options{ + Scheme: scheme, + }) + if err != nil { + logrus.Errorf("create k8s client failure: %v", err) + return err + } + + k.KubevirtCli, err = kubecli.GetKubevirtClientFromRESTConfig(config) + if err != nil { + logrus.Errorf("create kubevirt cli failure: %v", err) + return err + } + + gr, err := restmapper.GetAPIGroupResources(k.Clientset) + if err != nil { + return err + } + k.Mapper = restmapper.NewDiscoveryRESTMapper(gr) + logrus.Infof("init k8s client success") + return nil +} + +// CloseHandle - +func (k *Component) CloseHandle() { +} + +// Default - +func Default() *Component { + return defaultK8sComponent +} diff --git a/builder/build/dockerfile_build_test.go b/pkg/component/mq/mqComponent.go similarity index 55% rename from builder/build/dockerfile_build_test.go rename to pkg/component/mq/mqComponent.go index be2165ec2..e7093624f 100644 --- a/builder/build/dockerfile_build_test.go +++ b/pkg/component/mq/mqComponent.go @@ -1,5 +1,5 @@ // RAINBOND, Application Management Platform -// Copyright (C) 2014-2017 Goodrain Co., Ltd. +// Copyright (C) 2021-2024 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 @@ -16,22 +16,39 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package build +package mq import ( - "testing" + "context" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/mq/client" ) -func TestGetARGs(t *testing.T) { - buildEnvs := make(map[string]string) - buildEnvs["ARG_TEST"] = "abcdefg" - buildEnvs["PROC_ENV"] = "{\"procfile\": \"\", \"dependencies\": {}, \"language\": \"dockerfile\", \"runtimes\": \"\"}" +var defaultMqComponent *Component - args := GetARGs(buildEnvs) - if v := buildEnvs["ARG_TEST"]; *args["TEST"] != v { - t.Errorf("Expected %s for arg[\"%s\"], but returned %s", buildEnvs["ARG_TEST"], "ARG_TEST", *args["TEST"]) - } - if procEnv := args["PROC_ENV"]; procEnv != nil { - t.Errorf("Expected nil for args[\"PROC_ENV\"], but returned %v", procEnv) - } +// Component - +type Component struct { + MqClient client.MQClient +} + +// Start - +func (c *Component) Start(ctx context.Context, cfg *configs.Config) error { + mqClient, err := client.NewMqClient(cfg.APIConfig.MQAPI) + c.MqClient = mqClient + return err +} + +// CloseHandle - +func (c *Component) CloseHandle() { +} + +// MQ - +func MQ() *Component { + defaultMqComponent = &Component{} + return defaultMqComponent +} + +// Default - +func Default() *Component { + return defaultMqComponent } diff --git a/event/manager_test.go b/pkg/component/prom/promComponent.go similarity index 51% rename from event/manager_test.go rename to pkg/component/prom/promComponent.go index 69857ebe0..e63d629ad 100644 --- a/event/manager_test.go +++ b/pkg/component/prom/promComponent.go @@ -1,5 +1,5 @@ -// Copyright (C) 2014-2018 Goodrain Co., Ltd. // RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 @@ -16,30 +16,41 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package event +package prom import ( - "testing" - "time" - - etcdutil "github.com/goodrain/rainbond/util/etcd" + "context" + "github.com/goodrain/rainbond/api/client/prometheus" + "github.com/goodrain/rainbond/config/configs" ) -func TestLogger(t *testing.T) { - err := NewManager(EventConfig{ - EventLogServers: []string{"192.168.195.1:6366"}, - DiscoverArgs: &etcdutil.ClientArgs{Endpoints: []string{"192.168.195.1:2379"}}, - }) - if err != nil { - t.Fatal(err) - } - defer GetManager().Close() - time.Sleep(time.Second * 3) - for i := 0; i < 500; i++ { - GetManager().GetLogger("qwdawdasdasasfafa").Info("hello word", nil) - GetManager().GetLogger("asdasdasdasdads").Debug("hello word", nil) - GetManager().GetLogger("1234124124124").Error("hello word", nil) - time.Sleep(time.Millisecond * 1) - } - select {} +var defaultPromComponent *Component + +// Component - +type Component struct { + PrometheusCli prometheus.Interface +} + +// Prometheus - +func Prometheus() *Component { + defaultPromComponent = &Component{} + return defaultPromComponent +} + +// Start - +func (c *Component) Start(ctx context.Context, cfg *configs.Config) error { + prometheusCli, err := prometheus.NewPrometheus(&prometheus.Options{ + Endpoint: cfg.APIConfig.PrometheusEndpoint, + }) + c.PrometheusCli = prometheusCli + return err +} + +// CloseHandle - +func (c *Component) CloseHandle() { +} + +// Default - +func Default() *Component { + return defaultPromComponent } diff --git a/eventlog/store/new_monitor_message_store_test.go b/pkg/gogo/go.go similarity index 62% rename from eventlog/store/new_monitor_message_store_test.go rename to pkg/gogo/go.go index e6ae66fdd..f7acbecf1 100644 --- a/eventlog/store/new_monitor_message_store_test.go +++ b/pkg/gogo/go.go @@ -1,5 +1,5 @@ -// Copyright (C) 2014-2018 Goodrain Co., Ltd. // RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 @@ -16,24 +16,33 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package store +package gogo -import "testing" +import ( + "context" + "sync" +) -func TestMerge(t *testing.T) { - s1 := MonitorMessageList{ - MonitorMessage{ - Key: "/asdadasd", - }, +var wg sync.WaitGroup + +// Go 框架处理协程,用于优雅启停 +func Go(fun func(ctx context.Context) error, opts ...Option) error { + wg.Add(1) + options := &Options{} + for _, o := range opts { + o(options) } - s2 := MonitorMessageList{ - MonitorMessage{ - Key: "/asdadasd", - }, - MonitorMessage{ - Key: "/asda12dasd", - }, + if options.ctx == nil { + options.ctx = context.Background() } - re := merge(s1, s2) - t.Log(re) + go func() { + defer wg.Done() + _ = fun(options.ctx) + }() + return nil +} + +// Wait 等待所有协程结束 +func Wait() { + wg.Wait() } diff --git a/eventlog/store/barrel_test.go b/pkg/gogo/option.go similarity index 77% rename from eventlog/store/barrel_test.go rename to pkg/gogo/option.go index a5b9d7de4..d0dc90aa2 100644 --- a/eventlog/store/barrel_test.go +++ b/pkg/gogo/option.go @@ -1,5 +1,5 @@ -// Copyright (C) 2014-2018 Goodrain Co., Ltd. // RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 @@ -16,4 +16,18 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -package store +package gogo + +import "context" + +type Option func(*Options) + +type Options struct { + ctx context.Context +} + +func WithContext(ctx context.Context) Option { + return func(o *Options) { + o.ctx = ctx + } +} diff --git a/pkg/interceptors/http.go b/pkg/interceptors/http.go new file mode 100644 index 000000000..ff994c2dd --- /dev/null +++ b/pkg/interceptors/http.go @@ -0,0 +1,94 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package interceptors + +import ( + "encoding/json" + "fmt" + "github.com/goodrain/rainbond/pkg/component/etcd" + "github.com/goodrain/rainbond/pkg/component/grpc" + "github.com/goodrain/rainbond/pkg/component/hubregistry" + "github.com/goodrain/rainbond/pkg/component/mq" + "github.com/goodrain/rainbond/pkg/component/prom" + "net/http" + "strings" +) + +// Recoverer - +func Recoverer(next http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + defer func() { + if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler { + handleServiceUnavailable(w, r) + } + }() + + next.ServeHTTP(w, r) + } + + return http.HandlerFunc(fn) +} + +// isNilPointerException Check if the panic is a nil pointer exception +func isNilPointerException(p interface{}) bool { + if p == nil { + return false + } + + errMsg := fmt.Sprintf("%v", p) + return strings.Contains(errMsg, "runtime error: invalid memory address or nil pointer dereference") || strings.Contains(errMsg, "runtime error: slice bounds out of range") +} + +// handleServiceUnavailable - +func handleServiceUnavailable(w http.ResponseWriter, r *http.Request) { + // Additional information about why etcd service is not available + errorMessage := "部分服务不可用" + + if etcd.Default().EtcdClient == nil { + errorMessage = "Etcd 服务不可用" + } else if grpc.Default().StatusClient == nil { + errorMessage = "worker 服务不可用" + } else if hubregistry.Default().RegistryCli == nil { + errorMessage = "私有镜像仓库 服务不可用" + } else if mq.Default().MqClient == nil { + errorMessage = "mq 服务不可用" + } else if prom.Default().PrometheusCli == nil { + errorMessage = "monitor 服务不可用" + } + + // Create a response JSON + response := map[string]interface{}{ + "error": errorMessage, + } + + // Convert the response to JSON + responseJSON, err := json.Marshal(response) + if err != nil { + // Handle JSON marshaling error + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + + // Set appropriate headers + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusServiceUnavailable) + + // Write the JSON response to the client + _, _ = w.Write(responseJSON) +} diff --git a/pkg/rainbond/component.go b/pkg/rainbond/component.go new file mode 100644 index 000000000..b1f1c85f2 --- /dev/null +++ b/pkg/rainbond/component.go @@ -0,0 +1,56 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package rainbond + +import ( + "context" + "github.com/goodrain/rainbond/config/configs" +) + +type Component interface { + Start(ctx context.Context, cfg *configs.Config) error + CloseHandle() +} + +type ComponentCancel interface { + Component + StartCancel(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error +} + +type FuncComponent func(ctx context.Context, cfg *configs.Config) error + +func (f FuncComponent) Start(ctx context.Context, cfg *configs.Config) error { + return f(ctx, cfg) +} + +func (f FuncComponent) CloseHandle() { +} + +type FuncComponentCancel func(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error + +func (f FuncComponentCancel) Start(ctx context.Context, cfg *configs.Config) error { + return f.StartCancel(ctx, nil, cfg) +} + +func (f FuncComponentCancel) StartCancel(ctx context.Context, cancel context.CancelFunc, cfg *configs.Config) error { + return f(ctx, cancel, cfg) +} + +func (f FuncComponentCancel) CloseHandle() { +} diff --git a/pkg/rainbond/registry.go b/pkg/rainbond/registry.go new file mode 100644 index 000000000..c6f53e338 --- /dev/null +++ b/pkg/rainbond/registry.go @@ -0,0 +1,104 @@ +// RAINBOND, Application Management Platform +// Copyright (C) 2021-2024 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 . + +package rainbond + +import ( + "context" + "errors" + "github.com/goodrain/rainbond/config/configs" + "github.com/goodrain/rainbond/pkg/gogo" + "log" + "os" + "os/signal" + "reflect" + "syscall" + "time" +) + +type Rainbond struct { + ctx context.Context + cancel context.CancelFunc + cfg *configs.Config + components []Component + disableLog bool +} + +type CloseHandle func() + +// New 初始化cago +func New(ctx context.Context, cfg *configs.Config) *Rainbond { + ctx, cancel := context.WithCancel(ctx) + cago := &Rainbond{ + ctx: ctx, + cancel: cancel, + cfg: cfg, + } + return cago +} + +// Registry 注册组件 +func (r *Rainbond) Registry(component Component) *Rainbond { + err := component.Start(r.ctx, r.cfg) + if err != nil { + panic(err) + } + r.components = append(r.components, component) + return r +} + +// RegistryCancel 注册cancel组件 +func (r *Rainbond) RegistryCancel(component ComponentCancel) *Rainbond { + err := component.StartCancel(r.ctx, r.cancel, r.cfg) + if err != nil { + panic(errors.New("start component error: " + reflect.TypeOf(component).String() + " " + err.Error())) + } + r.components = append(r.components, component) + return r +} + +// Start 启动框架,在此之前组件已全部启动,此处只做停止等待 +func (r *Rainbond) Start() error { + quitSignal := make(chan os.Signal, 1) + // 优雅启停 + signal.Notify( + quitSignal, + syscall.SIGINT, syscall.SIGTERM, + ) + select { + case <-quitSignal: + r.cancel() + case <-r.ctx.Done(): + } + log.Println(r.cfg.AppName + " is stopping...") + for _, v := range r.components { + v.CloseHandle() + } + // 等待所有组件退出 + stopCh := make(chan struct{}) + go func() { + gogo.Wait() + close(stopCh) + }() + select { + case <-stopCh: + case <-time.After(time.Second * 10): + } + log.Println(r.cfg.AppName + " is stopped") + return nil +} diff --git a/test/shell/echo1.sh b/test/shell/echo1.sh deleted file mode 100644 index ad8c41b82..000000000 --- a/test/shell/echo1.sh +++ /dev/null @@ -1 +0,0 @@ -echo "{\"inner\":{\"HOME_PATH\":\"/home\"},\"type\":\"check\",\"exec_status\":\"Success\",\"status\":[{\"next_tasks\":[\"echo2\"]}]}" >&2 \ No newline at end of file diff --git a/test/shell/echo2.sh b/test/shell/echo2.sh deleted file mode 100644 index c439628fa..000000000 --- a/test/shell/echo2.sh +++ /dev/null @@ -1 +0,0 @@ - echo "{\"inner\":{\"HOME_PATH\":\"/home\"},\"type\":\"install\",\"exec_status\":\"Success\",\"status\":[{\"condition_type\":\"INIT_ECHO\",\"condition_status\":\"True\"}]}" >&2 diff --git a/util/comman_test.go b/util/comman_test.go index 61a070d6a..9a2206d9d 100644 --- a/util/comman_test.go +++ b/util/comman_test.go @@ -58,12 +58,6 @@ func TestZip(t *testing.T) { } } -func TestUnzip(t *testing.T) { - if err := Unzip("/tmp/cache.zip", "/tmp/cache0"); err != nil { - t.Fatal(err) - } -} - func TestCreateVersionByTime(t *testing.T) { if re := CreateVersionByTime(); re != "" { t.Log(re) diff --git a/worker/client/client.go b/worker/client/client.go index 977d963f7..75bfaffa3 100644 --- a/worker/client/client.go +++ b/worker/client/client.go @@ -25,71 +25,41 @@ import ( "github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/util" - etcdutil "github.com/goodrain/rainbond/util/etcd" - grpcutil "github.com/goodrain/rainbond/util/grpc" v1 "github.com/goodrain/rainbond/worker/appm/types/v1" "github.com/goodrain/rainbond/worker/server/pb" "github.com/sirupsen/logrus" "google.golang.org/grpc" ) -//AppRuntimeSyncClient grpc client +// AppRuntimeSyncClient grpc client type AppRuntimeSyncClient struct { pb.AppRuntimeSyncClient - AppRuntimeSyncClientConf cc *grpc.ClientConn ctx context.Context } -//AppRuntimeSyncClientConf client conf -type AppRuntimeSyncClientConf struct { - NonBlock bool - EtcdEndpoints []string - EtcdCaFile string - EtcdCertFile string - EtcdKeyFile string - DefaultServerAddress []string -} +// NewClient new client +// ctx must be cancel where client not used +func NewClient(ctx context.Context, grpcServer string) (c *AppRuntimeSyncClient, err error) { + c = new(AppRuntimeSyncClient) + c.ctx = ctx + logrus.Infof("discover app runtime sync server address %s", grpcServer) + c.cc, err = grpc.Dial(grpcServer, grpc.WithInsecure()) -//NewClient new client -//ctx must be cancel where client not used -func NewClient(ctx context.Context, conf AppRuntimeSyncClientConf) (*AppRuntimeSyncClient, error) { - var arsc AppRuntimeSyncClient - arsc.AppRuntimeSyncClientConf = conf - arsc.ctx = ctx - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: conf.EtcdEndpoints, - CaFile: conf.EtcdCaFile, - CertFile: conf.EtcdCertFile, - KeyFile: conf.EtcdKeyFile, - } - c, err := etcdutil.NewClient(ctx, etcdClientArgs) if err != nil { return nil, err } - r := &grpcutil.GRPCResolver{Client: c} - b := grpc.RoundRobin(r) - dialOpts := []grpc.DialOption{ - grpc.WithBalancer(b), - grpc.WithInsecure(), - } - if !conf.NonBlock { - dialOpts = append(dialOpts, grpc.WithBlock()) - } - arsc.cc, err = grpc.DialContext(ctx, "/rainbond/discover/app_sync_runtime_server", dialOpts...) - if err != nil { - return nil, err - } - arsc.AppRuntimeSyncClient = pb.NewAppRuntimeSyncClient(arsc.cc) - return &arsc, nil + c.AppRuntimeSyncClient = pb.NewAppRuntimeSyncClient(c.cc) + + return c, nil } -//when watch occurred error,will exec this method +// when watch occurred error,will exec this method func (a *AppRuntimeSyncClient) Error(err error) { logrus.Errorf("discover app runtime sync server address occurred err:%s", err.Error()) } -//GetStatus get status +// GetStatus get status func (a *AppRuntimeSyncClient) GetStatus(serviceID string) string { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -102,7 +72,7 @@ func (a *AppRuntimeSyncClient) GetStatus(serviceID string) string { return status.Status[serviceID] } -//GetOperatorWatchData get operator watch data +// GetOperatorWatchData get operator watch data func (a *AppRuntimeSyncClient) GetOperatorWatchData(appID string) (*pb.OperatorManaged, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -115,7 +85,7 @@ func (a *AppRuntimeSyncClient) GetOperatorWatchData(appID string) (*pb.OperatorM return status, nil } -//GetStatuss get multiple app status +// GetStatuss get multiple app status func (a *AppRuntimeSyncClient) GetStatuss(serviceIDs string) map[string]string { ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() @@ -133,7 +103,7 @@ func (a *AppRuntimeSyncClient) GetStatuss(serviceIDs string) map[string]string { return status.Status } -//GetAllStatus get all status +// GetAllStatus get all status func (a *AppRuntimeSyncClient) GetAllStatus() map[string]string { ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() @@ -146,7 +116,7 @@ func (a *AppRuntimeSyncClient) GetAllStatus() map[string]string { return status.Status } -//GetNeedBillingStatus get need billing status +// GetNeedBillingStatus get need billing status func (a *AppRuntimeSyncClient) GetNeedBillingStatus() (map[string]string, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() @@ -163,7 +133,7 @@ func (a *AppRuntimeSyncClient) GetNeedBillingStatus() (map[string]string, error) return res, nil } -//GetServiceDeployInfo get service deploy info +// GetServiceDeployInfo get service deploy info func (a *AppRuntimeSyncClient) GetServiceDeployInfo(serviceID string) (*pb.DeployInfo, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -176,12 +146,12 @@ func (a *AppRuntimeSyncClient) GetServiceDeployInfo(serviceID string) (*pb.Deplo return re, nil } -//IsClosedStatus check status +// IsClosedStatus check status func (a *AppRuntimeSyncClient) IsClosedStatus(curStatus string) bool { return curStatus == "" || curStatus == v1.BUILDEFAILURE || curStatus == v1.CLOSED || curStatus == v1.UNDEPLOY || curStatus == v1.BUILDING || curStatus == v1.UNKNOW } -//GetTenantResource get tenant resource +// GetTenantResource get tenant resource func (a *AppRuntimeSyncClient) GetTenantResource(tenantID string) (*pb.TenantResource, error) { if logrus.IsLevelEnabled(logrus.DebugLevel) { defer util.Elapsed("[AppRuntimeSyncClient] get tenant resource")() @@ -192,7 +162,7 @@ func (a *AppRuntimeSyncClient) GetTenantResource(tenantID string) (*pb.TenantRes return a.AppRuntimeSyncClient.GetTenantResource(ctx, &pb.TenantRequest{TenantId: tenantID}) } -//GetAllTenantResource get all tenant resource +// GetAllTenantResource get all tenant resource func (a *AppRuntimeSyncClient) GetAllTenantResource() (*pb.TenantResourceList, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) defer cancel() diff --git a/worker/client/client_test.go b/worker/client/client_test.go deleted file mode 100644 index ba06c47db..000000000 --- a/worker/client/client_test.go +++ /dev/null @@ -1,63 +0,0 @@ -// 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 . - -package client - -import ( - "context" - "testing" - "time" - - "github.com/goodrain/rainbond/worker/server/pb" -) - -func TestGetAppStatus(t *testing.T) { - client, err := NewClient(context.Background(), AppRuntimeSyncClientConf{ - EtcdEndpoints: []string{"192.168.3.252:2379"}, - }) - if err != nil { - t.Fatal(err) - } - time.Sleep(3 * time.Second) - status, err := client.GetAppStatus(context.Background(), &pb.AppStatusReq{ - AppId: "43eaae441859eda35b02075d37d83589", - }) - if err != nil { - t.Fatal(err) - } - t.Log(status) -} - -func TestGetPodDetail(t *testing.T) { - client, err := NewClient(context.Background(), AppRuntimeSyncClientConf{ - EtcdEndpoints: []string{"127.0.0.1:2379"}, - }) - if err != nil { - t.Fatalf("error creating grpc client: %v", err) - } - - time.Sleep(3 * time.Second) - - sid := "bc2e153243e4917acaf0c6088789fa12" - podname := "bc2e153243e4917acaf0c6088789fa12-deployment-6cc445949b-2dh25" - detail, err := client.GetPodDetail(sid, podname) - if err != nil { - t.Errorf("unexpected error: %v", err) - } - t.Logf("pod detail: %+v", detail) -} diff --git a/worker/discover/manager.go b/worker/discover/manager.go index 908894b3e..778b32bff 100644 --- a/worker/discover/manager.go +++ b/worker/discover/manager.go @@ -30,7 +30,6 @@ import ( "github.com/goodrain/rainbond/cmd/worker/option" "github.com/goodrain/rainbond/mq/api/grpc/pb" "github.com/goodrain/rainbond/mq/client" - etcdutil "github.com/goodrain/rainbond/util/etcd" "github.com/goodrain/rainbond/worker/appm/controller" "github.com/goodrain/rainbond/worker/appm/store" "github.com/goodrain/rainbond/worker/discover/model" @@ -42,13 +41,13 @@ import ( var healthStatus = make(map[string]string, 1) -//TaskNum exec task number +// TaskNum exec task number var TaskNum float64 -//TaskError exec error task number +// TaskError exec error task number var TaskError float64 -//TaskManager task +// TaskManager task type TaskManager struct { ctx context.Context cancel context.CancelFunc @@ -60,7 +59,7 @@ type TaskManager struct { clientset *kubernetes.Clientset } -//NewTaskManager return *TaskManager +// NewTaskManager return *TaskManager func NewTaskManager(cfg option.Config, store store.Storer, controllermanager *controller.Manager, @@ -84,15 +83,9 @@ func NewTaskManager(cfg option.Config, } } -//Start 启动 +// Start 启动 func (t *TaskManager) Start() error { - etcdClientArgs := &etcdutil.ClientArgs{ - Endpoints: t.config.EtcdEndPoints, - CaFile: t.config.EtcdCaFile, - CertFile: t.config.EtcdCertFile, - KeyFile: t.config.EtcdKeyFile, - } - client, err := client.NewMqClient(etcdClientArgs, t.config.MQAPI) + client, err := client.NewMqClient(t.config.MQAPI) if err != nil { logrus.Errorf("new Mq client error, %v", err) healthStatus["status"] = "unusual" @@ -105,7 +98,7 @@ func (t *TaskManager) Start() error { return nil } -//Do do +// Do do func (t *TaskManager) Do() { logrus.Info("start receive task from mq") hostname, _ := os.Hostname() @@ -164,7 +157,7 @@ func (t *TaskManager) Do() { } } -//Stop 停止 +// Stop 停止 func (t *TaskManager) Stop() error { logrus.Info("discover manager is stoping.") t.cancel() @@ -174,7 +167,7 @@ func (t *TaskManager) Stop() error { return nil } -//HealthCheck health check +// HealthCheck health check func HealthCheck() map[string]string { return healthStatus }