notification-controller icon indicating copy to clipboard operation
notification-controller copied to clipboard

Add new notifier to send CloudEvent / CDEvent

Open bradmccoydev opened this issue 2 years ago • 4 comments

This is a feature request to add the ability to send a CloudEvent / CDEvent from a flux notification to enable better interoperability between tools such as Keptn or Tekton for example.

This issue relates to #195 we can either update the forwarder notifier or create a new notifier for events.

The easiest and most likely accepted way of doing this is as follows:

  1. Extend the Provider secret to contain a Body Template for the HTTP request. As we are already getting the secret.Data it is easy to reference the "body" and pull it the same way we get the headers and address.

  2. Now that we have the Body template we need the key-value pairs of the metadata we require to substitute the template. This can be passed through the Provider.Metadata.Labels, as we are already pulling the provider object and Labels, is a mandatory field that can be used for the key values.

  3. Use basic go text/template to switch out these values in the body from the secret.

Please find below an example

apiVersion: v1
kind: Secret
metadata:
  name: keptn-flux-integration
  namespace: flux-system
stringData:
  address: http://keptn-flux-integration.flux-system.svc.cluster.local:80
  headers: |
     Content-Type: application/json
     x-token: 
  body: |
    {
      "data": {
        "configurationChange": {},
        "labels": {
          "servicename": "{{.keptnService}}"
        },
        "project": "{{.keptnProject}}",
        "service": "{{.keptnService}}",
        "stage": "{{.keptnStage}}"
      },
      "source": "flux",
      "specversion": "1.0",
      "type": "{{.keptnType}}",
      "shkeptnspecversion": "0.2.3"
    }
apiVersion: notification.toolkit.fluxcd.io/v1beta1
kind: Provider
metadata:
  name: keptn-flux-integration
  namespace: flux-system
  labels:
    keptnProject: gsoc
    keptnService: podtato-head
    keptnStage: qa
    keptnType: sh.keptn.event.qa.delivery.triggered
spec:
  type: generic
  address: http://keptn-flux-integration.flux-system.svc.cluster.local:80
  secretRef:
    name: keptn-flux-integration`
var template string
	for k, v := range secret.Data {
		switch k {
		case "body":
			template = string(v)
		case "headers":
			headers := make(map[interface{}]interface{})
			err := yaml.Unmarshal(v, &headers)

			if err != nil {
				log.Errorf("Error parsing headers: %s", err)
			}

			keptnEvent.Headers = headers
		}
	}

	tmpl, err := texttemplate.New("labels").Parse(template)

I'm happy to do a PR to demonstrate this if it will be accepted. Thanks

bradmccoydev avatar Jul 28 '22 19:07 bradmccoydev

Notes from Flux community meeting 11-Aug. We will investigate alternative ways of doing this and look into transforming a Kubernetes event into a Cloudevent and how we add our custom metadata. @bradmccoydev to investigate further, and see if anyone else is doing this.

bradmccoydev avatar Aug 11 '22 15:08 bradmccoydev

Hey @bradmccoydev, did you get to take a look at this? I'd be happy to pair up on this effort. Find me on Slack as @max.

makkes avatar Oct 28 '22 08:10 makkes

Hey @makkes sorry I haven't yet, it got stale, but still very interested. That sounds great I will reach out, is that CNCF slack right?

bradmccoydev avatar Oct 30 '22 23:10 bradmccoydev

is that CNCF slack right?

yes, it is.

makkes avatar Nov 14 '22 11:11 makkes