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

refact: EventerType and improve consistency #24665

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 6 additions & 5 deletions libpod/events/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ import (
"time"
)

// EventerType ...
type EventerType int
// EventerType describes the type of event logger
// The string values for EventerType should be entirely lowercase.
type EventerType string

const (
// LogFile indicates the event logger will be a logfile
LogFile EventerType = iota
LogFile EventerType = "file"
// Journald indicates journald should be used to log events
Journald EventerType = iota
Journald EventerType = "journald"
// Null is a no-op events logger. It does not read or write events.
Null EventerType = iota
Null EventerType = "none"
)

// Event describes the attributes of a libpod event
Expand Down
19 changes: 3 additions & 16 deletions libpod/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,13 @@ var ErrNoJournaldLogging = errors.New("no support for journald logging")

// String returns a string representation of EventerType
func (et EventerType) String() string {
switch et {
case LogFile:
return "file"
case Journald:
return "journald"
case Null:
return "none"
default:
return "invalid"
}
return string(et)
}

// IsValidEventer checks if the given string is a valid eventer type.
func IsValidEventer(eventer string) bool {
switch eventer {
case LogFile.String():
return true
case Journald.String():
return true
case Null.String():
switch EventerType(eventer) {
case LogFile, Journald, Null:
return true
default:
return false
Expand Down
8 changes: 4 additions & 4 deletions libpod/events/events_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
// NewEventer creates an eventer based on the eventer type
func NewEventer(options EventerOptions) (Eventer, error) {
logrus.Debugf("Initializing event backend %s", options.EventerType)
switch strings.ToUpper(options.EventerType) {
case strings.ToUpper(LogFile.String()):
switch EventerType(strings.ToLower(options.EventerType)) {
case LogFile:
return EventLogFile{options}, nil
case strings.ToUpper(Null.String()):
case Null:
return newNullEventer(), nil
default:
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType))
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType))
}
}
16 changes: 6 additions & 10 deletions libpod/events/events_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import (
// NewEventer creates an eventer based on the eventer type
func NewEventer(options EventerOptions) (Eventer, error) {
logrus.Debugf("Initializing event backend %s", options.EventerType)
switch strings.ToUpper(options.EventerType) {
case strings.ToUpper(Journald.String()):
eventer, err := newEventJournalD(options)
if err != nil {
return nil, fmt.Errorf("eventer creation: %w", err)
}
return eventer, nil
case strings.ToUpper(LogFile.String()):
switch EventerType(strings.ToLower(options.EventerType)) {
case Journald:
return newJournalDEventer(options)
case LogFile:
return newLogFileEventer(options)
case strings.ToUpper(Null.String()):
case Null:
return newNullEventer(), nil
default:
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToUpper(options.EventerType))
return nil, fmt.Errorf("unknown event logger type: %s", strings.ToLower(options.EventerType))
}
}
4 changes: 2 additions & 2 deletions libpod/events/journal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type EventJournalD struct {
options EventerOptions
}

// newEventJournalD creates a new journald Eventer
func newEventJournalD(options EventerOptions) (Eventer, error) {
// newJournalDEventer creates a new EventJournalD Eventer
func newJournalDEventer(options EventerOptions) (Eventer, error) {
return EventJournalD{options}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions libpod/events/journal_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package events
// DefaultEventerType is logfile when systemd is not present
const DefaultEventerType = LogFile

// newEventJournalD always returns an error if libsystemd not found
func newEventJournalD(options EventerOptions) (Eventer, error) {
// newJournalDEventer always returns an error if libsystemd not found
func newJournalDEventer(options EventerOptions) (Eventer, error) {
return nil, ErrNoJournaldLogging
}