Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongxuanzhang committed Nov 30, 2022
1 parent 26ca4bc commit a32e597
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
4 changes: 2 additions & 2 deletions pkg/reconciler/events/k8sevent/doc.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,4 +14,4 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package k8sevents
package k8sevent
31 changes: 12 additions & 19 deletions pkg/reconciler/events/k8sevent/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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())
}
}
33 changes: 17 additions & 16 deletions pkg/reconciler/events/k8sevent/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}
Expand All @@ -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 {
Expand Down

0 comments on commit a32e597

Please sign in to comment.