From 8af5fe8bff4717e900002ec70865711856985069 Mon Sep 17 00:00:00 2001 From: yangk Date: Thu, 5 Aug 2021 17:34:34 +0800 Subject: [PATCH 1/4] set service cpu --- worker/appm/conversion/version.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/worker/appm/conversion/version.go b/worker/appm/conversion/version.go index 303b4f9f4..c425bf289 100644 --- a/worker/appm/conversion/version.go +++ b/worker/appm/conversion/version.go @@ -498,6 +498,10 @@ func createResources(as *v1.AppService) corev1.ResourceRequirements { cpuRequest = int64(requestint) } } + if as.ContainerCPU > 0 && cpuRequest == 0 && cpuLimit == 0{ + cpuLimit = int64(as.ContainerCPU) + cpuRequest = int64(as.ContainerCPU) + } rr := createResourcesByDefaultCPU(as.ContainerMemory, cpuRequest, cpuLimit) // support set gpu, support application of single GPU video memory. if as.ContainerGPU > 0 { From 07dc0719b4c0cda75f0e55a265c0d855a07a6b54 Mon Sep 17 00:00:00 2001 From: yangk Date: Fri, 6 Aug 2021 15:22:59 +0800 Subject: [PATCH 2/4] Fix the inconsistency between the console and cluster data after opening the plug-in for the component --- api/handler/service_plugin.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/api/handler/service_plugin.go b/api/handler/service_plugin.go index 940ed5f70..99981e84f 100644 --- a/api/handler/service_plugin.go +++ b/api/handler/service_plugin.go @@ -154,14 +154,22 @@ func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID strin tx.Rollback() return nil, util.CreateAPIHandleErrorFromDBError("set service plugin env error ", err) } + tsprCPU := pluginversion.ContainerCPU + tsprMemory := pluginversion.ContainerMemory + if pss.Body.PluginCPU != 0 { + tsprCPU = pss.Body.PluginCPU + } + if pss.Body.PluginMemory != 0 { + tsprMemory = pss.Body.PluginMemory + } relation := &dbmodel.TenantServicePluginRelation{ VersionID: pss.Body.VersionID, ServiceID: serviceID, PluginID: pss.Body.PluginID, Switch: pss.Body.Switch, PluginModel: plugin.PluginModel, - ContainerCPU: pluginversion.ContainerCPU, - ContainerMemory: pluginversion.ContainerMemory, + ContainerCPU: tsprCPU, + ContainerMemory: tsprMemory, } if err := db.GetManager().TenantServicePluginRelationDaoTransactions(tx).AddModel(relation); err != nil { tx.Rollback() From 78df4345b29032e8c46c89673902837a55f02f34 Mon Sep 17 00:00:00 2001 From: yangk Date: Thu, 12 Aug 2021 23:24:32 +0800 Subject: [PATCH 3/4] support set cpu value is zero --- api/handler/plugin.go | 4 ++-- api/handler/service_plugin.go | 8 ++++---- db/model/plugin.go | 8 ++++---- db/model/tenant.go | 6 ++++-- worker/appm/conversion/resource.go | 18 +++++++++++------- 5 files changed, 25 insertions(+), 19 deletions(-) diff --git a/api/handler/plugin.go b/api/handler/plugin.go index 071c57115..5d13da170 100644 --- a/api/handler/plugin.go +++ b/api/handler/plugin.go @@ -328,10 +328,10 @@ func (p *PluginAction) buildPlugin(b *api_model.BuildPluginStruct, plugin *dbmod Info: b.Body.Info, Status: "building", } - if b.Body.PluginCPU == 0 { + if b.Body.PluginCPU < 0 { pbv.ContainerCPU = 125 } - if b.Body.PluginMemory == 0 { + if b.Body.PluginMemory < 0 { pbv.ContainerMemory = 50 } if err := db.GetManager().TenantPluginBuildVersionDao().AddModel(pbv); err != nil { diff --git a/api/handler/service_plugin.go b/api/handler/service_plugin.go index 99981e84f..066d507e6 100644 --- a/api/handler/service_plugin.go +++ b/api/handler/service_plugin.go @@ -156,10 +156,10 @@ func (s *ServiceAction) SetTenantServicePluginRelation(tenantID, serviceID strin } tsprCPU := pluginversion.ContainerCPU tsprMemory := pluginversion.ContainerMemory - if pss.Body.PluginCPU != 0 { + if pss.Body.PluginCPU >= 0 { tsprCPU = pss.Body.PluginCPU } - if pss.Body.PluginMemory != 0 { + if pss.Body.PluginMemory >= 0 { tsprMemory = pss.Body.PluginMemory } relation := &dbmodel.TenantServicePluginRelation{ @@ -190,10 +190,10 @@ func (s *ServiceAction) UpdateTenantServicePluginRelation(serviceID string, pss } relation.VersionID = pss.Body.VersionID relation.Switch = pss.Body.Switch - if pss.Body.PluginCPU != 0 { + if pss.Body.PluginCPU >= 0 { relation.ContainerCPU = pss.Body.PluginCPU } - if pss.Body.PluginMemory != 0 { + if pss.Body.PluginMemory >= 0 { relation.ContainerMemory = pss.Body.PluginMemory } err = db.GetManager().TenantServicePluginRelationDao().UpdateModel(relation) diff --git a/db/model/plugin.go b/db/model/plugin.go index 80be0b4c5..1008c2d91 100644 --- a/db/model/plugin.go +++ b/db/model/plugin.go @@ -89,9 +89,9 @@ type TenantPluginBuildVersion struct { Info string `gorm:"column:info" json:"info"` Status string `gorm:"column:status;size:24" json:"status"` // container default cpu - ContainerCPU int `gorm:"column:container_cpu;default:125" json:"container_cpu"` + ContainerCPU int `gorm:"column:container_cpu;default:0" json:"container_cpu"` // container default memory - ContainerMemory int `gorm:"column:container_memory;default:64" json:"container_memory"` + ContainerMemory int `gorm:"column:container_memory;default:0" json:"container_memory"` // container args ContainerCMD string `gorm:"column:container_cmd;size:2048" json:"container_cmd"` } @@ -155,9 +155,9 @@ type TenantServicePluginRelation struct { ServiceID string `gorm:"column:service_id;size:32" json:"service_id"` PluginModel string `gorm:"column:plugin_model;size:24" json:"plugin_model"` // container default cpu v3.5.1 add - ContainerCPU int `gorm:"column:container_cpu;default:125" json:"container_cpu"` + ContainerCPU int `gorm:"column:container_cpu;default:0" json:"container_cpu"` // container default memory v3.5.1 add - ContainerMemory int `gorm:"column:container_memory;default:64" json:"container_memory"` + ContainerMemory int `gorm:"column:container_memory;default:0" json:"container_memory"` Switch bool `gorm:"column:switch;default:0" json:"switch"` } diff --git a/db/model/tenant.go b/db/model/tenant.go index 66f13facf..31d29aa55 100644 --- a/db/model/tenant.go +++ b/db/model/tenant.go @@ -165,9 +165,11 @@ type TenantServices struct { // 服务描述 Comment string `gorm:"column:comment" json:"comment"` // 容器CPU权重 - ContainerCPU int `gorm:"column:container_cpu;default:500" json:"container_cpu"` + // default is 0, This means that CPU resources are not limited + ContainerCPU int `gorm:"column:container_cpu;default:0" json:"container_cpu"` // 容器最大内存 - ContainerMemory int `gorm:"column:container_memory;default:128" json:"container_memory"` + // default is 0, This means that Memory resources are not limited + ContainerMemory int `gorm:"column:container_memory;default:0" json:"container_memory"` // container GPU, The amount of video memory applied for GPU. The unit is MiB // default is 0, That means no GPU is required ContainerGPU int `gorm:"column:container_gpu;default:0" json:"container_gpu"` diff --git a/worker/appm/conversion/resource.go b/worker/appm/conversion/resource.go index 4f9bc59ef..7b361ed33 100644 --- a/worker/appm/conversion/resource.go +++ b/worker/appm/conversion/resource.go @@ -30,17 +30,21 @@ func createResourcesByDefaultCPU(memory int, setCPURequest, setCPULimit int64) c if base <= 0 { base = 1 } - if memory < 512 { - cpuRequest, cpuLimit = base*30, base*80 - } else if memory <= 1024 { - cpuRequest, cpuLimit = base*30, base*160 + if memory > 0 { + if memory < 512 { + cpuRequest, cpuLimit = base*30, base*80 + } else if memory <= 1024 { + cpuRequest, cpuLimit = base*30, base*160 + } else { + cpuRequest, cpuLimit = base*30, (int64(memory)-1024)/1024*500+1280 + } } else { - cpuRequest, cpuLimit = base*30, ((int64(memory)-1024)/1024*500 + 1280) + memory = 0 } - if setCPULimit > 0 { + if setCPULimit >= 0 { cpuLimit = setCPULimit } - if setCPURequest > 0 { + if setCPURequest >= 0 { cpuRequest = setCPURequest } From 37e9a2957dcd305d0c99f0280e5d9d0874c2bbd5 Mon Sep 17 00:00:00 2001 From: yangk Date: Fri, 13 Aug 2021 11:38:10 +0800 Subject: [PATCH 4/4] Refactoring the method of setting resources --- worker/appm/conversion/plugin.go | 6 ++-- worker/appm/conversion/resource.go | 51 ++++++++++++------------------ worker/appm/conversion/version.go | 12 +------ 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/worker/appm/conversion/plugin.go b/worker/appm/conversion/plugin.go index d756e4c68..8b9f1d4e8 100644 --- a/worker/appm/conversion/plugin.go +++ b/worker/appm/conversion/plugin.go @@ -440,7 +440,7 @@ func createPluginEnvs(pluginID, tenantID, serviceAlias string, mainEnvs []v1.Env } func createPluginResources(memory int, cpu int) v1.ResourceRequirements { - return createResourcesByDefaultCPU(memory, int64(cpu), int64(cpu)) + return createResourcesBySetting(memory, int64(cpu), int64(cpu), 0) } func createTCPUDPMeshRecources(as *typesv1.AppService) v1.ResourceRequirements { @@ -458,12 +458,12 @@ func createTCPUDPMeshRecources(as *typesv1.AppService) v1.ResourceRequirements { memory = requestint } } - return createResourcesByDefaultCPU(memory, cpu, func() int64 { + return createResourcesBySetting(memory, cpu, func() int64 { if cpu < 120 { return 120 } return cpu - }()) + }(), 0) } func xdsHostIPEnv(xdsHost string) corev1.EnvVar { diff --git a/worker/appm/conversion/resource.go b/worker/appm/conversion/resource.go index 7b361ed33..aee80334b 100644 --- a/worker/appm/conversion/resource.go +++ b/worker/appm/conversion/resource.go @@ -19,43 +19,34 @@ package conversion import ( + "fmt" + "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" ) -//Allocate the CPU at the ratio of 4g memory to 1 core CPU -func createResourcesByDefaultCPU(memory int, setCPURequest, setCPULimit int64) corev1.ResourceRequirements { - var cpuRequest, cpuLimit int64 - base := int64(memory) / 128 - if base <= 0 { - base = 1 - } - if memory > 0 { - if memory < 512 { - cpuRequest, cpuLimit = base*30, base*80 - } else if memory <= 1024 { - cpuRequest, cpuLimit = base*30, base*160 - } else { - cpuRequest, cpuLimit = base*30, (int64(memory)-1024)/1024*500+1280 - } - } else { - memory = 0 - } - if setCPULimit >= 0 { - cpuLimit = setCPULimit - } - if setCPURequest >= 0 { - cpuRequest = setCPURequest - } - +func createResourcesBySetting(memory int, setCPURequest, setCPULimit, setGPULimit int64) corev1.ResourceRequirements { limits := corev1.ResourceList{} - limits[corev1.ResourceCPU] = *resource.NewMilliQuantity(cpuLimit, resource.DecimalSI) - limits[corev1.ResourceMemory] = *resource.NewQuantity(int64(memory*1024*1024), resource.BinarySI) - request := corev1.ResourceList{} - request[corev1.ResourceCPU] = *resource.NewMilliQuantity(cpuRequest, resource.DecimalSI) - request[corev1.ResourceMemory] = *resource.NewQuantity(int64(memory*1024*1024), resource.BinarySI) + if memory > 0 { + limits[corev1.ResourceMemory] = *resource.NewQuantity(int64(memory*1024*1024), resource.BinarySI) + } + if setCPULimit > 0 { + limits[corev1.ResourceCPU] = *resource.NewMilliQuantity(setCPULimit, resource.DecimalSI) + } + if setGPULimit > 0 { + gpuLimit, err := resource.ParseQuantity(fmt.Sprintf("%d", setGPULimit)) + if err != nil { + logrus.Errorf("gpu request is invalid") + } else { + limits[getGPULableKey()] = gpuLimit + } + } + + if setCPURequest > 0 { + request[corev1.ResourceCPU] = *resource.NewMilliQuantity(setCPURequest, resource.DecimalSI) + } return corev1.ResourceRequirements{ Limits: limits, Requests: request, diff --git a/worker/appm/conversion/version.go b/worker/appm/conversion/version.go index c425bf289..2db5af9bd 100644 --- a/worker/appm/conversion/version.go +++ b/worker/appm/conversion/version.go @@ -38,7 +38,6 @@ import ( "github.com/jinzhu/gorm" "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/intstr" ) @@ -502,16 +501,7 @@ func createResources(as *v1.AppService) corev1.ResourceRequirements { cpuLimit = int64(as.ContainerCPU) cpuRequest = int64(as.ContainerCPU) } - rr := createResourcesByDefaultCPU(as.ContainerMemory, cpuRequest, cpuLimit) - // support set gpu, support application of single GPU video memory. - if as.ContainerGPU > 0 { - gpuLimit, err := resource.ParseQuantity(fmt.Sprintf("%d", as.ContainerGPU)) - if err != nil { - logrus.Errorf("gpu request is invalid") - } else { - rr.Limits[getGPULableKey()] = gpuLimit - } - } + rr := createResourcesBySetting(as.ContainerMemory, cpuRequest, cpuLimit, int64(as.ContainerGPU)) return rr }