Skip to content

Commit 16b84ee

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 16b84ee

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

pkg/cri/opts/spec_linux.go

Lines changed: 35 additions & 0 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"
@@ -175,3 +176,37 @@ func WithCDI(annotations map[string]string, CDIDevices []*runtime.CDIDevice) oci
175176
return oci.WithCDIDevices(devices...)(ctx, client, c, s)
176177
}
177178
}
179+
180+
func GenerateCDIDevicesOpts(containerMounts []*runtime.Mount) []*runtime.CDIDevice {
181+
// OCI Spec default Mounts
182+
defaultSpecMounts := []string{"/proc", "/run", "/dev", "/dev/pts", "/dev/shm", "/dev/mqueue", "/sys"}
183+
184+
// Default Mount skip.
185+
isDefaultSpecMount := func(defaultMnts []string, m *runtime.Mount) bool {
186+
for _, mntPath := range defaultMnts {
187+
if filepath.Clean(m.ContainerPath) == filepath.Clean(mntPath) {
188+
return true
189+
}
190+
}
191+
return false
192+
}
193+
194+
CDIDevices := []*runtime.CDIDevice{}
195+
for _, mount := range containerMounts {
196+
// Filter the default Mounts
197+
if isDefaultSpecMount(defaultSpecMounts, mount) {
198+
continue
199+
}
200+
201+
// Encoded Mount host path
202+
encodedHostPath := base64.URLEncoding.EncodeToString([]byte(mount.GetHostPath()))
203+
mntName := "kata.direct.volume/direct-volume=" + encodedHostPath
204+
cdiDevice := runtime.CDIDevice{
205+
Name: mntName,
206+
}
207+
208+
CDIDevices = append(CDIDevices, &cdiDevice)
209+
}
210+
211+
return CDIDevices
212+
}

pkg/cri/sbserver/container_create_linux.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ 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+
CDIDevices := config.CDIDevices
110+
devices := customopts.GenerateCDIDevicesOpts(config.GetMounts())
111+
CDIDevices = append(CDIDevices, devices...)
112+
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, CDIDevices))
110113
}
111114
return specOpts, nil
112115
}

pkg/cri/server/container_create_linux.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,14 @@ func (c *criService) containerSpecOpts(config *runtime.ContainerConfig, imageCon
436436
if seccompSpecOpts != nil {
437437
specOpts = append(specOpts, seccompSpecOpts)
438438
}
439+
439440
if c.config.EnableCDI {
440-
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, config.CDIDevices))
441+
CDIDevices := config.CDIDevices
442+
devices := customopts.GenerateCDIDevicesOpts(config.GetMounts())
443+
CDIDevices = append(CDIDevices, devices...)
444+
specOpts = append(specOpts, customopts.WithCDI(config.Annotations, CDIDevices))
441445
}
446+
442447
return specOpts, nil
443448
}
444449

0 commit comments

Comments
 (0)