Skip to content

Commit 149236b

Browse files
committed
Configure containerd config based on specified annotation prefixes
Signed-off-by: Evan Lezar <[email protected]>
1 parent ee141f9 commit 149236b

File tree

7 files changed

+81
-17
lines changed

7 files changed

+81
-17
lines changed

internal/config/engine/containerd/config_v1.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ func (c *ConfigV1) AddRuntime(name string, path string, setAsDefault bool) error
5050
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "runtime_engine"}, "")
5151
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "privileged_without_host_devices"}, false)
5252
}
53-
cdiAnnotations := []interface{}{"cdi.k8s.io/*"}
54-
containerAnnotations, ok := config.GetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"}).([]interface{})
55-
if ok && containerAnnotations != nil {
56-
cdiAnnotations = append(containerAnnotations, cdiAnnotations...)
53+
54+
if len(c.ContainerAnnotations) > 0 {
55+
annotations, err := (*Config)(c).getRuntimeAnnotations([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"})
56+
if err != nil {
57+
return err
58+
}
59+
annotations = append(c.ContainerAnnotations, annotations...)
60+
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"}, annotations)
5761
}
58-
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "container_annotations"}, cdiAnnotations)
5962

6063
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "BinaryName"}, path)
6164
config.SetPath([]string{"plugins", "cri", "containerd", "runtimes", name, "options", "Runtime"}, path)

internal/config/engine/containerd/config_v2.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
4545
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "privileged_without_host_devices"}, false)
4646
}
4747

48-
cdiAnnotations := []interface{}{"cdi.k8s.io/*"}
49-
containerAnnotations, ok := config.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"}).([]interface{})
50-
if ok && containerAnnotations != nil {
51-
cdiAnnotations = append(containerAnnotations, cdiAnnotations...)
48+
if len(c.ContainerAnnotations) > 0 {
49+
annotations, err := c.getRuntimeAnnotations([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"})
50+
if err != nil {
51+
return err
52+
}
53+
annotations = append(c.ContainerAnnotations, annotations...)
54+
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"}, annotations)
5255
}
53-
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "container_annotations"}, cdiAnnotations)
5456

5557
config.SetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "runtimes", name, "options", "BinaryName"}, path)
5658

@@ -62,6 +64,32 @@ func (c *Config) AddRuntime(name string, path string, setAsDefault bool) error {
6264
return nil
6365
}
6466

