1717package containerd
1818
1919import (
20- "fmt"
21- "os"
22-
23- "github.com/pelletier/go-toml"
24-
2520 "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
26- "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine "
21+ "github.com/NVIDIA/nvidia-container-toolkit/pkg/config/toml "
2722)
2823
2924const (
@@ -32,6 +27,7 @@ const (
3227
3328type builder struct {
3429 logger logger.Interface
30+ configSource toml.Loader
3531 path string
3632 runtimeType string
3733 useLegacyConfig bool
@@ -55,6 +51,13 @@ func WithPath(path string) Option {
5551 }
5652}
5753
54+ // WithConfigSource sets the source for the config.
55+ func WithConfigSource (configSource toml.Loader ) Option {
56+ return func (b * builder ) {
57+ b .configSource = configSource
58+ }
59+ }
60+
5861// WithRuntimeType sets the runtime type for the config builder
5962func WithRuntimeType (runtimeType string ) Option {
6063 return func (b * builder ) {
@@ -75,82 +78,3 @@ func WithContainerAnnotations(containerAnnotations ...string) Option {
7578 b .containerAnnotations = containerAnnotations
7679 }
7780}
78-
79- func (b * builder ) build () (engine.Interface , error ) {
80- if b .path == "" {
81- return nil , fmt .Errorf ("config path is empty" )
82- }
83-
84- if b .runtimeType == "" {
85- b .runtimeType = defaultRuntimeType
86- }
87-
88- config , err := b .loadConfig (b .path )
89- if err != nil {
90- return nil , fmt .Errorf ("failed to load config: %v" , err )
91- }
92- config .Logger = b .logger
93- config .RuntimeType = b .runtimeType
94- config .UseDefaultRuntimeName = ! b .useLegacyConfig
95- config .ContainerAnnotations = b .containerAnnotations
96-
97- version , err := config .parseVersion (b .useLegacyConfig )
98- if err != nil {
99- return nil , fmt .Errorf ("failed to parse config version: %v" , err )
100- }
101- switch version {
102- case 1 :
103- return (* ConfigV1 )(config ), nil
104- case 2 :
105- return config , nil
106- }
107-
108- return nil , fmt .Errorf ("unsupported config version: %v" , version )
109- }
110-
111- // loadConfig loads the containerd config from disk
112- func (b * builder ) loadConfig (config string ) (* Config , error ) {
113- info , err := os .Stat (config )
114- if os .IsExist (err ) && info .IsDir () {
115- return nil , fmt .Errorf ("config file is a directory" )
116- }
117-
118- if os .IsNotExist (err ) {
119- b .logger .Infof ("Config file does not exist; using empty config" )
120- config = "/dev/null"
121- } else {
122- b .logger .Infof ("Loading config from %v" , config )
123- }
124-
125- tomlConfig , err := toml .LoadFile (config )
126- if err != nil {
127- return nil , err
128- }
129-
130- cfg := Config {
131- Tree : tomlConfig ,
132- }
133- return & cfg , nil
134- }
135-
136- // parseVersion returns the version of the config
137- func (c * Config ) parseVersion (useLegacyConfig bool ) (int , error ) {
138- defaultVersion := 2
139- if useLegacyConfig {
140- defaultVersion = 1
141- }
142-
143- switch v := c .Get ("version" ).(type ) {
144- case nil :
145- switch len (c .Keys ()) {
146- case 0 : // No config exists, or the config file is empty, use version inferred from containerd
147- return defaultVersion , nil
148- default : // A config file exists, has content, and no version is set
149- return 1 , nil
150- }
151- case int64 :
152- return int (v ), nil
153- default :
154- return - 1 , fmt .Errorf ("unsupported type for version field: %v" , v )
155- }
156- }
0 commit comments