Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: replace string literals with variables #23

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import (
)

// Event message format "medik8s <operator shortname> <message>"
const customFmt = "[remediation] %s"
const (
customFmt = "[remediation] %s"

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 fixed message.
func NormalEvent(recorder record.EventRecorder, object runtime.Object, reason, message string) {
Expand Down Expand Up @@ -37,20 +48,25 @@ func WarningEventf(recorder record.EventRecorder, object runtime.Object, reason,

// RemediationStarted will record a Normal event with reason RemediationStarted and message "Remediation 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".
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".
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 with reason RemediationCannotStart and custom message.
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".
func GetTargetNodeFailed(recorder record.EventRecorder, object runtime.Object) {
WarningEvent(recorder, object, "RemediationCannotStart", "Could not get remediation target Node")
RemediationCannotStart(recorder, object, remediationCannotStartTargetNodeFailedEventMessage)
}
7 changes: 7 additions & 0 deletions pkg/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading