You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/04/08 06:35:12 UTC

[camel-k] branch master updated: Cannot switch between cron and normal deployment in dev mode #1394

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

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


The following commit(s) were added to refs/heads/master by this push:
     new c2df4ad  Cannot switch between cron and normal deployment in dev mode #1394
c2df4ad is described below

commit c2df4ad95bf4a94d3db3bc789e5f493b3f06713a
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Apr 7 16:27:26 2020 +0200

    Cannot switch between cron and normal deployment in dev mode #1394
---
 pkg/trait/cron_test.go         |  8 +++--
 pkg/trait/dependencies.go      | 19 +++-------
 pkg/trait/dependencies_test.go | 16 +++++----
 pkg/trait/init.go              | 81 ++++++++++++++++++++++++++++++++++++++++++
 pkg/trait/trait_catalog.go     | 12 +++++--
 pkg/trait/trait_register.go    |  1 +
 pkg/trait/trait_types.go       |  1 +
 7 files changed, 112 insertions(+), 26 deletions(-)

diff --git a/pkg/trait/cron_test.go b/pkg/trait/cron_test.go
index 5519a2a..28b7dad 100644
--- a/pkg/trait/cron_test.go
+++ b/pkg/trait/cron_test.go
@@ -286,6 +286,7 @@ func TestCronDeps(t *testing.T) {
 	assert.NotNil(t, ct)
 	assert.Nil(t, ct.Fallback)
 	assert.True(t, util.StringSliceExists(environment.Integration.Status.Capabilities, v1.CapabilityCron))
+	assert.Contains(t, environment.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-cron")
 }
 
 func TestCronDepsFallback(t *testing.T) {
@@ -361,7 +362,8 @@ func TestCronDepsFallback(t *testing.T) {
 	assert.NotNil(t, ct)
 	assert.NotNil(t, ct.Fallback)
 	assert.True(t, util.StringSliceExists(environment.Integration.Status.Capabilities, v1.CapabilityCron))
-	assert.True(t, util.StringSliceExists(environment.Integration.Status.Dependencies, genericCronComponentFallback))
+	assert.Contains(t, environment.Integration.Status.Dependencies, genericCronComponentFallback)
+	assert.Contains(t, environment.Integration.Status.Dependencies, "mvn:org.apache.camel.k/camel-k-runtime-cron")
 }
 
 func TestCronWithMain(t *testing.T) {
@@ -437,7 +439,7 @@ func TestCronWithMain(t *testing.T) {
 	ct := environment.GetTrait("cron").(*cronTrait)
 	assert.NotNil(t, ct)
 	assert.Nil(t, ct.Fallback)
-	assert.True(t, util.StringSliceExists(environment.Interceptors, "cron"))
+	assert.Contains(t, environment.Interceptors, "cron")
 }
 
 func TestCronWithQuarkus(t *testing.T) {
@@ -514,5 +516,5 @@ func TestCronWithQuarkus(t *testing.T) {
 	assert.NotNil(t, ct)
 	assert.NotNil(t, ct.Fallback)
 	assert.True(t, *ct.Fallback)
-	assert.True(t, util.StringSliceExists(environment.Interceptors, "cron"))
+	assert.Contains(t, environment.Interceptors, "cron")
 }
diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go
index 7d7e010..8a13087 100644
--- a/pkg/trait/dependencies.go
+++ b/pkg/trait/dependencies.go
@@ -19,7 +19,6 @@ package trait
 
 import (
 	"fmt"
-	"sort"
 
 	"github.com/apache/camel-k/pkg/metadata"
 
@@ -58,6 +57,10 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
 
 	dependencies := strset.New()
 
+	if e.Integration.Spec.Dependencies != nil {
+		dependencies.Add(e.Integration.Spec.Dependencies...)
+	}
+
 	// add runtime specific dependencies
 	for _, d := range e.CamelCatalog.Runtime.Dependencies {
 		dependencies.Add(fmt.Sprintf("mvn:%s/%s", d.GroupID, d.ArtifactID))
@@ -96,26 +99,12 @@ func (t *dependenciesTrait) Apply(e *Environment) error {
 		})
 	}
 
-	for _, dependency := range e.Integration.Spec.Dependencies {
-		util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, dependency)
-	}
-
-	// add runtime specific dependencies
-	for _, capability := range e.Integration.Status.Capabilities {
-		for _, dependency := range e.CamelCatalog.Runtime.CapabilityDependencies(capability) {
-			util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID))
-		}
-	}
-
 	// add dependencies back to integration
 	dependencies.Each(func(item string) bool {
 		util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, item)
 		return true
 	})
 
-	// sort the dependencies to get always the same list if they don't change
-	sort.Strings(e.Integration.Status.Dependencies)
-
 	return nil
 }
 
diff --git a/pkg/trait/dependencies_test.go b/pkg/trait/dependencies_test.go
index 1d28d1c..a7384ad 100644
--- a/pkg/trait/dependencies_test.go
+++ b/pkg/trait/dependencies_test.go
@@ -173,13 +173,17 @@ func TestIntegrationAutoGeneratedDeps(t *testing.T) {
 		},
 	}
 
