Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1654: enable sinkbinding automatically #1832

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deploy/resources.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion deploy/traits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ traits:
description: Enables Knative CamelSource pre 0.15 compatibility fixes (will be removed in future versions).
- name: sink-binding
type: bool
description: Allows binding the integration to a sink via a Knative SinkBinding resource.This can be used when the integration targets a single sink.It's disabled by default.
description: Allows binding the integration to a sink via a Knative SinkBinding resource.This can be used when the integration targets a single sink.It's enabled by default when the integration targets a single sink(except when the integration is owned by a Knative source).
- name: auto
type: bool
description: Enable automatic discovery of all trait properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8502,6 +8502,11 @@
"items": {
"description": "IntegrationCondition describes the state of a resource at a certain point.",
"properties": {
"firstTruthyTime": {
"description": "First time the condition status transitioned to True.",
"format": "date-time",
"type": "string"
},
"lastTransitionTime": {
"description": "Last time the condition transitioned from one status to another.",
"format": "date-time",
Expand Down Expand Up @@ -8690,6 +8695,11 @@
"kit": {
"type": "string"
},
"lastInitTimestamp": {
"description": "The timestamp representing the last time when this integration was initialized.",
"format": "date-time",
"type": "string"
},
"phase": {
"description": "IntegrationPhase --",
"type": "string"
Expand Down
3 changes: 2 additions & 1 deletion docs/modules/traits/pages/knative.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ listening from more than 1 channel.
| bool
| Allows binding the integration to a sink via a Knative SinkBinding resource.
This can be used when the integration targets a single sink.
It's disabled by default.
It's enabled by default when the integration targets a single sink
(except when the integration is owned by a Knative source).

| knative.auto
| bool
Expand Down
4 changes: 2 additions & 2 deletions e2e/yaks/common/apache-kamelet-catalog/kamelet.feature
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Feature: Camel K can run Kamelets from default catalog

Scenario: Integrations can use default catalog
Given integration logger is running
Then integration logger should print Camel K
Given Camel-K integration logger is running
Then Camel-K integration logger should print Camel K
2 changes: 1 addition & 1 deletion e2e/yaks/common/kamelet-binding/logger-sink-binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
source:
ref:
kind: InMemoryChannel
apiVersion: messaging.knative.dev/v1beta1
apiVersion: messaging.knative.dev/v1
name: messages
sink:
ref:
Expand Down
2 changes: 1 addition & 1 deletion e2e/yaks/common/kamelet-binding/messages-channel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
# ---------------------------------------------------------------------------

apiVersion: messaging.knative.dev/v1beta1
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
metadata:
name: messages
2 changes: 1 addition & 1 deletion e2e/yaks/common/kamelet-binding/timer-source-binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ spec:
sink:
ref:
kind: InMemoryChannel
apiVersion: messaging.knative.dev/v1beta1
apiVersion: messaging.knative.dev/v1
name: messages
2 changes: 1 addition & 1 deletion e2e/yaks/common/knative-sinkbinding/messages-channel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
# ---------------------------------------------------------------------------

apiVersion: messaging.knative.dev/v1beta1
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
metadata:
name: messages
2 changes: 1 addition & 1 deletion e2e/yaks/common/knative-sinkbinding/source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
- transform:
simple: "${body.toUpperCase()}"
- to: "log:info"
- to: "knative:channel/messages?apiVersion=messaging.knative.dev/v1beta1&kind=InMemoryChannel"
- to: "knative:channel/messages?apiVersion=messaging.knative.dev/v1&kind=InMemoryChannel"
24 changes: 23 additions & 1 deletion pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ type knativeTrait struct {
CamelSourceCompat *bool `property:"camel-source-compat" json:"camelSourceCompat,omitempty"`
// Allows binding the integration to a sink via a Knative SinkBinding resource.
// This can be used when the integration targets a single sink.
// It's disabled by default.
// It's enabled by default when the integration targets a single sink
// (except when the integration is owned by a Knative source).
SinkBinding *bool `property:"sink-binding" json:"sinkBinding,omitempty"`
// Enable automatic discovery of all trait properties.
Auto *bool `property:"auto" json:"auto,omitempty"`
Expand Down Expand Up @@ -195,6 +196,10 @@ func (t *knativeTrait) Configure(e *Environment) (bool, error) {
filter := true
t.FilterSourceChannels = &filter
}
if t.SinkBinding == nil {
allowed := t.isSinkBindingAllowed(e)
t.SinkBinding = &allowed
}
}

return true, nil
Expand Down Expand Up @@ -454,6 +459,23 @@ func (t *knativeTrait) configureEvents(e *Environment, env *knativeapi.CamelEnvi
return nil
}

func (t *knativeTrait) isSinkBindingAllowed(e *Environment) bool {
services := t.extractServices(t.ChannelSinks, knativeapi.CamelServiceTypeChannel)
services = append(services, t.extractServices(t.EndpointSinks, knativeapi.CamelServiceTypeEndpoint)...)
services = append(services, t.extractServices(t.EventSinks, knativeapi.CamelServiceTypeEvent)...)

if len(services) != 1 {
return false
}

for _, owner := range e.Integration.OwnerReferences {
if strings.Contains(owner.APIVersion, "sources.knative.dev") {
return false
}
}
return true
}

func (t *knativeTrait) configureSinkBinding(e *Environment, env *knativeapi.CamelEnvironment) error {
if t.SinkBinding == nil || !*t.SinkBinding {
return nil
Expand Down
10 changes: 1 addition & 9 deletions pkg/util/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ func TestBindings(t *testing.T) {
},
},
uri: "knative:endpoint/myservice?apiVersion=serving.knative.dev%2Fv1&kind=Service",
traits: asTraitSpec("knative", map[string]interface{}{
"sinkBinding": true,
}),
},
{
endpointType: v1alpha1.EndpointTypeSink,
Expand All @@ -67,9 +64,6 @@ func TestBindings(t *testing.T) {
}),
},
uri: "knative:endpoint/myservice?apiVersion=serving.knative.dev%2Fv1&ce.override.ce-type=mytype&kind=Service",
traits: asTraitSpec("knative", map[string]interface{}{
"sinkBinding": true,
}),
},
{
endpointType: v1alpha1.EndpointTypeSink,
Expand All @@ -81,9 +75,6 @@ func TestBindings(t *testing.T) {
},
},
uri: "knative:channel/mychannel?apiVersion=messaging.knative.dev%2Fv1&kind=Channel",
traits: asTraitSpec("knative", map[string]interface{}{
"sinkBinding": true,
}),
},
{
endpointType: v1alpha1.EndpointTypeSource,
Expand Down Expand Up @@ -155,6 +146,7 @@ func TestBindings(t *testing.T) {
},
uri: "knative:endpoint/sink?ce.override.ce-type=mytype",
traits: asTraitSpec("knative", map[string]interface{}{
"sinkBinding": false,
"configuration": asKnativeConfig("https://myurl/hey"),
}),
},
Expand Down
24 changes: 1 addition & 23 deletions pkg/util/bindings/knative_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ limitations under the License.
package bindings

import (
"encoding/json"
"errors"
"fmt"
"net/url"

v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
knativeapis "github.com/apache/camel-k/pkg/apis/camel/v1/knative"
"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
"github.com/apache/camel-k/pkg/util/knative"
Expand Down Expand Up @@ -91,28 +89,8 @@ func (k KnativeRefBindingProvider) Translate(ctx BindingContext, endpointType v1
}

serviceURI = uri.AppendParameters(serviceURI, props)

var traits map[string]v1.TraitSpec
if endpointType == v1alpha1.EndpointTypeSink {
knativeConfig := make(map[string]interface{})
// TODO remove this after making sinkbinding the default (https://github.com/apache/camel-k/issues/1654)
knativeConfig["sinkBinding"] = true
knativeConfigJSON, err := json.Marshal(knativeConfig)
if err != nil {
return nil, err
}
traits = map[string]v1.TraitSpec{
"knative": {
Configuration: v1.TraitConfiguration{
RawMessage: knativeConfigJSON,
},
},
}
}

return &Binding{
URI: serviceURI,
Traits: traits,
URI: serviceURI,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/util/bindings/knative_uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (k KnativeURIBindingProvider) Translate(ctx BindingContext, endpointType v1
return nil, err
}
knativeConfig["configuration"] = config
knativeConfig["sinkBinding"] = false
knativeConfigJSON, err := json.Marshal(knativeConfig)
if err != nil {
return nil, err
Expand Down