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 <[email protected]>
  • Loading branch information
saschagrunert committed Oct 7, 2022
1 parent 75562ef commit 9c61646
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 43 deletions.
49 changes: 21 additions & 28 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,19 +85,6 @@ type ConmonServerConfig struct {
CgroupManager CgroupManager
}

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

const (
// CgroupManagerSystemd specifies to use systemd to create and manage
// cgroups.
CgroupManagerSystemd CgroupManager = iota

// CgroupManagerCgroupfs specifies to use the cgroup filesystem to create
// and manage cgroups.
CgroupManagerCgroupfs
)

// NewConmonServerConfig creates a new ConmonServerConfig instance for the
// required arguments. Optional arguments are pointing to their corresponding
// default values.
Expand All @@ -116,7 +103,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 +247,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 +272,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 +530,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 +544,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 +755,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
45 changes: 33 additions & 12 deletions pkg/client/consts.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
package client

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

// LogDriverSystemd is the log driver printing to systemd journald.
LogDriverSystemd = "systemd"
// LogLevel is the enum for all available server log levels.
type LogLevel string

const (
// 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"
)

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

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

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

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

const (
// CgroupManagerSystemd specifies to use systemd to create and manage
// cgroups.
CgroupManagerSystemd CgroupManager = iota

// CgroupManagerCgroupfs specifies to use the cgroup filesystem to create
// and manage cgroups.
CgroupManagerCgroupfs
)
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 9c61646

Please sign in to comment.