Unverified Commit 87c784bc authored by barnettZQG's avatar barnettZQG Committed by GitHub
Browse files

Merge pull request #200 from GLYASAI/V5.0

tenant resource
parents 4fd7345e 33f55ccd
Showing with 151 additions and 75 deletions
+151 -75
......@@ -23,7 +23,7 @@ import (
"os"
"strconv"
"github.com/goodrain/rainbond/api/handler/publiccloud"
"github.com/goodrain/rainbond/api/handler/cloud"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
......@@ -54,11 +54,6 @@ import (
// "$ref": "#/responses/commandResponse"
// description: 状态码非200,表示验证过程发生错误。状态码200,msg代表实际状态:success, illegal_quantity, missing_tenant, owned_fee, region_unauthorized, lack_of_memory
func ChargesVerifyController(w http.ResponseWriter, r *http.Request) {
if publicCloud := os.Getenv("PUBLIC_CLOUD"); publicCloud != "true" {
httputil.ReturnSuccess(r, w, nil)
return
}
tenant := r.Context().Value(middleware.ContextKey("tenant")).(*model.Tenants)
if tenant.EID == "" {
eid := r.FormValue("eid")
......@@ -79,10 +74,20 @@ func ChargesVerifyController(w http.ResponseWriter, r *http.Request) {
httputil.ReturnError(r, w, 400, "quantity type must be int")
return
}
reason := r.FormValue("reason")
if err := publiccloud.ChargeSverify(tenant, quantityInt, reason); err != nil {
err.Handle(r, w)
return
if publicCloud := os.Getenv("PUBLIC_CLOUD"); publicCloud != "true" {
err := cloud.PriChargeSverify(tenant, quantityInt)
if err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
} else {
reason := r.FormValue("reason")
if err := cloud.PubChargeSverify(tenant, quantityInt, reason); err != nil {
err.Handle(r, w)
return
}
httputil.ReturnSuccess(r, w, nil)
}
httputil.ReturnSuccess(r, w, nil)
}
......@@ -156,6 +156,9 @@ func (t *TenantStruct) TenantResources(w http.ResponseWriter, r *http.Request) {
if !ok {
return
}
reqb, _ := json.Marshal(tr)
logrus.Debugf("request uri: %s; request body: %s", r.RequestURI, string(reqb))
rep, err := handler.GetTenantManager().GetTenantsResources(&tr)
if err != nil {
httputil.ReturnError(r, w, 500, fmt.Sprintf("get resources error, %v", err))
......
......@@ -200,7 +200,7 @@ func (t *TenantStruct) StartService(w http.ResponseWriter, r *http.Request) {
// if os.Getenv("PUBLIC_CLOUD") == "true" {
// tenant := r.Context().Value(middleware.ContextKey("tenant")).(*dbmodel.Tenants)
// service := r.Context().Value(middleware.ContextKey("service")).(*dbmodel.TenantServices)
// if err := publiccloud.ChargeSverify(tenant, service.ContainerMemory*service.Replicas, "start"); err != nil {
// if err := cloud.ChargeSverify(tenant, service.ContainerMemory*service.Replicas, "start"); err != nil {
// err.Handle(r, w)
// return
// }
......
......@@ -35,6 +35,7 @@ type TenantHandler interface {
StatsMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)
TotalMemCPU(services []*dbmodel.TenantServices) (*api_model.StatsInfo, error)
GetTenantsResources(tr *api_model.TenantResources) (map[string]map[string]interface{}, error)
GetAllocatableResources() (int64, int64, error)
GetServicesResources(tr *api_model.ServicesResources) (map[string]map[string]interface{}, error)
TenantsSum() (int, error)
GetProtocols() ([]*dbmodel.RegionProcotols, *util.APIHandleError)
......
......@@ -16,10 +16,12 @@
// 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 publiccloud
package cloud
import (
"fmt"
"github.com/goodrain/rainbond/api/handler"
"github.com/goodrain/rainbond/db"
"io/ioutil"
"net/http"
"os"
......@@ -32,8 +34,8 @@ import (
"github.com/goodrain/rainbond/db/model"
)
//ChargeSverify service Charge Sverify
func ChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.APIHandleError {
//PubChargeSverify service Charge Sverify
func PubChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.APIHandleError {
cloudAPI := os.Getenv("CLOUD_API")
if cloudAPI == "" {
cloudAPI = "http://api.goodrain.com"
......@@ -67,3 +69,29 @@ func ChargeSverify(tenant *model.Tenants, quantity int, reason string) *util.API
}
return util.CreateAPIHandleError(res.StatusCode, fmt.Errorf("none"))
}
// PriChargeSverify verifies that the resources requested in the private cloud are legal
func PriChargeSverify(tenant *model.Tenants, quantity int) *util.APIHandleError {
tenants, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
return util.CreateAPIHandleError(500, fmt.Errorf("error getting all tenants"))
}
var lm int
for _, t := range tenants {
if t.UUID == tenant.UUID {
continue
}
lm = t.LimitMemory + lm
}
_, allMem, err := handler.GetTenantManager().GetAllocatableResources()
if err != nil {
return util.CreateAPIHandleError(500, fmt.Errorf("error getting allocatable resources"))
}
if int64(lm + quantity) < allMem {
return util.CreateAPIHandleError(200, fmt.Errorf("success"))
} else {
return util.CreateAPIHandleError(200, fmt.Errorf("lack_of_memory"))
}
}
......@@ -16,7 +16,7 @@
// 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 publiccloud
package cloud
import (
"os"
......@@ -29,7 +29,7 @@ func TestChargeSverify(t *testing.T) {
tenant := &model.Tenants{EID: "daa5ed8b1e9747518f1c531bf3c12aca", UUID: "ddddd_DDD"}
os.Setenv("REGION_NAME", "ali-hz")
os.Setenv("CLOUD_API", "http://apitest.goodrain.com")
err := ChargeSverify(tenant, 522, "sss")
err := PubChargeSverify(tenant, 522, "sss")
if err != nil {
t.Fatal(err.Code, err.String())
}
......
// 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 handler
import (
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/dao"
"github.com/goodrain/rainbond/db/model"
"github.com/rafrombrc/gomock/gomock"
"testing"
)
func TestGatewayAction_TCPAvailable(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
dbmanager := db.NewMockManager(ctrl)
ipPortDao := dao.NewMockIPPortDao(ctrl)
ipport := &model.IPPort{
IP: "172.16.0.106",
Port: 8888,
}
ipPortDao.EXPECT().GetIPPortByIPAndPort("172.16.0.106", 8888).Return(ipport, nil)
dbmanager.EXPECT().IPPortDao().Return(ipPortDao)
g := GatewayAction{
dbmanager: dbmanager,
}
if g.TCPAvailable("172.16.0.106", 8888) {
t.Errorf("expected false for tcp available, but returned true")
}
}
......@@ -57,7 +57,7 @@ func InitHandle(conf option.Config, statusCli *client.AppRuntimeSyncClient) erro
defaultServieHandler = CreateManager(conf, mqClient, etcdCli, statusCli)
defaultPluginHandler = CreatePluginManager(mqClient)
defaultAppHandler = CreateAppManager(mqClient)
defaultTenantHandler = CreateTenManager(mqClient, statusCli)
defaultTenantHandler = CreateTenManager(mqClient, statusCli, &conf)
defaultNetRulesHandler = CreateNetRulesManager(etcdCli)
defaultSourcesHandler = CreateSourcesManager(etcdCli)
defaultCloudHandler = CreateCloudManager(conf)
......
......@@ -21,6 +21,7 @@ package handler
import (
"encoding/json"
"fmt"
"github.com/goodrain/rainbond/cmd/api/option"
"net/http"
"strconv"
"strings"
......@@ -31,6 +32,7 @@ import (
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/mq/api/grpc/pb"
cli "github.com/goodrain/rainbond/node/nodem/client"
"github.com/goodrain/rainbond/worker/client"
)
......@@ -38,13 +40,16 @@ import (
type TenantAction struct {
MQClient pb.TaskQueueClient
statusCli *client.AppRuntimeSyncClient
OptCfg *option.Config
}
//CreateTenManager create Manger
func CreateTenManager(MQClient pb.TaskQueueClient, statusCli *client.AppRuntimeSyncClient) *TenantAction {
func CreateTenManager(MQClient pb.TaskQueueClient, statusCli *client.AppRuntimeSyncClient,
optCfg *option.Config) *TenantAction {
return &TenantAction{
MQClient: MQClient,
statusCli: statusCli,
OptCfg: optCfg,
}
}
......@@ -166,6 +171,12 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
if err != nil {
return nil, err
}
// get cluster resources
allCPU, allMem, err := t.GetAllocatableResources()
if err != nil {
return nil, fmt.Errorf("error getting allocatalbe cpu and memory: %v", err)
}
var serviceIDs []string
var serviceMap = make(map[string]dbmodel.TenantServices, len(services))
var serviceTenantRunning = make(map[string]int, len(ids))
......@@ -181,26 +192,53 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
}
var result = make(map[string]map[string]interface{}, len(ids))
for k, v := range limits {
result[k] = map[string]interface{}{"tenant_id": k, "limit_memory": v,
result[k] = map[string]interface{}{
"tenant_id": k,
"limit_memory": v,
"service_running_num": serviceTenantRunning[k],
"service_total_num": serviceTenantCount[k],
"limit_cpu": 0, "cpu": 0, "memory": 0, "disk": 0}
"limit_cpu": v / 4,
"cpu": 0,
"memory": 0,
"disk": 0,
}
if v == 0 {
result[k]["limit_memory"] = allMem
if allMem/4 > allCPU {
result[k]["limit_cpu"] = allCPU
} else {
result[k]["limit_cpu"] = allMem / 4
}
}
}
status := t.statusCli.GetStatuss(strings.Join(serviceIDs, ","))
for k, v := range status {
if _, ok := serviceMap[k]; !ok {
continue
}
if _, ok := result[serviceMap[k].TenantID]; !ok {
result[serviceMap[k].TenantID] = map[string]interface{}{"tenant_id": k, "limit_memory": 0, "limit_cpu": 0, "cpu": 0, "memory": 0, "disk": 0}
result[serviceMap[k].TenantID] = map[string]interface{}{
"tenant_id": k,
"limit_memory": allMem,
"limit_cpu": allMem / 4,
"cpu": 0,
"memory": 0,
"disk": 0,
}
if allMem/4 > allCPU {
result[serviceMap[k].TenantID]["limit_cpu"] = allCPU
}
}
if !t.statusCli.IsClosedStatus(v) {
result[serviceMap[k].TenantID]["cpu"] = result[serviceMap[k].TenantID]["cpu"].(int) + (serviceMap[k].ContainerCPU * serviceMap[k].Replicas)
result[serviceMap[k].TenantID]["memory"] = result[serviceMap[k].TenantID]["memory"].(int) + (serviceMap[k].ContainerMemory * serviceMap[k].Replicas)
result[serviceMap[k].TenantID]["cpu"] = result[serviceMap[k].TenantID]["cpu"].(int) +
(serviceMap[k].ContainerCPU * serviceMap[k].Replicas)
result[serviceMap[k].TenantID]["memory"] = result[serviceMap[k].TenantID]["memory"].(int) +
(serviceMap[k].ContainerMemory * serviceMap[k].Replicas)
}
}
//query disk used in prometheus
proxy := GetPrometheusProxy()
pproxy := 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)
......@@ -208,9 +246,9 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
logrus.Error("create request prometheus api error ", err.Error())
return result, nil
}
presult, err := proxy.Do(req)
presult, err := pproxy.Do(req)
if err != nil {
logrus.Error("do proxy request prometheus api error ", err.Error())
logrus.Error("do pproxy request prometheus api error ", err.Error())
return result, nil
}
if presult.Body != nil {
......@@ -239,6 +277,55 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
return result, nil
}
// GetAllocatableResources returns allocatable cpu and memory
func (t *TenantAction) GetAllocatableResources() (int64, int64, error) {
var allCPU int64 // allocatable CPU
var allMem int64 // allocatable memory
nproxy := GetNodeProxy()
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/v2/nodes/rule/compute",
t.OptCfg.NodeAPI), nil)
if err != nil {
return 0, 0, fmt.Errorf("error creating http request: %v", err)
}
resp, err := nproxy.Do(req)
if err != nil {
return 0, 0, fmt.Errorf("error getting cluster resources: %v", err)
}
if resp.Body != nil {
defer resp.Body.Close()
if resp.StatusCode != 200 {
return 0, 0, fmt.Errorf("error getting cluster resources: status code: %d; "+
"response: %v", resp.StatusCode, resp)
}
type foo struct {
List []*cli.HostNode `json:"list"`
}
var f foo
err = json.NewDecoder(resp.Body).Decode(&f)
if err != nil {
return 0, 0, fmt.Errorf("error decoding response body: %v", err)
}
for _, n := range f.List {
if n.Status != "running" {
logrus.Warningf("node %s isn't running, ignore it", n.ID)
continue
}
if k := n.NodeStatus.KubeNode; k != nil {
s := strings.Replace(k.Status.Allocatable.Cpu().String(), "m", "", -1)
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, 0, fmt.Errorf("error converting string to int64: %v", err)
}
allCPU = allCPU + i
allMem = allMem + k.Status.Allocatable.Memory().Value()/(1024*1024)
}
}
}
return allCPU, allMem, 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) {
status := t.statusCli.GetStatuss(strings.Join(tr.Body.ServiceIDs, ","))
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment