Skip to content

Commit

Permalink
fix(knative): Make knative broker name configurable
Browse files Browse the repository at this point in the history
apache#2864

* Add e2e knative tests
* Change the kamelet-binding-broker yaks test to use a custom broker
  name
  • Loading branch information
claudio4j committed Jun 20, 2022
1 parent d71cddf commit d4fa0f5
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 23 deletions.
22 changes: 22 additions & 0 deletions e2e/knative/files/knative_broker_receiver.groovy
Original file line number Diff line number Diff line change
@@ -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}')
24 changes: 24 additions & 0 deletions e2e/knative/files/knative_broker_sender.groovy
Original file line number Diff line number Diff line change
@@ -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')
27 changes: 20 additions & 7 deletions e2e/knative/knative_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
Expand Down
16 changes: 16 additions & 0 deletions e2e/support/test_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions e2e/yaks/common/kamelet-binding-broker/kamelet.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ spec:
ref:
kind: Broker
apiVersion: eventing.knative.dev/v1
name: default
name: sample-broker
properties:
type: custom-type
sink:
Expand Down
4 changes: 4 additions & 0 deletions e2e/yaks/common/kamelet-binding-broker/sample-broker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: sample-broker
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions e2e/yaks/common/kamelet-binding-broker/yaks-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file modified examples/kamelets/timer-source/usage.groovy
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions pkg/apis/camel/v1/knative/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const (
CamelMetaKnativeKind = "knative.kind"
CamelMetaKnativeAPIVersion = "knative.apiVersion"
CamelMetaKnativeReply = "knative.reply"
CamelMetaKnativeName = "knative.name"

CamelMetaEndpointKind = "camel.endpoint.kind"

Expand Down
1 change: 1 addition & 0 deletions pkg/apis/camel/v1/knative/types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func BuildCamelServiceDefinition(name string, endpointKind CamelEndpointKind, se
CamelMetaEndpointKind: string(endpointKind),
CamelMetaKnativeAPIVersion: apiVersion,
CamelMetaKnativeKind: kind,
CamelMetaKnativeName: name,
},
}

Expand Down
1 change: 1 addition & 0 deletions pkg/trait/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/bindings/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions pkg/util/bindings/knative_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d4fa0f5

Please sign in to comment.