From ac9cb631f379a02d45ae0f50e20efd1530beb5cd Mon Sep 17 00:00:00 2001 From: ewezy Date: Fri, 24 May 2024 11:07:00 +0800 Subject: [PATCH] Refactor cpu limit as nullable field --- api/cluster/resource/templater.go | 8 ++++---- api/cluster/resource/templater_test.go | 7 ++++--- api/models/resource_request.go | 2 +- api/queue/work/model_service_deployment_test.go | 3 --- api/service/environment_service_test.go | 14 -------------- 5 files changed, 9 insertions(+), 25 deletions(-) diff --git a/api/cluster/resource/templater.go b/api/cluster/resource/templater.go index ec9c03540..e730c5850 100644 --- a/api/cluster/resource/templater.go +++ b/api/cluster/resource/templater.go @@ -198,7 +198,7 @@ func (t *InferenceServiceTemplater) createPredictorSpec(modelService *models.Ser limits := map[corev1.ResourceName]resource.Quantity{} // Set cpu resource limits automatically if they have not been set - if modelService.ResourceRequest.CPULimit.IsZero() { + if modelService.ResourceRequest.CPULimit == nil || modelService.ResourceRequest.CPULimit.IsZero() { if t.deploymentConfig.UserContainerCPULimitRequestFactor != 0 { limits[corev1.ResourceCPU] = ScaleQuantity( modelService.ResourceRequest.CPURequest, t.deploymentConfig.UserContainerCPULimitRequestFactor, @@ -214,7 +214,7 @@ func (t *InferenceServiceTemplater) createPredictorSpec(modelService *models.Ser envVars = models.MergeEnvVars(ParseEnvVars(t.deploymentConfig.DefaultEnvVarsWithoutCPULimits), envVars) } } else { - limits[corev1.ResourceCPU] = modelService.ResourceRequest.CPULimit + limits[corev1.ResourceCPU] = *modelService.ResourceRequest.CPULimit } if t.deploymentConfig.UserContainerMemoryLimitRequestFactor != 0 { @@ -406,7 +406,7 @@ func (t *InferenceServiceTemplater) createTransformerSpec( // Set resource limits to request * userContainerCPULimitRequestFactor or UserContainerMemoryLimitRequestFactor limits := map[corev1.ResourceName]resource.Quantity{} // Set cpu resource limits automatically if they have not been set - if transformer.ResourceRequest.CPULimit.IsZero() { + if transformer.ResourceRequest.CPULimit == nil || transformer.ResourceRequest.CPULimit.IsZero() { if t.deploymentConfig.UserContainerCPULimitRequestFactor != 0 { limits[corev1.ResourceCPU] = ScaleQuantity( transformer.ResourceRequest.CPURequest, t.deploymentConfig.UserContainerCPULimitRequestFactor, @@ -422,7 +422,7 @@ func (t *InferenceServiceTemplater) createTransformerSpec( envVars = models.MergeEnvVars(ParseEnvVars(t.deploymentConfig.DefaultEnvVarsWithoutCPULimits), envVars) } } else { - limits[corev1.ResourceCPU] = transformer.ResourceRequest.CPULimit + limits[corev1.ResourceCPU] = *transformer.ResourceRequest.CPULimit } if t.deploymentConfig.UserContainerMemoryLimitRequestFactor != 0 { diff --git a/api/cluster/resource/templater_test.go b/api/cluster/resource/templater_test.go index aed0e2b65..3fa170c2e 100644 --- a/api/cluster/resource/templater_test.go +++ b/api/cluster/resource/templater_test.go @@ -93,12 +93,13 @@ var ( MemoryRequest: resource.MustParse("1Gi"), } + cpuLimit = resource.MustParse("8") userResourceRequestsWithCPULimits = &models.ResourceRequest{ MinReplica: 1, MaxReplica: 10, CPURequest: resource.MustParse("1"), MemoryRequest: resource.MustParse("1Gi"), - CPULimit: resource.MustParse("8"), + CPULimit: &cpuLimit, } expUserResourceRequests = corev1.ResourceRequirements{ @@ -118,7 +119,7 @@ var ( corev1.ResourceMemory: userResourceRequestsWithCPULimits.MemoryRequest, }, Limits: corev1.ResourceList{ - corev1.ResourceCPU: userResourceRequestsWithCPULimits.CPULimit, + corev1.ResourceCPU: *userResourceRequestsWithCPULimits.CPULimit, corev1.ResourceMemory: ScaleQuantity(userResourceRequestsWithCPULimits.MemoryRequest, 2), }, } @@ -4627,7 +4628,7 @@ func TestCreateTransformerSpec(t *testing.T) { MaxReplica: 1, CPURequest: cpuRequest, MemoryRequest: memoryRequest, - CPULimit: customCPULimit, + CPULimit: &customCPULimit, }, EnvVars: models.EnvVars{ {Name: transformerpkg.JaegerCollectorURL, Value: "NEW_HOST"}, // test user overwrite diff --git a/api/models/resource_request.go b/api/models/resource_request.go index 803301b2f..f92fae9f8 100644 --- a/api/models/resource_request.go +++ b/api/models/resource_request.go @@ -30,7 +30,7 @@ type ResourceRequest struct { // CPU request of inference service CPURequest resource.Quantity `json:"cpu_request"` // CPU limit of inference service - CPULimit resource.Quantity `json:"cpu_limit"` + CPULimit *resource.Quantity `json:"cpu_limit,omitempty"` // Memory request of inference service MemoryRequest resource.Quantity `json:"memory_request"` // GPU name diff --git a/api/queue/work/model_service_deployment_test.go b/api/queue/work/model_service_deployment_test.go index 1efd73980..403858322 100644 --- a/api/queue/work/model_service_deployment_test.go +++ b/api/queue/work/model_service_deployment_test.go @@ -34,7 +34,6 @@ func TestExecuteDeployment(t *testing.T) { MinReplica: 0, MaxReplica: 1, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -535,7 +534,6 @@ func TestExecuteDeployment(t *testing.T) { MinReplica: 0, MaxReplica: 1, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPUName: "NVIDIA P4", GPURequest: resource.MustParse("1"), @@ -743,7 +741,6 @@ func TestExecuteRedeployment(t *testing.T) { MinReplica: 0, MaxReplica: 1, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, diff --git a/api/service/environment_service_test.go b/api/service/environment_service_test.go index 77615a624..46db6da95 100644 --- a/api/service/environment_service_test.go +++ b/api/service/environment_service_test.go @@ -197,7 +197,6 @@ func TestGetEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -224,7 +223,6 @@ func TestGetEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -255,7 +253,6 @@ func TestGetEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), }, CreatedUpdated: models.CreatedUpdated{ @@ -323,7 +320,6 @@ func TestGetDefaultEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -350,7 +346,6 @@ func TestGetDefaultEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -381,7 +376,6 @@ func TestGetDefaultEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), }, CreatedUpdated: models.CreatedUpdated{ @@ -449,7 +443,6 @@ func TestGetDefaultPredictionJobEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -476,7 +469,6 @@ func TestGetDefaultPredictionJobEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -507,7 +499,6 @@ func TestGetDefaultPredictionJobEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), }, CreatedUpdated: models.CreatedUpdated{ @@ -576,7 +567,6 @@ func TestListEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -604,7 +594,6 @@ func TestListEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -638,7 +627,6 @@ func TestListEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -666,7 +654,6 @@ func TestListEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), GPURequest: resource.MustParse("0"), }, @@ -700,7 +687,6 @@ func TestListEnvironment(t *testing.T) { MinReplica: 1, MaxReplica: 4, CPURequest: resource.MustParse("1"), - CPULimit: resource.MustParse("0"), MemoryRequest: resource.MustParse("1Gi"), }, CreatedUpdated: models.CreatedUpdated{