diff --git a/pkg/events/events.go b/pkg/events/events.go index cdc6620..07d50a0 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -9,9 +9,20 @@ import ( ) // Event message format "medik8s " -const customFmt = "[remediation] %s" +const ( + customFmt = "[remediation] %s" -// NormalEvent will record an event with type Normal and fixed message. + RemediationStartedEventReason = "RemediationStarted" + RemediationStoppedEventReason = "RemediationStopped" + RemediationFinishedEventReason = "RemediationFinished" + RemediationCannotStartEventReason = "RemediationCannotStart" + remediationStartedEventMessage = "Remediation started" + remediationStoppedEventMessage = "NHC added the timed-out annotation, remediation will be stopped" + remediationFinishedEventMessage = "Remediation finished" + remediationCannotStartTargetNodeFailedEventMessage = "Could not get remediation target Node" +) + +// NormalEvent will record an event with type Normal and custom message. func NormalEvent(recorder record.EventRecorder, object runtime.Object, reason, message string) { recorder.Event(object, corev1.EventTypeNormal, reason, fmt.Sprintf(customFmt, message)) } @@ -22,7 +33,7 @@ func NormalEventf(recorder record.EventRecorder, object runtime.Object, reason, recorder.Event(object, corev1.EventTypeNormal, reason, fmt.Sprintf(customFmt, message)) } -// WarningEvent will record an event with type Warning and fixed message. +// WarningEvent will record an event with type Warning and custom message. func WarningEvent(recorder record.EventRecorder, object runtime.Object, reason, message string) { recorder.Event(object, corev1.EventTypeWarning, reason, fmt.Sprintf(customFmt, message)) } @@ -35,22 +46,29 @@ func WarningEventf(recorder record.EventRecorder, object runtime.Object, reason, // Special case events -// RemediationStarted will record a Normal event with reason RemediationStarted and message "Remediation started". +// RemediationStarted will record a Normal event to signal that the remediation has started. func RemediationStarted(recorder record.EventRecorder, object runtime.Object) { - NormalEvent(recorder, object, "RemediationStarted", "Remediation started") + NormalEvent(recorder, object, RemediationStartedEventReason, remediationStartedEventMessage) } -// RemediationStoppedByNHC will record a Normal event with reason RemediationStopped and message "NHC added the timed-out annotation, remediation will be stopped". +// RemediationStoppedByNHC will record a Normal event to signal that the remediation was stopped by the Node Healthcheck operator. func RemediationStoppedByNHC(recorder record.EventRecorder, object runtime.Object) { - NormalEvent(recorder, object, "RemediationStopped", "NHC added the timed-out annotation, remediation will be stopped") + NormalEvent(recorder, object, RemediationStoppedEventReason, remediationStoppedEventMessage) } -// RemediationFinished will record a Normal event with reason RemediationFinished and message "Remediation finished". +// RemediationFinished will record a Normal event to signal that the remediation has finished. func RemediationFinished(recorder record.EventRecorder, object runtime.Object) { - NormalEvent(recorder, object, "RemediationFinished", "Remediation finished") + NormalEvent(recorder, object, RemediationFinishedEventReason, remediationFinishedEventMessage) +} + +// RemediationCannotStart will record a Warning event to signal that the remediation cannot start. A custom message can +// be used to further explain the reason. +func RemediationCannotStart(recorder record.EventRecorder, object runtime.Object, message string) { + WarningEvent(recorder, object, RemediationCannotStartEventReason, message) } -// GetTargetNodeFailed will record a Warning event with reason RemediationFailed and message "Could not get remediation target node". +// GetTargetNodeFailed will record a Warning event to signal that the remediation cannot start because the target Node +// could not be found. func GetTargetNodeFailed(recorder record.EventRecorder, object runtime.Object) { - WarningEvent(recorder, object, "RemediationCannotStart", "Could not get remediation target Node") + RemediationCannotStart(recorder, object, remediationCannotStartTargetNodeFailedEventMessage) } diff --git a/pkg/events/events_test.go b/pkg/events/events_test.go index fa43b0c..1252b22 100644 --- a/pkg/events/events_test.go +++ b/pkg/events/events_test.go @@ -32,6 +32,13 @@ var _ = Describe("Emit custom formatted Event", func() { }) }) + When("Remediation could not start", func() { + It("should see special RemediationCannotStart event with custom reason", func() { + RemediationCannotStart(r, nil, "custom event message") + verifyEvent(r, "Warning RemediationCannotStart [remediation] custom event message") + }) + }) + When("Remediation is stopped by NHC", func() { It("should see special RemediationStoppedByNHC event", func() { RemediationStoppedByNHC(r, nil)