From 5354579b3f6e261824975dc9e3c77b1817eb2091 Mon Sep 17 00:00:00 2001 From: jmcshane Date: Sat, 13 Feb 2021 19:45:09 -0600 Subject: [PATCH] Short term fix for #2676 SelfLink is deprecated and is causing an error on Kubernetes v1.20 clusters as outlined in the issue. This is a short term fix that would set the source field to a value that should match the current source URI without the value of the auto-populated selfLink field. Another field could be used for the source field without issue, but could cause concerns about backwards compatibility. --- pkg/reconciler/events/cloudevent/cloudevent.go | 13 ++++++++++++- pkg/reconciler/events/cloudevent/cloudevent_test.go | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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 {