Skip to content

Commit

Permalink
Copy additional information as extensions of CE in APIServerSource (#…
Browse files Browse the repository at this point in the history
…3741)

* Copy resource name as an attribute of CE

* fix UT

* fix format

* Add kind and namespace
  • Loading branch information
danyinggu authored Jul 28, 2020
1 parent e7ca751 commit aa01ce6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/adapter/apiserver/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func MakeDeleteEvent(source string, obj interface{}, ref bool) (cloudevents.Even
object := obj.(*unstructured.Unstructured)
var data interface{}
var eventType string

if ref {
data = getRef(object)
eventType = sourcesv1beta1.ApiServerSourceDeleteRefEventType
Expand All @@ -93,18 +94,24 @@ func getRef(object *unstructured.Unstructured) corev1.ObjectReference {
}

func makeEvent(source, eventType string, obj *unstructured.Unstructured, data interface{}) (cloudevents.Event, error) {
resourceName := obj.GetName()
kind := obj.GetKind()
namespace := obj.GetNamespace()
subject := createSelfLink(corev1.ObjectReference{
APIVersion: obj.GetAPIVersion(),
Kind: obj.GetKind(),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
Kind: kind,
Name: resourceName,
Namespace: namespace,
})

event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetType(eventType)
event.SetSource(source)
event.SetSubject(subject)

// We copy the resource kind, name and namespace as extensions so that triggers can do the filter based on these attributes
event.SetExtension("kind", kind)
event.SetExtension("name", resourceName)
event.SetExtension("namespace", namespace)
if err := event.SetData(cloudevents.ApplicationJSON, data); err != nil {
return event, err
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/adapter/apiserver/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func TestMakeAddEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"apiVersion":"v1","kind":"Pod","metadata":{"name":"unit","namespace":"test"}}`,
Expand Down Expand Up @@ -108,6 +113,11 @@ func TestMakeUpdateEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"apiVersion":"v1","kind":"Pod","metadata":{"name":"unit","namespace":"test"}}`,
Expand Down Expand Up @@ -144,6 +154,11 @@ func TestMakeDeleteEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"apiVersion":"v1","kind":"Pod","metadata":{"name":"unit","namespace":"test"}}`,
Expand Down Expand Up @@ -180,6 +195,11 @@ func TestMakeAddRefEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"kind":"Pod","namespace":"test","name":"unit","apiVersion":"v1"}`,
Expand Down Expand Up @@ -216,6 +236,11 @@ func TestMakeUpdateRefEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"kind":"Pod","namespace":"test","name":"unit","apiVersion":"v1"}`,
Expand Down Expand Up @@ -252,6 +277,11 @@ func TestMakeDeleteRefEvent(t *testing.T) {
Source: *cloudevents.ParseURIRef("unit-test"),
Subject: simpleSubject("unit", "test"),
DataContentType: &contentType,
Extensions: map[string]interface{}{
"kind": "Pod",
"name": "unit",
"namespace": "test",
},
}.AsV1(),
},
wantData: `{"kind":"Pod","namespace":"test","name":"unit","apiVersion":"v1"}`,
Expand Down

0 comments on commit aa01ce6

Please sign in to comment.