From 306221fbebce3d6e550e0e3a370b52707cf878fc Mon Sep 17 00:00:00 2001 From: Pasquale Congiusti Date: Tue, 24 Sep 2024 19:24:16 +0200 Subject: [PATCH] fix(trait): builder configuration from platform Closes #5838 --- pkg/trait/builder.go | 16 ++++++++++------ pkg/trait/builder_test.go | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go index bb4cdc159d..24d28d7449 100644 --- a/pkg/trait/builder.go +++ b/pkg/trait/builder.go @@ -212,7 +212,11 @@ func (t *builderTrait) Apply(e *Environment) error { var pipelineTasks []v1.Task // task configuration resources - tasksConf, err := t.parseTasksConf() + defaultBuildConf := &v1.BuildConfiguration{} + if e.Platform != nil { + defaultBuildConf = &e.Platform.Status.Build.BuildConfiguration + } + tasksConf, err := t.parseTasksConf(defaultBuildConf) if err != nil { return err } @@ -539,7 +543,7 @@ func taskConfOrDefault(tasksConf map[string]*v1.BuildConfiguration, taskName str return tasksConf[taskName] } -func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, error) { +func (t *builderTrait) parseTasksConf(defaultBuildConf *v1.BuildConfiguration) (map[string]*v1.BuildConfiguration, error) { tasksConf := make(map[string]*v1.BuildConfiguration) for _, t := range t.TasksRequestCPU { @@ -550,7 +554,7 @@ func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, erro taskName := splits[0] taskResource := splits[1] if tasksConf[taskName] == nil { - tasksConf[taskName] = &v1.BuildConfiguration{} + tasksConf[taskName] = defaultBuildConf } tasksConf[taskName].RequestCPU = taskResource } @@ -563,7 +567,7 @@ func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, erro taskName := splits[0] taskResource := splits[1] if tasksConf[taskName] == nil { - tasksConf[taskName] = &v1.BuildConfiguration{} + tasksConf[taskName] = defaultBuildConf } tasksConf[taskName].RequestMemory = taskResource } @@ -576,7 +580,7 @@ func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, erro taskName := splits[0] taskResource := splits[1] if tasksConf[taskName] == nil { - tasksConf[taskName] = &v1.BuildConfiguration{} + tasksConf[taskName] = defaultBuildConf } tasksConf[taskName].LimitCPU = taskResource } @@ -589,7 +593,7 @@ func (t *builderTrait) parseTasksConf() (map[string]*v1.BuildConfiguration, erro taskName := splits[0] taskResource := splits[1] if tasksConf[taskName] == nil { - tasksConf[taskName] = &v1.BuildConfiguration{} + tasksConf[taskName] = defaultBuildConf } tasksConf[taskName].LimitMemory = taskResource } diff --git a/pkg/trait/builder_test.go b/pkg/trait/builder_test.go index aaa30c00f7..0ba9166938 100644 --- a/pkg/trait/builder_test.go +++ b/pkg/trait/builder_test.go @@ -389,7 +389,7 @@ func TestBuilderCustomTasksConfiguration(t *testing.T) { builderTrait.TasksRequestMemory = append(builderTrait.TasksRequestMemory, "package:8Gi") builderTrait.TasksLimitMemory = append(builderTrait.TasksLimitMemory, "spectrum:4Gi") - tasksConf, err := builderTrait.parseTasksConf() + tasksConf, err := builderTrait.parseTasksConf(&v1.BuildConfiguration{}) require.NoError(t, err) assert.Equal(t, 4, len(tasksConf)) @@ -403,7 +403,7 @@ func TestBuilderCustomTasksConfigurationError(t *testing.T) { builderTrait := createNominalBuilderTraitTest() builderTrait.TasksLimitCPU = append(builderTrait.TasksLimitCPU, "syntax error") - _, err := builderTrait.parseTasksConf() + _, err := builderTrait.parseTasksConf(&v1.BuildConfiguration{}) require.Error(t, err) assert.Equal(t, "could not parse syntax error, expected format :", err.Error()) @@ -594,6 +594,20 @@ func TestBuilderTasksFilterAndReorderCustomTasks(t *testing.T) { assert.Equal(t, []string{"builder", "my-custom-task", "package", "my-custom-publish"}, pipelineTasks) } +func TestBuilderTasksNodeSelectorPlatformBuildStrategyPod(t *testing.T) { + env := createBuilderTestEnv(v1.IntegrationPlatformClusterKubernetes, v1.IntegrationPlatformBuildPublishStrategyJib, v1.BuildStrategyPod) + builderTrait := createNominalBuilderTraitTest() + builderTrait.NodeSelector = map[string]string{ + "kubernetes.io/arch": "amd64", + } + + err := builderTrait.Apply(env) + require.NoError(t, err) + builderTask := getBuilderTask(env.Pipeline) + assert.NotNil(t, builderTask) + assert.Equal(t, map[string]string{"kubernetes.io/arch": "amd64"}, builderTask.Configuration.NodeSelector) +} + func findCustomTaskByName(tasks []v1.Task, name string) v1.Task { for _, t := range tasks { if t.Custom != nil && t.Custom.Name == name {