[REV] change resource api

This commit is contained in:
barnett 2018-11-13 13:41:20 +08:00
parent a484aa3f5f
commit 23be445e63
5 changed files with 33 additions and 42 deletions

View File

@ -41,7 +41,6 @@ type TenantInterface interface {
GetManyDeployVersion(w http.ResponseWriter, r *http.Request)
LimitTenantMemory(w http.ResponseWriter, r *http.Request)
TenantResourcesStatus(w http.ResponseWriter, r *http.Request)
TenantResourcesLimit(w http.ResponseWriter, r *http.Request)
TenantServicesStatus(w http.ResponseWriter, r *http.Request)
}

View File

@ -31,6 +31,8 @@ import (
"os"
"io/ioutil"
"github.com/Sirupsen/logrus"
"github.com/go-chi/chi"
"github.com/goodrain/rainbond/api/handler"
@ -39,9 +41,8 @@ import (
tutil "github.com/goodrain/rainbond/util"
httputil "github.com/goodrain/rainbond/util/http"
"github.com/jinzhu/gorm"
"github.com/thedevsaddam/govalidator"
"io/ioutil"
"github.com/pquerna/ffjson/ffjson"
"github.com/thedevsaddam/govalidator"
)
//TIMELAYOUT timelayout
@ -899,7 +900,7 @@ func (t *TenantStruct) LimitTenantMemory(w http.ResponseWriter, r *http.Request)
}
type SourcesInfo struct {
TenantId string `json:"tenant_id"`
TenantID string `json:"tenant_id"`
AvailableMemory int `json:"available_memory"`
Status bool `json:"status"`
MemTotal int `json:"mem_total"`
@ -930,7 +931,7 @@ func (t *TenantStruct) TenantResourcesStatus(w http.ResponseWriter, r *http.Requ
if tenant.LimitMemory == 0 {
sourcesInfo := SourcesInfo{
TenantId: tenantID,
TenantID: tenantID,
AvailableMemory: 0,
Status: true,
MemTotal: tenant.LimitMemory,
@ -943,7 +944,7 @@ func (t *TenantStruct) TenantResourcesStatus(w http.ResponseWriter, r *http.Requ
}
if statsInfo.MEM >= tenant.LimitMemory {
sourcesInfo := SourcesInfo{
TenantId: tenantID,
TenantID: tenantID,
AvailableMemory: tenant.LimitMemory - statsInfo.MEM,
Status: false,
MemTotal: tenant.LimitMemory,
@ -954,7 +955,7 @@ func (t *TenantStruct) TenantResourcesStatus(w http.ResponseWriter, r *http.Requ
httputil.ReturnSuccess(r, w, sourcesInfo)
} else {
sourcesInfo := SourcesInfo{
TenantId: tenantID,
TenantID: tenantID,
AvailableMemory: tenant.LimitMemory - statsInfo.MEM,
Status: true,
MemTotal: tenant.LimitMemory,
@ -966,40 +967,6 @@ func (t *TenantStruct) TenantResourcesStatus(w http.ResponseWriter, r *http.Requ
}
}
func (t *TenantStruct) TenantResourcesLimit(w http.ResponseWriter, r *http.Request) {
var cpuLimit int
tenantsResources := make(map[string]map[string]int, 0)
tenants, err := db.GetManager().TenantDao().GetALLTenants()
if err != nil {
logrus.Errorf("get all tenants errors:", err)
httputil.ReturnError(r, w, 500, err.Error())
return
}
for _, tenant := range tenants {
services, err := handler.GetServiceManager().GetService(tenant.UUID)
if err != nil {
httputil.ReturnError(r, w, 501, fmt.Sprintf("get service error, %v", err))
return
}
if tenant.LimitMemory == 0 {
cpuLimit = 0
} else {
cpuLimit = tenant.LimitMemory / 4
}
statsInfo, _ := handler.GetTenantManager().StatsMemCPU(services)
tenantsResources[tenant.UUID] = map[string]int{
"mem_limit": tenant.LimitMemory,
"mem_used": statsInfo.MEM,
"cpu_limit": cpuLimit,
"cpu_used": statsInfo.CPU,
}
}
httputil.ReturnSuccess(r, w, tenantsResources)
}
func (t *TenantStruct) TenantServicesStatus(w http.ResponseWriter, r *http.Request) {
info := make(map[string]map[string]int, 0)
tenants, err := db.GetManager().TenantDao().GetALLTenants()

View File

@ -164,6 +164,10 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
if err != nil {
return nil, err
}
limits, err := db.GetManager().TenantDao().GetTenantLimitsByNames(tr.Body.TenantNames)
if err != nil {
return nil, err
}
services, err := db.GetManager().TenantServiceDao().GetServicesByTenantIDs(ids)
if err != nil {
return nil, err
@ -175,13 +179,16 @@ func (t *TenantAction) GetTenantsResources(tr *api_model.TenantResources) (map[s
serviceMap[s.ServiceID] = *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, "limit_cpu": 0, "cpu": 0, "memory": 0, "disk": 0}
}
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, "cpu": 0, "memory": 0, "disk": 0}
result[serviceMap[k].TenantID] = map[string]interface{}{"tenant_id": k,"limit_memory": 0, "limit_cpu": 0 "cpu": 0, "memory": 0, "disk": 0}
}
if !t.statusCli.IsClosedStatus(v) {
result[serviceMap[k].TenantID]["cpu"] = result[serviceMap[k].TenantID]["cpu"].(int) + (serviceMap[k].ContainerCPU * serviceMap[k].Replicas)

View File

@ -44,6 +44,7 @@ type TenantDao interface {
GetTenantByEid(eid string) ([]*model.Tenants, error)
GetPagedTenants(offset, len int) ([]*model.Tenants, error)
GetTenantIDsByNames(names []string) ([]string, error)
GetTenantLimitsByNames(names []string) (map[string]int, error)
GetTenantByUUIDIsExist(uuid string) bool
}

View File

@ -118,6 +118,23 @@ func (t *TenantDaoImpl) GetTenantIDsByNames(names []string) (re []string, err er
return
}
//GetTenantLimitsByNames get tenants memory limit
func (t *TenantDaoImpl) GetTenantLimitsByNames(names []string) (limit map[string]int, err error) {
limit = make(map[string]int)
rows, err := t.DB.Raw("select uuid,limit_memory from tenants where name in (?)", names).Rows()
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var limitmemory int
var uuid string
rows.Scan(&uuid, &limitmemory)
limit[uuid] = limitmemory
}
return
}
//GetALLTenants GetALLTenants
func (t *TenantDaoImpl) GetPagedTenants(offset, len int) ([]*model.Tenants, error) {