This is an automated email from the ASF dual-hosted git repository.

astefanutti pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git

commit 41581b0720da80a282da10f5f94837eacd384257
Author: Antonin Stefanutti <[email protected]>
AuthorDate: Mon Aug 16 13:00:50 2021 +0200

    feat(native): Configure Build depending on Quarkus packaging type
---
 pkg/builder/builder.go    | 13 ++++------
 pkg/builder/image.go      | 43 +++++++++++++++++--------------
 pkg/builder/project.go    |  7 ++---
 pkg/builder/quarkus.go    | 15 +++++------
 pkg/builder/steps.go      | 12 +++++++++
 pkg/trait/builder.go      |  7 ++---
 pkg/trait/quarkus.go      | 65 +++++++++++++++++++++++++++++++++++++++--------
 pkg/trait/quarkus_test.go | 28 +++++++++++---------
 8 files changed, 124 insertions(+), 66 deletions(-)

diff --git a/pkg/builder/builder.go b/pkg/builder/builder.go
index 12d3718..5668119 100644
--- a/pkg/builder/builder.go
+++ b/pkg/builder/builder.go
@@ -86,14 +86,11 @@ func (t *builderTask) Do(ctx context.Context) 
v1.BuildStatus {
                })
        }
 
-       steps := make([]Step, 0)
-       for _, step := range t.task.Steps {
-               s, ok := stepsByID[step]
-               if !ok {
-                       log.Info("Skipping unknown build step", "step", step)
-                       continue
-               }
-               steps = append(steps, s)
+       steps, err := StepsFrom(t.task.Steps...)
+       if err != nil {
+               t.log.Errorf(err,"invalid builder steps: %s", t.task.Steps)
+               result.Failed(err)
+               return result
        }
        // Sort steps by phase
        sort.SliceStable(steps, func(i, j int) bool {
diff --git a/pkg/builder/image.go b/pkg/builder/image.go
index 3d13d13..4a35be5 100644
--- a/pkg/builder/image.go
+++ b/pkg/builder/image.go
@@ -23,7 +23,8 @@ import (
        "path"
 
        "k8s.io/apimachinery/pkg/selection"
-       k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
+
+       ctrl "sigs.k8s.io/controller-runtime/pkg/client"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/platform"
@@ -34,12 +35,28 @@ import (
 
 const (
        ContextDir      = "context"
-       DeploymentDir   = "/home/nonroot"
+       DeploymentDir   = "/deployments"
        DependenciesDir = "dependencies"
 )
 
 type artifactsSelector func(ctx *builderContext) error
 
+func nativeImageContext(ctx *builderContext) error {
+       return imageContext(ctx, func(ctx *builderContext) error {
+               runner := "camel-k-integration-" + defaults.Version + "-runner"
+
+               ctx.BaseImage = "quay.io/quarkus/quarkus-distroless-image:1.0"
+               ctx.SelectedArtifacts = []v1.Artifact{
+                       {
+                               Location: path.Join(ctx.Path, "maven", 
"target", runner),
+                               Target:   runner,
+                       },
+               }
+
+               return nil
+       })
+}
+
 func standardImageContext(ctx *builderContext) error {
        return imageContext(ctx, func(ctx *builderContext) error {
                ctx.SelectedArtifacts = ctx.Artifacts
@@ -112,22 +129,10 @@ func imageContext(ctx *builderContext, selector 
artifactsSelector) error {
                }
        }
 
-       runner := "camel-k-integration-" + defaults.Version + "-runner"
-       _, err = util.CopyFile(path.Join(ctx.Path, "maven", "target", runner), 
path.Join(contextDir, runner))
-       if err != nil {
-               return err
-       }
-
-       err = os.Chmod(path.Join(contextDir, runner), 0755)
-       if err != nil {
-               return err
-       }
-
        // #nosec G202
        dockerfile := []byte(`
-               FROM quay.io/quarkus/quarkus-distroless-image:1.0
-               WORKDIR ` + DeploymentDir + `
-               COPY --chown=nonroot:root ` + runner + ` ` + runner + `
+               FROM ` + ctx.BaseImage + `
+               COPY --chown=nonroot:root . ` + DeploymentDir + `
                USER nonroot
        `)
 
@@ -140,9 +145,9 @@ func imageContext(ctx *builderContext, selector 
artifactsSelector) error {
 }
 
 func listPublishedImages(context *builderContext) ([]v1.IntegrationKitStatus, 
error) {
-       options := []k8sclient.ListOption{
-               k8sclient.InNamespace(context.Namespace),
-               k8sclient.MatchingLabels{
+       options := []ctrl.ListOption{
+               ctrl.InNamespace(context.Namespace),
+               ctrl.MatchingLabels{
                        "camel.apache.org/runtime.version":  
context.Catalog.Runtime.Version,
                        "camel.apache.org/runtime.provider": 
string(context.Catalog.Runtime.Provider),
                },
diff --git a/pkg/builder/project.go b/pkg/builder/project.go
index 39182e0..6cb8a0b 100644
--- a/pkg/builder/project.go
+++ b/pkg/builder/project.go
@@ -35,8 +35,9 @@ type steps struct {
        GenerateProjectSettings Step
        InjectDependencies      Step
        SanitizeDependencies    Step
-       StandardImageContext    Step
        IncrementalImageContext Step
+       NativeImageContext      Step
+       StandardImageContext    Step
 }
 
 var Steps = steps{
@@ -45,8 +46,9 @@ var Steps = steps{
        GenerateProjectSettings: NewStep(ProjectGenerationPhase+1, 
generateProjectSettings),
        InjectDependencies:      NewStep(ProjectGenerationPhase+2, 
injectDependencies),
        SanitizeDependencies:    NewStep(ProjectGenerationPhase+3, 
sanitizeDependencies),
-       StandardImageContext:    NewStep(ApplicationPackagePhase, 
standardImageContext),
        IncrementalImageContext: NewStep(ApplicationPackagePhase, 
incrementalImageContext),
+       NativeImageContext:      NewStep(ApplicationPackagePhase, 
nativeImageContext),
+       StandardImageContext:    NewStep(ApplicationPackagePhase, 
standardImageContext),
 }
 
 var DefaultSteps = []Step{
@@ -55,7 +57,6 @@ var DefaultSteps = []Step{
        Steps.GenerateProjectSettings,
        Steps.InjectDependencies,
        Steps.SanitizeDependencies,
-       Steps.IncrementalImageContext,
 }
 
 func cleanUpBuildDir(ctx *builderContext) error {
diff --git a/pkg/builder/quarkus.go b/pkg/builder/quarkus.go
index 3757af3..1e367b2 100644
--- a/pkg/builder/quarkus.go
+++ b/pkg/builder/quarkus.go
@@ -99,18 +99,17 @@ func GenerateQuarkusProjectCommon(camelQuarkusVersion 
string, runtimeVersion str
        p.Dependencies = make([]maven.Dependency, 0)
        p.Build = &maven.Build{Plugins: make([]maven.Plugin, 0)}
 
-       // camel-quarkus does routes discovery at startup but we don't want
+       // camel-quarkus does route discovery at startup, but we don't want
        // this to happen as routes are loaded at runtime and looking for
        // routes at build time may try to load camel-k-runtime routes builder
-       // proxies which in some case may fail
+       // proxies which in some case may fail.
        p.Properties["quarkus.camel.routes-discovery.enabled"] = "false"
 
-       // disable quarkus banner ...
+       // disable quarkus banner
        p.Properties["quarkus.banner.enabled"] = "false"
 
-       // set fast-jar packaging since it gives some startup time improvements
-       p.Properties["quarkus.package.type"] = "native"
-       // p.Properties["quarkus.native.additional-build-args"] = 
"--language:js"
+       // set fast-jar packaging by default, since it gives some startup time 
improvements
+       p.Properties["quarkus.package.type"] = "fast-jar"
 
        // DependencyManagement
        p.DependencyManagement.Dependencies = 
append(p.DependencyManagement.Dependencies,
@@ -199,7 +198,7 @@ func computeQuarkusDependencies(ctx *builderContext) error {
        mc.SettingsContent = ctx.Maven.SettingsData
        mc.LocalRepository = ctx.Build.Maven.LocalRepository
 
-       // Process artifacts list and add it to existing artifacts.
+       // Process artifacts list and add it to existing artifacts
        artifacts, err := ProcessQuarkusTransitiveDependencies(mc)
        if err != nil {
                return err
@@ -212,7 +211,7 @@ func computeQuarkusDependencies(ctx *builderContext) error {
 func ProcessQuarkusTransitiveDependencies(mc maven.Context) ([]v1.Artifact, 
error) {
        var artifacts []v1.Artifact
 
-       // Quarkus fast-jar format is split into various sub directories in 
quarkus-app
+       // Quarkus fast-jar format is split into various sub-directories in 
quarkus-app
        quarkusAppDir := path.Join(mc.Path, "target", "quarkus-app")
 
        // Discover application dependencies from the Quarkus fast-jar 
directory tree
diff --git a/pkg/builder/steps.go b/pkg/builder/steps.go
index d78e433..245a389 100644
--- a/pkg/builder/steps.go
+++ b/pkg/builder/steps.go
@@ -57,6 +57,18 @@ func NewStep(phase int32, task StepTask) Step {
        return &s
 }
 
+func StepsFrom(ids ...string) ([]Step, error) {
+       steps := make([]Step, 0)
+       for _, id := range ids {
+               s, ok := stepsByID[id]
+               if !ok {
+                       return steps, fmt.Errorf("unknown build step: %s", id)
+               }
+               steps = append(steps, s)
+       }
+       return steps, nil
+}
+
 func StepIDsFor(steps ...Step) []string {
        IDs := make([]string, 0)
        for _, step := range steps {
diff --git a/pkg/trait/builder.go b/pkg/trait/builder.go
index c4da97b..d85414e 100644
--- a/pkg/trait/builder.go
+++ b/pkg/trait/builder.go
@@ -21,12 +21,12 @@ import (
        "fmt"
        "sort"
 
-       "github.com/apache/camel-k/pkg/util/property"
        corev1 "k8s.io/api/core/v1"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/builder"
        mvn "github.com/apache/camel-k/pkg/util/maven"
+       "github.com/apache/camel-k/pkg/util/property"
 )
 
 // The builder trait is internally used to determine the best strategy to
@@ -137,7 +137,7 @@ func (t *builderTrait) Apply(e *Environment) error {
 func (t *builderTrait) builderTask(e *Environment) (*v1.BuilderTask, error) {
        maven := e.Platform.Status.Build.Maven
 
-       // Add Maven repositories defined in the IntergrationKit
+       // Add Maven repositories defined in the IntegrationKit
        for _, repo := range e.IntegrationKit.Spec.Repositories {
                maven.Repositories = append(maven.Repositories, 
mvn.NewRepository(repo))
        }
@@ -168,9 +168,6 @@ func (t *builderTrait) builderTask(e *Environment) 
(*v1.BuilderTask, error) {
        steps := make([]builder.Step, 0)
        steps = append(steps, builder.DefaultSteps...)
 
-       quarkus := e.Catalog.GetTrait("quarkus").(*quarkusTrait)
-       quarkus.addBuildSteps(&steps)
-
        // sort steps by phase
        sort.SliceStable(steps, func(i, j int) bool {
                return steps[i].Phase() < steps[j].Phase()
diff --git a/pkg/trait/quarkus.go b/pkg/trait/quarkus.go
index cec04cf..47f1f95 100644
--- a/pkg/trait/quarkus.go
+++ b/pkg/trait/quarkus.go
@@ -19,6 +19,7 @@ package trait
 
 import (
        "fmt"
+       "sort"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/builder"
@@ -61,19 +62,56 @@ func (t *quarkusTrait) Configure(e *Environment) (bool, 
error) {
                t.PackageType = &packageType
        }
 
-       return e.InPhase(v1.IntegrationKitPhaseReady, 
v1.IntegrationPhaseDeploying) ||
+       return e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) ||
+               e.InPhase(v1.IntegrationKitPhaseReady, 
v1.IntegrationPhaseDeploying) ||
                e.InPhase(v1.IntegrationKitPhaseReady, 
v1.IntegrationPhaseRunning), nil
 }
 
 func (t *quarkusTrait) Apply(e *Environment) error {
-       if t.isNativePackageType() {
-               container := e.getIntegrationContainer()
-               if container == nil {
-                       return fmt.Errorf("unable to find integration 
container: %s", e.Integration.Name)
+       if e.IntegrationKitInPhase(v1.IntegrationKitPhaseBuildSubmitted) {
+               build := getBuilderTask(e.BuildTasks)
+               if build == nil {
+                       return fmt.Errorf("unable to find builder task: %s", 
e.Integration.Name)
                }
 
-               container.Command = []string{"./camel-k-integration-" + 
defaults.Version + "-runner"}
-               container.WorkingDir = builder.DeploymentDir
+               if build.Maven.Properties == nil {
+                       build.Maven.Properties = make(map[string]string)
+               }
+
+               steps, err := builder.StepsFrom(build.Steps...)
+               if err != nil {
+                       return err
+               }
+
+               steps = append(steps, builder.QuarkusSteps...)
+
+               if t.isNativePackageType() {
+                       build.Maven.Properties["quarkus.package.type"] = 
string(nativePackageType)
+                       steps = append(steps, builder.Steps.NativeImageContext)
+               } else {
+                       build.Maven.Properties["quarkus.package.type"] = 
string(fastJarPackageType)
+                       steps = append(steps, 
builder.Steps.IncrementalImageContext)
+               }
+
+               // Sort steps by phase
+               sort.SliceStable(steps, func(i, j int) bool {
+                       return steps[i].Phase() < steps[j].Phase()
+               })
+
+               build.Steps = builder.StepIDsFor(steps...)
+       }
+
+       if e.InPhase(v1.IntegrationKitPhaseReady, v1.IntegrationPhaseDeploying) 
||
+               e.InPhase(v1.IntegrationKitPhaseReady, 
v1.IntegrationPhaseRunning) {
+               if t.isNativePackageType() {
+                       container := e.getIntegrationContainer()
+                       if container == nil {
+                               return fmt.Errorf("unable to find integration 
container: %s", e.Integration.Name)
+                       }
+
+                       container.Command = []string{"./camel-k-integration-" + 
defaults.Version + "-runner"}
+                       container.WorkingDir = builder.DeploymentDir
+               }
        }
 
        return nil
@@ -89,10 +127,15 @@ func (t *quarkusTrait) InfluencesKit() bool {
        return true
 }
 
-func (t *quarkusTrait) addBuildSteps(steps *[]builder.Step) {
-       *steps = append(*steps, builder.QuarkusSteps...)
-}
-
 func (t *quarkusTrait) isNativePackageType() bool {
        return t.PackageType != nil && *t.PackageType == nativePackageType
 }
+
+func getBuilderTask(tasks []v1.Task) *v1.BuilderTask {
+       for i, task := range tasks {
+               if task.Builder != nil {
+                       return tasks[i].Builder
+               }
+       }
+       return nil
+}
diff --git a/pkg/trait/quarkus_test.go b/pkg/trait/quarkus_test.go
index 318bbb3..688194a 100644
--- a/pkg/trait/quarkus_test.go
+++ b/pkg/trait/quarkus_test.go
@@ -27,13 +27,22 @@ import (
        "github.com/apache/camel-k/pkg/util/camel"
 )
 
-func TestConfigureQuarkusTraitShouldSucceed(t *testing.T) {
+func TestConfigureQuarkusTraitBuildSubmitted(t *testing.T) {
        quarkusTrait, environment := createNominalQuarkusTest()
+       environment.IntegrationKit.Status.Phase = 
v1.IntegrationKitPhaseBuildSubmitted
 
        configured, err := quarkusTrait.Configure(environment)
 
        assert.True(t, configured)
        assert.Nil(t, err)
+
+       err = quarkusTrait.Apply(environment)
+       assert.Nil(t, err)
+
+       build := getBuilderTask(environment.BuildTasks)
+       assert.NotNil(t, t, build)
+
+       assert.Len(t, build.Steps, len(builder.QuarkusSteps)+1)
 }
 
 func TestConfigureDisabledQuarkusTraitShouldFail(t *testing.T) {
@@ -54,17 +63,6 @@ func TestApplyQuarkusTraitDoesNothing(t *testing.T) {
        assert.Nil(t, err)
 }
 
-func TestQuarkusTraitAddBuildStepsShouldSucceed(t *testing.T) {
-       quarkusTrait, _ := createNominalQuarkusTest()
-
-       steps := make([]builder.Step, 0)
-       steps = append(steps, builder.DefaultSteps...)
-
-       quarkusTrait.addBuildSteps(&steps)
-
-       assert.Len(t, steps, 
len(builder.DefaultSteps)+len(builder.QuarkusSteps))
-}
-
 func createNominalQuarkusTest() (*quarkusTrait, *Environment) {
        trait := newQuarkusTrait().(*quarkusTrait)
        trait.Enabled = BoolP(true)
@@ -80,6 +78,12 @@ func createNominalQuarkusTest() (*quarkusTrait, 
*Environment) {
                                },
                        },
                },
+               IntegrationKit: &v1.IntegrationKit{},
+               BuildTasks: []v1.Task{
+                       {
+                               Builder: &v1.BuilderTask{},
+                       },
+               },
                Platform: &v1.IntegrationPlatform{},
        }
 

Reply via email to