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

nferraro 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 9d9ad64  Fix #1419: fix knative sources workflow
9d9ad64 is described below

commit 9d9ad64e78ad5a366f27437cc04b629c82d616dd
Author: Nicola Ferraro <ni.ferr...@gmail.com>
AuthorDate: Tue Apr 21 21:53:52 2020 +0200

    Fix #1419: fix knative sources workflow
---
 pkg/controller/integration/initialize.go     | 28 -------------------------
 pkg/controller/integration/platform_setup.go | 31 ++++++++++++++++++++++++++++
 pkg/trait/knative.go                         | 26 +++++++++++++++++++++++
 pkg/trait/trait_types.go                     | 13 ++++++++++++
 pkg/util/util.go                             | 10 +++++++++
 5 files changed, 80 insertions(+), 28 deletions(-)

diff --git a/pkg/controller/integration/initialize.go 
b/pkg/controller/integration/initialize.go
index f47fa39..ed98d88 100644
--- a/pkg/controller/integration/initialize.go
+++ b/pkg/controller/integration/initialize.go
@@ -21,11 +21,8 @@ import (
        "context"
 
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
-       "github.com/apache/camel-k/pkg/client"
-       "github.com/apache/camel-k/pkg/platform"
        "github.com/apache/camel-k/pkg/trait"
        "github.com/apache/camel-k/pkg/util/defaults"
-       "github.com/apache/camel-k/pkg/util/knative"
 )
 
 // NewInitializeAction creates a new initialize action
@@ -53,36 +50,11 @@ func (action *initializeAction) Handle(ctx context.Context, 
integration *v1.Inte
                return nil, err
        }
 
-       pl, err := platform.GetCurrentPlatform(ctx, action.client, 
integration.Namespace)
-       if err != nil {
-               return nil, err
-       }
-
        kit := v1.NewIntegrationKit(integration.Namespace, integration.Spec.Kit)
 
        integration.Status.Phase = v1.IntegrationPhaseBuildingKit
        integration.SetIntegrationKit(&kit)
-       integration.Status.Profile = determineBestProfile(ctx, action.client, 
integration, pl)
        integration.Status.Version = defaults.Version
 
        return integration, nil
 }
-
-// DetermineBestProfile tries to detect the best trait profile for the 
integration
-func determineBestProfile(ctx context.Context, c client.Client, integration 
*v1.Integration, p *v1.IntegrationPlatform) v1.TraitProfile {
-       if integration.Spec.Profile != "" {
-               return integration.Spec.Profile
-       }
-       if integration.Status.Profile != "" {
-               // Integration already has a profile
-               return integration.Status.Profile
-       }
-       if p.Status.Profile != "" {
-               // Use platform profile if set
-               return p.Status.Profile
-       }
-       if knative.IsEnabledInNamespace(ctx, c, p.Namespace) {
-               return v1.TraitProfileKnative
-       }
-       return platform.GetProfile(p)
-}
diff --git a/pkg/controller/integration/platform_setup.go 
b/pkg/controller/integration/platform_setup.go
index aed9212..fa4f40c 100644
--- a/pkg/controller/integration/platform_setup.go
+++ b/pkg/controller/integration/platform_setup.go
@@ -20,6 +20,11 @@ package integration
 import (
        "context"
 
+       "github.com/apache/camel-k/pkg/client"
+       "github.com/apache/camel-k/pkg/platform"
+       "github.com/apache/camel-k/pkg/util/knative"
+       k8serrors "k8s.io/apimachinery/pkg/api/errors"
+
        v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
        "github.com/apache/camel-k/pkg/trait"
 )
@@ -50,5 +55,31 @@ func (action *platformSetupAction) Handle(ctx 
context.Context, integration *v1.I
                return nil, err
        }
 
+       pl, err := platform.GetCurrentPlatform(ctx, action.client, 
integration.Namespace)
+       if err != nil && !k8serrors.IsNotFound(err) {
+               return nil, err
+       } else if pl != nil {
+               integration.Status.Profile = determineBestProfile(ctx, 
action.client, integration, pl)
+       }
+
        return integration, nil
 }
