Skip to content

Commit

Permalink
feat(ctrl): default builder Pod resources
Browse files Browse the repository at this point in the history
Closes #5811
  • Loading branch information
squakez committed Jan 29, 2025
1 parent e685450 commit 516fd84
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/controller/build/build_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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).
Expand Down
59 changes: 59 additions & 0 deletions pkg/controller/build/build_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

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

0 comments on commit 516fd84

Please sign in to comment.