Skip to content

Commit deabe04

Browse files
committed
TOFIX: POC for containerd imports adjustment
Signed-off-by: Evan Lezar <[email protected]>
1 parent 15125f4 commit deabe04

File tree

3 files changed

+146
-19
lines changed

3 files changed

+146
-19
lines changed

pkg/config/engine/containerd/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (c *Config) AddRuntimeWithOptions(name string, path string, setAsDefault bo
6060
config.SetPath([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name}, options)
6161
}
6262
if len(c.ContainerAnnotations) > 0 {
63-
annotations, err := c.getRuntimeAnnotations([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "container_annotations"})
63+
annotations, err := c.getStringArrayValue([]string{"plugins", c.CRIRuntimePluginName, "containerd", "runtimes", name, "container_annotations"})
6464
if err != nil {
6565
return err
6666
}
@@ -82,7 +82,7 @@ func (c *Config) AddRuntimeWithOptions(name string, path string, setAsDefault bo
8282
return nil
8383
}
8484

85-
func (c *Config) getRuntimeAnnotations(path []string) ([]string, error) {
85+
func (c *Config) getStringArrayValue(path []string) ([]string, error) {
8686
if c == nil || c.Tree == nil {
8787
return nil, nil
8888
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package containerd
2+
3+
import (
4+
"path/filepath"
5+
6+
"github.com/NVIDIA/nvidia-container-toolkit/pkg/config/engine"
7+
)
8+
9+
type WithTopLevel struct {
10+
engine.Interface
11+
topLevelConfig *topLevelConfig
12+
}
13+
14+
type topLevelConfig struct {
15+
filename string
16+
config *Config
17+
}
18+
19+
func (c *WithTopLevel) Save(path string) (int64, error) {
20+
n, err := c.Interface.Save(path)
21+
if err != nil {
22+
return 0, err
23+
}
24+
25+
switch {
26+
case n > 0:
27+
c.topLevelConfig.ensureImports(path)
28+
case n == 0:
29+
c.topLevelConfig.simplify(path)
30+
}
31+
32+
return c.topLevelConfig.config.Save(c.topLevelConfig.filename)
33+
}
34+
35+
func (c *WithTopLevel) RemoveRuntime(name string) error {
36+
if err := c.topLevelConfig.RemoveRuntime(name); err != nil {
37+
return err
38+
}
39+
return c.Interface.RemoveRuntime(name)
40+
}
41+
42+
func (c *topLevelConfig) RemoveRuntime(name string) error {
43+
return c.config.RemoveRuntime(name)
44+
}
45+
46+
func (c *topLevelConfig) simplify(dropInFilename string) {
47+
c.removeImports(dropInFilename)
48+
c.removeVersion()
49+
}
50+
51+
// removeImports removes the imports specified in the file if the only entry
52+
// corresponds to the path for the drop-in-file and the only other field in the
53+
// file is the version field.
54+
func (c *topLevelConfig) removeImports(dropInFilename string) {
55+
if len(c.config.Keys()) != 2 {
56+
return
57+
}
58+
if c.config.Get("version") == nil || c.config.Get("imports") == nil {
59+
return
60+
}
61+
62+
currentImports, _ := c.config.getStringArrayValue([]string{"imports"})
63+
if len(currentImports) != 1 {
64+
return
65+
}
66+
67+
requiredImport := filepath.Dir(dropInFilename) + "/*.toml"
68+
if currentImports[0] != requiredImport {
69+
return
70+
}
71+
c.config.Delete("imports")
72+
}
73+
74+
// removeVersion removes the version if it is the ONLY field in the file.
75+
func (c *topLevelConfig) removeVersion() {
76+
if len(c.config.Keys()) > 1 {
77+
return
78+
}
79+
if c.config.Get("version") == nil {
80+
return
81+
}
82+
c.config.Delete("version")
83+
}
84+
85+
func (c *topLevelConfig) ensureImports(dropInFilename string) {
86+
config := c.config.Tree
87+
// TODO: Load the current imports from the config file.
88+
var currentImports []string
89+
if ci, ok := c.config.Get("imports").([]string); ok {
90+
currentImports = ci
91+
}
92+
93+
requiredImport := filepath.Dir(dropInFilename) + "/*.toml"
94+
for _, currentImport := range currentImports {
95+
// If the requiredImport is already present, then we need not update the config.
96+
if currentImport == requiredImport {
97+
return
98+
}
99+
}
100+
101+
currentImports = append(currentImports, requiredImport)
102+
103+
// If the config is empty we need to set the version too.
104+
if len(config.Keys()) == 0 {
105+
config.Set("version", c.config.Version)
106+
}
107+
config.Set("imports", currentImports)
108+
}

pkg/config/engine/containerd/containerd.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,43 @@ func New(opts ...Option) (engine.Interface, error) {
113113
}
114114
return (*ConfigV1)(cfg), nil
115115
default:
116-
cfg := &engine.DropInConfig{
117-
Source: &Config{
118-
Tree: sourceConfig,
119-
Version: configVersion,
120-
CRIRuntimePluginName: criRuntimePluginName,
121-
Logger: b.logger,
122-
RuntimeType: b.runtimeType,
123-
UseLegacyConfig: b.useLegacyConfig,
124-
ContainerAnnotations: b.containerAnnotations,
116+
117+
cfg := &WithTopLevel{
118+
Interface: &engine.DropInConfig{
119+
Source: &Config{
120+
Tree: sourceConfig,
121+
Version: configVersion,
122+
CRIRuntimePluginName: criRuntimePluginName,
123+
Logger: b.logger,
124+
RuntimeType: b.runtimeType,
125+
UseLegacyConfig: b.useLegacyConfig,
126+
ContainerAnnotations: b.containerAnnotations,
127+
},
128+
Destination: &Config{
129+
Tree: toml.NewEmpty(),
130+
Version: configVersion,
131+
CRIRuntimePluginName: criRuntimePluginName,
132+
Logger: b.logger,
133+
RuntimeType: b.runtimeType,
134+
UseLegacyConfig: b.useLegacyConfig,
135+
ContainerAnnotations: b.containerAnnotations,
136+
},
125137
},
126-
Destination: &Config{
127-
Tree: toml.NewEmpty(),
128-
Version: configVersion,
129-
CRIRuntimePluginName: criRuntimePluginName,
130-
Logger: b.logger,
131-
RuntimeType: b.runtimeType,
132-
UseLegacyConfig: b.useLegacyConfig,
133-
ContainerAnnotations: b.containerAnnotations,
138+
topLevelConfig: &topLevelConfig{
139+
// TODO: It should be clearer that b.path is the top-level config.
140+
filename: b.path,
141+
config: &Config{
142+
Tree: func() *toml.Tree {
143+
t, _ := toml.FromFile(b.path).Load()
144+
return t
145+
}(),
146+
Version: configVersion,
147+
CRIRuntimePluginName: criRuntimePluginName,
148+
Logger: b.logger,
149+
RuntimeType: b.runtimeType,
150+
UseLegacyConfig: b.useLegacyConfig,
151+
ContainerAnnotations: b.containerAnnotations,
152+
},
134153
},
135154
}
136155

0 commit comments

Comments
 (0)