+
+// DetermineBestProfile tries to detect the best trait profile for the 
integration
+func determineBestProfile(ctx context.Context, c client.Client, integration 
*v1.Integration, p *v1.IntegrationPlatform) v1.TraitProfile {
+       if integration.Spec.Profile != "" {
+               return integration.Spec.Profile
+       }
+       if integration.Status.Profile != "" {
+               // Integration already has a profile
+               return integration.Status.Profile
+       }
+       if p.Status.Profile != "" {
+               // Use platform profile if set
+               return p.Status.Profile
+       }
+       if knative.IsEnabledInNamespace(ctx, c, integration.Namespace) {
+               return v1.TraitProfileKnative
+       }
+       return platform.GetProfile(p)
+}
diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go
index ef7fdb3..f66db3b 100644
--- a/pkg/trait/knative.go
+++ b/pkg/trait/knative.go
@@ -69,6 +69,8 @@ type knativeTrait struct {
        // that can be removed in a future version of Knative, filtering is 
enabled only when the integration is
        // listening from more than 1 channel.
        FilterSourceChannels *bool `property:"filter-source-channels"`
+       // Enables Knative CamelSource pre 0.15 compatibility fixes (will be 
removed in future versions).
+       CamelSourceCompat *bool `property:"camel-source-compat"`
        // Enable automatic discovery of all trait properties.
        Auto *bool `property:"auto"`
 }
@@ -173,6 +175,30 @@ func (t *knativeTrait) Configure(e *Environment) (bool, 
error) {
 }
 
 func (t *knativeTrait) Apply(e *Environment) error {
+       // To be removed when Knative CamelSources < 0.15 will no longer be 
supported
+       // Older versions of Knative Sources use a loader rather than an 
interceptor
+       if t.CamelSourceCompat == nil || *t.CamelSourceCompat {
+               for i, s := range e.Integration.Spec.Sources {
+                       if s.Loader == "knative-source" {
+                               s.Loader = ""
+                               util.StringSliceUniqueAdd(&s.Interceptors, 
"knative-source")
+                               e.Integration.Spec.Sources[i] = s
+                       }
+               }
+       }
+       // End of temporary code
+
+       if e.IntegrationInPhase(v1.IntegrationPhaseInitialization) {
+               // Interceptor may have been set by a Knative CamelSource
+               if util.StringSliceExists(e.getAllInterceptors(), 
"knative-source") {
+                       // Adding required libraries for Camel sources
+                       
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, 
"mvn:org.apache.camel.k/camel-knative")
+                       
util.StringSliceUniqueAdd(&e.Integration.Status.Dependencies, 
"mvn:org.apache.camel.k/camel-k-runtime-knative")
+                       // Adding platform HTTP
+                       
util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, 
v1.CapabilityPlatformHTTP)
+               }
+       }
+
        if len(t.ChannelSources) > 0 || len(t.EndpointSources) > 0 || 
len(t.EventSources) > 0 {
                util.StringSliceUniqueAdd(&e.Integration.Status.Capabilities, 
v1.CapabilityPlatformHTTP)
        }
diff --git a/pkg/trait/trait_types.go b/pkg/trait/trait_types.go
index 13a1ff5..457afcf 100644
--- a/pkg/trait/trait_types.go
+++ b/pkg/trait/trait_types.go
@@ -25,6 +25,7 @@ import (
        "strconv"
        "strings"
 
+       "github.com/apache/camel-k/pkg/util"
        corev1 "k8s.io/api/core/v1"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        controller "sigs.k8s.io/controller-runtime/pkg/client"
@@ -736,3 +737,15 @@ func (e *Environment) getIntegrationContainer() 
*corev1.Container {
 
        return e.Resources.GetContainerByName(containerName)
 }
+
+func (e *Environment) getAllInterceptors() []string {
+       res := make([]string, 0)
+       util.StringSliceUniqueConcat(&res, e.Interceptors)
+
+       if e.Integration != nil {
+               for _, s := range e.Integration.Sources() {
+                       util.StringSliceUniqueConcat(&res, s.Interceptors)
+               }
+       }
+       return res
+}
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 50ff567..34ac490 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -88,6 +88,16 @@ func StringSliceUniqueAdd(slice *[]string, item string) bool 
{
        return true
 }
 
+// StringSliceUniqueConcat append all the items of the "items" slice if they 
are not already present in the slice
+func StringSliceUniqueConcat(slice *[]string, items []string) bool {
+       for _, item := range items {
+               if !StringSliceUniqueAdd(slice, item) {
+                       return false
+               }
+       }
+       return true
+}
+
 // EncodeXML --
 func EncodeXML(content interface{}) ([]byte, error) {
        w := &bytes.Buffer{}

Reply via email to