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 22, 2020
1 parent 61a60e0 commit 05b1f8a
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion deploy/resources.go

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

24 changes: 24 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,16 @@ func (action *initializeAction) Handle(ctx context.Context, kameletbinding *v1al
it.Spec = *kameletbinding.Spec.Integration.DeepCopy()
}

profile, err := action.getProfile(ctx, kameletbinding)
if err != nil {
return nil, err
}

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 +192,18 @@ func (action *initializeAction) findIcon(ctx context.Context, binding *v1alpha1.
}
return kamelet.Annotations[v1alpha1.AnnotationIcon], nil
}

func (action *initializeAction) getProfile(ctx context.Context, binding *v1alpha1.KameletBinding) (v1.TraitProfile, error) {
pl, err := platform.GetCurrentPlatform(ctx, action.client, binding.Namespace)
if err != nil && k8serrors.IsNotFound(err) {
if knative.IsEnabledInNamespace(ctx, action.client, binding.Namespace) {
return v1.TraitProfileKnative, nil
}
// No need to distinguish between openshift and kubernetes, but OpenShift is likely to use automatic platform creation
return v1.TraitProfileOpenShift, nil

} else if err != nil {
return "", errors.Wrap(err, "error while retrieving the integration platform")
}
return pl.Status.Profile, 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 05b1f8a

Please sign in to comment.