spring-cloud / spring-cloud-function

Apache License 2.0
1.04k stars 617 forks source link

Functions with CloudEvent and KNative cause events on response #1091

Open cldfzn opened 11 months ago

cldfzn commented 11 months ago

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()
        }
    }
github-actions[bot] commented 1 day ago

This issue has been stale for over 60 days