Skip to content

Commit

Permalink
CDI implementation
Browse files Browse the repository at this point in the history
This commit implements Container Device Interface [1] support.

[1] https://github.com/container-orchestrated-devices/container-device-interface
  • Loading branch information
e0ne committed Jun 26, 2023
1 parent 7e7f979 commit b8caf78
Show file tree
Hide file tree
Showing 84 changed files with 10,536 additions and 137 deletions.
11 changes: 11 additions & 0 deletions cmd/sriovdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func flagInit(cp *cliParams) {
"JSON device pool config file location")
flag.StringVar(&cp.resourcePrefix, "resource-prefix", "intel.com",
"resource name prefix used for K8s extended resource")
flag.BoolVar(&cp.useCdi, "use-cdi", true,
"Use Container Device Interface to expose devices in containers")
flag.BoolVar(&cp.systemd, "systemd", false,
"Run as systemd service to keep CDI configuration consistent")
}

func main() {
Expand Down Expand Up @@ -63,6 +67,13 @@ func main() {
return
}

if cp.systemd {
if err := rm.createCdiSpec(); err != nil {
glog.Errorf("error CDI spec %v", err)
}
return
}

glog.Infof("Initializing resource servers")
if err := rm.initServers(); err != nil {
glog.Errorf("error initializing resource servers %v", err)
Expand Down
59 changes: 58 additions & 1 deletion cmd/sriovdp/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/golang/glog"
"github.com/jaypipes/ghw"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/cdi"

"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/factory"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
Expand All @@ -34,6 +35,8 @@ const (
type cliParams struct {
configFile string
resourcePrefix string
useCdi bool
systemd bool
}

type resourceManager struct {
Expand All @@ -53,7 +56,7 @@ func newResourceManager(cp *cliParams) *resourceManager {
glog.Infof("Using Deprecated Device Plugin Registry Path")
}

rf := factory.NewResourceFactory(cp.resourcePrefix, socketSuffix, pluginWatchMode)
rf := factory.NewResourceFactory(cp.resourcePrefix, socketSuffix, pluginWatchMode, cp.useCdi)
dp := make(map[types.DeviceType]types.DeviceProvider)
for k := range types.SupportedDevices {
dp[k] = rf.GetDeviceProvider(k)
Expand Down Expand Up @@ -100,6 +103,48 @@ func (rm *resourceManager) readConfig() error {
return nil
}

func (rm *resourceManager) createCdiSpec() error {
glog.Infof("number of config: %d\n", len(rm.configList))
deviceAllocated := make(map[string]bool)
for _, rc := range rm.configList {
// Create new ResourcePool
glog.Infof("")
glog.Infof("Creating new ResourcePool: %s", rc.ResourceName)
glog.Infof("DeviceType: %+v", rc.DeviceType)
dp, ok := rm.deviceProviders[rc.DeviceType]
if !ok {
glog.Infof("Unable to get device provider from deviceType: %s", rc.DeviceType)
return fmt.Errorf("error getting device provider")
}

devices := dp.GetDevices(rc)
filteredDevices, err := dp.GetFilteredDevices(devices, rc)
if err != nil {
glog.Errorf("createCdiSpec(): error getting filtered devices for config %+v: %q", rc, err)
}

filteredDevices = rm.excludeAllocatedDevices(filteredDevices, deviceAllocated)

if len(filteredDevices) < 1 {
glog.Infof("no devices in device pool, skipping creating resource server for %s", rc.ResourceName)
continue
}

rPool, err := rm.rFactory.GetResourcePool(rc, filteredDevices)
if err != nil {
glog.Errorf("createCdiSpec(): error creating ResourcePool with config %+v: %q", rc, err)
return err
}

err = cdi.CreateCDISpec(rm.resourcePrefix, filteredDevices, rPool)
if err != nil {
glog.Errorf("createCdiSpec(): error creating CDI spec: %v", err)
return err
}
}
return nil
}

func (rm *resourceManager) initServers() error {
rf := rm.rFactory
glog.Infof("number of config: %d\n", len(rm.configList))
Expand All @@ -120,16 +165,28 @@ func (rm *resourceManager) initServers() error {
if err != nil {
glog.Errorf("initServers(): error getting filtered devices for config %+v: %q", rc, err)
}

filteredDevices = rm.excludeAllocatedDevices(filteredDevices, deviceAllocated)

if len(filteredDevices) < 1 {
glog.Infof("no devices in device pool, skipping creating resource server for %s", rc.ResourceName)
continue
}

rPool, err := rm.rFactory.GetResourcePool(rc, filteredDevices)
if err != nil {
glog.Errorf("initServers(): error creating ResourcePool with config %+v: %q", rc, err)
return err
}

if rm.useCdi {
err = cdi.CreateCDISpec(rm.resourcePrefix, filteredDevices, rPool)
if err != nil {
glog.Errorf("initServers(): error creating CDI spec: %v", err)
return err
}
}

// Create ResourceServer with this ResourcePool
s, err := rf.GetResourceServer(rPool)
if err != nil {
Expand Down
15 changes: 11 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/Mellanox/rdmamap v1.1.0
github.com/container-orchestrated-devices/container-device-interface v0.5.4
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/jaypipes/ghw v0.9.0
github.com/jaypipes/pcidb v1.0.0
Expand All @@ -26,7 +27,7 @@ require (
github.com/containernetworking/cni v0.7.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful v2.16.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
Expand All @@ -36,8 +37,9 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
Expand All @@ -46,10 +48,15 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.4 // indirect
github.com/opencontainers/runc v1.1.2 // indirect
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb // indirect
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.9.4 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
Expand All @@ -63,7 +70,7 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
howett.net/plist v1.0.0 // indirect
k8s.io/api v0.24.0 // indirect
k8s.io/apimachinery v0.24.0 // indirect
Expand All @@ -73,7 +80,7 @@ require (
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace (
Expand Down
Loading

0 comments on commit b8caf78

Please sign in to comment.