diff --git a/pkg/reconciler/events/cloudevent/cloudevent.go b/pkg/reconciler/events/cloudevent/cloudevent.go index 3883bf76276..1ae7ac411db 100644 --- a/pkg/reconciler/events/cloudevent/cloudevent.go +++ b/pkg/reconciler/events/cloudevent/cloudevent.go @@ -95,7 +95,18 @@ func EventForObjectWithCondition(runObject objectWithCondition) (*cloudevents.Ev event := cloudevents.NewEvent() event.SetID(uuid.New().String()) event.SetSubject(runObject.GetObjectMeta().GetName()) - event.SetSource(runObject.GetObjectMeta().GetSelfLink()) // TODO: SelfLink is deprecated https://github.com/tektoncd/pipeline/issues/2676 + // TODO: SelfLink is deprecated https://github.com/tektoncd/pipeline/issues/2676 + source := runObject.GetObjectMeta().GetSelfLink() + if source == "" { + gvk := runObject.GetObjectKind().GroupVersionKind() + source = fmt.Sprintf("/apis/%s/%s/namespaces/%s/%s/%s", + gvk.Group, + gvk.Version, + runObject.GetObjectMeta().GetNamespace(), + gvk.Kind, + runObject.GetObjectMeta().GetName()) + } + event.SetSource(source) eventType, err := getEventType(runObject) if err != nil { return nil, err diff --git a/pkg/reconciler/events/cloudevent/cloudevent_test.go b/pkg/reconciler/events/cloudevent/cloudevent_test.go index 6397b87d202..f35af6d1742 100644 --- a/pkg/reconciler/events/cloudevent/cloudevent_test.go +++ b/pkg/reconciler/events/cloudevent/cloudevent_test.go @@ -108,6 +108,15 @@ func TestEventForTaskRun(t *testing.T) { desc: "send a cloud event with successful status taskrun", taskRun: getTaskRunByCondition(corev1.ConditionTrue, "yay"), wantEventType: TaskRunSuccessfulEventV1, + }, { + desc: "send a cloud event with successful status taskrun, empty selflink", + taskRun: func() *v1beta1.TaskRun { + tr := getTaskRunByCondition(corev1.ConditionTrue, "yay") + // v1.20 does not set selfLink in controller + tr.ObjectMeta.SelfLink = "" + return tr + }(), + wantEventType: TaskRunSuccessfulEventV1, }} for _, c := range taskRunTests {