2023-02-23 12:49:54 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package crio
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/pelletier/go-toml"
|
2023-12-01 01:10:10 +00:00
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-02-23 12:49:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type builder struct {
|
2023-03-22 12:27:43 +00:00
|
|
|
logger logger.Interface
|
|
|
|
path string
|
2023-02-23 12:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Option defines a function that can be used to configure the config builder
|
|
|
|
type Option func(*builder)
|
|
|
|
|
2023-05-26 08:15:27 +00:00
|
|
|
// WithLogger sets the logger for the config builder
|
|
|
|
func WithLogger(logger logger.Interface) Option {
|
|
|
|
return func(b *builder) {
|
|
|
|
b.logger = logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 12:49:54 +00:00
|
|
|
// WithPath sets the path for the config builder
|
|
|
|
func WithPath(path string) Option {
|
|
|
|
return func(b *builder) {
|
|
|
|
b.path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *builder) build() (*Config, error) {
|
|
|
|
if b.path == "" {
|
|
|
|
empty := toml.Tree{}
|
|
|
|
return (*Config)(&empty), nil
|
|
|
|
}
|
2023-03-22 12:27:43 +00:00
|
|
|
if b.logger == nil {
|
|
|
|
b.logger = logger.New()
|
|
|
|
}
|
2023-02-23 12:49:54 +00:00
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
return b.loadConfig(b.path)
|
2023-02-23 12:49:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadConfig loads the cri-o config from disk
|
2023-03-22 12:27:43 +00:00
|
|
|
func (b *builder) loadConfig(config string) (*Config, error) {
|
|
|
|
b.logger.Infof("Loading config: %v", config)
|
2023-02-23 12:49:54 +00:00
|
|
|
|
|
|
|
info, err := os.Stat(config)
|
|
|
|
if os.IsExist(err) && info.IsDir() {
|
|
|
|
return nil, fmt.Errorf("config file is a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
2023-07-03 13:14:22 +00:00
|
|
|
b.logger.Infof("Config file does not exist; using empty config")
|
|
|
|
config = "/dev/null"
|
|
|
|
} else {
|
|
|
|
b.logger.Infof("Loading config from %v", config)
|
2023-02-23 12:49:54 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 13:14:22 +00:00
|
|
|
cfg, err := toml.LoadFile(config)
|
2023-02-23 12:49:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
b.logger.Infof("Successfully loaded config")
|
2023-02-23 12:49:54 +00:00
|
|
|
|
|
|
|
return (*Config)(cfg), nil
|
|
|
|
}
|