67+
func (c *Config) getRuntimeAnnotations(path []string) ([]string, error) {
68+
if c == nil || c.Tree == nil {
69+
return nil, nil
70+
}
71+
72+
config := *c.Tree
73+
if !config.HasPath(path) {
74+
return nil, nil
75+
}
76+
annotationsI, ok := config.GetPath(path).([]interface{})
77+
if !ok {
78+
return nil, fmt.Errorf("invalid annotations: %v", annotationsI)
79+
}
80+
81+
var annotations []string
82+
for _, annotation := range annotationsI {
83+
a, ok := annotation.(string)
84+
if !ok {
85+
return nil, fmt.Errorf("invalid annotation: %v", annotation)
86+
}
87+
annotations = append(annotations, a)
88+
}
89+
90+
return annotations, nil
91+
}
92+
6593
// DefaultRuntime returns the default runtime for the cri-o config
6694
func (c Config) DefaultRuntime() string {
6795
if runtime, ok := c.GetPath([]string{"plugins", "io.containerd.grpc.v1.cri", "containerd", "default_runtime_name"}).(string); ok {

internal/config/engine/containerd/containerd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Config struct {
2626
*toml.Tree
2727
RuntimeType string
2828
UseDefaultRuntimeName bool
29+
ContainerAnnotations []string
2930
}
3031

3132
// New creates a containerd config with the specified options

internal/config/engine/containerd/option.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ const (
3030
)
3131

3232
type builder struct {
33-
path string
34-
runtimeType string
35-
useLegacyConfig bool
33+
path string
34+
runtimeType string
35+
useLegacyConfig bool
36+
containerAnnotations []string
3637
}
3738

3839
// Option defines a function that can be used to configure the config builder
@@ -59,6 +60,13 @@ func WithUseLegacyConfig(useLegacyConfig bool) Option {
5960
}
6061
}
6162

63+
// WithContainerAnnotations sets the container annotations for the config builder
64+
func WithContainerAnnotations(containerAnnotations ...string) Option {
65+
return func(b *builder) {
66+
b.containerAnnotations = containerAnnotations
67+
}
68+
}
69+
6270
func (b *builder) build() (engine.Interface, error) {
6371
if b.path == "" {
6472
return nil, fmt.Errorf("config path is empty")
@@ -74,6 +82,7 @@ func (b *builder) build() (engine.Interface, error) {
7482
}
7583
config.RuntimeType = b.runtimeType
7684
config.UseDefaultRuntimeName = !b.useLegacyConfig
85+
config.ContainerAnnotations = b.containerAnnotations
7786

7887
version, err := config.parseVersion(b.useLegacyConfig)
7988
if err != nil {

tools/container/containerd/config_v1_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ func TestUpdateV1Config(t *testing.T) {
332332
Tree: config,
333333
UseDefaultRuntimeName: true,
334334
RuntimeType: runtimeType,
335+
ContainerAnnotations: []string{"cdi.k8s.io/*"},
335336
}
336337

337338
err = UpdateConfig(v1, o)
@@ -585,6 +586,7 @@ func TestUpdateV1ConfigWithRuncPresent(t *testing.T) {
585586
Tree: config,
586587
UseDefaultRuntimeName: true,
587588
RuntimeType: runtimeType,
589+
ContainerAnnotations: []string{"cdi.k8s.io/*"},
588590
}
589591

590592
err = UpdateConfig(v1, o)

tools/container/containerd/config_v2_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,9 @@ func TestUpdateV2Config(t *testing.T) {
279279
require.NoError(t, err)
280280

281281
v2 := &containerd.Config{
282-
Tree: config,
283-
RuntimeType: runtimeType,
282+
Tree: config,
283+
RuntimeType: runtimeType,
284+
ContainerAnnotations: []string{"cdi.k8s.io/*"},
284285
}
285286

286287
err = UpdateConfig(v2, o)
@@ -520,8 +521,9 @@ func TestUpdateV2ConfigWithRuncPresent(t *testing.T) {
520521
require.NoError(t, err)
521522

522523
v2 := &containerd.Config{
523-
Tree: config,
524-
RuntimeType: runtimeType,
524+
Tree: config,
525+
RuntimeType: runtimeType,
526+
ContainerAnnotations: []string{"cdi.k8s.io/*"},
525527
}
526528

527529
err = UpdateConfig(v2, o)

tools/container/containerd/containerd.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ type options struct {
7272
hostRootMount string
7373
runtimeDir string
7474
useLegacyConfig bool
75+
76+
ContainerRuntimeModesCDIAnnotationPrefixes cli.StringSlice
7577
}
7678

7779
func main() {
@@ -173,6 +175,11 @@ func main() {
173175
Destination: &options.useLegacyConfig,
174176
EnvVars: []string{"CONTAINERD_USE_LEGACY_CONFIG"},
175177
},
178+
&cli.StringSliceFlag{
179+
Name: "nvidia-container-runtime-modes.cdi.annotation-prefixes",
180+
Destination: &options.ContainerRuntimeModesCDIAnnotationPrefixes,
181+
EnvVars: []string{"NVIDIA_CONTAINER_RUNTIME_MODES_CDI_ANNOTATION_PREFIXES"},
182+
},
176183
}
177184

178185
// Update the subcommand flags with the common subcommand flags
@@ -199,6 +206,7 @@ func Setup(c *cli.Context, o *options) error {
199206
containerd.WithPath(o.config),
200207
containerd.WithRuntimeType(o.runtimeType),
201208
containerd.WithUseLegacyConfig(o.useLegacyConfig),
209+
containerd.WithContainerAnnotations(o.containerAnnotationsFromCDIPrefixes()...),
202210
)
203211
if err != nil {
204212
return fmt.Errorf("unable to load config: %v", err)
@@ -241,6 +249,7 @@ func Cleanup(c *cli.Context, o *options) error {
241249
containerd.WithPath(o.config),
242250
containerd.WithRuntimeType(o.runtimeType),
243251
containerd.WithUseLegacyConfig(o.useLegacyConfig),
252+
containerd.WithContainerAnnotations(o.containerAnnotationsFromCDIPrefixes()...),
244253
)
245254
if err != nil {
246255
return fmt.Errorf("unable to load config: %v", err)
@@ -434,3 +443,13 @@ func RestartContainerdSystemd(hostRootMount string) error {
434443

435444
return nil
436445
}
446+
447+
// containerAnnotationsFromCDIPrefixes returns the container annotations to set for the given CDI prefixes.
448+
func (o *options) containerAnnotationsFromCDIPrefixes() []string {
449+
var annotations []string
450+
for _, prefix := range o.ContainerRuntimeModesCDIAnnotationPrefixes.Value() {
451+
annotations = append(annotations, prefix+"*")
452+
}
453+
454+
return annotations
455+
}

0 commit comments

Comments
 (0)