Skip to content

Commit 02f8dcd

Browse files
committed
CDI: Add support Edit Container Mount for kata/runtime-rs using direct volume
Try to use CDI to 'patch up' the Container Mount for kata/runtime-rs using direct volume in K8S/CSI scenario. related issue and PR as below: CDI issue: cncf-tags/container-device-interface#162 CDI Mount PR: cncf-tags/container-device-interface#169 Fixes: containerd#9203 Signed-off-by: alex.lyn <[email protected]>
1 parent b30e016 commit 02f8dcd

File tree

8 files changed

+28
-9
lines changed

8 files changed

+28
-9
lines changed

cmd/ctr/commands/run/run_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ func NewContainer(ctx gocontext.Context, client *containerd.Client, context *cli
366366
if len(cdiDeviceIDs) > 0 {
367367
opts = append(opts, withStaticCDIRegistry())
368368
}
369-
opts = append(opts, oci.WithCDIDevices(cdiDeviceIDs...))
369+
opts = append(opts, oci.WithCDIDevices(cdiDeviceIDs, []string{}))
370370

371371
rootfsPropagation := context.String("rootfs-propagation")
372372
if rootfsPropagation != "" {

oci/spec_opts.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ func WithWindowsNetworkNamespace(ns string) SpecOpts {
16331633
}
16341634

16351635
// WithCDIDevices injects the requested CDI devices into the OCI specification.
1636-
func WithCDIDevices(devices ...string) SpecOpts {
1636+
func WithCDIDevices(devices, mounts []string) SpecOpts {
16371637
return func(ctx context.Context, _ Client, c *containers.Container, s *Spec) error {
16381638
if len(devices) == 0 {
16391639
return nil
@@ -1653,6 +1653,10 @@ func WithCDIDevices(devices ...string) SpecOpts {
16531653
return fmt.Errorf("CDI device injection failed: %w", err)
16541654
}
16551655

1656+
if _, err := registry.UpdateMounts(s, mounts...); err != nil {
1657+
return fmt.Errorf("CDI mount update failed: %w", err)
1658+
}
1659+
16561660
// One crucial thing to keep in mind is that CDI device injection
16571661
// might add OCI Spec environment variables, hooks, and mounts as
16581662
// well. Therefore it is important that none of the corresponding

pkg/cri/opts/spec_linux.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package opts
1818

1919
import (
2020
"context"
21+
"encoding/base64"
2122
"errors"
2223
"fmt"
2324
"os"
@@ -134,11 +135,12 @@ func IsCgroup2UnifiedMode() bool {
134135
}
135136

136137
// WithCDI updates OCI spec with CDI content
137-
func WithCDI(annotations map[string]string, CDIDevices []*runtime.CDIDevice) oci.SpecOpts {
138+
func WithCDI(annotations map[string]string, CDIDevices []*runtime.CDIDevice, CDIMounts []*runtime.Mount) oci.SpecOpts {
138139
return func(ctx context.Context, client oci.Client, c *containers.Container, s *oci.Spec) error {
139140
seen := make(map[string]bool)
140141
// Add devices from CDIDevices CRI field
141142
var devices []string
143+
var mounts []string
142144
var err error
143145
for _, device := range CDIDevices {
144146
deviceName := device.Name
@@ -172,6 +174,19 @@ func WithCDI(annotations map[string]string, CDIDevices []*runtime.CDIDevice) oci
172174
log.G(ctx).Debug("Passing CDI devices as annotations will be deprecated soon, please use CRI CDIDevices instead")
173175
}
174176

175-
return oci.WithCDIDevices(devices...)(ctx, client, c, s)
177+
for _, mount := range CDIMounts {
178+
encodedMntPath := base64.StdEncoding.EncodeToString([]byte(mount.HostPath))
179+
// encoded mount path
180+
mntName := "direct.volume/direct-volume=" + encodedMntPath
181+
if seen[mntName] {
182+
log.G(ctx).Debugf("Skipping duplicated CDI Mount %s", mntName)
183+
continue
184+
}
185+
186+
mounts = append(mounts, mntName)
187+
seen[mntName] = true
188+
}
189+
190+
return oci.WithCDIDevices(devices, mounts)(ctx, client, c, s)
176191
}
177192
}

pkg/cri/opts/spec_nonlinux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func SwapControllerAvailable() bool {
3535
}
3636

3737
// WithCDI does nothing on non-Linux platforms.
38-
func WithCDI(_ map[string]string, _ []*runtime.CDIDevice) oci.SpecOpts {
38+
func WithCDI(_ map[string]string, _ []*runtime.CDIDevice, CDIMounts []*runtime.Mount) oci.SpecOpts {
3939
return func(ctx context.Context, client oci.Client, container *containers.Container, spec *oci.Spec) error {
4040
return nil
4141
}

pkg/cri/sbserver/container_create_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
106106
specOpts = append(specOpts, seccompSpecOpts)
107107
}
108108
if c.config.EnableCDI {
109-
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, config.CDIDevices))
109+
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, config.CDIDevices, config.GetMounts()))
110110
}
111111
return specOpts, nil
112112
}

pkg/cri/sbserver/container_create_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ containerEdits:
20542054
err = reg.Configure(cdi.WithSpecDirs(cdiDir))
20552055
require.NoError(t, err)
20562056

2057-
injectFun := customopts.WithCDI(test.annotations, test.cdiDevices)
2057+
injectFun := customopts.WithCDI(test.annotations, test.cdiDevices, []*runtime.Mount{})
20582058
err = injectFun(ctx, nil, testContainer, spec)
20592059
assert.Equal(t, test.expectError, err != nil)
20602060

pkg/cri/server/container_create_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
437437
specOpts = append(specOpts, seccompSpecOpts)
438438
}
439439
if c.config.EnableCDI {
440-
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, config.CDIDevices))
440+
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, config.CDIDevices, config.GetMounts()))
441441
}
442442
return specOpts, nil
443443
}

pkg/cri/server/container_create_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2292,7 +2292,7 @@ containerEdits:
22922292
err = reg.Configure(cdi.WithSpecDirs(cdiDir))
22932293
require.NoError(t, err)
22942294

2295-
injectFun := customopts.WithCDI(test.annotations, test.cdiDevices)
2295+
injectFun := customopts.WithCDI(test.annotations, test.cdiDevices, []*runtime.Mount{})
22962296
err = injectFun(ctx, nil, testContainer, spec)
22972297
assert.Equal(t, test.expectError, err != nil)
22982298

0 commit comments

Comments
 (0)