Skip to content

Commit

Permalink
fix(trait): builder configuration from platform
Browse files Browse the repository at this point in the history
Closes #5838
  • Loading branch information
squakez committed Sep 26, 2024
1 parent d211f5a commit ba142b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 10 additions & 6 deletions pkg/trait/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/trait/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 <task-name>:<task-resource>", err.Error())
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ba142b9

Please sign in to comment.