2017-11-07 11:40:44 +08:00
|
|
|
// RAINBOND, Application Management Platform
|
|
|
|
// Copyright (C) 2014-2017 Goodrain Co., Ltd.
|
2018-03-14 14:07:12 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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.
|
2018-03-14 14:07:12 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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.
|
2018-03-14 14:07:12 +08:00
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
// 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 (
|
2018-03-13 16:55:53 +08:00
|
|
|
"encoding/json"
|
2017-11-07 11:40:44 +08:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-03-13 16:55:53 +08:00
|
|
|
"strconv"
|
2017-11-07 11:40:44 +08:00
|
|
|
"time"
|
|
|
|
|
2018-01-02 18:02:08 +08:00
|
|
|
"github.com/goodrain/rainbond/pkg/api/util"
|
2017-11-07 11:40:44 +08:00
|
|
|
"github.com/goodrain/rainbond/pkg/mq/api/grpc/pb"
|
|
|
|
|
|
|
|
api_model "github.com/goodrain/rainbond/pkg/api/model"
|
|
|
|
"github.com/goodrain/rainbond/pkg/db"
|
|
|
|
dbmodel "github.com/goodrain/rainbond/pkg/db/model"
|
|
|
|
|
2018-01-02 18:02:08 +08:00
|
|
|
"strings"
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
)
|
|
|
|
|
|
|
|
//TenantAction tenant act
|
|
|
|
type TenantAction struct {
|
|
|
|
MQClient pb.TaskQueueClient
|
|
|
|
KubeClient *kubernetes.Clientset
|
|
|
|
//OpentsdbClient tsdbClient.Client
|
|
|
|
OpentsdbAPI string
|
|
|
|
}
|
|
|
|
|
|
|
|
//CreateTenManager create Manger
|
2018-03-04 22:48:50 +08:00
|
|
|
func CreateTenManager(MQClient pb.TaskQueueClient, KubeClient *kubernetes.Clientset, opentsdb string) *TenantAction {
|
|
|
|
opentsdbAPI := fmt.Sprintf("%s/api", opentsdb)
|
2017-11-07 11:40:44 +08:00
|
|
|
return &TenantAction{
|
2018-03-04 22:48:50 +08:00
|
|
|
MQClient: MQClient,
|
|
|
|
KubeClient: KubeClient,
|
2017-11-07 11:40:44 +08:00
|
|
|
//OpentsdbClient: opentsdb,
|
|
|
|
OpentsdbAPI: opentsdbAPI,
|
2018-03-04 22:48:50 +08:00
|
|
|
}
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//GetTenants get tenants
|
2017-12-07 12:08:25 +08:00
|
|
|
func (t *TenantAction) GetTenants() ([]*dbmodel.Tenants, error) {
|
|
|
|
tenants, err := db.GetManager().TenantDao().GetALLTenants()
|
2017-11-07 11:40:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-07 12:08:25 +08:00
|
|
|
return tenants, err
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
2018-01-04 10:58:20 +08:00
|
|
|
|
|
|
|
//GetTenantsPaged GetTenantsPaged
|
2018-01-02 18:02:08 +08:00
|
|
|
func (t *TenantAction) GetTenantsPaged(offset, len int) ([]*dbmodel.Tenants, error) {
|
2017-12-15 16:49:24 +08:00
|
|
|
tenants, err := db.GetManager().TenantDao().GetALLTenants()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tenants, err
|
|
|
|
}
|
|
|
|
|
2018-01-04 10:58:20 +08:00
|
|
|
//TotalMemCPU StatsMemCPU
|
2018-01-02 18:02:08 +08:00
|
|
|
func (t *TenantAction) TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
|
2017-12-06 15:26:21 +08:00
|
|
|
cpus := 0
|
|
|
|
mem := 0
|
|
|
|
for _, service := range services {
|
|
|
|
logrus.Debugf("service is %s, cpus is %v, mem is %v", service.ID, service.ContainerCPU, service.ContainerMemory)
|
|
|
|
cpus += service.ContainerCPU
|
|
|
|
mem += service.ContainerMemory
|
|
|
|
}
|
|
|
|
si := &api_model.StatsInfo{
|
|
|
|
CPU: cpus,
|
|
|
|
MEM: mem,
|
|
|
|
}
|
|
|
|
return si, nil
|
|
|
|
}
|
|
|
|
|
2017-12-07 12:08:25 +08:00
|
|
|
//GetTenantsName get tenants name
|
|
|
|
func (t *TenantAction) GetTenantsName() ([]string, error) {
|
|
|
|
tenants, err := db.GetManager().TenantDao().GetALLTenants()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result []string
|
2017-12-12 12:52:29 +08:00
|
|
|
for _, v := range tenants {
|
|
|
|
result = append(result, strings.ToLower(v.Name))
|
2017-12-07 12:08:25 +08:00
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
2017-12-12 12:52:29 +08:00
|
|
|
//GetTenantsByName get tenants
|
2017-12-07 12:08:25 +08:00
|
|
|
func (t *TenantAction) GetTenantsByName(name string) (*dbmodel.Tenants, error) {
|
|
|
|
tenant, err := db.GetManager().TenantDao().GetTenantIDByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-07 13:42:45 +08:00
|
|
|
|
2017-12-07 12:08:25 +08:00
|
|
|
return tenant, err
|
|
|
|
}
|
2018-01-02 18:02:08 +08:00
|
|
|
|
2018-01-04 10:58:20 +08:00
|
|
|
//GetTenantsByUUID get tenants
|
2017-12-15 16:49:24 +08:00
|
|
|
func (t *TenantAction) GetTenantsByUUID(uuid string) (*dbmodel.Tenants, error) {
|
|
|
|
tenant, err := db.GetManager().TenantDao().GetTenantByUUID(uuid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tenant, err
|
|
|
|
}
|
|
|
|
|
2017-11-07 11:40:44 +08:00
|
|
|
//StatsMemCPU StatsMemCPU
|
|
|
|
func (t *TenantAction) StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error) {
|
|
|
|
cpus := 0
|
|
|
|
mem := 0
|
|
|
|
for _, service := range services {
|
|
|
|
status := service.CurStatus
|
|
|
|
label := CheckLabel(service.ServiceID)
|
|
|
|
if label {
|
|
|
|
servicesStatus, err := db.GetManager().TenantServiceStatusDao().GetTenantServiceStatus(service.ServiceID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
status = servicesStatus.Status
|
|
|
|
}
|
|
|
|
if status == "undeploy" || status == "closed" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cpus += service.ContainerCPU
|
|
|
|
mem += service.ContainerMemory
|
|
|
|
}
|
|
|
|
si := &api_model.StatsInfo{
|
|
|
|
CPU: cpus,
|
|
|
|
MEM: mem,
|
|
|
|
}
|
|
|
|
return si, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//HTTPTsdb HTTPTsdb
|
|
|
|
func (t *TenantAction) HTTPTsdb(md *api_model.MontiorData) ([]byte, error) {
|
|
|
|
uri := fmt.Sprintf("/query?start=%s&m=%s", md.Body.Start, md.Body.Queries)
|
|
|
|
logrus.Debugf(fmt.Sprintf("uri is %v", uri))
|
|
|
|
url := fmt.Sprintf("http://%s%s", t.OpentsdbAPI, uri)
|
|
|
|
logrus.Debugf(fmt.Sprintf("url is %v", url))
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 3,
|
|
|
|
}
|
|
|
|
response, errR := client.Do(req)
|
|
|
|
if errR != nil {
|
|
|
|
return nil, errR
|
|
|
|
}
|
|
|
|
body, errB := ioutil.ReadAll(response.Body)
|
|
|
|
if errB != nil {
|
|
|
|
return nil, errB
|
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:55:53 +08:00
|
|
|
// QueryResult contains result data for a query.
|
|
|
|
type QueryResult struct {
|
|
|
|
Data struct {
|
|
|
|
Type string `json:"resultType"`
|
|
|
|
Result []map[string]interface{} `json:"result"`
|
|
|
|
} `json:"data"`
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetTenantsResources Gets the resource usage of the specified tenant.
|
|
|
|
func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (res []map[string]interface{}, err error) {
|
|
|
|
ids, err := db.GetManager().TenantDao().GetTenantIDsByNames(tr.Body.TenantNames)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resmp, err := db.GetManager().TenantServiceDao().GetServiceMemoryByTenantIDs(ids)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for k, v := range resmp {
|
|
|
|
v["tenant_id"] = k
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
//query disk used in prometheus
|
|
|
|
proxy := GetPrometheusProxy()
|
|
|
|
query := fmt.Sprintf(`sum(app_resource_appfs{tenant_id=~"%s"}) by(tenant_id)`, strings.Join(ids, "|"))
|
|
|
|
query = strings.Replace(query, " ", "%20", -1)
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:9999/api/v1/query?query=%s", query), nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("create request prometheus api error ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result, err := proxy.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("do proxy request prometheus api error ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if result.Body != nil {
|
|
|
|
defer result.Body.Close()
|
|
|
|
if result.StatusCode != 200 {
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
var qres QueryResult
|
|
|
|
err = json.NewDecoder(result.Body).Decode(&qres)
|
|
|
|
if err == nil {
|
|
|
|
for _, re := range qres.Data.Result {
|
|
|
|
var tenantID string
|
|
|
|
var disk int
|
|
|
|
if tid, ok := re["metric"].(map[string]interface{}); ok {
|
|
|
|
tenantID = tid["tenant_id"].(string)
|
|
|
|
}
|
|
|
|
if re, ok := (re["value"]).([]interface{}); ok && len(re) == 2 {
|
|
|
|
disk, _ = strconv.Atoi(re[1].(string))
|
|
|
|
}
|
|
|
|
if _, ok := resmp[tenantID]; ok {
|
2018-03-13 18:33:01 +08:00
|
|
|
resmp[tenantID]["disk"] = disk / 1024
|
2018-03-13 16:55:53 +08:00
|
|
|
} else {
|
|
|
|
resmp[tenantID] = make(map[string]interface{})
|
2018-03-13 18:33:01 +08:00
|
|
|
resmp[tenantID]["disk"] = disk / 1024
|
2018-03-13 16:55:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 17:55:22 +08:00
|
|
|
//set disk 0
|
|
|
|
for i, v := range res {
|
|
|
|
if _, ok := v["disk"]; !ok {
|
|
|
|
res[i]["disk"] = 0
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 16:55:53 +08:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//GetServicesResources Gets the resource usage of the specified service.
|
|
|
|
func (t *TenantAction) GetServicesResources(tr *api_model.ServicesResources) (re map[string]map[string]interface{}, err error) {
|
|
|
|
resmp, err := db.GetManager().TenantServiceDao().GetServiceMemoryByServiceIDs(tr.Body.ServiceIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
re = resmp
|
|
|
|
//query disk used in prometheus
|
|
|
|
proxy := GetPrometheusProxy()
|
|
|
|
query := fmt.Sprintf(`app_resource_appfs{service_id=~"%s"}`, strings.Join(tr.Body.ServiceIDs, "|"))
|
|
|
|
query = strings.Replace(query, " ", "%20", -1)
|
|
|
|
req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:9999/api/v1/query?query=%s", query), nil)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("create request prometheus api error ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
result, err := proxy.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error("do proxy request prometheus api error ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if result.Body != nil {
|
|
|
|
defer result.Body.Close()
|
|
|
|
if result.StatusCode != 200 {
|
|
|
|
return re, nil
|
|
|
|
}
|
|
|
|
var qres QueryResult
|
|
|
|
err = json.NewDecoder(result.Body).Decode(&qres)
|
|
|
|
if err == nil {
|
|
|
|
for _, re := range qres.Data.Result {
|
|
|
|
var serviceID string
|
|
|
|
var disk int
|
|
|
|
if tid, ok := re["metric"].(map[string]interface{}); ok {
|
|
|
|
serviceID = tid["service_id"].(string)
|
|
|
|
}
|
|
|
|
if re, ok := (re["value"]).([]interface{}); ok && len(re) == 2 {
|
|
|
|
disk, _ = strconv.Atoi(re[1].(string))
|
|
|
|
}
|
|
|
|
if _, ok := resmp[serviceID]; ok {
|
2018-03-13 18:33:01 +08:00
|
|
|
resmp[serviceID]["disk"] = disk / 1024
|
2018-03-13 16:55:53 +08:00
|
|
|
} else {
|
|
|
|
resmp[serviceID] = make(map[string]interface{})
|
2018-03-13 18:33:01 +08:00
|
|
|
resmp[serviceID]["disk"] = disk / 1024
|
2018-03-13 16:55:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resmp, nil
|
2017-11-07 11:40:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//TenantsSum TenantsSum
|
|
|
|
func (t *TenantAction) TenantsSum() (int, error) {
|
|
|
|
s, err := db.GetManager().TenantDao().GetALLTenants()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(s), nil
|
|
|
|
}
|
2018-01-02 18:02:08 +08:00
|
|
|
|
|
|
|
//GetProtocols GetProtocols
|
|
|
|
func (t *TenantAction) GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError) {
|
|
|
|
rps, err := db.GetManager().RegionProcotolsDao().GetAllSupportProtocol("v2")
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.CreateAPIHandleErrorFromDBError("get all support protocols", err)
|
|
|
|
}
|
|
|
|
return rps, nil
|
|
|
|
}
|
2018-01-18 13:45:24 +08:00
|
|
|
|
|
|
|
//TransPlugins TransPlugins
|
2018-03-04 22:48:50 +08:00
|
|
|
func (t *TenantAction) TransPlugins(tenantID, tenantName, fromTenant string, pluginList []string) *util.APIHandleError {
|
2018-01-18 13:45:24 +08:00
|
|
|
tenantInfo, err := db.GetManager().TenantDao().GetTenantIDByName(fromTenant)
|
|
|
|
if err != nil {
|
|
|
|
return util.CreateAPIHandleErrorFromDBError("get tenant infos", err)
|
|
|
|
}
|
|
|
|
goodrainID := tenantInfo.UUID
|
|
|
|
tx := db.GetManager().Begin()
|
|
|
|
for _, p := range pluginList {
|
|
|
|
pluginInfo, err := db.GetManager().TenantPluginDao().GetPluginByID(p, goodrainID)
|
|
|
|
if err != nil {
|
|
|
|
tx.Rollback()
|
|
|
|
return util.CreateAPIHandleErrorFromDBError("get plugin infos", err)
|
|
|
|
}
|
|
|
|
pluginInfo.TenantID = tenantID
|
|
|
|
pluginInfo.Domain = tenantName
|
|
|
|
pluginInfo.ID = 0
|
|
|
|
err = db.GetManager().TenantPluginDaoTransactions(tx).AddModel(pluginInfo)
|
|
|
|
if err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "is exist") {
|
|
|
|
tx.Rollback()
|
|
|
|
return util.CreateAPIHandleErrorFromDBError("add plugin Info", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tx.Commit().Error; err != nil {
|
|
|
|
return util.CreateAPIHandleErrorFromDBError("trans plugins infos", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|