Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CloudEvents content reflects correct emitter namespace and subject type #5279

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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