spring-cloud-function
spring-cloud-function copied to clipboard
Functions with CloudEvent and KNative cause events on response
I'm trying out spring-cloud-function with a KNative installation. I have simple spring boot application with the following configuration:
@Configuration
class CloudEventConfiguration {
private val logger = KotlinLogging.logger {}
@Bean
fun log(): (Message<String>) -> Unit = { input ->
logger.error { input }
}
}
spring:
cloud:
function:
web:
path: /events
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: spring-trigger
namespace: knative-eventing
annotations:
knative-eventing-injection: enabled
spec:
broker: rabbitmq-broker
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: spring
namespace: default
uri: /events/hire
When I set up a KNative trigger to call /events/log with a cloud event, the response creates a new cloud event to be triggered with an empty payload:
< 202 ACCEPTED Accepted
< webtestclient-request-id: [1]
< ce-id: [12345]
< ce-specversion: [1.0]
< ce-type: [io.spring.event]
< ce-source: [https://spring.io/events]
< uri: [/events/log]
This seems to cause an infinite loop, since it's the same message headers, creating the same message with no payload. I'm not sure if I'm doing something incorrect here or if it's not the intended use case for spring-cloud-function. If I create an endpoint, then the event propagation doesn't occur (since there are no ce headers):
@Bean
fun router(): RouterFunction<ServerResponse> = coRouter {
POST("/events/log") { req ->
val body = req.awaitBody<String>()
logger.error { body }
accepted().buildAndAwait()
}
}