Skip to content

Commit

Permalink
Add native types for server LogDriver and LogLevel
Browse files Browse the repository at this point in the history
This allows a better API usage by having native types around possible
values. We also now rename the LogDriver for the container creation to
`ContainerLogDriver` to be more distinct compared to the server log
driver.

Signed-off-by: Sascha Grunert <sgrunert@redhat.com>
  • Loading branch information
saschagrunert committed Oct 7, 2022
1 parent 75562ef commit ea196d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
42 changes: 27 additions & 15 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ type ConmonServerConfig struct {

// LogLevel of the server to be used.
// Can be "trace", "debug", "info", "warn", "error" or "off".
LogLevel string
LogLevel LogLevel

// LogDriver is the possible server logging driver.
// Can be "stdout" or "systemd".
LogDriver string
LogDriver LogDriver

// Runtime is the binary path of the OCI runtime to use to operate on the
// containers.
Expand All @@ -85,6 +85,12 @@ type ConmonServerConfig struct {
CgroupManager CgroupManager
}

// LogLevel is the enum for all available server log levels.
type LogLevel string

// LogDriver is the enum for all available server log drivers.
type LogDriver string

// CgroupManager is the enum for all available cgroup managers.
type CgroupManager int

Expand Down Expand Up @@ -116,7 +122,7 @@ func NewConmonServerConfig(
}

// FromLogrusLevel converts the logrus.Level to a conmon-rs server log level.
func FromLogrusLevel(level logrus.Level) string {
func FromLogrusLevel(level logrus.Level) LogLevel {
switch level {
case logrus.PanicLevel, logrus.FatalLevel:
return LogLevelOff
Expand Down Expand Up @@ -260,14 +266,14 @@ func (c *ConmonClient) toArgs(config *ConmonServerConfig) (entrypoint string, ar
if err := validateLogLevel(config.LogLevel); err != nil {
return "", args, fmt.Errorf("validate log level: %w", err)
}
args = append(args, "--log-level", config.LogLevel)
args = append(args, "--log-level", string(config.LogLevel))
}

if config.LogDriver != "" {
if err := validateLogDriver(config.LogDriver); err != nil {
return "", args, fmt.Errorf("validate log driver: %w", err)
}
args = append(args, "--log-driver", config.LogDriver)
args = append(args, "--log-driver", string(config.LogDriver))
}

const cgroupManagerFlag = "--cgroup-manager"
Expand All @@ -285,19 +291,25 @@ func (c *ConmonClient) toArgs(config *ConmonServerConfig) (entrypoint string, ar
return entrypoint, args, nil
}

func validateLogLevel(level string) error {
func validateLogLevel(level LogLevel) error {
return validateStringSlice(
"log level",
level,
LogLevelTrace, LogLevelDebug, LogLevelInfo, LogLevelWarn, LogLevelError, LogLevelOff,
string(level),
string(LogLevelTrace),
string(LogLevelDebug),
string(LogLevelInfo),
string(LogLevelWarn),
string(LogLevelError),
string(LogLevelOff),
)
}

func validateLogDriver(driver string) error {
func validateLogDriver(driver LogDriver) error {
return validateStringSlice(
"log driver",
driver,
LogDriverStdout, LogDriverSystemd,
string(driver),
string(LogDriverStdout),
string(LogDriverSystemd),
)
}

Expand Down Expand Up @@ -537,7 +549,7 @@ type CreateContainerConfig struct {
OOMExitPaths []string

// LogDrivers is a slice of selected log drivers.
LogDrivers []LogDriver
LogDrivers []ContainerLogDriver

// CleanupCmd is the command that will be executed once the container exits
CleanupCmd []string
Expand All @@ -551,8 +563,8 @@ type CreateContainerConfig struct {
CommandArgs []string
}

// LogDriver specifies a selected logging mechanism.
type LogDriver struct {
// ContainerLogDriver specifies a selected logging mechanism.
type ContainerLogDriver struct {
// Type defines the log driver variant.
Type LogDriverType

Expand Down Expand Up @@ -762,7 +774,7 @@ func stringSliceToTextList(src []string, newFunc func(int32) (capnp.TextList, er
return nil
}

func (c *ConmonClient) initLogDrivers(req *proto.Conmon_CreateContainerRequest, logDrivers []LogDriver) error {
func (c *ConmonClient) initLogDrivers(req *proto.Conmon_CreateContainerRequest, logDrivers []ContainerLogDriver) error {
newLogDrivers, err := req.NewLogDrivers(int32(len(logDrivers)))
if err != nil {
return fmt.Errorf("create log drivers: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ var _ = Describe("ConmonClient", func() {
Terminal: terminal,
ExitPaths: []string{tr.exitPath()},
OOMExitPaths: []string{tr.oomExitPath()},
LogDrivers: []client.LogDriver{{
LogDrivers: []client.ContainerLogDriver{{
Type: client.LogDriverTypeContainerRuntimeInterface,
Path: tr.logPath(),
}},
Expand All @@ -146,7 +146,7 @@ var _ = Describe("ConmonClient", func() {
ID: tr.ctrID,
BundlePath: tr.tmpDir,
Terminal: terminal,
LogDrivers: []client.LogDriver{{
LogDrivers: []client.ContainerLogDriver{{
Type: client.LogDriverTypeContainerRuntimeInterface,
Path: tr.logPath(),
}},
Expand Down
16 changes: 8 additions & 8 deletions pkg/client/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package client

const (
// LogDriverStdout is the log driver printing to stdio.
LogDriverStdout = "stdout"
LogDriverStdout LogDriver = "stdout"

// LogDriverSystemd is the log driver printing to systemd journald.
LogDriverSystemd = "systemd"
LogDriverSystemd LogDriver = "systemd"

// LogLevelTrace is the log level printing only "trace" messages.
LogLevelTrace = "trace"
LogLevelTrace LogLevel = "trace"

// LogLevelDebug is the log level printing only "debug" messages.
LogLevelDebug = "debug"
LogLevelDebug LogLevel = "debug"

// LogLevelInfo is the log level printing only "info" messages.
LogLevelInfo = "info"
LogLevelInfo LogLevel = "info"

// LogLevelWarn is the log level printing only "warn" messages.
LogLevelWarn = "warn"
LogLevelWarn LogLevel = "warn"

// LogLevelError is the log level printing only "error" messages.
LogLevelError = "error"
LogLevelError LogLevel = "error"

// LogLevelOff is the log level printing no messages.
LogLevelOff = "off"
LogLevelOff LogLevel = "off"
)
2 changes: 1 addition & 1 deletion pkg/client/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (tr *testRunner) defaultConfig(terminal bool) *client.CreateContainerConfig
Stdin: true,
ExitPaths: []string{tr.exitPath()},
OOMExitPaths: []string{tr.oomExitPath()},
LogDrivers: []client.LogDriver{{
LogDrivers: []client.ContainerLogDriver{{
Type: client.LogDriverTypeContainerRuntimeInterface,
Path: tr.logPath(),
}},
Expand Down

0 comments on commit ea196d8

Please sign in to comment.