perf: Reduce API component dependency on ETCD (#1833)

* perf: optimize communication exceptions
This commit is contained in:
逆流而上 2024-01-10 13:36:35 +08:00 committed by GitHub
parent 3464882cf5
commit 2a4e3b219d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
66 changed files with 1177 additions and 2222 deletions

View File

@ -22,11 +22,9 @@ import (
"net/http" "net/http"
"github.com/goodrain/rainbond/api/api" "github.com/goodrain/rainbond/api/api"
"github.com/goodrain/rainbond/api/discover"
"github.com/goodrain/rainbond/api/proxy" "github.com/goodrain/rainbond/api/proxy"
"github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/cmd/api/option"
mqclient "github.com/goodrain/rainbond/mq/client" mqclient "github.com/goodrain/rainbond/mq/client"
etcdutil "github.com/goodrain/rainbond/util/etcd"
"github.com/goodrain/rainbond/worker/client" "github.com/goodrain/rainbond/worker/client"
) )
@ -69,13 +67,7 @@ func GetManager() V2Manager {
// NewManager new manager // NewManager new manager
func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) { func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2Routes, error) {
etcdClientArgs := &etcdutil.ClientArgs{ mqClient, err := mqclient.NewMqClient(conf.MQAPI)
Endpoints: conf.EtcdEndpoint,
CaFile: conf.EtcdCaFile,
CertFile: conf.EtcdCertFile,
KeyFile: conf.EtcdKeyFile,
}
mqClient, err := mqclient.NewMqClient(etcdClientArgs, conf.MQAPI)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -86,7 +78,7 @@ func NewManager(conf option.Config, statusCli *client.AppRuntimeSyncClient) (*V2
v2r.GatewayStruct.cfg = &conf v2r.GatewayStruct.cfg = &conf
v2r.LabelController.optconfig = &conf v2r.LabelController.optconfig = &conf
eventServerProxy := proxy.CreateProxy("eventlog", "http", []string{"local=>rbd-eventlog:6363"}) 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 v2r.EventLogStruct.EventlogServerProxy = eventServerProxy
return &v2r, nil return &v2r, nil
} }

View File

