From d4fa0f5963294da2af3f81ed03672a29e5f68a65 Mon Sep 17 00:00:00 2001 From: Claudio Miranda Date: Mon, 10 Jan 2022 23:43:09 -0400 Subject: [PATCH] fix(knative): Make knative broker name configurable https://github.com/apache/camel-k/issues/2864 * Add e2e knative tests * Change the kamelet-binding-broker yaks test to use a custom broker name --- .../files/knative_broker_receiver.groovy | 22 +++++++++++++++ .../files/knative_broker_sender.groovy | 24 +++++++++++++++++ e2e/knative/knative_test.go | 27 ++++++++++++++----- e2e/support/test_support.go | 16 +++++++++++ .../kamelet-binding-broker/kamelet.feature | 4 +-- .../logger-sink-binding.yaml | 2 +- .../kamelet-binding-broker/sample-broker.yaml | 4 +++ .../timer-source-binding.yaml | 4 +-- .../kamelet-binding-broker/yaks-config.yaml | 5 +--- examples/kamelets/timer-source/usage.groovy | 0 pkg/apis/camel/v1/knative/types.go | 1 + pkg/apis/camel/v1/knative/types_support.go | 1 + pkg/trait/knative.go | 1 + pkg/util/bindings/bindings_test.go | 2 +- pkg/util/bindings/knative_ref.go | 9 +++---- 15 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 e2e/knative/files/knative_broker_receiver.groovy create mode 100644 e2e/knative/files/knative_broker_sender.groovy create mode 100644 e2e/yaks/common/kamelet-binding-broker/sample-broker.yaml mode change 100755 => 100644 examples/kamelets/timer-source/usage.groovy diff --git a/e2e/knative/files/knative_broker_receiver.groovy b/e2e/knative/files/knative_broker_receiver.groovy new file mode 100644 index 0000000000..f88c5325cd --- /dev/null +++ b/e2e/knative/files/knative_broker_receiver.groovy @@ -0,0 +1,22 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +from('knative:event/hello.1?name=sample-broker') + .log('Received 1: ${body}') + +from('knative:event/hello.2?name=sample-broker') + .log('Received 2: ${body}') diff --git a/e2e/knative/files/knative_broker_sender.groovy b/e2e/knative/files/knative_broker_sender.groovy new file mode 100644 index 0000000000..04345d8aae --- /dev/null +++ b/e2e/knative/files/knative_broker_sender.groovy @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +from('timer:tick') + .setBody().constant("Hello 1 from sample-broker") + .to('knative:event/hello.1?name=sample-broker') + +from('timer:tick') + .setBody().constant("Hello 2 from sample-broker") + .to('knative:event/hello.2?name=sample-broker') diff --git a/e2e/knative/knative_test.go b/e2e/knative/knative_test.go index 7d7c780dce..293aad1228 100644 --- a/e2e/knative/knative_test.go +++ b/e2e/knative/knative_test.go @@ -107,13 +107,26 @@ func TestRunMultiChannelChain(t *testing.T) { func TestRunBroker(t *testing.T) { WithNewTestNamespaceWithKnativeBroker(t, func(ns string) { Expect(Kamel("install", "-n", ns, "--trait-profile", "knative").Execute()).To(Succeed()) - Expect(Kamel("run", "-n", ns, "files/knativeevt1.groovy").Execute()).To(Succeed()) - Expect(Kamel("run", "-n", ns, "files/knativeevt2.groovy").Execute()).To(Succeed()) - Eventually(IntegrationPodPhase(ns, "knativeevt1"), TestTimeoutLong).Should(Equal(v1.PodRunning)) - Eventually(IntegrationPodPhase(ns, "knativeevt2"), TestTimeoutLong).Should(Equal(v1.PodRunning)) - Eventually(IntegrationLogs(ns, "knativeevt2"), TestTimeoutMedium).Should(ContainSubstring("Received 1: Hello 1")) - Eventually(IntegrationLogs(ns, "knativeevt2"), TestTimeoutMedium).Should(ContainSubstring("Received 2: Hello 2")) - Eventually(IntegrationLogs(ns, "knativeevt2")).ShouldNot(ContainSubstring("Received 1: Hello 2")) + + t.Run("run knative eventing with default broker", func(t *testing.T) { + Expect(Kamel("run", "-n", ns, "files/knativeevt1.groovy").Execute()).To(Succeed()) + Expect(Kamel("run", "-n", ns, "files/knativeevt2.groovy").Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, "knativeevt1"), TestTimeoutLong).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns, "knativeevt2"), TestTimeoutLong).Should(Equal(v1.PodRunning)) + Eventually(IntegrationLogs(ns, "knativeevt2"), TestTimeoutMedium).Should(ContainSubstring("Received 1: Hello 1")) + Eventually(IntegrationLogs(ns, "knativeevt2"), TestTimeoutMedium).Should(ContainSubstring("Received 2: Hello 2")) + Eventually(IntegrationLogs(ns, "knativeevt2")).ShouldNot(ContainSubstring("Received 1: Hello 2")) + }) + + t.Run("run knative eventing with custom broker name", func(t *testing.T) { + Expect(CreateKnativeBroker(ns, "sample-broker")()).To(Succeed()) + Expect(Kamel("run", "-n", ns, "files/knative_broker_sender.groovy").Execute()).To(Succeed()) + Expect(Kamel("run", "-n", ns, "files/knative_broker_receiver.groovy").Execute()).To(Succeed()) + Eventually(IntegrationPodPhase(ns, "knative-broker-sender"), TestTimeoutLong).Should(Equal(v1.PodRunning)) + Eventually(IntegrationPodPhase(ns, "knative-broker-receiver"), TestTimeoutLong).Should(Equal(v1.PodRunning)) + Eventually(IntegrationLogs(ns, "knative-broker-receiver"), TestTimeoutMedium).Should(ContainSubstring("Received 1: Hello 1 from sample-broker")) + Eventually(IntegrationLogs(ns, "knative-broker-receiver"), TestTimeoutMedium).Should(ContainSubstring("Received 2: Hello 2 from sample-broker")) + }) Expect(Kamel("delete", "--all", "-n", ns).Execute()).To(Succeed()) }) } diff --git a/e2e/support/test_support.go b/e2e/support/test_support.go index e1e87ec2c6..5f694026e6 100644 --- a/e2e/support/test_support.go +++ b/e2e/support/test_support.go @@ -1568,6 +1568,22 @@ func CreateKnativeChannel(ns string, name string) func() error { } } +func CreateKnativeBroker(ns string, name string) func() error { + return func() error { + broker := eventing.Broker{ + TypeMeta: metav1.TypeMeta{ + Kind: "Broker", + APIVersion: eventing.SchemeGroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Namespace: ns, + Name: name, + }, + } + return TestClient().Create(TestContext, &broker) + } +} + /* Kamelets */ diff --git a/e2e/yaks/common/kamelet-binding-broker/kamelet.feature b/e2e/yaks/common/kamelet-binding-broker/kamelet.feature index 539416ebfb..c4d10c636e 100644 --- a/e2e/yaks/common/kamelet-binding-broker/kamelet.feature +++ b/e2e/yaks/common/kamelet-binding-broker/kamelet.feature @@ -5,6 +5,6 @@ Feature: Camel K can bind Kamelets to the broker | maxAttempts | 40 | | delayBetweenAttempts | 3000 | - Scenario: Sending event to the broker with KameletBinding + Scenario: Sending event to the custom broker with KameletBinding Given Camel K integration logger-sink-binding is running - Then Camel K integration logger-sink-binding should print message: Hello Custom Event + Then Camel K integration logger-sink-binding should print message: Hello Custom Event from sample-broker diff --git a/e2e/yaks/common/kamelet-binding-broker/logger-sink-binding.yaml b/e2e/yaks/common/kamelet-binding-broker/logger-sink-binding.yaml index e4b17b98d3..f155cb71a7 100644 --- a/e2e/yaks/common/kamelet-binding-broker/logger-sink-binding.yaml +++ b/e2e/yaks/common/kamelet-binding-broker/logger-sink-binding.yaml @@ -24,7 +24,7 @@ spec: ref: kind: Broker apiVersion: eventing.knative.dev/v1 - name: default + name: sample-broker properties: type: custom-type sink: diff --git a/e2e/yaks/common/kamelet-binding-broker/sample-broker.yaml b/e2e/yaks/common/kamelet-binding-broker/sample-broker.yaml new file mode 100644 index 0000000000..1e04f0d470 --- /dev/null +++ b/e2e/yaks/common/kamelet-binding-broker/sample-broker.yaml @@ -0,0 +1,4 @@ +apiVersion: eventing.knative.dev/v1 +kind: Broker +metadata: + name: sample-broker diff --git a/e2e/yaks/common/kamelet-binding-broker/timer-source-binding.yaml b/e2e/yaks/common/kamelet-binding-broker/timer-source-binding.yaml index 564800825e..d14bd263c3 100644 --- a/e2e/yaks/common/kamelet-binding-broker/timer-source-binding.yaml +++ b/e2e/yaks/common/kamelet-binding-broker/timer-source-binding.yaml @@ -26,12 +26,12 @@ spec: apiVersion: camel.apache.org/v1alpha1 name: timer-source properties: - message: Hello Custom Event + message: Hello Custom Event from sample-broker period: 1000 sink: ref: kind: Broker apiVersion: eventing.knative.dev/v1 - name: default + name: sample-broker properties: type: custom-type diff --git a/e2e/yaks/common/kamelet-binding-broker/yaks-config.yaml b/e2e/yaks/common/kamelet-binding-broker/yaks-config.yaml index e1e609f079..c96d7d4fa4 100644 --- a/e2e/yaks/common/kamelet-binding-broker/yaks-config.yaml +++ b/e2e/yaks/common/kamelet-binding-broker/yaks-config.yaml @@ -21,12 +21,9 @@ config: pre: - name: installation run: | - # One of the two labels should work - oc label namespace $YAKS_NAMESPACE eventing.knative.dev/injection=enabled - oc label namespace $YAKS_NAMESPACE knative-eventing-injection=enabled - kamel install -n $YAKS_NAMESPACE --operator-env-vars KAMEL_INSTALL_DEFAULT_KAMELETS=false + kubectl apply -f sample-broker.yaml -n $YAKS_NAMESPACE kubectl apply -f timer-source.kamelet.yaml -n $YAKS_NAMESPACE kubectl apply -f logger-sink.kamelet.yaml -n $YAKS_NAMESPACE diff --git a/examples/kamelets/timer-source/usage.groovy b/examples/kamelets/timer-source/usage.groovy old mode 100755 new mode 100644 diff --git a/pkg/apis/camel/v1/knative/types.go b/pkg/apis/camel/v1/knative/types.go index dcf293b459..5ed1ad2144 100644 --- a/pkg/apis/camel/v1/knative/types.go +++ b/pkg/apis/camel/v1/knative/types.go @@ -75,6 +75,7 @@ const ( CamelMetaKnativeKind = "knative.kind" CamelMetaKnativeAPIVersion = "knative.apiVersion" CamelMetaKnativeReply = "knative.reply" + CamelMetaKnativeName = "knative.name" CamelMetaEndpointKind = "camel.endpoint.kind" diff --git a/pkg/apis/camel/v1/knative/types_support.go b/pkg/apis/camel/v1/knative/types_support.go index f98677436d..c14f386399 100644 --- a/pkg/apis/camel/v1/knative/types_support.go +++ b/pkg/apis/camel/v1/knative/types_support.go @@ -34,6 +34,7 @@ func BuildCamelServiceDefinition(name string, endpointKind CamelEndpointKind, se CamelMetaEndpointKind: string(endpointKind), CamelMetaKnativeAPIVersion: apiVersion, CamelMetaKnativeKind: kind, + CamelMetaKnativeName: name, }, } diff --git a/pkg/trait/knative.go b/pkg/trait/knative.go index f3b611bb05..23eb6a8d57 100644 --- a/pkg/trait/knative.go +++ b/pkg/trait/knative.go @@ -389,6 +389,7 @@ func (t *knativeTrait) configureEvents(e *Environment, env *knativeapi.CamelEnvi knativeapi.CamelMetaEndpointKind: string(knativeapi.CamelEndpointKindSource), knativeapi.CamelMetaKnativeAPIVersion: ref.APIVersion, knativeapi.CamelMetaKnativeKind: ref.Kind, + knativeapi.CamelMetaKnativeName: ref.Name, knativeapi.CamelMetaKnativeReply: "false", }, } diff --git a/pkg/util/bindings/bindings_test.go b/pkg/util/bindings/bindings_test.go index f9c101746c..ec13d24951 100644 --- a/pkg/util/bindings/bindings_test.go +++ b/pkg/util/bindings/bindings_test.go @@ -122,7 +122,7 @@ func TestBindings(t *testing.T) { "type": "myeventtype", }), }, - uri: "knative:event/myeventtype?apiVersion=eventing.knative.dev%2Fv1&kind=Broker", + uri: "knative:event/myeventtype?apiVersion=eventing.knative.dev%2Fv1&kind=Broker&name=default", }, { endpointType: v1alpha1.EndpointTypeSource, diff --git a/pkg/util/bindings/knative_ref.go b/pkg/util/bindings/knative_ref.go index 90ee357ef6..a7ba2accdb 100644 --- a/pkg/util/bindings/knative_ref.go +++ b/pkg/util/bindings/knative_ref.go @@ -68,12 +68,9 @@ func (k KnativeRefBindingProvider) Translate(ctx BindingContext, endpointCtx End var serviceURI string if *serviceType == knativeapis.CamelServiceTypeEvent { - // TODO enable this when the runtime will support changing the broker name (https://github.com/apache/camel-k-runtime/issues/535) - /* - if props["name"] == "" { - props["name"] = e.Ref.Name - } - */ + if props["name"] == "" { + props["name"] = e.Ref.Name + } if eventType, ok := props["type"]; ok { // consume prop delete(props, "type")