diff --git a/pkg/reconciler/events/k8sevent/doc.go b/pkg/reconciler/events/k8sevent/doc.go index 810274bdeee..9a6f44b22aa 100644 --- a/pkg/reconciler/events/k8sevent/doc.go +++ b/pkg/reconciler/events/k8sevent/doc.go @@ -1,5 +1,5 @@ /* -Copyright 2020 The Tekton Authors +Copyright 2022 The Tekton Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and limitations under the License. */ -package k8sevents +package k8sevent diff --git a/pkg/reconciler/events/k8sevent/event.go b/pkg/reconciler/events/k8sevent/event.go index 15082f0eadf..f0e1b390935 100644 --- a/pkg/reconciler/events/k8sevent/event.go +++ b/pkg/reconciler/events/k8sevent/event.go @@ -38,24 +38,10 @@ const ( EventReasonError = "Error" ) -// Emit emits events for object -// Two types of events are supported, k8s and cloud events. -// +// EmitK8sEvents emits kubernetes events for object // k8s events are always sent if afterCondition is different from beforeCondition -// Cloud events are always sent if enabled, i.e. if a sink is available func EmitK8sEvents(ctx context.Context, beforeCondition *apis.Condition, afterCondition *apis.Condition, object runtime.Object) { recorder := controller.GetEventRecorder(ctx) - sendKubernetesEvents(recorder, beforeCondition, afterCondition, object) -} - -// EmitError emits a failure associated to an error -func EmitError(c record.EventRecorder, err error, object runtime.Object) { - if err != nil { - c.Event(object, corev1.EventTypeWarning, EventReasonError, err.Error()) - } -} - -func sendKubernetesEvents(c record.EventRecorder, beforeCondition *apis.Condition, afterCondition *apis.Condition, object runtime.Object) { // Events that are going to be sent // // Status "ConditionUnknown": @@ -68,20 +54,27 @@ func sendKubernetesEvents(c record.EventRecorder, beforeCondition *apis.Conditio // If the condition changed, and the target condition is not empty, we send an event switch afterCondition.Status { case corev1.ConditionTrue: - c.Event(object, corev1.EventTypeNormal, EventReasonSucceded, afterCondition.Message) + recorder.Event(object, corev1.EventTypeNormal, EventReasonSucceded, afterCondition.Message) case corev1.ConditionFalse: - c.Event(object, corev1.EventTypeWarning, EventReasonFailed, afterCondition.Message) + recorder.Event(object, corev1.EventTypeWarning, EventReasonFailed, afterCondition.Message) case corev1.ConditionUnknown: if beforeCondition == nil { // If the condition changed, the status is "unknown", and there was no condition before, // we emit the "Started event". We ignore further updates of the "unknown" status. - c.Event(object, corev1.EventTypeNormal, EventReasonStarted, "") + recorder.Event(object, corev1.EventTypeNormal, EventReasonStarted, "") } else { // If the condition changed, the status is "unknown", and there was a condition before, // we emit an event that matches the reason and message of the condition. // This is used for instance to signal the transition from "started" to "running" - c.Event(object, corev1.EventTypeNormal, afterCondition.Reason, afterCondition.Message) + recorder.Event(object, corev1.EventTypeNormal, afterCondition.Reason, afterCondition.Message) } } } } + +// EmitError emits a failure associated to an error +func EmitError(c record.EventRecorder, err error, object runtime.Object) { + if err != nil { + c.Event(object, corev1.EventTypeWarning, EventReasonError, err.Error()) + } +} diff --git a/pkg/reconciler/events/k8sevent/event_test.go b/pkg/reconciler/events/k8sevent/event_test.go index dc6e6aa230d..3cfbfecc3c7 100644 --- a/pkg/reconciler/events/k8sevent/event_test.go +++ b/pkg/reconciler/events/k8sevent/event_test.go @@ -33,7 +33,7 @@ import ( rtesting "knative.dev/pkg/reconciler/testing" ) -func TestSendKubernetesEvents(t *testing.T) { +func TestEmitK8sEventsOnConditions(t *testing.T) { testcases := []struct { name string before *apis.Condition @@ -138,10 +138,11 @@ func TestSendKubernetesEvents(t *testing.T) { }} for _, ts := range testcases { - fr := record.NewFakeRecorder(1) tr := &corev1.Pod{} - sendKubernetesEvents(fr, ts.before, ts.after, tr) - err := eventstest.CheckEventsOrdered(t, fr.Events, ts.name, ts.wantEvents) + ctx, _ := rtesting.SetupFakeContext(t) + recorder := controller.GetEventRecorder(ctx).(*record.FakeRecorder) + EmitK8sEvents(ctx, ts.before, ts.after, tr) + err := eventstest.CheckEventsOrdered(t, recorder.Events, ts.name, ts.wantEvents) if err != nil { t.Errorf(err.Error()) } @@ -168,21 +169,21 @@ func TestEmitK8sEvents(t *testing.T) { Message: "just starting", } testcases := []struct { - name string - data map[string]string - wantEvents []string + name string + data map[string]string + wantEvents []string }{{ - name: "without sink", - data: map[string]string{}, - wantEvents: []string{"Normal Started"}, + name: "without sink", + data: map[string]string{}, + wantEvents: []string{"Normal Started"}, }, { - name: "with empty string sink", - data: map[string]string{"default-cloud-events-sink": ""}, - wantEvents: []string{"Normal Started"}, + name: "with empty string sink", + data: map[string]string{"default-cloud-events-sink": ""}, + wantEvents: []string{"Normal Started"}, }, { - name: "with sink", - data: map[string]string{"default-cloud-events-sink": "http://mysink"}, - wantEvents: []string{"Normal Started"}, + name: "with sink", + data: map[string]string{"default-cloud-events-sink": "http://mysink"}, + wantEvents: []string{"Normal Started"}, }} for _, tc := range testcases {