From 9c6164600988b201307994cf93a644d489676570 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Fri, 7 Oct 2022 10:06:24 +0200 Subject: [PATCH] Add native types for server LogDriver and LogLevel 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 --- pkg/client/client.go | 49 +++++++++++++++++---------------------- pkg/client/client_test.go | 4 ++-- pkg/client/consts.go | 45 +++++++++++++++++++++++++---------- pkg/client/suite_test.go | 2 +- 4 files changed, 57 insertions(+), 43 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 911b4f0d42..2d78f04e5f 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -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. @@ -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. @@ -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 @@ -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" @@ -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), ) } @@ -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 @@ -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 @@ -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) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 0fc3da25ab..4839a5c8c5 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -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(), }}, @@ -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(), }}, diff --git a/pkg/client/consts.go b/pkg/client/consts.go index fde64dce48..dbefac2cda 100644 --- a/pkg/client/consts.go +++ b/pkg/client/consts.go @@ -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 ) diff --git a/pkg/client/suite_test.go b/pkg/client/suite_test.go index f27c9e6fc3..c4f2ce77fc 100644 --- a/pkg/client/suite_test.go +++ b/pkg/client/suite_test.go @@ -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(), }},