From 516fd846dead1a30912e7109f218e353969f2dbc Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Wed, 29 Jan 2025 08:28:45 +0100 Subject: [PATCH] feat(ctrl): default builder Pod resources Closes #5811 --- pkg/controller/build/build_pod.go | 16 +++---- pkg/controller/build/build_pod_test.go | 59 ++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/pkg/controller/build/build_pod.go b/pkg/controller/build/build_pod.go index 2b2b877980..69414c0f6a 100644 --- a/pkg/controller/build/build_pod.go +++ b/pkg/controller/build/build_pod.go @@ -24,6 +24,7 @@ import ( corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime/pkg/client" @@ -108,17 +109,16 @@ func newBuildPod(ctx context.Context, client client.Client, build *v1.Build) *co } func configureResources(taskName string, build *v1.Build, container *corev1.Container) { - conf := build.TaskConfiguration(taskName) - requestsList := container.Resources.Requests - limitsList := container.Resources.Limits var err error - if requestsList == nil { - requestsList = make(corev1.ResourceList) - } - if limitsList == nil { - limitsList = make(corev1.ResourceList) + requestsList := corev1.ResourceList{} + // Default limit to prevent resource Node starvation + limitsList := corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("1Gi"), } + conf := build.TaskConfiguration(taskName) + requestsList, err = kubernetes.ConfigureResource(conf.RequestCPU, requestsList, corev1.ResourceCPU) if err != nil { Log.WithValues("request-namespace", build.Namespace, "request-name", build.Name). diff --git a/pkg/controller/build/build_pod_test.go b/pkg/controller/build/build_pod_test.go index 7a41d3923c..e7fec5e62f 100644 --- a/pkg/controller/build/build_pod_test.go +++ b/pkg/controller/build/build_pod_test.go @@ -25,6 +25,7 @@ import ( "github.com/apache/camel-k/v2/pkg/internal" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -67,3 +68,61 @@ func TestNewBuildPodConfiguration(t *testing.T) { assert.Equal(t, map[string]string{"node": "selector"}, pod.Spec.NodeSelector) assert.Equal(t, map[string]string{"annotation": "value"}, pod.Annotations) } + +func TestConfigureResourcesDefault(t *testing.T) { + build := v1.Build{ + ObjectMeta: metav1.ObjectMeta{ + Name: "theBuildName", + }, + Spec: v1.BuildSpec{ + Tasks: []v1.Task{ + { + Builder: &v1.BuilderTask{ + BaseTask: v1.BaseTask{ + Name: "builder", + Configuration: v1.BuildConfiguration{}, + }, + }, + }, + }, + }, + } + container := corev1.Container{} + + configureResources("builder", &build, &container) + assert.Equal(t, corev1.ResourceList{}, container.Resources.Requests) + assert.Equal(t, "500m", container.Resources.Limits.Cpu().String()) + assert.Equal(t, "1Gi", container.Resources.Limits.Memory().String()) +} + +func TestConfigureResources(t *testing.T) { + build := v1.Build{ + ObjectMeta: metav1.ObjectMeta{ + Name: "theBuildName", + }, + Spec: v1.BuildSpec{ + Tasks: []v1.Task{ + { + Builder: &v1.BuilderTask{ + BaseTask: v1.BaseTask{ + Name: "builder", + Configuration: v1.BuildConfiguration{ + RequestCPU: "500m", + LimitCPU: "1000m", + RequestMemory: "512Mi", + LimitMemory: "2048Mi", + }, + }, + }, + }, + }, + }, + } + container := corev1.Container{} + + configureResources("builder", &build, &container) + assert.Equal(t, "500m", container.Resources.Requests.Cpu().String()) + assert.Equal(t, "1", container.Resources.Limits.Cpu().String()) + assert.Equal(t, "512Mi", container.Resources.Requests.Memory().String()) + assert.Equal(t, "2Gi", container.Resources.Limits.Memory().String()) +}