Skip to content

Commit

Permalink
Fix apache#1986: use properties instead of URL encoding for Kamelets …
Browse files Browse the repository at this point in the history
…in bindings
  • Loading branch information
nicolaferraro committed Feb 2, 2021
1 parent f7d70f4 commit 636a564
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
19 changes: 19 additions & 0 deletions pkg/controller/kameletbinding/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package kameletbinding
import (
"context"
"encoding/json"
"fmt"
"sort"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
Expand Down Expand Up @@ -90,6 +92,23 @@ func createIntegrationFor(ctx context.Context, c client.Client, kameletbinding *
}
}

if len(from.ApplicationProperties) > 0 || len(to.ApplicationProperties) > 0 {
propList := make([]string, 0, len(from.ApplicationProperties)+len(to.ApplicationProperties))
for k, v := range from.ApplicationProperties {
propList = append(propList, fmt.Sprintf("%s=%s", k, v))
}
for k, v := range to.ApplicationProperties {
propList = append(propList, fmt.Sprintf("%s=%s", k, v))
}
sort.Strings(propList)
for _, p := range propList {
it.Spec.Configuration = append(it.Spec.Configuration, v1.ConfigurationSpec{
Type: "property",
Value: p,
})
}
}

flow := map[string]interface{}{
"from": map[string]interface{}{
"uri": from.URI,
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/bindings/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Binding struct {
URI string
// Traits is a partial trait specification that should be merged into the integration
Traits map[string]v1.TraitSpec
// ApplicationProperties contain properties that should be set on the integration for the binding to work
ApplicationProperties map[string]string
}

// BindingProvider maps a KameletBinding endpoint into Camel K resources
Expand Down
29 changes: 27 additions & 2 deletions pkg/util/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestBindings(t *testing.T) {
profile camelv1.TraitProfile
uri string
traits map[string]camelv1.TraitSpec
props map[string]string
}{
{
endpointType: v1alpha1.EndpointTypeSink,
Expand Down Expand Up @@ -113,16 +114,18 @@ func TestBindings(t *testing.T) {
uri: "knative:event/myeventtype?apiVersion=eventing.knative.dev%2Fv1beta1&kind=Broker",
},
{
endpointType: v1alpha1.EndpointTypeSource,
endpoint: v1alpha1.Endpoint{
Ref: &corev1.ObjectReference{
Kind: "Kamelet",
APIVersion: "camel.apache.org/v1any1",
Name: "mykamelet",
},
},
uri: "kamelet:mykamelet",
uri: "kamelet:mykamelet/source",
},
{
endpointType: v1alpha1.EndpointTypeSink,
endpoint: v1alpha1.Endpoint{
Ref: &corev1.ObjectReference{
Kind: "Kamelet",
Expand All @@ -134,7 +137,28 @@ func TestBindings(t *testing.T) {
"encodedkey?": "encoded=val",
}),
},
uri: "kamelet:mykamelet?encodedkey%3F=encoded%3Dval&mymessage=myval",
uri: "kamelet:mykamelet/sink",
props: map[string]string{
"camel.kamelet.mykamelet.sink.encodedkey?": "encoded=val",
"camel.kamelet.mykamelet.sink.mymessage": "myval",
},
},
{
endpoint: v1alpha1.Endpoint{
Ref: &corev1.ObjectReference{
Kind: "Kamelet",
APIVersion: "camel.apache.org/v1any1",
Name: "mykamelet",
},
Properties: asEndpointProperties(map[string]string{
"id": "myid?",
"mymessage": "myval",
}),
},
uri: "kamelet:mykamelet/myid%3F",
props: map[string]string{
"camel.kamelet.mykamelet.myid?.mymessage": "myval",
},
},
{
endpoint: v1alpha1.Endpoint{
Expand Down Expand Up @@ -206,6 +230,7 @@ func TestBindings(t *testing.T) {
assert.NotNil(t, binding)
assert.Equal(t, tc.uri, binding.URI)
assert.Equal(t, tc.traits, binding.Traits)
assert.Equal(t, tc.props, binding.ApplicationProperties)
})
}
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/util/bindings/kamelet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"net/url"

"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/uri"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -44,22 +43,35 @@ func (k KameletBindingProvider) Translate(ctx BindingContext, endpointType v1alp
}
// it translates only Kamelet refs
if e.Ref.Kind == v1alpha1.KameletKind && gv.Group == v1alpha1.SchemeGroupVersion.Group {
kameletURI := fmt.Sprintf("kamelet:%s", url.PathEscape(e.Ref.Name))
kameletName := url.PathEscape(e.Ref.Name)
kameletURI := fmt.Sprintf("kamelet:%s", kameletName)

props, err := e.Properties.GetPropertyMap()
if err != nil {
return nil, err
}

if id, ok := props[v1alpha1.KameletIDProperty]; ok && id != "" {
id, idPresent := props[v1alpha1.KameletIDProperty]
if idPresent {
delete(props, v1alpha1.KameletIDProperty)
kameletURI = fmt.Sprintf("%s/%s", kameletURI, url.PathEscape(id))
} else {
// Let's use literal "source" or "sink" as ID for the Kamelet
id = string(endpointType)
}
kameletURI = fmt.Sprintf("%s/%s", kameletURI, url.PathEscape(id))

kameletURI = uri.AppendParameters(kameletURI, props)
var applicationProperties map[string]string
if len(props) > 0 {
applicationProperties = make(map[string]string, len(props))
for k, v := range props {
propKey := fmt.Sprintf("camel.kamelet.%s.%s.%s", kameletName, id, k)
applicationProperties[propKey] = v
}
}

return &Binding{
URI: kameletURI,
URI: kameletURI,
ApplicationProperties: applicationProperties,
}, nil
}
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions script/gen_release_notes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ if [ "$#" -ne 2 ]; then
fi

location=$(dirname $0)
last_tag=$1
new_tag=$2
last_tag=v$1
new_tag=v$2

echo "Generating release notes for version $new_tag starting from tag $last_tag"

Expand Down

0 comments on commit 636a564

Please sign in to comment.