Skip to content

Commit

Permalink
fix: CloudEvents content reflects correct emitter namespace and subje…
Browse files Browse the repository at this point in the history
…ct type (#5279)
  • Loading branch information
JorTurFer committed Dec 12, 2023
1 parent 646bb6b commit a2c21ec
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio

Here is an overview of all new **experimental** features:

- **General**: Emit CloudEvents on major KEDA events ([#3533](https://github.com/kedacore/keda/issues/3533))
- **General**: Emit CloudEvents on major KEDA events ([#3533](https://github.com/kedacore/keda/issues/3533)|[#5278](https://github.com/kedacore/keda/issues/5278))

### Improvements

Expand Down
9 changes: 7 additions & 2 deletions pkg/eventemitter/cloudevent_http_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/kedacore/keda/v2/pkg/eventemitter/eventdata"
"github.com/kedacore/keda/v2/pkg/util"
)

const (
cloudEventSourceType = "com.cloudeventsource.keda"
)

var (
kedaNamespace, _ = util.GetClusterObjectNamespace()
)

type CloudEventHTTPHandler struct {
ctx context.Context
logger logr.Logger
Expand Down Expand Up @@ -86,8 +91,8 @@ func (c *CloudEventHTTPHandler) CloseHandler() {
}

func (c *CloudEventHTTPHandler) EmitEvent(eventData eventdata.EventData, failureFunc func(eventData eventdata.EventData, err error)) {
source := "/" + c.clusterName + "/" + eventData.Namespace + "/keda"
subject := "/" + c.clusterName + "/" + eventData.Namespace + "/workload/" + eventData.ObjectName
source := fmt.Sprintf("/%s/%s/keda", c.clusterName, kedaNamespace)
subject := fmt.Sprintf("/%s/%s/%s/%s", c.clusterName, eventData.Namespace, eventData.ObjectType, eventData.ObjectName)

event := cloudevents.NewEvent()
event.SetSource(source)
Expand Down
1 change: 1 addition & 0 deletions pkg/eventemitter/eventdata/eventdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
type EventData struct {
Namespace string
ObjectName string
ObjectType string
EventType string
Reason string
Message string
Expand Down
6 changes: 4 additions & 2 deletions pkg/eventemitter/eventemitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,12 @@ func (e *EventEmitter) Emit(object runtime.Object, namesapce types.NamespacedNam
return
}

name, _ := meta.NewAccessor().Name(object)
objectName, _ := meta.NewAccessor().Name(object)
objectType, _ := meta.NewAccessor().Kind(object)
eventData := eventdata.EventData{
Namespace: namesapce.Namespace,
ObjectName: name,
ObjectName: strings.ToLower(objectName),
ObjectType: strings.ToLower(objectType),
EventType: eventType,
Reason: reason,
Message: message,
Expand Down
4 changes: 2 additions & 2 deletions tests/internals/cloudevent_source/cloudevent_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var (
cloudEventHTTPServiceName = fmt.Sprintf("%s-cloudevent-http-service", testName)
cloudEventHTTPServiceURL = fmt.Sprintf("http://%s.%s.svc.cluster.local:8899", cloudEventHTTPServiceName, namespace)
clusterName = "test-cluster"
expectedSubject = fmt.Sprintf("/%s/%s/workload/%s", clusterName, namespace, scaledObjectName)
expectedSource = fmt.Sprintf("/%s/%s/keda", clusterName, namespace)
expectedSubject = fmt.Sprintf("/%s/%s/scaledobject/%s", clusterName, namespace, scaledObjectName)
expectedSource = fmt.Sprintf("/%s/keda/keda", clusterName)
)

type templateData struct {
Expand Down

0 comments on commit a2c21ec

Please sign in to comment.