-	trait := newDependenciesTrait()
-	enabled, err := trait.Configure(e)
-	assert.Nil(t, err)
-	assert.True(t, enabled)
+	for _, trait := range []Trait{newInitTrait(), newDependenciesTrait()} {
+		enabled, err := trait.Configure(e)
+		assert.Nil(t, err)
+		assert.True(t, enabled)
+		assert.Nil(t, trait.Apply(e))
+	}
+
+	for _, processor := range e.PostStepProcessors {
+		assert.Nil(t, processor(e))
+	}
 
-	err = trait.Apply(e)
-	assert.Nil(t, err)
 	assert.ElementsMatch(
 		t,
 		[]string{
diff --git a/pkg/trait/init.go b/pkg/trait/init.go
new file mode 100644
index 0000000..cf77f6b
--- /dev/null
+++ b/pkg/trait/init.go
@@ -0,0 +1,81 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"fmt"
+	"sort"
+
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/pkg/errors"
+
+	"github.com/apache/camel-k/pkg/util"
+)
+
+// Internal trait
+type initTrait struct {
+	BaseTrait `property:",squash"`
+}
+
+func newInitTrait() Trait {
+	return &initTrait{
+		BaseTrait: NewBaseTrait("init", 1),
+	}
+}
+
+func (t *initTrait) Configure(e *Environment) (bool, error) {
+	if t.Enabled != nil && !*t.Enabled {
+		return false, errors.New("trait init cannot be disabled")
+	}
+
+	return true, nil
+}
+
+func (t *initTrait) Apply(e *Environment) error {
+	if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
+		//
+		// Dependencies need to be recomputed in case of a trait declares a capability but as
+		// the dependencies trait runs earlier than some task such as the cron one, we need to
+		// register a post step processor that recompute the dependencies based on the declared
+		// capabilities
+		//
+		e.PostStepProcessors = append(e.PostStepProcessors, func(environment *Environment) error {
+			//
+			// The camel catalog is set-up by the camel trait so it may not be available for
+			// traits executed before that one
+			//
+			if e.CamelCatalog != nil {
+				// add runtime specific dependencies
+				for _, capability := range e.Integration.Status.Capabilities {
+					for _, dependency := range e.CamelCatalog.Runtime.CapabilityDependencies(capability) {
+						util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, fmt.Sprintf("mvn:%s/%s", dependency.GroupID, dependency.ArtifactID))
+					}
+				}
+			}
+
+			if e.Integration.Status.Dependencies != nil {
+				// sort the dependencies to get always the same list if they don't change
+				sort.Strings(e.Integration.Status.Dependencies)
+			}
+
+			return nil
+		})
+	}
+
+	return nil
+}
diff --git a/pkg/trait/trait_catalog.go b/pkg/trait/trait_catalog.go
index 75bbcef..5242004 100644
--- a/pkg/trait/trait_catalog.go
+++ b/pkg/trait/trait_catalog.go
@@ -19,7 +19,6 @@ package trait
 
 import (
 	"context"
-	"errors"
 	"reflect"
 	"sort"
 	"strings"
@@ -28,6 +27,7 @@ import (
 	"github.com/apache/camel-k/pkg/client"
 	"github.com/apache/camel-k/pkg/util/log"
 	"github.com/fatih/structs"
+	"github.com/pkg/errors"
 )
 
 // Catalog collects all information about traits in one place
@@ -118,6 +118,14 @@ func (c *Catalog) apply(environment *Environment) error {
 			}
 
 			environment.ExecutedTraits = append(environment.ExecutedTraits, trait)
+
+			// execute post step processors
+			for _, processor := range environment.PostStepProcessors {
+				err := processor(environment)
+				if err != nil {
+					return errors.Wrap(err, "error executing post step action")
+				}
+			}
 		}
 	}
 
@@ -128,7 +136,7 @@ func (c *Catalog) apply(environment *Environment) error {
 	for _, processor := range environment.PostProcessors {
 		err := processor(environment)
 		if err != nil {
-			return err
+			return errors.Wrap(err, "error executing post processor")
 		}
 	}
 
diff --git a/pkg/trait/trait_register.go b/pkg/trait/trait_register.go
index 265cfa9..993efdf 100644
--- a/pkg/trait/trait_register.go
+++ b/pkg/trait/trait_register.go
@@ -20,6 +20,7 @@ package trait
 func init() {
 	// List of default trait factories.
 	// Declaration order is not important, but let's keep them sorted for debugging.
+	AddToTraits(newInitTrait)
 	AddToTraits(newPlatformTrait)
 	AddToTraits(newCamelTrait)
 	AddToTraits(newOpenAPITrait)
diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go
index 61ab07d..13a1ff5 100644
--- a/pkg/trait/trait_types.go
+++ b/pkg/trait/trait_types.go
@@ -203,6 +203,7 @@ type Environment struct {
 	Integration           *v1.Integration
 	Resources             *kubernetes.Collection
 	PostActions           []func(*Environment) error
+	PostStepProcessors    []func(*Environment) error
 	PostProcessors        []func(*Environment) error
 	BuildTasks            []v1.Task
 	ConfiguredTraits      []Trait