Skip to content

Commit

Permalink
Update controller, allow set different cpu/memory request rate. (#5195)
Browse files Browse the repository at this point in the history
Signed-off-by: zzjin <[email protected]>
  • Loading branch information
zzjin authored Nov 8, 2024
1 parent e0bba03 commit 73a0093
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions controllers/devbox/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion controllers/devbox/internal/controller/devbox_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import (
// DevboxReconciler reconciles a Devbox object
type DevboxReconciler struct {
CommitImageRegistry string
RequestCPURate float64
RequestMemoryRate float64
RequestEphemeralStorage string
LimitEphemeralStorage string

Expand Down Expand Up @@ -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),
},
}

Expand Down
21 changes: 16 additions & 5 deletions controllers/devbox/internal/controller/helper/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
)

const (
rate = 10
DevBoxPartOf = "devbox"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -381,14 +388,18 @@ 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{
corev1.ResourceCPU: devbox.Spec.Resource["cpu"],
corev1.ResourceMemory: devbox.Spec.Resource["memory"],
corev1.ResourceEphemeralStorage: resource.MustParse(requestEphemeralStorage),
},
requestCPURate, requestMemoryRate,
),
Limits: corev1.ResourceList{
corev1.ResourceCPU: devbox.Spec.Resource["cpu"],
Expand All @@ -402,21 +413,21 @@ 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
}
request := make(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)
}

Expand Down

0 comments on commit 73a0093

Please sign in to comment.