diff --git a/cmd/nvidia-ctk/runtime/configure/configure.go b/cmd/nvidia-ctk/runtime/configure/configure.go index f978b403..cd5a245a 100644 --- a/cmd/nvidia-ctk/runtime/configure/configure.go +++ b/cmd/nvidia-ctk/runtime/configure/configure.go @@ -155,14 +155,17 @@ func (m command) configureWrapper(c *cli.Context, config *config) error { switch config.runtime { case "containerd": cfg, err = containerd.New( + containerd.WithLogger(m.logger), containerd.WithPath(configFilePath), ) case "crio": cfg, err = crio.New( + crio.WithLogger(m.logger), crio.WithPath(configFilePath), ) case "docker": cfg, err = docker.New( + docker.WithLogger(m.logger), docker.WithPath(configFilePath), ) default: diff --git a/pkg/config/engine/containerd/containerd.go b/pkg/config/engine/containerd/containerd.go index ab782ca6..17ba9a0c 100644 --- a/pkg/config/engine/containerd/containerd.go +++ b/pkg/config/engine/containerd/containerd.go @@ -17,6 +17,7 @@ package containerd import ( + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" "github.com/pelletier/go-toml" ) @@ -36,5 +37,9 @@ func New(opts ...Option) (engine.Interface, error) { opt(b) } + if b.logger == nil { + b.logger = logger.New() + } + return b.build() } diff --git a/pkg/config/engine/containerd/option.go b/pkg/config/engine/containerd/option.go index 400d5925..bb0bda6f 100644 --- a/pkg/config/engine/containerd/option.go +++ b/pkg/config/engine/containerd/option.go @@ -20,9 +20,9 @@ import ( "fmt" "os" + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" "github.com/pelletier/go-toml" - log "github.com/sirupsen/logrus" ) const ( @@ -30,6 +30,7 @@ const ( ) type builder struct { + logger logger.Interface path string runtimeType string useLegacyConfig bool @@ -39,6 +40,13 @@ type builder struct { // Option defines a function that can be used to configure the config builder type Option func(*builder) +// WithLogger sets the logger for the config builder +func WithLogger(logger logger.Interface) Option { + return func(b *builder) { + b.logger = logger + } +} + // WithPath sets the path for the config builder func WithPath(path string) Option { return func(b *builder) { @@ -76,7 +84,7 @@ func (b *builder) build() (engine.Interface, error) { b.runtimeType = defaultRuntimeType } - config, err := loadConfig(b.path) + config, err := b.loadConfig(b.path) if err != nil { return nil, fmt.Errorf("failed to load config: %v", err) } @@ -99,8 +107,8 @@ func (b *builder) build() (engine.Interface, error) { } // loadConfig loads the containerd config from disk -func loadConfig(config string) (*Config, error) { - log.Infof("Loading config: %v", config) +func (b *builder) loadConfig(config string) (*Config, error) { + b.logger.Infof("Loading config: %v", config) info, err := os.Stat(config) if os.IsExist(err) && info.IsDir() { @@ -110,7 +118,7 @@ func loadConfig(config string) (*Config, error) { configFile := config if os.IsNotExist(err) { configFile = "/dev/null" - log.Infof("Config file does not exist, creating new one") + b.logger.Infof("Config file does not exist, creating new one") } tomlConfig, err := toml.LoadFile(configFile) @@ -118,7 +126,7 @@ func loadConfig(config string) (*Config, error) { return nil, err } - log.Infof("Successfully loaded config") + b.logger.Infof("Successfully loaded config") cfg := Config{ Tree: tomlConfig, diff --git a/pkg/config/engine/crio/option.go b/pkg/config/engine/crio/option.go index 82d3af32..f6660f68 100644 --- a/pkg/config/engine/crio/option.go +++ b/pkg/config/engine/crio/option.go @@ -22,7 +22,6 @@ import ( "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/pelletier/go-toml" - log "github.com/sirupsen/logrus" ) type builder struct { @@ -33,6 +32,13 @@ type builder struct { // Option defines a function that can be used to configure the config builder type Option func(*builder) +// WithLogger sets the logger for the config builder +func WithLogger(logger logger.Interface) Option { + return func(b *builder) { + b.logger = logger + } +} + // WithPath sets the path for the config builder func WithPath(path string) Option { return func(b *builder) { @@ -64,7 +70,7 @@ func (b *builder) loadConfig(config string) (*Config, error) { configFile := config if os.IsNotExist(err) { configFile = "/dev/null" - log.Infof("Config file does not exist, creating new one") + b.logger.Infof("Config file does not exist, creating new one") } cfg, err := toml.LoadFile(configFile) diff --git a/pkg/config/engine/docker/docker.go b/pkg/config/engine/docker/docker.go index 260be267..dc64a3c0 100644 --- a/pkg/config/engine/docker/docker.go +++ b/pkg/config/engine/docker/docker.go @@ -21,6 +21,7 @@ import ( "fmt" "os" + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" ) @@ -39,6 +40,10 @@ func New(opts ...Option) (engine.Interface, error) { opt(b) } + if b.logger == nil { + b.logger = logger.New() + } + return b.build() } diff --git a/pkg/config/engine/docker/option.go b/pkg/config/engine/docker/option.go index 238cc3f1..893ed0d1 100644 --- a/pkg/config/engine/docker/option.go +++ b/pkg/config/engine/docker/option.go @@ -23,16 +23,24 @@ import ( "io/ioutil" "os" - log "github.com/sirupsen/logrus" + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" ) type builder struct { - path string + logger logger.Interface + path string } // Option defines a function that can be used to configure the config builder type Option func(*builder) +// WithLogger sets the logger for the config builder +func WithLogger(logger logger.Interface) Option { + return func(b *builder) { + b.logger = logger + } +} + // WithPath sets the path for the config builder func WithPath(path string) Option { return func(b *builder) { @@ -46,12 +54,12 @@ func (b *builder) build() (*Config, error) { return &empty, nil } - return loadConfig(b.path) + return b.loadConfig(b.path) } // loadConfig loads the docker config from disk -func loadConfig(configFilePath string) (*Config, error) { - log.Infof("Loading docker config from %v", configFilePath) +func (b *builder) loadConfig(configFilePath string) (*Config, error) { + b.logger.Infof("Loading docker config from %v", configFilePath) info, err := os.Stat(configFilePath) if os.IsExist(err) && info.IsDir() { @@ -61,7 +69,7 @@ func loadConfig(configFilePath string) (*Config, error) { cfg := make(Config) if os.IsNotExist(err) { - log.Infof("Config file does not exist, creating new one") + b.logger.Infof("Config file does not exist, creating new one") return &cfg, nil } @@ -75,6 +83,6 @@ func loadConfig(configFilePath string) (*Config, error) { return nil, err } - log.Infof("Successfully loaded config") + b.logger.Infof("Successfully loaded config") return &cfg, nil }