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 Sep 22, 2023
1 parent 70efb55 commit 69fa533
Show file tree
Hide file tree
Showing 105 changed files with 10,892 additions and 205 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ mockery: | $(BASE) $(GOMOCKERY) ; $(info Running mockery...) @ ## Run golint on
# $Q cd $(BASE)/pkg/types && rm -rf mocks && $(GOMOCKERY) --all 2>/dev/null
$Q $(GOMOCKERY) --name=".*" --dir=pkg/types --output=pkg/types/mocks --recursive=false --log-level=debug
$Q $(GOMOCKERY) --name=".*" --dir=pkg/utils --output=pkg/utils/mocks --recursive=false --log-level=debug
$Q $(GOMOCKERY) --name=".*" --dir=pkg/cdi --output=pkg/cdi/mocks --recursive=false --log-level=debug

.PHONY: help
help: ; @ ## Display this help message
Expand Down
7 changes: 6 additions & 1 deletion cmd/sriovdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ const (
defaultConfig = "/etc/pcidp/config.json"
)

// Parse Command line flags
// flagInit parse command line flags
func flagInit(cp *cliParams) {
flag.StringVar(&cp.configFile, "config-file", defaultConfig,
"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", false,
"Use Container Device Interface to expose devices in containers")
}

func main() {
Expand Down Expand Up @@ -86,4 +88,7 @@ func main() {
if err := rm.stopAllServers(); err != nil {
glog.Errorf("stopping servers produced error: %s", err.Error())
}
if err := rm.cleanupCDISpecs(); err != nil {
glog.Errorf("cleaning up CDI Specs produced error: %s", err.Error())
}
}
25 changes: 23 additions & 2 deletions cmd/sriovdp/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"os"

cdiPkg "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/cdi"

"github.com/golang/glog"
"github.com/jaypipes/ghw"

Expand All @@ -31,20 +33,25 @@ const (
socketSuffix = "sock"
)

// cliParams presents CLI parameters for SR-IOV Network Device Plugin
type cliParams struct {
configFile string
resourcePrefix string
useCdi bool
}

// resourceManager manages resources for SR-IOV Network Device Plugin binaries
type resourceManager struct {
cliParams
pluginWatchMode bool
rFactory types.ResourceFactory
configList []*types.ResourceConfig
resourceServers []types.ResourceServer
deviceProviders map[types.DeviceType]types.DeviceProvider
cdi cdiPkg.CDI
}

// newResourceManager initiates a new instance of resourceManager
func newResourceManager(cp *cliParams) *resourceManager {
pluginWatchMode := utils.DetectPluginWatchMode(types.SockDir)
if pluginWatchMode {
Expand All @@ -53,7 +60,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 All @@ -64,6 +71,7 @@ func newResourceManager(cp *cliParams) *resourceManager {
pluginWatchMode: pluginWatchMode,
rFactory: rf,
deviceProviders: dp,
cdi: cdiPkg.New(),
}
}

Expand Down Expand Up @@ -101,12 +109,16 @@ func (rm *resourceManager) readConfig() error {
}

func (rm *resourceManager) initServers() error {
err := rm.cleanupCDISpecs()
if err != nil {
glog.Errorf("Unable to delete CDI specs: %s", err)
return err
}
rf := rm.rFactory
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]
Expand Down Expand Up @@ -249,3 +261,12 @@ func (rm *resourceManager) discoverHostDevices() error {
}
return nil
}

func (rm *resourceManager) cleanupCDISpecs() error {
if rm.cliParams.useCdi {
if err := rm.cdi.CleanupSpecs(); err != nil {
return fmt.Errorf("unable to delete CDI specs: %v", err)
}
}
return nil
}
23 changes: 22 additions & 1 deletion cmd/sriovdp/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"os"
"testing"

CDImocks "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/cdi/mocks"

"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/factory"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/netdevice"
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
Expand Down Expand Up @@ -441,7 +443,7 @@ var _ = Describe("Resource manager", func() {
_ = os.Unsetenv("GHW_CHROOT")
}()

rf := factory.NewResourceFactory("fake", "fake", true)
rf := factory.NewResourceFactory("fake", "fake", true, false)

rm := &resourceManager{
rFactory: rf,
Expand Down Expand Up @@ -615,5 +617,24 @@ var _ = Describe("Resource manager", func() {
Expect(err).To(HaveOccurred())
})
})
Context("when resource servers cleans CDI specs ", func() {
rs := &mocks.ResourceServer{}
rs.On("Stop").Return(nil)

cp = &cliParams{
configFile: "/tmp/sriovdp/test_config",
resourcePrefix: "test_",
useCdi: true,
}
rm = newResourceManager(cp)
cdi := &CDImocks.CDI{}
cdi.On("CleanupSpecs").Return(nil)
rm.cdi = &CDImocks.CDI{}

err := rm.stopAllServers()
It("shouldn't fail", func() {
Expect(err).NotTo(HaveOccurred())
})
})
})
})
11 changes: 11 additions & 0 deletions deployments/cdi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# SR-IOV Network Device Plugin in CDI mode deployment

SR-IOV Network Device Plugin supports [Container Device Interface (CDI)](https://github.com/container-orchestrated-devices/container-device-interface).

To enable CDI mode, SR-IOV Network Device Plugin should be started with `--use-cdi` CLI argument.
This mode has different deployment requirements: `sriovdp-daemonset.yaml`

```yaml
- mountPath: /var/run/cdi
name: dynamic-cdi
```
Loading

0 comments on commit 69fa533

Please sign in to comment.