Skip to content

Commit

Permalink
runtime-tools/generate: add support CDI injection.
Browse files Browse the repository at this point in the history
Signed-off-by: Krisztian Litkey <krisztian.litkey@intel.com>
  • Loading branch information
klihub committed Aug 12, 2024
1 parent dc3c5bd commit 646502f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/runtime-tools/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Generator struct {
filterAnnotations func(map[string]string) (map[string]string, error)
resolveBlockIO func(string) (*rspec.LinuxBlockIO, error)
resolveRdt func(string) (*rspec.LinuxIntelRdt, error)
injectCDIDevices func(*rspec.Spec, []string) error
checkResources func(*rspec.LinuxResources) error
}

Expand Down Expand Up @@ -91,6 +92,14 @@ func WithResourceChecker(fn func(*rspec.LinuxResources) error) GeneratorOption {
}
}

// WithCDIDeviceInjector specifies a runtime-specific function to use for CDI
// device resolution and injection into an OCI Spec.
func WithCDIDeviceInjector(fn func(*rspec.Spec, []string) error) GeneratorOption {
return func(g *Generator) {
g.injectCDIDevices = fn
}
}

// Adjust adjusts all aspects of the OCI Spec that NRI knows/cares about.
func (g *Generator) Adjust(adjust *nri.ContainerAdjustment) error {
if adjust == nil {
Expand All @@ -103,6 +112,9 @@ func (g *Generator) Adjust(adjust *nri.ContainerAdjustment) error {
g.AdjustEnv(adjust.GetEnv())
g.AdjustHooks(adjust.GetHooks())
g.AdjustDevices(adjust.GetLinux().GetDevices())
if err := g.InjectCDIDevices(adjust.GetCDIDevices()); err != nil {
return err
}
g.AdjustCgroupsPath(adjust.GetLinux().GetCgroupsPath())

resources := adjust.GetLinux().GetResources()
Expand Down Expand Up @@ -325,6 +337,23 @@ func (g *Generator) AdjustDevices(devices []*nri.LinuxDevice) {
}
}

// InjectCDIDevices injects the given CDI devices into the OCI Spec.
// Devices are given by their fully qualified CDI device names. The
// actual device injection is done using a runtime-specific CDI
// injection function, set using the WithCDIDeviceInjector option.
func (g *Generator) InjectCDIDevices(devices []*nri.CDIDevice) error {
if len(devices) == 0 || g.injectCDIDevices == nil {
return nil
}

names := []string{}
for _, d := range devices {
names = append(names, d.Name)
}

return g.injectCDIDevices(g.Config, names)
}

func (g *Generator) AdjustRlimits(rlimits []*nri.POSIXRlimit) error {
for _, l := range rlimits {
if l == nil {
Expand Down

0 comments on commit 646502f

Please sign in to comment.