From 8535c51385d7d6d6bd3bd6fcb1b78ec32ba0b268 Mon Sep 17 00:00:00 2001 From: ewezy Date: Fri, 24 May 2024 11:00:23 +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 +- 3 files changed, 9 insertions(+), 8 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