This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch release-1.2.x in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 88cab28a9895ccc2a68f70df61868c089eb0d37f Author: nicolaferraro <ni.ferr...@gmail.com> AuthorDate: Fri Oct 23 12:07:58 2020 +0200 Fix #1774: use direct HTTP binding when Knative is not in use --- addons/strimzi/strimzi_test.go | 3 +++ deploy/resources.go | 4 +-- pkg/controller/kameletbinding/initialize.go | 39 +++++++++++++++++++++++++++++ pkg/util/bindings/api.go | 2 ++ pkg/util/bindings/bindings_test.go | 15 +++++++++++ pkg/util/bindings/knative_uri.go | 4 +++ 6 files changed, 65 insertions(+), 2 deletions(-) diff --git a/addons/strimzi/strimzi_test.go b/addons/strimzi/strimzi_test.go index 627283c..13fd4ed 100644 --- a/addons/strimzi/strimzi_test.go +++ b/addons/strimzi/strimzi_test.go @@ -22,6 +22,7 @@ import ( "encoding/json" "github.com/apache/camel-k/addons/strimzi/duck/v1beta1" "github.com/apache/camel-k/addons/strimzi/duck/v1beta1/client/internalclientset/fake" + camelv1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/util/bindings" "github.com/apache/camel-k/pkg/util/test" @@ -42,6 +43,7 @@ func TestStrimziDirect(t *testing.T) { Ctx: ctx, Client: client, Namespace: "test", + Profile: camelv1.TraitProfileKubernetes, } endpoint := v1alpha1.Endpoint{ @@ -102,6 +104,7 @@ func TestStrimziLookup(t *testing.T) { bindingContext := bindings.BindingContext{ Ctx: ctx, Namespace: "test", + Profile: camelv1.TraitProfileKubernetes, } endpoint := v1alpha1.Endpoint{ diff --git a/deploy/resources.go b/deploy/resources.go index bb299ae..cf92514 100644 --- a/deploy/resources.go +++ b/deploy/resources.go @@ -161,9 +161,9 @@ var assets = func() http.FileSystem { "/operator-deployment.yaml": &vfsgen۰CompressedFileInfo{ name: "operator-deployment.yaml", modTime: time.Time{}, - uncompressedSize: 2139, + uncompressedSize: 2148, - compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xbc\x54\x41\x6f\xe3\x36\x13\xbd\xeb\x57\x3c\x58\x97\x5d\x20\xb6\xbf\x7c\x47\xf5\xa4\x26\x0e\x56\x68\x2a\x1b\x96\xb7\xc1\x9e\x8a\x09\x35\x92\x88\x50\xa4\x4a\x52\xd1\xea\xdf\x17\x94\xed\xd8\xce\x66\xd3\x1e\x82\xf2\x24\x71\x86\x6f\xde\x9b\x37\x64\x8c\xf9\xc7\xad\x28\xc6\xbd\x14\xac\x1d\x97\xf0\x06\xbe\x61\xa4\x1d\x89\x86\x51\x98\xca\x0f\x64\x19\x77\xa6\xd7\x25\x79\x69\x34\x3e\xa5\xc5\xdd\x67\xf4\xba\x64\x0b\xa3\x19\xc6\x [...] + compressedContent: []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xbc\x54\x41\x6f\xe3\x36\x13\xbd\xeb\x57\x3c\x58\x97\x5d\x20\xb6\x37\xdf\x77\x53\x4f\x6a\xe2\x20\x46\x53\xc9\xb0\xbc\x0d\xf6\x54\x4c\xa8\x91\x44\x84\x22\x55\x92\x8a\x56\xff\xbe\xa0\x6c\x27\x76\x36\x9b\xf6\x10\x94\x27\x9b\x33\xf3\xe6\xbd\x79\x23\xc6\x98\x7f\xdc\x89\x62\xdc\x49\xc1\xda\x71\x09\x6f\xe0\x1b\x46\xda\x91\x68\x18\x85\xa9\xfc\x40\x96\x71\x63\x7a\x5d\x92\x97\x46\xe3\x53\x5a\xdc\x7c\x46\xaf\x4b\xb6\x30\x9a\x61\x [...] }, "/operator-role-binding-events.yaml": &vfsgen۰CompressedFileInfo{ name: "operator-role-binding-events.yaml", diff --git a/pkg/controller/kameletbinding/initialize.go b/pkg/controller/kameletbinding/initialize.go index d3874d6..8f794d6 100644 --- a/pkg/controller/kameletbinding/initialize.go +++ b/pkg/controller/kameletbinding/initialize.go @@ -24,11 +24,14 @@ import ( v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" + "github.com/apache/camel-k/pkg/platform" "github.com/apache/camel-k/pkg/util/bindings" + "github.com/apache/camel-k/pkg/util/knative" "github.com/apache/camel-k/pkg/util/kubernetes" "github.com/apache/camel-k/pkg/util/patch" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" @@ -75,10 +78,17 @@ func (action *initializeAction) Handle(ctx context.Context, kameletbinding *v1al it.Spec = *kameletbinding.Spec.Integration.DeepCopy() } + profile, err := action.determineProfile(ctx, kameletbinding) + if err != nil { + return nil, err + } + it.Spec.Profile = profile + bindingContext := bindings.BindingContext{ Ctx: ctx, Client: action.client, Namespace: it.Namespace, + Profile: profile, } from, err := bindings.Translate(bindingContext, v1alpha1.EndpointTypeSource, kameletbinding.Spec.Source) @@ -183,3 +193,32 @@ func (action *initializeAction) findIcon(ctx context.Context, binding *v1alpha1. } return kamelet.Annotations[v1alpha1.AnnotationIcon], nil } + +func (action *initializeAction) determineProfile(ctx context.Context, binding *v1alpha1.KameletBinding) (v1.TraitProfile, error) { + if binding.Spec.Integration != nil && binding.Spec.Integration.Profile != "" { + return binding.Spec.Integration.Profile, nil + } + pl, err := platform.GetCurrentPlatform(ctx, action.client, binding.Namespace) + if err != nil && !k8serrors.IsNotFound(err) { + return "", errors.Wrap(err, "error while retrieving the integration platform") + } + if pl != nil { + if pl.Status.Profile != "" { + return pl.Status.Profile, nil + } + if pl.Spec.Profile != "" { + return pl.Spec.Profile, nil + } + } + if knative.IsEnabledInNamespace(ctx, action.client, binding.Namespace) { + return v1.TraitProfileKnative, nil + } + if pl != nil { + // Determine profile from cluster type + plProfile := platform.GetProfile(pl) + if plProfile != "" { + return plProfile, nil + } + } + return v1.DefaultTraitProfile, nil +} diff --git a/pkg/util/bindings/api.go b/pkg/util/bindings/api.go index f07d8cc..05ea123 100644 --- a/pkg/util/bindings/api.go +++ b/pkg/util/bindings/api.go @@ -20,6 +20,7 @@ package bindings import ( "context" + v1 "github.com/apache/camel-k/pkg/apis/camel/v1" "github.com/apache/camel-k/pkg/apis/camel/v1alpha1" "github.com/apache/camel-k/pkg/client" @@ -53,4 +54,5 @@ type BindingContext struct { Ctx context.Context Client client.Client Namespace string + Profile v1.TraitProfile } diff --git a/pkg/util/bindings/bindings_test.go b/pkg/util/bindings/bindings_test.go index 385febf..266402a 100644 --- a/pkg/util/bindings/bindings_test.go +++ b/pkg/util/bindings/bindings_test.go @@ -36,6 +36,7 @@ func TestBindings(t *testing.T) { testcases := []struct { endpointType v1alpha1.EndpointType endpoint v1alpha1.Endpoint + profile camelv1.TraitProfile uri string traits map[string]camelv1.TraitSpec }{ @@ -160,6 +161,14 @@ func TestBindings(t *testing.T) { { endpointType: v1alpha1.EndpointTypeSink, endpoint: v1alpha1.Endpoint{ + URI: asStringPointer("https://myurl/hey"), + }, + profile: camelv1.TraitProfileKubernetes, + uri: "https://myurl/hey", + }, + { + endpointType: v1alpha1.EndpointTypeSink, + endpoint: v1alpha1.Endpoint{ URI: asStringPointer("docker://xxx"), }, uri: "docker://xxx", @@ -174,10 +183,16 @@ func TestBindings(t *testing.T) { client, err := test.NewFakeClient() assert.NoError(t, err) + profile := tc.profile + if profile == "" { + profile = camelv1.TraitProfileKnative + } + bindingContext := BindingContext{ Ctx: ctx, Client: client, Namespace: "test", + Profile: profile, } binding, err := Translate(bindingContext, tc.endpointType, tc.endpoint) diff --git a/pkg/util/bindings/knative_uri.go b/pkg/util/bindings/knative_uri.go index 88bdd0c..01cafa5 100644 --- a/pkg/util/bindings/knative_uri.go +++ b/pkg/util/bindings/knative_uri.go @@ -40,6 +40,10 @@ func (k KnativeURIBindingProvider) Translate(ctx BindingContext, endpointType v1 // works only on uris return nil, nil } + if ctx.Profile != v1.TraitProfileKnative { + // use cloudevent binding only in Knative trait profile + return nil, nil + } if !strings.HasPrefix(*e.URI, "http:") && !strings.HasPrefix(*e.URI, "https:") { // only translates http/https uri to Knative calls return nil, nil