Repository: apache/camel-k
Pull request #1962 (2021-01-29 17:07:20)

chore(build): Sort builder steps by phase in Build spec
**Release Note**
```release-note
NONE
```



Patch:
diff --git a/docs/modules/ROOT/pages/contributing/developers.adoc b/docs/modules/ROOT/pages/contributing/developers.adoc
index 80463ca34..5ef434d66 100644
--- a/docs/modules/ROOT/pages/contributing/developers.adoc
+++ b/docs/modules/ROOT/pages/contributing/developers.adoc
@@ -152,7 +152,7 @@ To add additional dependencies to your routes:
 
 [source]
 ----
-./kamel run -d camel:dns examples/dns.js
+./kamel run -d camel-dns examples/dns.js
 ----
 
 [[local-development]]
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index 3fa45d784..1c1c4df7b 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -20,10 +20,10 @@ package trait
 import (
 	"fmt"
 	"path"
+	"sort"
 	"strconv"
 	"strings"
 
-	"github.com/apache/camel-k/pkg/builder/spectrum"
 	"github.com/pkg/errors"
 
 	corev1 "k8s.io/api/core/v1"
@@ -34,6 +34,7 @@ import (
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/builder/kaniko"
 	"github.com/apache/camel-k/pkg/builder/s2i"
+	"github.com/apache/camel-k/pkg/builder/spectrum"
 	"github.com/apache/camel-k/pkg/util/defaults"
 )
 
@@ -179,25 +180,33 @@ func (t *builderTrait) builderTask(e *Environment) *v1.BuilderTask {
 		BaseImage:    e.Platform.Status.Build.BaseImage,
 		Runtime:      e.CamelCatalog.Runtime,
 		Dependencies: e.IntegrationKit.Spec.Dependencies,
-		//TODO: sort steps for easier read
-		Steps:      builder.StepIDsFor(builder.DefaultSteps...),
-		Properties: e.Platform.Status.Build.Properties,
-		Timeout:    e.Platform.Status.Build.GetTimeout(),
-		Maven:      e.Platform.Status.Build.Maven,
+		Properties:   e.Platform.Status.Build.Properties,
+		Timeout:      e.Platform.Status.Build.GetTimeout(),
+		Maven:        e.Platform.Status.Build.Maven,
 	}
 
+	steps := make([]builder.Step, 0)
+	steps = append(steps, builder.DefaultSteps...)
+
 	switch e.Platform.Status.Build.PublishStrategy {
 	case v1.IntegrationPlatformBuildPublishStrategyBuildah, v1.IntegrationPlatformBuildPublishStrategyKaniko:
 		task.BuildDir = path.Join(builderDir, e.IntegrationKit.Name)
 
 	case v1.IntegrationPlatformBuildPublishStrategyS2I:
-		task.Steps = append(task.Steps, builder.StepIDsFor(s2i.S2iSteps...)...)
+		steps = append(steps, s2i.S2iSteps...)
 	case v1.IntegrationPlatformBuildPublishStrategySpectrum:
-		task.Steps = append(task.Steps, builder.StepIDsFor(spectrum.SpectrumSteps...)...)
+		steps = append(steps, spectrum.SpectrumSteps...)
 	}
 
 	quarkus := e.Catalog.GetTrait("quarkus").(*quarkusTrait)
-	quarkus.addBuildSteps(task)
+	quarkus.addBuildSteps(&steps)
+
+	// sort steps by phase
+	sort.SliceStable(steps, func(i, j int) bool {
+		return steps[i].Phase() < steps[j].Phase()
+	})
+
+	task.Steps = builder.StepIDsFor(steps...)
 
 	return task
 }
@@ -241,7 +250,7 @@ func (t *builderTrait) buildahTask(e *Environment) (*v1.ImageTask, error) {
 		}
 		mountRegistryConfigMap(e.Platform.Status.Build.Registry.CA, config, &volumes, &volumeMounts)
 		// This is easier to use the --cert-dir option, otherwise Buildah defaults to looking up certificates
-		//into a directory named after the registry address
+		// into a directory named after the registry address
 		bud = append(bud[:2], append([]string{"--cert-dir=/etc/containers/certs.d"}, bud[2:]...)...)
 		push = append(push[:2], append([]string{"--cert-dir=/etc/containers/certs.d"}, push[2:]...)...)
 	}
diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go
index ea2b3d512..4227221f5 100644
--- a/pkg/trait/quarkus.go
+++ b/pkg/trait/quarkus.go
@@ -18,7 +18,6 @@ limitations under the License.
 package trait
 
 import (
-	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/builder/runtime"
 )
@@ -62,6 +61,6 @@ func (t *quarkusTrait) InfluencesKit() bool {
 	return true
 }
 
-func (t *quarkusTrait) addBuildSteps(task *v1.BuilderTask) {
-	task.Steps = append(task.Steps, builder.StepIDsFor(runtime.QuarkusSteps...)...)
+func (t *quarkusTrait) addBuildSteps(steps *[]builder.Step) {
+	*steps = append(*steps, runtime.QuarkusSteps...)
 }
diff --git a/pkg/util/camel/camel_runtime_catalog.go b/pkg/util/camel/camel_runtime_catalog.go
index 2482effe3..dc71a4094 100644
--- a/pkg/util/camel/camel_runtime_catalog.go
+++ b/pkg/util/camel/camel_runtime_catalog.go
@@ -36,6 +36,15 @@ func NewRuntimeCatalog(spec v1.CamelCatalogSpec) *RuntimeCatalog {
 	for id, artifact := range catalog.Artifacts {
 		for _, scheme := range artifact.Schemes {
 			scheme := scheme
+
+			// In case of duplicate only, choose the "org.apache.camel.quarkus" artifact (if present).
+			// Workaround for https://github.com/apache/camel-k-runtime/issues/592
+			if _, duplicate := catalog.artifactByScheme[scheme.ID]; duplicate {
+				if artifact.GroupID != "org.apache.camel.quarkus" {
+					continue
+				}
+			}
+
 			catalog.artifactByScheme[scheme.ID] = id
 			catalog.schemesByID[scheme.ID] = scheme
 		}


Test patch:
diff --git a/pkg/trait/quarkus_test.go b/pkg/trait/quarkus_test.go
index 9ee8ae9ab..9061f574f 100644
--- a/pkg/trait/quarkus_test.go
+++ b/pkg/trait/quarkus_test.go
@@ -20,11 +20,11 @@ package trait
 import (
 	"testing"
 
-	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/stretchr/testify/assert"
 
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/util/camel"
-	"github.com/stretchr/testify/assert"
 )
 
 func TestConfigureQuarkusTraitShouldSucceed(t *testing.T) {
@@ -57,13 +57,12 @@ func TestApplyQuarkusTraitDoesNothing(t *testing.T) {
 func TestQuarkusTraitAddBuildStepsShouldSucceed(t *testing.T) {
 	quarkusTrait, _ := createNominalQuarkusTest()
 
-	task := &v1.BuilderTask{
-		Steps: builder.StepIDsFor(builder.DefaultSteps...),
-	}
+	steps := make([]builder.Step, 0)
+	steps = append(steps, builder.DefaultSteps...)
 
-	quarkusTrait.addBuildSteps(task)
+	quarkusTrait.addBuildSteps(&steps)
 
-	assert.Len(t, task.Steps, 9)
+	assert.Len(t, steps, 9)
 }
 
 func createNominalQuarkusTest() (*quarkusTrait, *Environment) {