From 73a00935f3d35e002ee33949ce1778bc529ad166 Mon Sep 17 00:00:00 2001 From: zzjin Date: Fri, 8 Nov 2024 10:37:54 +0800 Subject: [PATCH] Update controller, allow set different cpu/memory request rate. (#5195) Signed-off-by: zzjin --- controllers/devbox/cmd/main.go | 6 ++++++ .../internal/controller/devbox_controller.go | 4 +++- .../internal/controller/helper/devbox.go | 21 ++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/controllers/devbox/cmd/main.go b/controllers/devbox/cmd/main.go index fba7e1dd261..62416e9441d 100644 --- a/controllers/devbox/cmd/main.go +++ b/controllers/devbox/cmd/main.go @@ -69,6 +69,8 @@ func main() { var registryUser string var registryPassword string var authAddr string + var requestCPURate float64 + var requestMemoryRate float64 var requestEphemeralStorage string var limitEphemeralStorage string var debugMode bool @@ -87,6 +89,8 @@ func main() { flag.BoolVar(&enableHTTP2, "enable-http2", false, "If set, HTTP/2 will be enabled for the metrics and webhook servers") flag.BoolVar(&debugMode, "debug", false, "If set, debug mode will be enabled") + flag.Float64Var(&requestCPURate, "request-cpu-rate", 10, "The request rate of cpu limit in devbox.") + flag.Float64Var(&requestMemoryRate, "request-memory-rate", 10, "The request rate of memory limit in devbox.") flag.StringVar(&requestEphemeralStorage, "request-ephemeral-storage", "500Mi", "The request value of ephemeral storage in devbox.") flag.StringVar(&limitEphemeralStorage, "limit-ephemeral-storage", "10Gi", "The limit value of ephemeral storage in devbox.") opts := zap.Options{ @@ -183,6 +187,8 @@ func main() { Scheme: mgr.GetScheme(), CommitImageRegistry: registryAddr, Recorder: mgr.GetEventRecorderFor("devbox-controller"), + RequestCPURate: requestCPURate, + RequestMemoryRate: requestMemoryRate, RequestEphemeralStorage: requestEphemeralStorage, LimitEphemeralStorage: limitEphemeralStorage, DebugMode: debugMode, diff --git a/controllers/devbox/internal/controller/devbox_controller.go b/controllers/devbox/internal/controller/devbox_controller.go index 0de5776e765..b8d76504e6c 100644 --- a/controllers/devbox/internal/controller/devbox_controller.go +++ b/controllers/devbox/internal/controller/devbox_controller.go @@ -45,6 +45,8 @@ import ( // DevboxReconciler reconciles a Devbox object type DevboxReconciler struct { CommitImageRegistry string + RequestCPURate float64 + RequestMemoryRate float64 RequestEphemeralStorage string LimitEphemeralStorage string @@ -547,7 +549,7 @@ func (r *DevboxReconciler) generateDevboxPod(devbox *devboxv1alpha1.Devbox, runt WorkingDir: helper.GenerateWorkingDir(devbox, runtime), Command: helper.GenerateCommand(devbox, runtime), Args: helper.GenerateDevboxArgs(devbox, runtime), - Resources: helper.GenerateResourceRequirements(devbox, r.RequestEphemeralStorage, r.LimitEphemeralStorage), + Resources: helper.GenerateResourceRequirements(devbox, r.RequestCPURate, r.RequestMemoryRate, r.RequestEphemeralStorage, r.LimitEphemeralStorage), }, } diff --git a/controllers/devbox/internal/controller/helper/devbox.go b/controllers/devbox/internal/controller/helper/devbox.go index eb969bc56f0..64e8fbd98b4 100644 --- a/controllers/devbox/internal/controller/helper/devbox.go +++ b/controllers/devbox/internal/controller/helper/devbox.go @@ -34,7 +34,6 @@ import ( ) const ( - rate = 10 DevBoxPartOf = "devbox" ) @@ -210,10 +209,18 @@ func PodMatchExpectations(expectPod *corev1.Pod, pod *corev1.Pod) bool { expectContainer := expectPod.Spec.Containers[0] // Check CPU and memory limits + if container.Resources.Requests.Cpu().Cmp(*expectContainer.Resources.Requests.Cpu()) != 0 { + slog.Info("CPU requests are not equal") + return false + } if container.Resources.Limits.Cpu().Cmp(*expectContainer.Resources.Limits.Cpu()) != 0 { slog.Info("CPU limits are not equal") return false } + if container.Resources.Requests.Memory().Cmp(*expectContainer.Resources.Requests.Memory()) != 0 { + slog.Info("Memory requests are not equal") + return false + } if container.Resources.Limits.Memory().Cmp(*expectContainer.Resources.Limits.Memory()) != 0 { slog.Info("Memory limits are not equal") return false @@ -381,7 +388,10 @@ func GenerateSSHVolume(devbox *devboxv1alpha1.Devbox) corev1.Volume { } } -func GenerateResourceRequirements(devbox *devboxv1alpha1.Devbox, requestEphemeralStorage, limitEphemeralStorage string) corev1.ResourceRequirements { +func GenerateResourceRequirements(devbox *devboxv1alpha1.Devbox, + requestCPURate, requestMemoryRate float64, + requestEphemeralStorage, limitEphemeralStorage string, +) corev1.ResourceRequirements { return corev1.ResourceRequirements{ Requests: calculateResourceRequest( corev1.ResourceList{ @@ -389,6 +399,7 @@ func GenerateResourceRequirements(devbox *devboxv1alpha1.Devbox, requestEphemera corev1.ResourceMemory: devbox.Spec.Resource["memory"], corev1.ResourceEphemeralStorage: resource.MustParse(requestEphemeralStorage), }, + requestCPURate, requestMemoryRate, ), Limits: corev1.ResourceList{ corev1.ResourceCPU: devbox.Spec.Resource["cpu"], @@ -402,7 +413,7 @@ func IsExceededQuotaError(err error) bool { return strings.Contains(err.Error(), "exceeded quota") } -func calculateResourceRequest(limit corev1.ResourceList) corev1.ResourceList { +func calculateResourceRequest(limit corev1.ResourceList, requestCPURate, requestMemoryRate float64) corev1.ResourceList { if limit == nil { return nil } @@ -410,13 +421,13 @@ func calculateResourceRequest(limit corev1.ResourceList) corev1.ResourceList { // Calculate CPU request if cpu, ok := limit[corev1.ResourceCPU]; ok { cpuValue := cpu.AsApproximateFloat64() - cpuRequest := cpuValue / rate + cpuRequest := cpuValue / requestCPURate request[corev1.ResourceCPU] = *resource.NewMilliQuantity(int64(cpuRequest*1000), resource.DecimalSI) } // Calculate memory request if memory, ok := limit[corev1.ResourceMemory]; ok { memoryValue := memory.AsApproximateFloat64() - memoryRequest := memoryValue / rate + memoryRequest := memoryValue / requestMemoryRate request[corev1.ResourceMemory] = *resource.NewQuantity(int64(memoryRequest), resource.BinarySI) }