Skip to content

Commit

Permalink
Fix apache#1654: enable sinkbinding automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Nov 27, 2020
1 parent eb6a292 commit 337582d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion deploy/traits.yaml
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
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
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
24 changes: 23 additions & 1 deletion pkg/trait/knative.go
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, string) {
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, fmt.Sprintf("sinkbinding can only be used with a single sink: found %d sinks", len(services))
}

for _, owner := range e.Integration.OwnerReferences {
if strings.Contains(owner.APIVersion, "sources.knative.dev") {
return false, fmt.Sprintf("sinkbinding should not be enabled when the integration is owned by a Knative source: %s %s", owner.APIVersion, owner.Kind)
}
}
return true, ""
}

func (t *knativeTrait) configureSinkBinding(e *Environment, env *knativeapi.CamelEnvironment) error {
if t.SinkBinding == nil || !*t.SinkBinding {
return nil
Expand Down
9 changes: 0 additions & 9 deletions pkg/util/bindings/bindings_test.go
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
24 changes: 1 addition & 23 deletions pkg/util/bindings/knative_ref.go
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

0 comments on commit 337582d

Please sign in to comment.