From 5ef9fac1a51c807b8eddf12f975720437146c9b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E7=BE=8A=E7=BE=8A?= Date: Fri, 30 Aug 2019 17:23:28 +0800 Subject: [PATCH] remove resource check in wrapEL --- api/middleware/middleware.go | 61 ------------------------------------ 1 file changed, 61 deletions(-) diff --git a/api/middleware/middleware.go b/api/middleware/middleware.go index e108a5b0f..ab438e80d 100644 --- a/api/middleware/middleware.go +++ b/api/middleware/middleware.go @@ -22,10 +22,8 @@ import ( "bytes" "context" "encoding/json" - "fmt" "io/ioutil" "net/http" - "os" "strings" "github.com/goodrain/rainbond/api/handler" @@ -34,7 +32,6 @@ import ( "github.com/jinzhu/gorm" "github.com/goodrain/rainbond/db" - "github.com/goodrain/rainbond/db/model" dbmodel "github.com/goodrain/rainbond/db/model" "github.com/goodrain/rainbond/event" @@ -272,11 +269,6 @@ func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.Handle // tenantID can not null tenantID := r.Context().Value(ContextKey("tenant_id")).(string) var ctx context.Context - // check resource is enough or not - if err := checkResource(optType, reqData, r); err != nil { - httputil.ReturnError(r, w, 400, err.Error()) - return - } event, err := util.CreateEvent(target, optType, targetID, tenantID, string(body), operator, synType) if err != nil { @@ -294,56 +286,3 @@ func WrapEL(f http.HandlerFunc, target, optType string, synType int) http.Handle } } } - -func checkResource(optType string, reqData map[string]interface{}, r *http.Request) error { - if optType == "start-service" || optType == "restart-service" || optType == "deploy-service" || optType == "horizontal-service" || optType == "vertical-service" || optType == "upgrade-service" { - if publicCloud := os.Getenv("PUBLIC_CLOUD"); publicCloud != "true" { - tenant := r.Context().Value(ContextKey("tenant")).(*model.Tenants) - if service, ok := r.Context().Value(ContextKey("service")).(*model.TenantServices); ok { - var containerMemory, replicas = service.ContainerMemory, service.Replicas - logrus.Debugf("service memory: %d, replicas: %d", containerMemory, replicas) - if optType == "horizontal-service" { - if replicasI, ok := reqData["node_num"]; ok { - if replicasF, ok := replicasI.(float64); ok { - replicas = int(replicasF) - } - } - } else if optType == "vertical-service" { - if ContainerMemoryI, ok := reqData["container_memory"]; ok { - if containerMemoryF, ok := ContainerMemoryI.(float64); ok { - containerMemory = int(containerMemoryF) - } - } - } - logrus.Debugf("service per memory: %d, replicas: %d, all memory: %d", containerMemory, replicas, containerMemory*replicas) - return priChargeSverify(tenant, containerMemory*replicas) - } - } - } - return nil -} - -func priChargeSverify(t *model.Tenants, quantity int) error { - if t.LimitMemory == 0 { - clusterStats, err := handler.GetTenantManager().GetAllocatableResources() - if err != nil { - return fmt.Errorf("error getting allocatable resources: %v", err) - } - availMem := clusterStats.AllMemory - clusterStats.RequestMemory - logrus.Debugf("cluster allMemory: %d, requestMemory: %d, availMemory: %d", clusterStats.AllMemory, clusterStats.RequestMemory, clusterStats.AllMemory-clusterStats.RequestMemory) - if availMem >= int64(quantity) { - return nil - } - return fmt.Errorf("cluster_lack_of_memory") - } - tenantStas, err := handler.GetTenantManager().GetTenantResource(t.UUID) - if err != nil { - return fmt.Errorf("error getting tenant resource: %v", err) - } - // TODO: it should be limit, not request - availMem := int64(t.LimitMemory) - (tenantStas.MemoryRequest + tenantStas.UnscdMemoryReq) - if availMem >= int64(quantity) { - return nil - } - return fmt.Errorf("lack_of_memory") -}