@ -20,12 +20,12 @@ package controller
import ( import (
"context" "context"
"github.com/goodrain/rainbond/config/configs"
"net/http" "net/http"
"os" "os"
"path" "path"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/discover"
"github.com/goodrain/rainbond/api/handler" "github.com/goodrain/rainbond/api/handler"
"github.com/goodrain/rainbond/api/proxy" "github.com/goodrain/rainbond/api/proxy"
ctxutil "github.com/goodrain/rainbond/api/util/ctx" ctxutil "github.com/goodrain/rainbond/api/util/ctx"
@ -33,110 +33,107 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
//DockerConsole docker console // DockerConsole docker console
type DockerConsole struct { type DockerConsole struct {
socketproxy proxy.Proxy socketproxy proxy.Proxy
} }
var defaultDockerConsoleEndpoints = []string{"127.0.0.1:7171"}
var defaultEventLogEndpoints = []string{"local=>rbd-eventlog:6363"}
var dockerConsole *DockerConsole var dockerConsole *DockerConsole
//GetDockerConsole get Docker console // GetDockerConsole get Docker console
func GetDockerConsole() *DockerConsole { func GetDockerConsole() *DockerConsole {
if dockerConsole != nil { if dockerConsole != nil {
return dockerConsole return dockerConsole
} }
dockerConsole = &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 return dockerConsole
} }
//Get get // Get get
func (d DockerConsole) Get(w http.ResponseWriter, r *http.Request) { func (d DockerConsole) Get(w http.ResponseWriter, r *http.Request) {
d.socketproxy.Proxy(w, r) d.socketproxy.Proxy(w, r)
} }
var dockerLog *DockerLog var dockerLog *DockerLog
//DockerLog docker log // DockerLog docker log
type DockerLog struct { type DockerLog struct {
socketproxy proxy.Proxy socketproxy proxy.Proxy
} }
//GetDockerLog get docker log // GetDockerLog get docker log
func GetDockerLog() *DockerLog { func GetDockerLog() *DockerLog {
if dockerLog == nil { if dockerLog == nil {
dockerLog = &DockerLog{ 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 return dockerLog
} }
//Get get // Get get
func (d DockerLog) Get(w http.ResponseWriter, r *http.Request) { func (d DockerLog) Get(w http.ResponseWriter, r *http.Request) {
d.socketproxy.Proxy(w, r) d.socketproxy.Proxy(w, r)
} }
//MonitorMessage monitor message // MonitorMessage monitor message
type MonitorMessage struct { type MonitorMessage struct {
socketproxy proxy.Proxy socketproxy proxy.Proxy
} }
var monitorMessage *MonitorMessage var monitorMessage *MonitorMessage
//GetMonitorMessage get MonitorMessage // GetMonitorMessage get MonitorMessage
func GetMonitorMessage() *MonitorMessage { func GetMonitorMessage() *MonitorMessage {
if monitorMessage == nil { if monitorMessage == nil {
monitorMessage = &MonitorMessage{ 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 return monitorMessage
} }
//Get get // Get get
func (d MonitorMessage) Get(w http.ResponseWriter, r *http.Request) { func (d MonitorMessage) Get(w http.ResponseWriter, r *http.Request) {
d.socketproxy.Proxy(w, r) d.socketproxy.Proxy(w, r)
} }
//EventLog event log // EventLog event log
type EventLog struct { type EventLog struct {
socketproxy proxy.Proxy socketproxy proxy.Proxy
} }
var eventLog *EventLog var eventLog *EventLog
//GetEventLog get event log // GetEventLog get event log
func GetEventLog() *EventLog { func GetEventLog() *EventLog {
if eventLog == nil { if eventLog == nil {
eventLog = &EventLog{ 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 return eventLog
} }
//Get get // Get get
func (d EventLog) Get(w http.ResponseWriter, r *http.Request) { func (d EventLog) Get(w http.ResponseWriter, r *http.Request) {
d.socketproxy.Proxy(w, r) d.socketproxy.Proxy(w, r)
} }
//LogFile log file down server // LogFile log file down server
type LogFile struct { type LogFile struct {
Root string Root string
} }
var logFile *LogFile var logFile *LogFile
//GetLogFile get log file // GetLogFile get log file
func GetLogFile() *LogFile { func GetLogFile() *LogFile {
root := os.Getenv("SERVICE_LOG_ROOT") root := os.Getenv("SERVICE_LOG_ROOT")
if root == "" { if root == "" {
@ -151,7 +148,7 @@ func GetLogFile() *LogFile {
return logFile return logFile
} }
//Get get // Get get
func (d LogFile) Get(w http.ResponseWriter, r *http.Request) { func (d LogFile) Get(w http.ResponseWriter, r *http.Request) {
gid := chi.URLParam(r, "gid") gid := chi.URLParam(r, "gid")
filename := chi.URLParam(r, "filename") filename := chi.URLParam(r, "filename")
@ -180,23 +177,23 @@ func (d LogFile) GetInstallLog(w http.ResponseWriter, r *http.Request) {
var pubSubControll *PubSubControll var pubSubControll *PubSubControll
//PubSubControll service pub sub // PubSubControll service pub sub
type PubSubControll struct { type PubSubControll struct {
socketproxy proxy.Proxy socketproxy proxy.Proxy
} }
//GetPubSubControll get service pub sub controller // GetPubSubControll get service pub sub controller
func GetPubSubControll() *PubSubControll { func GetPubSubControll() *PubSubControll {
if pubSubControll == nil { if pubSubControll == nil {
pubSubControll = &PubSubControll{ 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 return pubSubControll
} }
//Get pubsub controller // Get pubsub controller
func (d PubSubControll) Get(w http.ResponseWriter, r *http.Request) { func (d PubSubControll) Get(w http.ResponseWriter, r *http.Request) {
serviceID := chi.URLParam(r, "serviceID") serviceID := chi.URLParam(r, "serviceID")
name, _ := handler.GetEventHandler().GetLogInstance(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) 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) { func (d PubSubControll) GetHistoryLog(w http.ResponseWriter, r *http.Request) {
serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string) serviceID := r.Context().Value(ctxutil.ContextKey("service_id")).(string)
name, _ := handler.GetEventHandler().GetLogInstance(serviceID) name, _ := handler.GetEventHandler().GetLogInstance(serviceID)

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}

View File

@ -19,37 +19,38 @@
package db package db
import ( import (
"encoding/json" "context"
"github.com/goodrain/rainbond/config/configs"
"time" "time"
tsdbClient "github.com/bluebreezecf/opentsdb-goclient/client" tsdbClient "github.com/bluebreezecf/opentsdb-goclient/client"
tsdbConfig "github.com/bluebreezecf/opentsdb-goclient/config" tsdbConfig "github.com/bluebreezecf/opentsdb-goclient/config"
"github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/config" "github.com/goodrain/rainbond/db/config"
dbModel "github.com/goodrain/rainbond/db/model" 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/goodrain/rainbond/worker/discover/model"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
//ConDB struct // ConDB struct
type ConDB struct { type ConDB struct {
ConnectionInfo string ConnectionInfo string
DBType string DBType string
} }
//CreateDBManager get db manager // Database -
//TODO: need to try when happened error, try 4 times func Database() *ConDB {
func CreateDBManager(conf option.Config) error { return &ConDB{}
}
// Start -
func (d *ConDB) Start(ctx context.Context, cfg *configs.Config) error {
logrus.Info("start db client...")
dbCfg := config.Config{ dbCfg := config.Config{
MysqlConnectionInfo: conf.DBConnectionInfo, MysqlConnectionInfo: cfg.APIConfig.DBConnectionInfo,
DBType: conf.DBType, DBType: cfg.APIConfig.DBType,
ShowSQL: conf.ShowSQL, ShowSQL: cfg.APIConfig.ShowSQL,
} }
if err := db.CreateManager(dbCfg); err != nil { if err := db.CreateManager(dbCfg); err != nil {
logrus.Errorf("get db manager failed,%s", err.Error()) logrus.Errorf("get db manager failed,%s", err.Error())
@ -61,65 +62,27 @@ func CreateDBManager(conf option.Config) error {
return nil return nil
} }
//CreateEventManager create event manager // CloseHandle -
func CreateEventManager(conf option.Config) error { func (d *ConDB) CloseHandle() {
var tryTime time.Duration err := db.CloseManager()
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
}
}
if err != nil { if err != nil {
logrus.Errorf("get event manager failed. %v", err.Error()) logrus.Errorf("close db manager failed,%s", err.Error())
return err
} }
logrus.Debugf("init event manager success")
return nil
} }
//MQManager mq manager // TaskStruct task struct
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
type TaskStruct struct { type TaskStruct struct {
TaskType string TaskType string
TaskBody model.TaskBody TaskBody model.TaskBody
User string User string
} }
//OpentsdbManager OpentsdbManager // OpentsdbManager OpentsdbManager
type OpentsdbManager struct { type OpentsdbManager struct {
Endpoint string Endpoint string
} }
//NewOpentsdbManager NewOpentsdbManager // NewOpentsdbManager NewOpentsdbManager
func (o *OpentsdbManager) NewOpentsdbManager() (tsdbClient.Client, error) { func (o *OpentsdbManager) NewOpentsdbManager() (tsdbClient.Client, error) {
opentsdbCfg := tsdbConfig.OpenTSDBConfig{ opentsdbCfg := tsdbConfig.OpenTSDBConfig{
OpentsdbHost: o.Endpoint, OpentsdbHost: o.Endpoint,
@ -131,25 +94,7 @@ func (o *OpentsdbManager) NewOpentsdbManager() (tsdbClient.Client, error) {
return tc, nil return tc, nil
} }
//BuildTask build task // GetBegin get db transaction
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
func GetBegin() *gorm.DB { func GetBegin() *gorm.DB {
return db.GetManager().Begin() return db.GetManager().Begin()
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

View File

@ -19,64 +19,46 @@
package handler package handler
import ( 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/group"
"github.com/goodrain/rainbond/api/handler/share" "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/cmd/api/option"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/pkg/generated/clientset/versioned" "github.com/goodrain/rainbond/pkg/component/etcd"
etcdutil "github.com/goodrain/rainbond/util/etcd" "github.com/goodrain/rainbond/pkg/component/grpc"
"github.com/goodrain/rainbond/worker/client" "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" "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 // InitHandle 初始化handle
func InitHandle(conf option.Config, func InitHandle(conf option.Config) error {
etcdClientArgs *etcdutil.ClientArgs,
statusCli *client.AppRuntimeSyncClient, // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可
etcdcli *clientv3.Client, // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可
kubeClient *kubernetes.Clientset, // 注意:以下 client 将不要再次通过参数形式传递 !!!直接在你想调用的地方调用即可
rainbondClient versioned.Interface,
k8sClient k8sclient.Client, etcdcli := etcd.Default().EtcdClient
config *rest.Config, statusCli := grpc.Default().StatusClient
mapper meta.RESTMapper, clientset := k8s.Default().Clientset
dynamicClient dynamic.Interface, rainbondClient := k8s.Default().RainbondClient
gatewayClient *gateway.GatewayV1beta1Client, k8sClient := k8s.Default().K8sClient
kubevirtCli kubecli.KubevirtClient, restconfig := k8s.Default().RestConfig
registryCli *registry.Registry, dynamicClient := k8s.Default().DynamicClient
) error { gatewayClient := k8s.Default().GatewayClient
mq := api_db.MQManager{ kubevirtCli := k8s.Default().KubevirtCli
EtcdClientArgs: etcdClientArgs, mapper := k8s.Default().Mapper
DefaultServer: conf.MQAPI, registryCli := hubregistry.Default().RegistryCli
} mqClient := mq.Default().MqClient
mqClient, errMQ := mq.NewMQManager() prometheusCli := prom.Default().PrometheusCli
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
}
dbmanager := db.GetManager() 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) defaultPluginHandler = CreatePluginManager(mqClient)
defaultAppHandler = CreateAppManager(mqClient) defaultAppHandler = CreateAppManager(mqClient)
defaultTenantHandler = CreateTenManager(mqClient, statusCli, &conf, kubeClient, prometheusCli, k8sClient) defaultTenantHandler = CreateTenManager(mqClient, statusCli, &conf, clientset, prometheusCli, k8sClient)
defaultHelmHandler = CreateHelmManager(kubeClient, rainbondClient, config, mapper) defaultHelmHandler = CreateHelmManager(clientset, rainbondClient, restconfig, mapper)
defaultNetRulesHandler = CreateNetRulesManager(etcdcli) defaultNetRulesHandler = CreateNetRulesManager(etcdcli)
defaultCloudHandler = CreateCloudManager(conf) defaultCloudHandler = CreateCloudManager(conf)
defaultAPPBackupHandler = group.CreateBackupHandle(mqClient, statusCli, etcdcli) defaultAPPBackupHandler = group.CreateBackupHandle(mqClient, statusCli, etcdcli)
@ -87,20 +69,20 @@ func InitHandle(conf option.Config,
logrus.Errorf("create token identification mannager error, %v", err) logrus.Errorf("create token identification mannager error, %v", err)
return err return err
} }
defaultGatewayHandler = CreateGatewayManager(dbmanager, mqClient, etcdcli, gatewayClient, kubeClient) defaultGatewayHandler = CreateGatewayManager(dbmanager, mqClient, etcdcli, gatewayClient, clientset)
def3rdPartySvcHandler = Create3rdPartySvcHandler(dbmanager, statusCli) def3rdPartySvcHandler = Create3rdPartySvcHandler(dbmanager, statusCli)
operationHandler = CreateOperationHandler(mqClient) operationHandler = CreateOperationHandler(mqClient)
batchOperationHandler = CreateBatchOperationHandler(mqClient, statusCli, operationHandler) batchOperationHandler = CreateBatchOperationHandler(mqClient, statusCli, operationHandler)
defaultAppRestoreHandler = NewAppRestoreHandler() defaultAppRestoreHandler = NewAppRestoreHandler()
defPodHandler = NewPodHandler(statusCli) 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) defaultVolumeTypeHandler = CreateVolumeTypeManger(statusCli)
defaultEtcdHandler = NewEtcdHandler(etcdcli) defaultEtcdHandler = NewEtcdHandler(etcdcli)
defaultmonitorHandler = NewMonitorHandler(prometheusCli) defaultmonitorHandler = NewMonitorHandler(prometheusCli)
defServiceEventHandler = NewServiceEventHandler() defServiceEventHandler = NewServiceEventHandler()
defApplicationHandler = NewApplicationHandler(statusCli, prometheusCli, rainbondClient, kubeClient, dynamicClient) defApplicationHandler = NewApplicationHandler(statusCli, prometheusCli, rainbondClient, clientset, dynamicClient)
defRegistryAuthSecretHandler = CreateRegistryAuthSecretManager(dbmanager, mqClient, etcdcli) defRegistryAuthSecretHandler = CreateRegistryAuthSecretManager(dbmanager, mqClient)
defNodesHandler = NewNodesHandler(kubeClient, conf.RbdNamespace, config, mapper, prometheusCli) defNodesHandler = NewNodesHandler(clientset, conf.RbdNamespace, restconfig, mapper, prometheusCli)
return nil return nil
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
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))
}

View File

@ -19,7 +19,6 @@
package handler package handler
import ( import (
"github.com/goodrain/rainbond/api/discover"
"github.com/goodrain/rainbond/api/proxy" "github.com/goodrain/rainbond/api/proxy"
"github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/cmd/api/option"
) )
@ -30,11 +29,10 @@ var prometheusProxy proxy.Proxy
var monitorProxy proxy.Proxy var monitorProxy proxy.Proxy
var kubernetesDashboard proxy.Proxy var kubernetesDashboard proxy.Proxy
//InitProxy 初始化 // InitProxy 初始化
func InitProxy(conf option.Config) { func InitProxy(conf option.Config) {
if nodeProxy == nil { if nodeProxy == nil {
nodeProxy = proxy.CreateProxy("acp_node", "http", conf.NodeAPI) nodeProxy = proxy.CreateProxy("acp_node", "http", conf.NodeAPI)
discover.GetEndpointDiscover().AddProject("acp_node", nodeProxy)
} }
if builderProxy == nil { if builderProxy == nil {
builderProxy = proxy.CreateProxy("builder", "http", conf.BuilderAPI) builderProxy = proxy.CreateProxy("builder", "http", conf.BuilderAPI)
@ -42,34 +40,34 @@ func InitProxy(conf option.Config) {
if prometheusProxy == nil { if prometheusProxy == nil {
prometheusProxy = proxy.CreateProxy("prometheus", "http", []string{conf.PrometheusEndpoint}) prometheusProxy = proxy.CreateProxy("prometheus", "http", []string{conf.PrometheusEndpoint})
} }
if monitorProxy == nil { //if monitorProxy == nil {
monitorProxy = proxy.CreateProxy("monitor", "http", []string{"127.0.0.1:3329"}) // monitorProxy = proxy.CreateProxy("monitor", "http", []string{"127.0.0.1:3329"})
discover.GetEndpointDiscover().AddProject("monitor", monitorProxy) // discover.GetEndpointDiscover().AddProject("monitor", monitorProxy)
} //}
if kubernetesDashboard == nil { if kubernetesDashboard == nil {
kubernetesDashboard = proxy.CreateProxy("kubernetesdashboard", "http", []string{conf.KuberentesDashboardAPI}) kubernetesDashboard = proxy.CreateProxy("kubernetesdashboard", "http", []string{conf.KuberentesDashboardAPI})
} }
} }
//GetNodeProxy GetNodeProxy // GetNodeProxy GetNodeProxy
func GetNodeProxy() proxy.Proxy { func GetNodeProxy() proxy.Proxy {
return nodeProxy return nodeProxy
} }
//GetBuilderProxy GetNodeProxy // GetBuilderProxy GetNodeProxy
func GetBuilderProxy() proxy.Proxy { func GetBuilderProxy() proxy.Proxy {
return builderProxy return builderProxy
} }
//GetPrometheusProxy GetPrometheusProxy // GetPrometheusProxy GetPrometheusProxy
func GetPrometheusProxy() proxy.Proxy { func GetPrometheusProxy() proxy.Proxy {
return prometheusProxy return prometheusProxy
} }
//GetMonitorProxy GetMonitorProxy //GetMonitorProxy GetMonitorProxy
func GetMonitorProxy() proxy.Proxy { //func GetMonitorProxy() proxy.Proxy {
return monitorProxy // return monitorProxy
} //}
// GetKubernetesDashboardProxy returns the kubernetes dashboard proxy. // GetKubernetesDashboardProxy returns the kubernetes dashboard proxy.
func GetKubernetesDashboardProxy() proxy.Proxy { func GetKubernetesDashboardProxy() proxy.Proxy {

View File

@ -21,7 +21,6 @@ package handler
import ( import (
"fmt" "fmt"
"github.com/coreos/etcd/clientv3"
apimodel "github.com/goodrain/rainbond/api/model" apimodel "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db" "github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/mq/client" "github.com/goodrain/rainbond/mq/client"
@ -31,15 +30,13 @@ import (
type RegistryAuthSecretAction struct { type RegistryAuthSecretAction struct {
dbmanager db.Manager dbmanager db.Manager
mqclient client.MQClient mqclient client.MQClient
etcdCli *clientv3.Client
} }
// CreateRegistryAuthSecretManager creates registry auth secret manager // 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{ return &RegistryAuthSecretAction{
dbmanager: dbmanager, dbmanager: dbmanager,
mqclient: mqclient, mqclient: mqclient,
etcdCli: etcdCli,
} }
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
package handler

View File

@ -34,7 +34,7 @@ import (
"github.com/twinj/uuid" "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) { func (s *ServiceAction) ServiceCheck(scs *api_model.ServiceCheckStruct) (string, string, *util.APIHandleError) {
checkUUID := uuid.NewV4().String() checkUUID := uuid.NewV4().String()
scs.Body.CheckUUID = checkUUID 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) { func (s *ServiceAction) GetServiceCheckInfo(uuid string) (*exector.ServiceCheckResult, *util.APIHandleError) {
k := fmt.Sprintf("/servicecheck/%s", uuid) k := fmt.Sprintf("/servicecheck/%s", uuid)
var si exector.ServiceCheckResult var si exector.ServiceCheckResult
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel() defer cancel()
resp, err := s.EtcdCli.Get(ctx, k) resp, err := s.EtcdCli.Get(ctx, k)
if err != nil { if err != nil {
logrus.Errorf("get etcd k %s error, %v", k, err) 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 { if resp.Count == 0 {
return &si, nil return &si, nil

View File

@ -46,7 +46,7 @@ func init() {
} }
} }
//InitTenant 实现中间件 // InitTenant 实现中间件
func InitTenant(next http.Handler) http.Handler { func InitTenant(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
debugRequestBody(r) debugRequestBody(r)
@ -75,7 +75,7 @@ func InitTenant(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
//InitService 实现serviceinit中间件 // InitService 实现serviceinit中间件
func InitService(next http.Handler) http.Handler { func InitService(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
serviceAlias := chi.URLParam(r, "service_alias") serviceAlias := chi.URLParam(r, "service_alias")
@ -120,7 +120,7 @@ func InitApplication(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
//InitPlugin 实现plugin init中间件 // InitPlugin 实现plugin init中间件
func InitPlugin(next http.Handler) http.Handler { func InitPlugin(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
debugRequestBody(r) debugRequestBody(r)
@ -147,7 +147,7 @@ func InitPlugin(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
//SetLog SetLog // SetLog SetLog
func SetLog(next http.Handler) http.Handler { func SetLog(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
eventID := chi.URLParam(r, "event_id") eventID := chi.URLParam(r, "event_id")
@ -160,7 +160,7 @@ func SetLog(next http.Handler) http.Handler {
return http.HandlerFunc(fn) return http.HandlerFunc(fn)
} }
//Proxy 反向代理中间件 // Proxy 反向代理中间件
func Proxy(next http.Handler) http.Handler { func Proxy(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) { fn := func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.RequestURI, "/v2/nodes") { if strings.HasPrefix(r.RequestURI, "/v2/nodes") {
@ -191,10 +191,10 @@ func Proxy(next http.Handler) http.Handler {
handler.GetNodeProxy().Proxy(w, r) handler.GetNodeProxy().Proxy(w, r)
return return
} }
if strings.HasPrefix(r.RequestURI, "/v2/rules") { //if strings.HasPrefix(r.RequestURI, "/v2/rules") {
handler.GetMonitorProxy().Proxy(w, r) // handler.GetMonitorProxy().Proxy(w, r)
return // return
} //}
if strings.HasPrefix(r.RequestURI, "/kubernetes/dashboard") { if strings.HasPrefix(r.RequestURI, "/kubernetes/dashboard") {
proxy := handler.GetKubernetesDashboardProxy() proxy := handler.GetKubernetesDashboardProxy()
r.URL.Path = strings.Replace(r.URL.Path, "/kubernetes/dashboard", "", 1) 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 { func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var ( var (
serviceKind string serviceKind string
) )
serviceObj := r.Context().Value(ctxutil.ContextKey("service")) serviceObj := r.Context().Value(ctxutil.ContextKey("service"))
if serviceObj != nil { if serviceObj != nil {

View File

@ -23,6 +23,7 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/clientv3"
"github.com/goodrain/rainbond/pkg/interceptors"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -105,7 +106,7 @@ func (m *Manager) SetMiddleware() {
r.Use(middleware.DefaultLogger) r.Use(middleware.DefaultLogger)
} }
//Gracefully absorb panics and prints the stack trace //Gracefully absorb panics and prints the stack trace
r.Use(middleware.Recoverer) r.Use(interceptors.Recoverer)
//request time out //request time out
r.Use(middleware.Timeout(time.Second * 5)) r.Use(middleware.Timeout(time.Second * 5))
//simple authz //simple authz

View File

@ -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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

View File

@ -20,12 +20,15 @@
package main package main
import ( 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" "os"
"github.com/goodrain/rainbond/cmd" "github.com/goodrain/rainbond/cmd"
"github.com/goodrain/rainbond/cmd/api/option" "github.com/goodrain/rainbond/cmd/api/option"
"github.com/goodrain/rainbond/cmd/api/server"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
@ -38,8 +41,25 @@ func main() {
s.AddFlags(pflag.CommandLine) s.AddFlags(pflag.CommandLine)
pflag.Parse() pflag.Parse()
s.SetLog() s.SetLog()
if err := server.Run(s); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err) configs.SetDefault(&configs.Config{
os.Exit(1) 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())
} }
} }

View File

@ -33,6 +33,8 @@ type Config struct {
APIAddrSSL string APIAddrSSL string
DBConnectionInfo string DBConnectionInfo string
EventLogServers []string EventLogServers []string
DockerConsoleServers []string
EventLogEndpoints []string
NodeAPI []string NodeAPI []string
BuilderAPI []string BuilderAPI []string
V1API string V1API string
@ -64,6 +66,8 @@ type Config struct {
RbdNamespace string RbdNamespace string
ShowSQL bool ShowSQL bool
GrctlImage string GrctlImage string
RbdHub string
RbdWorker string
} }
// APIServer apiserver server // 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.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.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.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.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.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.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.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") 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.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.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.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.StringVar(&a.RbdNamespace, "rbd-namespace", "rbd-system", "rbd component namespace")
fs.BoolVar(&a.ShowSQL, "show-sql", false, "The trigger for showing sql.") 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.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 // SetLog 设置log

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -80,7 +80,6 @@ func NewBuilder() *Builder {
// AddFlags config // AddFlags config
func (a *Builder) AddFlags(fs *pflag.FlagSet) { func (a *Builder) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the builder log level") 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.EtcdCaFile, "etcd-ca", "", "")
fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "")
fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "") 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.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.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.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.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.MaxTasks, "max-tasks", 50, "Maximum number of simultaneous build tasks")
fs.IntVar(&a.APIPort, "api-port", 3228, "the port for api server") 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.RunMode, "run", "sync", "sync data when worker start")
fs.StringVar(&a.DockerEndpoint, "dockerd", "127.0.0.1:2376", "dockerd endpoint") fs.StringVar(&a.DockerEndpoint, "dockerd", "127.0.0.1:2376", "dockerd endpoint")
fs.StringVar(&a.HostIP, "hostIP", "", "Current node Intranet IP") 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.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.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.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 // SetLog 设置log

View File

@ -67,12 +67,11 @@ func Run(s *option.Builder) error {
} }
if err := event.NewManager(event.EventConfig{ if err := event.NewManager(event.EventConfig{
EventLogServers: s.Config.EventLogServers, EventLogServers: s.Config.EventLogServers,
DiscoverArgs: etcdClientArgs,
}); err != nil { }); err != nil {
return err return err
} }
defer event.CloseManager() defer event.CloseManager()
mqClient, err := client.NewMqClient(etcdClientArgs, s.Config.MQAPI) mqClient, err := client.NewMqClient(s.Config.MQAPI)
if err != nil { if err != nil {
logrus.Errorf("new Mq mqClient error, %v", err) logrus.Errorf("new Mq mqClient error, %v", err)
return err return err

View File

@ -56,7 +56,7 @@ func NewLogServer() *LogServer {
} }
} }
//AddFlags 添加参数 // AddFlags 添加参数
func (s *LogServer) AddFlags(fs *pflag.FlagSet) { 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.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") 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.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.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.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.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.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") 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") 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() { func (s *LogServer) InitLog() {
log := logrus.New() log := logrus.New()
if l, err := logrus.ParseLevel(s.Conf.Log.LogLevel); err == nil { if l, err := logrus.ParseLevel(s.Conf.Log.LogLevel); err == nil {
@ -155,7 +155,7 @@ func (s *LogServer) InitLog() {
s.Logger = log s.Logger = log
} }
//InitConf 初始化配置 // InitConf 初始化配置
func (s *LogServer) InitConf() { func (s *LogServer) InitConf() {
s.Conf.Cluster.Discover.ClusterMode = s.Conf.ClusterMode s.Conf.Cluster.Discover.ClusterMode = s.Conf.ClusterMode
s.Conf.Cluster.PubSub.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 { func (s *LogServer) Run() error {
s.Logger.Debug("Start run server.") s.Logger.Debug("Start run server.")
log := s.Logger log := s.Logger

View File

@ -41,7 +41,7 @@ func NewGWServer() *GWServer {
return &GWServer{} return &GWServer{}
} }
//Config contains all configuration // Config contains all configuration
type Config struct { type Config struct {
K8SConfPath string K8SConfPath string
EtcdEndpoint []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.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") 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 // 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.IntVar(&g.EtcdTimeout, "etcd-timeout", 10, "etcd http timeout seconds")
fs.StringVar(&g.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") fs.StringVar(&g.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ")
fs.StringVar(&g.EtcdCertFile, "etcd-cert", "", "etcd tls cert 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.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.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.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 // SetLog sets log
@ -138,7 +140,7 @@ func (g *GWServer) SetLog() {
logrus.SetLevel(level) logrus.SetLevel(level)
} }
//CheckConfig check config // CheckConfig check config
func (g *GWServer) CheckConfig() error { func (g *GWServer) CheckConfig() error {
if g.NodeName == "" { if g.NodeName == "" {
g.NodeName, _ = os.Hostname() g.NodeName, _ = os.Hostname()

View File

@ -99,7 +99,7 @@ func NewConfig() *Config {
host, _ := os.Hostname() host, _ := os.Hostname()
config := &Config{ config := &Config{
EtcdEndpointsLine: "http://127.0.0.1:2379", EtcdEndpointsLine: "http://rbd-etcd:2379",
EtcdEndpoints: []string{}, EtcdEndpoints: []string{},
AdvertiseAddr: host + ":9999", AdvertiseAddr: host + ":9999",
BindIP: host, BindIP: host,
@ -134,7 +134,7 @@ func NewConfig() *Config {
return config return config
} }
//AddFlag monitor flag // AddFlag monitor flag
func (c *Config) AddFlag(cmd *pflag.FlagSet) { func (c *Config) AddFlag(cmd *pflag.FlagSet) {
cmd.StringVar(&c.EtcdEndpointsLine, "etcd-endpoints", c.EtcdEndpointsLine, "etcd endpoints list.") cmd.StringVar(&c.EtcdEndpointsLine, "etcd-endpoints", c.EtcdEndpointsLine, "etcd endpoints list.")
cmd.StringVar(&c.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") 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") cmd.StringVar(&c.KubeConfig, "kube-config", "", "kubernetes api server config file")
} }
//AddPrometheusFlag prometheus flag // AddPrometheusFlag prometheus flag
func (c *Config) AddPrometheusFlag(cmd *pflag.FlagSet) { func (c *Config) AddPrometheusFlag(cmd *pflag.FlagSet) {
cmd.StringVar(&c.ConfigFile, "config.file", c.ConfigFile, "Prometheus configuration file path.") cmd.StringVar(&c.ConfigFile, "config.file", c.ConfigFile, "Prometheus configuration file path.")

View File

@ -22,7 +22,7 @@ import "github.com/spf13/pflag"
import "github.com/sirupsen/logrus" import "github.com/sirupsen/logrus"
import "fmt" import "fmt"
//Config config server // Config config server
type Config struct { type Config struct {
EtcdEndPoints []string EtcdEndPoints []string
EtcdCaFile string EtcdCaFile string
@ -38,21 +38,20 @@ type Config struct {
HostName string HostName string
} }
//MQServer lb worker server // MQServer lb worker server
type MQServer struct { type MQServer struct {
Config Config
LogLevel string LogLevel string
} }
//NewMQServer new server // NewMQServer new server
func NewMQServer() *MQServer { func NewMQServer() *MQServer {
return &MQServer{} return &MQServer{}
} }
//AddFlags config // AddFlags config
func (a *MQServer) AddFlags(fs *pflag.FlagSet) { func (a *MQServer) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the mq log level") 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.IntVar(&a.EtcdTimeout, "etcd-timeout", 10, "etcd http timeout seconds")
fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ") fs.StringVar(&a.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ")
fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "etcd tls cert 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.PrometheusMetricPath, "metric", "/metrics", "prometheus metrics path")
fs.StringVar(&a.HostIP, "hostIP", "", "Current node Intranet IP") fs.StringVar(&a.HostIP, "hostIP", "", "Current node Intranet IP")
fs.StringVar(&a.HostName, "hostName", "", "Current node host name") 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() { func (a *MQServer) SetLog() {
level, err := logrus.ParseLevel(a.LogLevel) level, err := logrus.ParseLevel(a.LogLevel)
if err != nil { if err != nil {

View File

@ -42,7 +42,7 @@ var mode string
func main() { func main() {
AddFlags(pflag.CommandLine) AddFlags(pflag.CommandLine)
pflag.Parse() pflag.Parse()
c, err := client.NewMqClient(nil, server) c, err := client.NewMqClient(server)
if err != nil { if err != nil {
logrus.Error("new mq client error.", err.Error()) logrus.Error("new mq client error.", err.Error())
os.Exit(1) os.Exit(1)
@ -83,8 +83,9 @@ func main() {
} }
// AddFlags -
func AddFlags(fs *pflag.FlagSet) { 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(&topic, "topic", "builder", "mq topic")
fs.StringVar(&taskbody, "task-body", "", "mq task body") fs.StringVar(&taskbody, "task-body", "", "mq task body")
fs.StringVar(&taskfile, "task-file", "", "mq task body file") fs.StringVar(&taskfile, "task-file", "", "mq task body file")

View File

@ -154,15 +154,12 @@ type UDPMonitorConfig struct {
func (a *Conf) AddFlags(fs *pflag.FlagSet) { func (a *Conf) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the log level") 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.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.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.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.HostIP, "hostIP", "", "the host ip you can define. default get ip from eth0")
fs.StringVar(&a.PodIP, "podIP", "", "The pod ip of node.") 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.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.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.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.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") 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.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.ContainerRuntime, "container-runtime", sources.ContainerRuntimeContainerd, "container runtime, support docker and containerd")
fs.StringVar(&a.RuntimeEndpoint, "runtime-endpoint", sources.RuntimeEndpointContainerd, "container runtime endpoint") 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 // SetLog 设置log

View File

@ -44,7 +44,7 @@ import (
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
) )
//Run start run // Run start run
func Run(cfg *option.Conf) error { func Run(cfg *option.Conf) error {
var stoped = make(chan struct{}) var stoped = make(chan struct{})
stopfunc := func() error { stopfunc := func() error {
@ -88,7 +88,6 @@ func Run(cfg *option.Conf) error {
err = eventLog.NewManager(eventLog.EventConfig{ err = eventLog.NewManager(eventLog.EventConfig{
EventLogServers: cfg.EventLogServer, EventLogServers: cfg.EventLogServer,
DiscoverArgs: etcdClientArgs,
}) })
if err != nil { if err != nil {
logrus.Errorf("error creating eventlog manager") logrus.Errorf("error creating eventlog manager")

View File

@ -25,7 +25,7 @@ import (
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
//Config config server // Config config server
type Config struct { type Config struct {
EtcdEndPoints []string EtcdEndPoints []string
EtcdCaFile string EtcdCaFile string
@ -40,21 +40,21 @@ type Config struct {
K8SConfPath string K8SConfPath string
} }
//WebCliServer container webcli server // WebCliServer container webcli server
type WebCliServer struct { type WebCliServer struct {
Config Config
LogLevel string LogLevel string
} }
//NewWebCliServer new server // NewWebCliServer new server
func NewWebCliServer() *WebCliServer { func NewWebCliServer() *WebCliServer {
return &WebCliServer{} return &WebCliServer{}
} }
//AddFlags config // AddFlags config
func (a *WebCliServer) AddFlags(fs *pflag.FlagSet) { func (a *WebCliServer) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the webcli log level") 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.EtcdCaFile, "etcd-ca", "", "etcd tls ca file ")
fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "etcd tls cert file") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "etcd tls cert file")
fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "etcd http tls cert key 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") fs.StringVar(&a.PrometheusMetricPath, "metric", "/metrics", "prometheus metrics path")
} }
//SetLog 设置log // SetLog 设置log
func (a *WebCliServer) SetLog() { func (a *WebCliServer) SetLog() {
level, err := logrus.ParseLevel(a.LogLevel) level, err := logrus.ParseLevel(a.LogLevel)
if err != nil { if err != nil {

View File

@ -28,7 +28,7 @@ import (
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
) )
//Config config server // Config config server
type Config struct { type Config struct {
EtcdEndPoints []string EtcdEndPoints []string
EtcdCaFile string EtcdCaFile string
@ -67,22 +67,21 @@ type Helm struct {
ChartCache string ChartCache string
} }
//Worker worker server // Worker worker server
type Worker struct { type Worker struct {
Config Config
LogLevel string LogLevel string
RunMode string //default,sync RunMode string //default,sync
} }
//NewWorker new server // NewWorker new server
func NewWorker() *Worker { func NewWorker() *Worker {
return &Worker{} return &Worker{}
} }
//AddFlags config // AddFlags config
func (a *Worker) AddFlags(fs *pflag.FlagSet) { func (a *Worker) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&a.LogLevel, "log-level", "info", "the worker log level") 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.EtcdCaFile, "etcd-ca", "", "")
fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "") fs.StringVar(&a.EtcdCertFile, "etcd-cert", "", "")
fs.StringVar(&a.EtcdKeyFile, "etcd-key", "", "") 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.Listen, "listen", ":6369", "prometheus listen host and port")
fs.StringVar(&a.DBType, "db-type", "mysql", "db type mysql or etcd") 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.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.StringVar(&a.KubeConfig, "kube-config", "", "kubernetes api server config file")
fs.IntVar(&a.KubeAPIQPS, "kube-api-qps", 50, "kube client qps") 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.KubeAPIBurst, "kube-api-burst", 10, "kube clint burst")
fs.IntVar(&a.MaxTasks, "max-tasks", 50, "the max tasks for per node") 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.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.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") 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.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.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.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.RepoFile = path.Join(a.Helm.DataDir, "repo/repositories.yaml")
a.Helm.RepoCache = path.Join(a.Helm.DataDir, "cache") a.Helm.RepoCache = path.Join(a.Helm.DataDir, "cache")
a.Helm.ChartCache = path.Join(a.Helm.DataDir, "chart") a.Helm.ChartCache = path.Join(a.Helm.DataDir, "chart")
} }
//SetLog 设置log // SetLog 设置log
func (a *Worker) SetLog() { func (a *Worker) SetLog() {
level, err := logrus.ParseLevel(a.LogLevel) level, err := logrus.ParseLevel(a.LogLevel)
if err != nil { if err != nil {
@ -123,7 +125,7 @@ func (a *Worker) SetLog() {
logrus.SetLevel(level) logrus.SetLevel(level)
} }
//CheckEnv 检测环境变量 // CheckEnv 检测环境变量
func (a *Worker) CheckEnv() error { func (a *Worker) CheckEnv() error {
if err := os.Setenv("GRDATA_PVC_NAME", a.Config.GrdataPVCName); err != nil { if err := os.Setenv("GRDATA_PVC_NAME", a.Config.GrdataPVCName); err != nil {
return fmt.Errorf("set env 'GRDATA_PVC_NAME': %v", err) return fmt.Errorf("set env 'GRDATA_PVC_NAME': %v", err)

View File

@ -32,7 +32,6 @@ import (
"github.com/goodrain/rainbond/event" "github.com/goodrain/rainbond/event"
"github.com/goodrain/rainbond/pkg/common" "github.com/goodrain/rainbond/pkg/common"
"github.com/goodrain/rainbond/pkg/generated/clientset/versioned" "github.com/goodrain/rainbond/pkg/generated/clientset/versioned"
etcdutil "github.com/goodrain/rainbond/util/etcd"
k8sutil "github.com/goodrain/rainbond/util/k8s" k8sutil "github.com/goodrain/rainbond/util/k8s"
"github.com/goodrain/rainbond/worker/appm/componentdefinition" "github.com/goodrain/rainbond/worker/appm/componentdefinition"
"github.com/goodrain/rainbond/worker/appm/controller" "github.com/goodrain/rainbond/worker/appm/controller"
@ -62,15 +61,8 @@ func Run(s *option.Worker) error {
return err return err
} }
defer db.CloseManager() 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{ if err := event.NewManager(event.EventConfig{
EventLogServers: s.Config.EventLogServers, EventLogServers: s.Config.EventLogServers,
DiscoverArgs: etcdClientArgs,
}); err != nil { }); err != nil {
return err return err
} }

27
config/configs/config.go Normal file
View File

@ -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
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}

View File

@ -1,4 +1,4 @@
// Copyright (C) 2014-2018 Goodrain Co., Ltd. // Copyright (C) 2014-2024 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform // RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
@ -30,7 +30,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
//Manager db manager // Manager db manager
type Manager interface { type Manager interface {
CloseManager() error CloseManager() error
Begin() *gorm.DB Begin() *gorm.DB
@ -152,11 +152,11 @@ func init() {
supportDrivers = map[string]struct{}{ supportDrivers = map[string]struct{}{
"mysql": {}, "mysql": {},
"cockroachdb": {}, "cockroachdb": {},
"sqlite": {}, "sqlite": {},
} }
} }
//CreateManager 创建manager // CreateManager 创建manager
func CreateManager(config config.Config) (err error) { func CreateManager(config config.Config) (err error) {
if _, ok := supportDrivers[config.DBType]; !ok { if _, ok := supportDrivers[config.DBType]; !ok {
return fmt.Errorf("DB drivers: %s not supported", config.DBType) return fmt.Errorf("DB drivers: %s not supported", config.DBType)
@ -176,7 +176,7 @@ func CreateManager(config config.Config) (err error) {
return return
} }
//CloseManager close db manager // CloseManager close db manager
func CloseManager() error { func CloseManager() error {
if defaultManager == nil { if defaultManager == nil {
return errors.New("default db manager not init") return errors.New("default db manager not init")
@ -184,7 +184,7 @@ func CloseManager() error {
return defaultManager.CloseManager() return defaultManager.CloseManager()
} }
//GetManager get db manager // GetManager get db manager
func GetManager() Manager { func GetManager() Manager {
return defaultManager return defaultManager
} }

View File

@ -20,6 +20,7 @@ package etcd
import ( import (
"context" "context"
"github.com/coreos/etcd/clientv3"
"sync" "sync"
"github.com/goodrain/rainbond/db/config" "github.com/goodrain/rainbond/db/config"

View File

@ -27,19 +27,17 @@ import (
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/goodrain/rainbond/discover"
"github.com/goodrain/rainbond/discover/config" "github.com/goodrain/rainbond/discover/config"
eventclient "github.com/goodrain/rainbond/eventlog/entry/grpc/client" eventclient "github.com/goodrain/rainbond/eventlog/entry/grpc/client"
eventpb "github.com/goodrain/rainbond/eventlog/entry/grpc/pb" eventpb "github.com/goodrain/rainbond/eventlog/entry/grpc/pb"
"github.com/goodrain/rainbond/util" "github.com/goodrain/rainbond/util"
etcdutil "github.com/goodrain/rainbond/util/etcd"
"github.com/pquerna/ffjson/ffjson" "github.com/pquerna/ffjson/ffjson"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
//Manager 操作日志,客户端服务 // Manager 操作日志,客户端服务
//客户端负载均衡 // 客户端负载均衡
type Manager interface { type Manager interface {
GetLogger(eventID string) Logger GetLogger(eventID string) Logger
Start() error Start() error
@ -50,7 +48,6 @@ type Manager interface {
// EventConfig event config struct // EventConfig event config struct
type EventConfig struct { type EventConfig struct {
EventLogServers []string EventLogServers []string
DiscoverArgs *etcdutil.ClientArgs
} }
type manager struct { type manager struct {
ctx context.Context ctx context.Context
@ -62,7 +59,6 @@ type manager struct {
lock sync.Mutex lock sync.Mutex
eventServer []string eventServer []string
abnormalServer map[string]string abnormalServer map[string]string
dis discover.Discover
} }
var defaultManager Manager var defaultManager Manager
@ -75,15 +71,8 @@ const (
buffersize = 1000 buffersize = 1000
) )
//NewManager 创建manager // NewManager 创建manager
func NewManager(conf EventConfig) error { 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()) ctx, cancel := context.WithCancel(context.Background())
defaultManager = &manager{ defaultManager = &manager{
ctx: ctx, ctx: ctx,
@ -92,13 +81,12 @@ func NewManager(conf EventConfig) error {
loggers: make(map[string]Logger, 1024), loggers: make(map[string]Logger, 1024),
handles: make(map[string]handle), handles: make(map[string]handle),
eventServer: conf.EventLogServers, eventServer: conf.EventLogServers,
dis: dis,
abnormalServer: make(map[string]string), abnormalServer: make(map[string]string),
} }
return defaultManager.Start() return defaultManager.Start()
} }
//GetManager 获取日志服务 // GetManager 获取日志服务
func GetManager() Manager { func GetManager() Manager {
return defaultManager return defaultManager
} }
@ -108,13 +96,14 @@ func NewTestManager(m Manager) {
defaultManager = m defaultManager = m
} }
//CloseManager 关闭日志服务 // CloseManager 关闭日志服务
func CloseManager() { func CloseManager() {
if defaultManager != nil { if defaultManager != nil {
defaultManager.Close() defaultManager.Close()
} }
} }
// Start -
func (m *manager) Start() error { func (m *manager) Start() error {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
@ -129,13 +118,14 @@ func (m *manager) Start() error {
m.handles[m.eventServer[i]] = h m.handles[m.eventServer[i]] = h
go h.HandleLog() go h.HandleLog()
} }
if m.dis != nil { //if m.dis != nil {
m.dis.AddProject("event_log_event_grpc", m) // m.dis.AddProject("event_log_event_grpc", m)
} //}
go m.GC() go m.GC()
return nil return nil
} }
// UpdateEndpoints -
func (m *manager) UpdateEndpoints(endpoints ...*config.Endpoint) { func (m *manager) UpdateEndpoints(endpoints ...*config.Endpoint) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() 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)) 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) { func (m *manager) Error(err error) {
} }
// Close -
func (m *manager) Close() error { func (m *manager) Close() error {
m.cancel() m.cancel()
if m.dis != nil {
m.dis.Stop()
}
return nil return nil
} }
// GC -
func (m *manager) GC() { func (m *manager) GC() {
util.IntermittentExec(m.ctx, func() { util.IntermittentExec(m.ctx, func() {
m.lock.Lock() m.lock.Lock()
@ -208,8 +199,7 @@ func (m *manager) GC() {
}, time.Second*20) }, time.Second*20)
} }
//GetLogger // GetLogger 使用完成后必须调用ReleaseLogger方法
//使用完成后必须调用ReleaseLogger方法
func (m *manager) GetLogger(eventID string) Logger { func (m *manager) GetLogger(eventID string) Logger {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
@ -224,6 +214,7 @@ func (m *manager) GetLogger(eventID string) Logger {
return l return l
} }
// ReleaseLogger 释放logger
func (m *manager) ReleaseLogger(l Logger) { func (m *manager) ReleaseLogger(l Logger) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
@ -240,6 +231,7 @@ type handle struct {
manager *manager manager *manager
} }
// DiscardedLoggerChan -
func (m *manager) DiscardedLoggerChan(cacheChan chan []byte) { func (m *manager) DiscardedLoggerChan(cacheChan chan []byte) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
@ -285,6 +277,8 @@ func (m *manager) getLBChan() chan []byte {
} }
return nil return nil
} }
// RemoveHandle -
func (m *manager) RemoveHandle(server string) { func (m *manager) RemoveHandle(server string) {
m.lock.Lock() m.lock.Lock()
defer m.lock.Unlock() defer m.lock.Unlock()
@ -292,6 +286,8 @@ func (m *manager) RemoveHandle(server string) {
delete(m.handles, server) delete(m.handles, server)
} }
} }
// HandleLog -
func (m *handle) HandleLog() error { func (m *handle) HandleLog() error {
defer m.manager.RemoveHandle(m.server) defer m.manager.RemoveHandle(m.server)
return util.Exec(m.ctx, func() error { return util.Exec(m.ctx, func() error {
@ -332,11 +328,12 @@ func (m *handle) HandleLog() error {
}, time.Second*3) }, time.Second*3)
} }
// Stop -
func (m *handle) Stop() { func (m *handle) Stop() {
close(m.stop) close(m.stop)
} }
//Logger 日志发送器 // Logger 日志发送器
type Logger interface { type Logger interface {
Info(string, map[string]string) Info(string, map[string]string)
Error(string, map[string]string) Error(string, map[string]string)
@ -363,9 +360,12 @@ type logger struct {
createTime time.Time createTime time.Time
} }
// GetChan -
func (l *logger) GetChan() chan []byte { func (l *logger) GetChan() chan []byte {
return l.sendChan return l.sendChan
} }
// SetChan -
func (l *logger) SetChan(ch chan []byte) { func (l *logger) SetChan(ch chan []byte) {
l.sendChan = ch 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 { type LoggerWriter interface {
io.Writer io.Writer
SetFormat(map[string]interface{}) SetFormat(map[string]interface{})
@ -475,7 +475,7 @@ func (l *loggerWriter) Write(b []byte) (n int, err error) {
return len(b), nil return len(b), nil
} }
//GetTestLogger GetTestLogger // GetTestLogger GetTestLogger
func GetTestLogger() Logger { func GetTestLogger() Logger {
return &testLogger{} return &testLogger{}
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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<? order by a.top_tag desc,a.update_time desc limit ?,?","avgtime":"10.365219238095236","sumtime":"217.66960399999996","counts":"21"}]`)}
manager.MonitorMessageChan() <- [][]byte{[]byte("xxx"), []byte(urlData)}
}
}
func TestHandleMonitorMessage(t *testing.T) {
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 {
t.Fatal(err)
}
err = manager.Run()
if err != nil {
t.Fatal(err)
}
defer manager.Stop()
manager.MonitorMessageChan() <- [][]byte{[]byte("xxx"), []byte(urlData)}
}

View File

@ -1,120 +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 <http://www.gnu.org/licenses/>.
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)
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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()
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}
}

View File

@ -28,7 +28,7 @@ import (
httputil "github.com/goodrain/rainbond/util/http" httputil "github.com/goodrain/rainbond/util/http"
) )
//Server api server // Server api server
func Server(c *controller.RuleControllerManager) *chi.Mux { func Server(c *controller.RuleControllerManager) *chi.Mux {
r := chi.NewRouter() r := chi.NewRouter()
r.Route("/monitor", func(r chi.Router) { r.Route("/monitor", func(r chi.Router) {
@ -37,13 +37,13 @@ func Server(c *controller.RuleControllerManager) *chi.Mux {
httputil.ReturnSuccess(r, w, bean) httputil.ReturnSuccess(r, w, bean)
}) })
}) })
r.Route("/v2/rules", func(r chi.Router) { //r.Route("/v2/rules", func(r chi.Router) {
r.Post("/", c.AddRules) // r.Post("/", c.AddRules)
r.Put("/{rules_name}", c.RegRules) // r.Put("/{rules_name}", c.RegRules)
r.Delete("/{rules_name}", c.DelRules) // r.Delete("/{rules_name}", c.DelRules)
r.Get("/{rules_name}", c.GetRules) // r.Get("/{rules_name}", c.GetRules)
r.Get("/all", c.GetAllRules) // r.Get("/all", c.GetAllRules)
}) //})
util.ProfilerSetup(r) util.ProfilerSetup(r)
return r return r
} }

View File

@ -24,23 +24,21 @@ import (
"time" "time"
"github.com/goodrain/rainbond/mq/api/grpc/pb" "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" "github.com/sirupsen/logrus"
context "golang.org/x/net/context" context "golang.org/x/net/context"
grpc "google.golang.org/grpc" grpc "google.golang.org/grpc"
) )
//BuilderTopic builder for linux // BuilderTopic builder for linux
var BuilderTopic = "builder" var BuilderTopic = "builder"
//WindowsBuilderTopic builder for windows // WindowsBuilderTopic builder for windows
var WindowsBuilderTopic = "windows_builder" var WindowsBuilderTopic = "windows_builder"
//WorkerTopic worker topic // WorkerTopic worker topic
var WorkerTopic = "worker" var WorkerTopic = "worker"
//MQClient mq client // MQClient mq client
type MQClient interface { type MQClient interface {
pb.TaskQueueClient pb.TaskQueueClient
Close() Close()
@ -53,27 +51,12 @@ type mqClient struct {
cancel context.CancelFunc cancel context.CancelFunc
} }
//NewMqClient new a mq client // NewMqClient new a mq client
func NewMqClient(etcdClientArgs *etcdutil.ClientArgs, defaultserver string) (MQClient, error) { func NewMqClient(mqAddr string) (MQClient, error) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
var conn *grpc.ClientConn conn, err := grpc.DialContext(ctx, mqAddr, grpc.WithInsecure())
if etcdClientArgs != nil && etcdClientArgs.Endpoints != nil && len(defaultserver) > 1 { if err != nil {
c, err := etcdutil.NewClient(ctx, etcdClientArgs) return nil, err
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
}
} }
cli := pb.NewTaskQueueClient(conn) cli := pb.NewTaskQueueClient(conn)
client := &mqClient{ client := &mqClient{
@ -84,12 +67,12 @@ func NewMqClient(etcdClientArgs *etcdutil.ClientArgs, defaultserver string) (MQC
return client, nil 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() { func (m *mqClient) Close() {
m.cancel() m.cancel()
} }
//TaskStruct task struct // TaskStruct task struct
type TaskStruct struct { type TaskStruct struct {
Topic string Topic string
Arch string Arch string
@ -97,7 +80,7 @@ type TaskStruct struct {
TaskBody interface{} TaskBody interface{}
} }
//buildTask build task // buildTask build task
func buildTask(t TaskStruct) (*pb.EnqueueRequest, error) { func buildTask(t TaskStruct) (*pb.EnqueueRequest, error) {
var er pb.EnqueueRequest var er pb.EnqueueRequest
taskJSON, err := json.Marshal(t.TaskBody) taskJSON, err := json.Marshal(t.TaskBody)

View File

@ -19,6 +19,7 @@
package router package router
import ( import (
"github.com/goodrain/rainbond/pkg/interceptors"
"time" "time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -30,7 +31,7 @@ import (
"github.com/go-chi/chi/middleware" "github.com/go-chi/chi/middleware"
) )
//Routers 路由 // Routers 路由
func Routers(mode string) *chi.Mux { func Routers(mode string) *chi.Mux {
r := chi.NewRouter() r := chi.NewRouter()
r.Use(middleware.RequestID) //每个请求的上下文中注册一个id r.Use(middleware.RequestID) //每个请求的上下文中注册一个id
@ -41,7 +42,7 @@ func Routers(mode string) *chi.Mux {
logger.SetLevel(logrus.GetLevel()) logger.SetLevel(logrus.GetLevel())
r.Use(log.NewStructuredLogger(logger)) r.Use(log.NewStructuredLogger(logger))
//Gracefully absorb panics and prints the stack trace //Gracefully absorb panics and prints the stack trace
r.Use(middleware.Recoverer) r.Use(interceptors.Recoverer)
//request time out //request time out
r.Use(middleware.Timeout(time.Second * 5)) r.Use(middleware.Timeout(time.Second * 5))
r.Mount("/v1", DisconverRoutes()) r.Mount("/v1", DisconverRoutes())

View File

@ -35,31 +35,22 @@ import (
"github.com/goodrain/rainbond/node/utils" "github.com/goodrain/rainbond/node/utils"
"github.com/goodrain/rainbond/util" "github.com/goodrain/rainbond/util"
ansibleUtil "github.com/goodrain/rainbond/util/ansible" ansibleUtil "github.com/goodrain/rainbond/util/ansible"
etcdutil "github.com/goodrain/rainbond/util/etcd"
licutil "github.com/goodrain/rainbond/util/license" licutil "github.com/goodrain/rainbond/util/license"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/twinj/uuid" "github.com/twinj/uuid"
) )
//NodeService node service // NodeService node service
type NodeService struct { type NodeService struct {
c *option.Conf c *option.Conf
nodecluster *node.Cluster nodecluster *node.Cluster
kubecli kubecache.KubeClient kubecli kubecache.KubeClient
} }
//CreateNodeService create // CreateNodeService create
func CreateNodeService(c *option.Conf, nodecluster *node.Cluster, kubecli kubecache.KubeClient) *NodeService { 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{ if err := event.NewManager(event.EventConfig{
EventLogServers: c.EventLogServer, EventLogServers: c.EventLogServer,
DiscoverArgs: etcdClientArgs,
}); err != nil { }); err != nil {
logrus.Errorf("create event manager faliure") 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) { func (n *NodeService) AddNode(node *client.APIHostNode) (*client.HostNode, *utils.APIHandleError) {
if n.nodecluster == nil { if n.nodecluster == nil {
return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) 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 return rbnode, nil
} }
//InstallNode install node // InstallNode install node
func (n *NodeService) InstallNode(node *client.HostNode) *utils.APIHandleError { func (n *NodeService) InstallNode(node *client.HostNode) *utils.APIHandleError {
node.Status = client.Installing node.Status = client.Installing
node.NodeStatus.Status = client.Installing node.NodeStatus.Status = client.Installing
@ -153,7 +144,7 @@ func (n *NodeService) writeHostsFile() error {
return nil return nil
} }
//UpdateNodeStatus update node status // UpdateNodeStatus update node status
func (n *NodeService) UpdateNodeStatus(nodeID, status string) *utils.APIHandleError { func (n *NodeService) UpdateNodeStatus(nodeID, status string) *utils.APIHandleError {
node := n.nodecluster.GetNode(nodeID) node := n.nodecluster.GetNode(nodeID)
if node == nil { if node == nil {
@ -168,7 +159,7 @@ func (n *NodeService) UpdateNodeStatus(nodeID, status string) *utils.APIHandleEr
return nil return nil
} }
//AsynchronousInstall AsynchronousInstall // AsynchronousInstall AsynchronousInstall
func (n *NodeService) AsynchronousInstall(node *client.HostNode, eventID string) { func (n *NodeService) AsynchronousInstall(node *client.HostNode, eventID string) {
// write ansible hosts file // write ansible hosts file
err := n.writeHostsFile() err := n.writeHostsFile()
@ -206,8 +197,8 @@ func (n *NodeService) AsynchronousInstall(node *client.HostNode, eventID string)
logrus.Infof("Install node %s successful", node.ID) logrus.Infof("Install node %s successful", node.ID)
} }
//DeleteNode delete node // DeleteNode delete node
//only node status is offline and node can be deleted // only node status is offline and node can be deleted
func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError { func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError {
node := n.nodecluster.GetNode(nodeID) node := n.nodecluster.GetNode(nodeID)
if node == nil { if node == nil {
@ -224,7 +215,7 @@ func (n *NodeService) DeleteNode(nodeID string) *utils.APIHandleError {
return nil return nil
} }
//GetNode get node info // GetNode get node info
func (n *NodeService) GetNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { func (n *NodeService) GetNode(nodeID string) (*client.HostNode, *utils.APIHandleError) {
node := n.nodecluster.GetNode(nodeID) node := n.nodecluster.GetNode(nodeID)
if node == nil { if node == nil {
@ -233,7 +224,7 @@ func (n *NodeService) GetNode(nodeID string) (*client.HostNode, *utils.APIHandle
return node, nil return node, nil
} }
//GetAllNode get all node // GetAllNode get all node
func (n *NodeService) GetAllNode() ([]*client.HostNode, *utils.APIHandleError) { func (n *NodeService) GetAllNode() ([]*client.HostNode, *utils.APIHandleError) {
if n.nodecluster == nil { if n.nodecluster == nil {
return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) 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 return nodes, nil
} }
//GetServicesHealthy get service health // GetServicesHealthy get service health
func (n *NodeService) GetServicesHealthy() (map[string][]map[string]string, *utils.APIHandleError) { func (n *NodeService) GetServicesHealthy() (map[string][]map[string]string, *utils.APIHandleError) {
if n.nodecluster == nil { if n.nodecluster == nil {
return nil, utils.CreateAPIHandleError(400, fmt.Errorf("this node can not support this api")) 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 return StatusMap, nil
} }
//CordonNode set node is unscheduler // CordonNode set node is unscheduler
func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHandleError { func (n *NodeService) CordonNode(nodeID string, unschedulable bool) *utils.APIHandleError {
hostNode, apierr := n.GetNode(nodeID) hostNode, apierr := n.GetNode(nodeID)
if apierr != nil { if apierr != nil {
@ -302,7 +293,7 @@ func (n *NodeService) GetNodeLabels(nodeID string) (*model.LabelsResp, *utils.AP
return labels, nil 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) { func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID) hostNode, apierr := n.GetNode(nodeID)
if apierr != nil { if apierr != nil {
@ -327,7 +318,7 @@ func (n *NodeService) PutNodeLabel(nodeID string, labels map[string]string) (map
return hostNode.CustomLabels, nil 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) { func (n *NodeService) DeleteNodeLabel(nodeID string, labels map[string]string) (map[string]string, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID) hostNode, apierr := n.GetNode(nodeID)
if apierr != nil { if apierr != nil {
@ -353,7 +344,7 @@ func (n *NodeService) DeleteNodeLabel(nodeID string, labels map[string]string) (
return hostNode.CustomLabels, nil return hostNode.CustomLabels, nil
} }
//DownNode down node // DownNode down node
func (n *NodeService) DownNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { func (n *NodeService) DownNode(nodeID string) (*client.HostNode, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID) hostNode, apierr := n.GetNode(nodeID)
if apierr != nil { if apierr != nil {
@ -373,7 +364,7 @@ func (n *NodeService) DownNode(nodeID string) (*client.HostNode, *utils.APIHandl
return hostNode, nil return hostNode, nil
} }
//UpNode up node // UpNode up node
func (n *NodeService) UpNode(nodeID string) (*client.HostNode, *utils.APIHandleError) { func (n *NodeService) UpNode(nodeID string) (*client.HostNode, *utils.APIHandleError) {
hostNode, apierr := n.GetNode(nodeID) hostNode, apierr := n.GetNode(nodeID)
if apierr != nil { if apierr != nil {
@ -396,7 +387,7 @@ func (n *NodeService) UpNode(nodeID string) (*client.HostNode, *utils.APIHandleE
return hostNode, nil return hostNode, nil
} }
//GetNodeResource get node resource // GetNodeResource get node resource
func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *utils.APIHandleError) { func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *utils.APIHandleError) {
node, err := n.GetNode(nodeUID) node, err := n.GetNode(nodeUID)
if err != nil { if err != nil {
@ -448,13 +439,13 @@ func (n *NodeService) GetNodeResource(nodeUID string) (*model.NodePodResource, *
return &res, nil return &res, nil
} }
//CheckNode check node install status // CheckNode check node install status
func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.APIHandleError) { func (n *NodeService) CheckNode(nodeUID string) (*model.InstallStatus, *utils.APIHandleError) {
return nil, nil return nil, nil
} }
//DeleteNodeCondition delete node condition // DeleteNodeCondition delete node condition
func (n *NodeService) DeleteNodeCondition(nodeUID string, condition client.NodeConditionType) (*client.HostNode, *utils.APIHandleError) { func (n *NodeService) DeleteNodeCondition(nodeUID string, condition client.NodeConditionType) (*client.HostNode, *utils.APIHandleError) {
node, err := n.GetNode(nodeUID) node, err := n.GetNode(nodeUID)
if err != nil { if err != nil {

View File

@ -35,7 +35,6 @@ import (
) )
var ( var (
//DefalutClient etcd client
DefalutClient *Client DefalutClient *Client
) )
@ -66,8 +65,7 @@ func NewClient(ctx context.Context, cfg *conf.Conf, etcdClientArgs *etcdutil.Cli
// ErrKeyExists key exist error // ErrKeyExists key exist error
var ErrKeyExists = errors.New("key already exists") var ErrKeyExists = errors.New("key already exists")
// Post attempts to create the given key, only succeeding if the key did // Post attempts to create the given key, only succeeding if the key did not yet exist.
// not yet exist.
func (c *Client) Post(key, val string, opts ...client.OpOption) (*client.PutResponse, error) { func (c *Client) Post(key, val string, opts ...client.OpOption) (*client.PutResponse, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout) ctx, cancel := context.WithTimeout(context.Background(), c.reqTimeout)
defer cancel() defer cancel()

1
pkg/component/README.md Normal file
View File

@ -0,0 +1 @@
# 通用的封装的组件

130
pkg/component/core.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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
}
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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() {
}

View File

@ -1,5 +1,5 @@
// RAINBOND, Application Management Platform // 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 // 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 // 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 // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
package componentdefinition package grpc
import ( import (
"encoding/json" "context"
"testing" "github.com/goodrain/rainbond/config/configs"
"github.com/goodrain/rainbond/worker/client"
v1 "github.com/goodrain/rainbond/worker/appm/types/v1"
) )
func TestTemplateContext(t *testing.T) { var defaultGrpcComponent *Component
ctx := NewTemplateContext(&v1.AppService{AppServiceBase: v1.AppServiceBase{ServiceID: "1234567890", ServiceAlias: "niasdjaj", TenantID: "098765432345678"}}, cueTemplate, map[string]interface{}{
"kubernetes": map[string]interface{}{ // Component -
"name": "service-name", type Component struct {
"namespace": "t-namesapce", StatusClient *client.AppRuntimeSyncClient
}, }
"port": []map[string]interface{}{},
}) // Start -
manifests, err := ctx.GenerateComponentManifests() func (c *Component) Start(ctx context.Context, cfg *configs.Config) (err error) {
if err != nil { c.StatusClient, err = client.NewClient(ctx, cfg.APIConfig.RbdWorker)
t.Fatal(err) return err
} }
show, _ := json.Marshal(manifests)
t.Log(string(show)) // CloseHandle -
func (c *Component) CloseHandle() {
}
// Grpc -
func Grpc() *Component {
defaultGrpcComponent = &Component{}
return defaultGrpcComponent
}
// Default -
func Default() *Component {
return defaultGrpcComponent
} }

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -1,5 +1,5 @@
// RAINBOND, Application Management Platform // 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 // 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 // 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 // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
package build package mq
import ( import (
"testing" "context"
"github.com/goodrain/rainbond/config/configs"
"github.com/goodrain/rainbond/mq/client"
) )
func TestGetARGs(t *testing.T) { var defaultMqComponent *Component
buildEnvs := make(map[string]string)
buildEnvs["ARG_TEST"] = "abcdefg"
buildEnvs["PROC_ENV"] = "{\"procfile\": \"\", \"dependencies\": {}, \"language\": \"dockerfile\", \"runtimes\": \"\"}"
args := GetARGs(buildEnvs) // Component -
if v := buildEnvs["ARG_TEST"]; *args["TEST"] != v { type Component struct {
t.Errorf("Expected %s for arg[\"%s\"], but returned %s", buildEnvs["ARG_TEST"], "ARG_TEST", *args["TEST"]) MqClient client.MQClient
} }
if procEnv := args["PROC_ENV"]; procEnv != nil {
t.Errorf("Expected nil for args[\"PROC_ENV\"], but returned %v", procEnv) // 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
} }

View File

@ -1,5 +1,5 @@
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform // RAINBOND, Application Management Platform
// Copyright (C) 2021-2024 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify // 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 // 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 // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
package event package prom
import ( import (
"testing" "context"
"time" "github.com/goodrain/rainbond/api/client/prometheus"
"github.com/goodrain/rainbond/config/configs"
etcdutil "github.com/goodrain/rainbond/util/etcd"
) )
func TestLogger(t *testing.T) { var defaultPromComponent *Component
err := NewManager(EventConfig{
EventLogServers: []string{"192.168.195.1:6366"}, // Component -
DiscoverArgs: &etcdutil.ClientArgs{Endpoints: []string{"192.168.195.1:2379"}}, type Component struct {
}) PrometheusCli prometheus.Interface
if err != nil { }
t.Fatal(err)
} // Prometheus -
defer GetManager().Close() func Prometheus() *Component {
time.Sleep(time.Second * 3) defaultPromComponent = &Component{}
for i := 0; i < 500; i++ { return defaultPromComponent
GetManager().GetLogger("qwdawdasdasasfafa").Info("hello word", nil) }
GetManager().GetLogger("asdasdasdasdads").Debug("hello word", nil)
GetManager().GetLogger("1234124124124").Error("hello word", nil) // Start -
time.Sleep(time.Millisecond * 1) func (c *Component) Start(ctx context.Context, cfg *configs.Config) error {
} prometheusCli, err := prometheus.NewPrometheus(&prometheus.Options{
select {} Endpoint: cfg.APIConfig.PrometheusEndpoint,
})
c.PrometheusCli = prometheusCli
return err
}
// CloseHandle -
func (c *Component) CloseHandle() {
}
// Default -
func Default() *Component {
return defaultPromComponent
} }

View File

@ -1,5 +1,5 @@
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform // RAINBOND, Application Management Platform
// Copyright (C) 2021-2024 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify // 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 // 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 // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
package store package gogo
import "testing" import (
"context"
"sync"
)
func TestMerge(t *testing.T) { var wg sync.WaitGroup
s1 := MonitorMessageList{
MonitorMessage{ // Go 框架处理协程,用于优雅启停
Key: "/asdadasd", func Go(fun func(ctx context.Context) error, opts ...Option) error {
}, wg.Add(1)
options := &Options{}
for _, o := range opts {
o(options)
} }
s2 := MonitorMessageList{ if options.ctx == nil {
MonitorMessage{ options.ctx = context.Background()
Key: "/asdadasd",
},
MonitorMessage{
Key: "/asda12dasd",
},
} }
re := merge(s1, s2) go func() {
t.Log(re) defer wg.Done()
_ = fun(options.ctx)
}()
return nil
}
// Wait 等待所有协程结束
func Wait() {
wg.Wait()
} }

View File

@ -1,5 +1,5 @@
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform // RAINBOND, Application Management Platform
// Copyright (C) 2021-2024 Goodrain Co., Ltd.
// This program is free software: you can redistribute it and/or modify // 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 // 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 // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
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
}
}

94
pkg/interceptors/http.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

56
pkg/rainbond/component.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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() {
}

104
pkg/rainbond/registry.go Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
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
}

View File

@ -1 +0,0 @@
echo "{\"inner\":{\"HOME_PATH\":\"/home\"},\"type\":\"check\",\"exec_status\":\"Success\",\"status\":[{\"next_tasks\":[\"echo2\"]}]}" >&2

View File

@ -1 +0,0 @@
echo "{\"inner\":{\"HOME_PATH\":\"/home\"},\"type\":\"install\",\"exec_status\":\"Success\",\"status\":[{\"condition_type\":\"INIT_ECHO\",\"condition_status\":\"True\"}]}" >&2

View File

@ -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) { func TestCreateVersionByTime(t *testing.T) {
if re := CreateVersionByTime(); re != "" { if re := CreateVersionByTime(); re != "" {
t.Log(re) t.Log(re)

View File

@ -25,71 +25,41 @@ import (
"github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util" "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" v1 "github.com/goodrain/rainbond/worker/appm/types/v1"
"github.com/goodrain/rainbond/worker/server/pb" "github.com/goodrain/rainbond/worker/server/pb"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
//AppRuntimeSyncClient grpc client // AppRuntimeSyncClient grpc client
type AppRuntimeSyncClient struct { type AppRuntimeSyncClient struct {
pb.AppRuntimeSyncClient pb.AppRuntimeSyncClient
AppRuntimeSyncClientConf
cc *grpc.ClientConn cc *grpc.ClientConn
ctx context.Context ctx context.Context
} }
//AppRuntimeSyncClientConf client conf // NewClient new client
type AppRuntimeSyncClientConf struct { // ctx must be cancel where client not used
NonBlock bool func NewClient(ctx context.Context, grpcServer string) (c *AppRuntimeSyncClient, err error) {
EtcdEndpoints []string c = new(AppRuntimeSyncClient)
EtcdCaFile string c.ctx = ctx
EtcdCertFile string logrus.Infof("discover app runtime sync server address %s", grpcServer)
EtcdKeyFile string c.cc, err = grpc.Dial(grpcServer, grpc.WithInsecure())
DefaultServerAddress []string
}
//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 { if err != nil {
return nil, err return nil, err
} }
r := &grpcutil.GRPCResolver{Client: c} c.AppRuntimeSyncClient = pb.NewAppRuntimeSyncClient(c.cc)
b := grpc.RoundRobin(r)
dialOpts := []grpc.DialOption{ return c, nil
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
} }
//when watch occurred error,will exec this method // when watch occurred error,will exec this method
func (a *AppRuntimeSyncClient) Error(err error) { func (a *AppRuntimeSyncClient) Error(err error) {
logrus.Errorf("discover app runtime sync server address occurred err:%s", 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 { func (a *AppRuntimeSyncClient) GetStatus(serviceID string) string {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
@ -102,7 +72,7 @@ func (a *AppRuntimeSyncClient) GetStatus(serviceID string) string {
return status.Status[serviceID] return status.Status[serviceID]
} }
//GetOperatorWatchData get operator watch data // GetOperatorWatchData get operator watch data
func (a *AppRuntimeSyncClient) GetOperatorWatchData(appID string) (*pb.OperatorManaged, error) { func (a *AppRuntimeSyncClient) GetOperatorWatchData(appID string) (*pb.OperatorManaged, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
@ -115,7 +85,7 @@ func (a *AppRuntimeSyncClient) GetOperatorWatchData(appID string) (*pb.OperatorM
return status, nil return status, nil
} }
//GetStatuss get multiple app status // GetStatuss get multiple app status
func (a *AppRuntimeSyncClient) GetStatuss(serviceIDs string) map[string]string { func (a *AppRuntimeSyncClient) GetStatuss(serviceIDs string) map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel() defer cancel()
@ -133,7 +103,7 @@ func (a *AppRuntimeSyncClient) GetStatuss(serviceIDs string) map[string]string {
return status.Status return status.Status
} }
//GetAllStatus get all status // GetAllStatus get all status
func (a *AppRuntimeSyncClient) GetAllStatus() map[string]string { func (a *AppRuntimeSyncClient) GetAllStatus() map[string]string {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel() defer cancel()
@ -146,7 +116,7 @@ func (a *AppRuntimeSyncClient) GetAllStatus() map[string]string {
return status.Status return status.Status
} }
//GetNeedBillingStatus get need billing status // GetNeedBillingStatus get need billing status
func (a *AppRuntimeSyncClient) GetNeedBillingStatus() (map[string]string, error) { func (a *AppRuntimeSyncClient) GetNeedBillingStatus() (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) ctx, cancel := context.WithTimeout(context.Background(), time.Second*15)
defer cancel() defer cancel()
@ -163,7 +133,7 @@ func (a *AppRuntimeSyncClient) GetNeedBillingStatus() (map[string]string, error)
return res, nil return res, nil
} }
//GetServiceDeployInfo get service deploy info // GetServiceDeployInfo get service deploy info
func (a *AppRuntimeSyncClient) GetServiceDeployInfo(serviceID string) (*pb.DeployInfo, error) { func (a *AppRuntimeSyncClient) GetServiceDeployInfo(serviceID string) (*pb.DeployInfo, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel() defer cancel()
@ -176,12 +146,12 @@ func (a *AppRuntimeSyncClient) GetServiceDeployInfo(serviceID string) (*pb.Deplo
return re, nil return re, nil
} }
//IsClosedStatus check status // IsClosedStatus check status
func (a *AppRuntimeSyncClient) IsClosedStatus(curStatus string) bool { func (a *AppRuntimeSyncClient) IsClosedStatus(curStatus string) bool {
return curStatus == "" || curStatus == v1.BUILDEFAILURE || curStatus == v1.CLOSED || curStatus == v1.UNDEPLOY || curStatus == v1.BUILDING || curStatus == v1.UNKNOW 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) { func (a *AppRuntimeSyncClient) GetTenantResource(tenantID string) (*pb.TenantResource, error) {
if logrus.IsLevelEnabled(logrus.DebugLevel) { if logrus.IsLevelEnabled(logrus.DebugLevel) {
defer util.Elapsed("[AppRuntimeSyncClient] get tenant resource")() 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}) 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) { func (a *AppRuntimeSyncClient) GetAllTenantResource() (*pb.TenantResourceList, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel() defer cancel()

View File

@ -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 <http://www.gnu.org/licenses/>.
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)
}

View File

@ -30,7 +30,6 @@ import (
"github.com/goodrain/rainbond/cmd/worker/option" "github.com/goodrain/rainbond/cmd/worker/option"
"github.com/goodrain/rainbond/mq/api/grpc/pb" "github.com/goodrain/rainbond/mq/api/grpc/pb"
"github.com/goodrain/rainbond/mq/client" "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/controller"
"github.com/goodrain/rainbond/worker/appm/store" "github.com/goodrain/rainbond/worker/appm/store"
"github.com/goodrain/rainbond/worker/discover/model" "github.com/goodrain/rainbond/worker/discover/model"
@ -42,13 +41,13 @@ import (
var healthStatus = make(map[string]string, 1) var healthStatus = make(map[string]string, 1)
//TaskNum exec task number // TaskNum exec task number
var TaskNum float64 var TaskNum float64
//TaskError exec error task number // TaskError exec error task number
var TaskError float64 var TaskError float64
//TaskManager task // TaskManager task
type TaskManager struct { type TaskManager struct {
ctx context.Context ctx context.Context
cancel context.CancelFunc cancel context.CancelFunc
@ -60,7 +59,7 @@ type TaskManager struct {
clientset *kubernetes.Clientset clientset *kubernetes.Clientset
} }
//NewTaskManager return *TaskManager // NewTaskManager return *TaskManager
func NewTaskManager(cfg option.Config, func NewTaskManager(cfg option.Config,
store store.Storer, store store.Storer,
controllermanager *controller.Manager, controllermanager *controller.Manager,
@ -84,15 +83,9 @@ func NewTaskManager(cfg option.Config,
} }
} }
//Start 启动 // Start 启动
func (t *TaskManager) Start() error { func (t *TaskManager) Start() error {
etcdClientArgs := &etcdutil.ClientArgs{ client, err := client.NewMqClient(t.config.MQAPI)
Endpoints: t.config.EtcdEndPoints,
CaFile: t.config.EtcdCaFile,
CertFile: t.config.EtcdCertFile,
KeyFile: t.config.EtcdKeyFile,
}
client, err := client.NewMqClient(etcdClientArgs, t.config.MQAPI)
if err != nil { if err != nil {
logrus.Errorf("new Mq client error, %v", err) logrus.Errorf("new Mq client error, %v", err)
healthStatus["status"] = "unusual" healthStatus["status"] = "unusual"
@ -105,7 +98,7 @@ func (t *TaskManager) Start() error {
return nil return nil
} }
//Do do // Do do
func (t *TaskManager) Do() { func (t *TaskManager) Do() {
logrus.Info("start receive task from mq") logrus.Info("start receive task from mq")
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
@ -164,7 +157,7 @@ func (t *TaskManager) Do() {
} }
} }
//Stop 停止 // Stop 停止
func (t *TaskManager) Stop() error { func (t *TaskManager) Stop() error {
logrus.Info("discover manager is stoping.") logrus.Info("discover manager is stoping.")
t.cancel() t.cancel()
@ -174,7 +167,7 @@ func (t *TaskManager) Stop() error {
return nil return nil
} }
//HealthCheck health check // HealthCheck health check
func HealthCheck() map[string]string { func HealthCheck() map[string]string {
return healthStatus return healthStatus
} }