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 {
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:

View File

@ -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()
}

View File

@ -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,

View File

@ -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)

View File

@ -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()
}

View File

@ -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
}