Skip to content

Commit

Permalink
Fix apache#1774: use direct HTTP binding when Knative is not in use
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Oct 26, 2020
1 parent 37aebfc commit 33ccb18
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions addons/strimzi/strimzi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,6 +43,7 @@ func TestStrimziDirect(t *testing.T) {
Ctx: ctx,
Client: client,
Namespace: "test",
Profile: camelv1.TraitProfileKubernetes,
}

endpoint := v1alpha1.Endpoint{
Expand Down Expand Up @@ -102,6 +104,7 @@ func TestStrimziLookup(t *testing.T) {
bindingContext := bindings.BindingContext{
Ctx: ctx,
Namespace: "test",
Profile: camelv1.TraitProfileKubernetes,
}

endpoint := v1alpha1.Endpoint{
Expand Down
4 changes: 2 additions & 2 deletions deploy/resources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions pkg/controller/kameletbinding/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions pkg/util/bindings/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -53,4 +54,5 @@ type BindingContext struct {
Ctx context.Context
Client client.Client
Namespace string
Profile v1.TraitProfile
}
15 changes: 15 additions & 0 deletions pkg/util/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}{
Expand Down Expand Up @@ -157,6 +158,14 @@ func TestBindings(t *testing.T) {
"configuration": asKnativeConfig("https://myurl/hey"),
}),
},
{
endpointType: v1alpha1.EndpointTypeSink,
endpoint: v1alpha1.Endpoint{
URI: asStringPointer("https://myurl/hey"),
},
profile: camelv1.TraitProfileKubernetes,
uri: "https://myurl/hey",
},
{
endpointType: v1alpha1.EndpointTypeSink,
endpoint: v1alpha1.Endpoint{
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/bindings/knative_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 33ccb18

Please sign in to comment.