Use logger Interface in runtime configuration

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-05-26 10:15:27 +02:00
parent a02bc27c3e
commit c9b05d8fed
6 changed files with 50 additions and 15 deletions

View File

@ -155,14 +155,17 @@ func (m command) configureWrapper(c *cli.Context, config *config) error {
switch config.runtime { switch config.runtime {
case "containerd": case "containerd":
cfg, err = containerd.New( cfg, err = containerd.New(
containerd.WithLogger(m.logger),
containerd.WithPath(configFilePath), containerd.WithPath(configFilePath),
) )
case "crio": case "crio":
cfg, err = crio.New( cfg, err = crio.New(
crio.WithLogger(m.logger),
crio.WithPath(configFilePath), crio.WithPath(configFilePath),
) )
case "docker": case "docker":
cfg, err = docker.New( cfg, err = docker.New(
docker.WithLogger(m.logger),
docker.WithPath(configFilePath), docker.WithPath(configFilePath),
) )
default: default:

View File

@ -17,6 +17,7 @@
package containerd package containerd
import ( import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
) )
@ -36,5 +37,9 @@ func New(opts ...Option) (engine.Interface, error) {
opt(b) opt(b)
} }
if b.logger == nil {
b.logger = logger.New()
}
return b.build() return b.build()
} }

View File

@ -20,9 +20,9 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
) )
const ( const (
@ -30,6 +30,7 @@ const (
) )
type builder struct { type builder struct {
logger logger.Interface
path string path string
runtimeType string runtimeType string
useLegacyConfig bool useLegacyConfig bool
@ -39,6 +40,13 @@ type builder struct {
// Option defines a function that can be used to configure the config builder // Option defines a function that can be used to configure the config builder
type Option func(*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 // WithPath sets the path for the config builder
func WithPath(path string) Option { func WithPath(path string) Option {
return func(b *builder) { return func(b *builder) {
@ -76,7 +84,7 @@ func (b *builder) build() (engine.Interface, error) {
b.runtimeType = defaultRuntimeType b.runtimeType = defaultRuntimeType
} }
config, err := loadConfig(b.path) config, err := b.loadConfig(b.path)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to load config: %v", err) 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 // loadConfig loads the containerd config from disk
func loadConfig(config string) (*Config, error) { func (b *builder) loadConfig(config string) (*Config, error) {
log.Infof("Loading config: %v", config) b.logger.Infof("Loading config: %v", config)
info, err := os.Stat(config) info, err := os.Stat(config)
if os.IsExist(err) && info.IsDir() { if os.IsExist(err) && info.IsDir() {
@ -110,7 +118,7 @@ func loadConfig(config string) (*Config, error) {
configFile := config configFile := config
if os.IsNotExist(err) { if os.IsNotExist(err) {
configFile = "/dev/null" 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) tomlConfig, err := toml.LoadFile(configFile)
@ -118,7 +126,7 @@ func loadConfig(config string) (*Config, error) {
return nil, err return nil, err
} }
log.Infof("Successfully loaded config") b.logger.Infof("Successfully loaded config")
cfg := Config{ cfg := Config{
Tree: tomlConfig, Tree: tomlConfig,

View File

@ -22,7 +22,6 @@ import (
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/pelletier/go-toml" "github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
) )
type builder struct { type builder struct {
@ -33,6 +32,13 @@ type builder struct {
// Option defines a function that can be used to configure the config builder // Option defines a function that can be used to configure the config builder
type Option func(*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 // WithPath sets the path for the config builder
func WithPath(path string) Option { func WithPath(path string) Option {
return func(b *builder) { return func(b *builder) {
@ -64,7 +70,7 @@ func (b *builder) loadConfig(config string) (*Config, error) {
configFile := config configFile := config
if os.IsNotExist(err) { if os.IsNotExist(err) {
configFile = "/dev/null" 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) cfg, err := toml.LoadFile(configFile)

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine" "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
) )
@ -39,6 +40,10 @@ func New(opts ...Option) (engine.Interface, error) {
opt(b) opt(b)
} }
if b.logger == nil {
b.logger = logger.New()
}
return b.build() return b.build()
} }

View File

@ -23,16 +23,24 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
log "github.com/sirupsen/logrus" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
) )
type builder struct { type builder struct {
logger logger.Interface
path string path string
} }
// Option defines a function that can be used to configure the config builder // Option defines a function that can be used to configure the config builder
type Option func(*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 // WithPath sets the path for the config builder
func WithPath(path string) Option { func WithPath(path string) Option {
return func(b *builder) { return func(b *builder) {
@ -46,12 +54,12 @@ func (b *builder) build() (*Config, error) {
return &empty, nil return &empty, nil
} }
return loadConfig(b.path) return b.loadConfig(b.path)
} }
// loadConfig loads the docker config from disk // loadConfig loads the docker config from disk
func loadConfig(configFilePath string) (*Config, error) { func (b *builder) loadConfig(configFilePath string) (*Config, error) {
log.Infof("Loading docker config from %v", configFilePath) b.logger.Infof("Loading docker config from %v", configFilePath)
info, err := os.Stat(configFilePath) info, err := os.Stat(configFilePath)
if os.IsExist(err) && info.IsDir() { if os.IsExist(err) && info.IsDir() {
@ -61,7 +69,7 @@ func loadConfig(configFilePath string) (*Config, error) {
cfg := make(Config) cfg := make(Config)
if os.IsNotExist(err) { 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 return &cfg, nil
} }
@ -75,6 +83,6 @@ func loadConfig(configFilePath string) (*Config, error) {
return nil, err return nil, err
} }
log.Infof("Successfully loaded config") b.logger.Infof("Successfully loaded config")
return &cfg, nil return &cfg, nil
} }