Implement function for DeviceInfo creation

- Made DeviceInfo fields private
- Implement NewDeviceInfo constructor
This commit is contained in:
Ed Bartosh 2020-02-05 17:32:20 +02:00
parent f036b72cff
commit 1f4928790f
11 changed files with 295 additions and 322 deletions

View File

@ -124,61 +124,57 @@ func getDevicesDFL() []device {
func TestGetRegionDevelTreeDFL(t *testing.T) { func TestGetRegionDevelTreeDFL(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region1", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/dfl-port.0",
{ ContainerPath: "/dev/dfl-port.0",
HostPath: "/dev/dfl-port.0", Permissions: "rw",
ContainerPath: "/dev/dfl-port.0",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-fme.0",
ContainerPath: "/dev/dfl-fme.0",
Permissions: "rw",
},
}, },
}) {
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region2", dpapi.DeviceInfo{ HostPath: "/dev/dfl-fme.0",
State: pluginapi.Healthy, ContainerPath: "/dev/dfl-fme.0",
Nodes: []pluginapi.DeviceSpec{ Permissions: "rw",
{
HostPath: "/dev/dfl-port.1",
ContainerPath: "/dev/dfl-port.1",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.2",
ContainerPath: "/dev/dfl-port.2",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-fme.1",
ContainerPath: "/dev/dfl-fme.1",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "region3", dpapi.DeviceInfo{ expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dfl-port.3", HostPath: "/dev/dfl-port.1",
ContainerPath: "/dev/dfl-port.3", ContainerPath: "/dev/dfl-port.1",
Permissions: "rw", Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.4",
ContainerPath: "/dev/dfl-port.4",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-fme.2",
ContainerPath: "/dev/dfl-fme.2",
Permissions: "rw",
},
}, },
}) {
HostPath: "/dev/dfl-port.2",
ContainerPath: "/dev/dfl-port.2",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-fme.1",
ContainerPath: "/dev/dfl-fme.1",
Permissions: "rw",
},
}
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region2", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
nodes = []pluginapi.DeviceSpec{
{
HostPath: "/dev/dfl-port.3",
ContainerPath: "/dev/dfl-port.3",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.4",
ContainerPath: "/dev/dfl-port.4",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-fme.2",
ContainerPath: "/dev/dfl-fme.2",
Permissions: "rw",
},
}
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "region3", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
result := getRegionDevelTree(getDevicesDFL()) result := getRegionDevelTree(getDevicesDFL())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
@ -188,46 +184,42 @@ func TestGetRegionDevelTreeDFL(t *testing.T) {
func TestGetRegionTreeDFL(t *testing.T) { func TestGetRegionTreeDFL(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region1", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/dfl-port.0",
{ ContainerPath: "/dev/dfl-port.0",
HostPath: "/dev/dfl-port.0", Permissions: "rw",
ContainerPath: "/dev/dfl-port.0",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region2", dpapi.DeviceInfo{ expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dfl-port.1", HostPath: "/dev/dfl-port.1",
ContainerPath: "/dev/dfl-port.1", ContainerPath: "/dev/dfl-port.1",
Permissions: "rw", Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.2",
ContainerPath: "/dev/dfl-port.2",
Permissions: "rw",
},
}, },
}) {
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "region3", dpapi.DeviceInfo{ HostPath: "/dev/dfl-port.2",
State: pluginapi.Unhealthy, ContainerPath: "/dev/dfl-port.2",
Nodes: []pluginapi.DeviceSpec{ Permissions: "rw",
{
HostPath: "/dev/dfl-port.3",
ContainerPath: "/dev/dfl-port.3",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.4",
ContainerPath: "/dev/dfl-port.4",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "region2", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
nodes = []pluginapi.DeviceSpec{
{
HostPath: "/dev/dfl-port.3",
ContainerPath: "/dev/dfl-port.3",
Permissions: "rw",
},
{
HostPath: "/dev/dfl-port.4",
ContainerPath: "/dev/dfl-port.4",
Permissions: "rw",
},
}
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "region3", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
result := getRegionTree(getDevicesDFL()) result := getRegionTree(getDevicesDFL())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
@ -237,56 +229,51 @@ func TestGetRegionTreeDFL(t *testing.T) {
func TestGetAfuTreeDFL(t *testing.T) { func TestGetAfuTreeDFL(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.0", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/dfl-port.0",
{ ContainerPath: "/dev/dfl-port.0",
HostPath: "/dev/dfl-port.0", Permissions: "rw",
ContainerPath: "/dev/dfl-port.0",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.1", dpapi.DeviceInfo{ expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.0", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dfl-port.1", HostPath: "/dev/dfl-port.1",
ContainerPath: "/dev/dfl-port.1", ContainerPath: "/dev/dfl-port.1",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.2", dpapi.DeviceInfo{
State: pluginapi.Healthy, expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
Nodes: []pluginapi.DeviceSpec{
{ nodes = []pluginapi.DeviceSpec{
HostPath: "/dev/dfl-port.2", {
ContainerPath: "/dev/dfl-port.2", HostPath: "/dev/dfl-port.2",
Permissions: "rw", ContainerPath: "/dev/dfl-port.2",
}, Permissions: "rw",
}, },
}) }
expected.AddDevice(afMode+"-"+unhealthyAfuID, "dfl-port.3", dpapi.DeviceInfo{ expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.2", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dfl-port.3", HostPath: "/dev/dfl-port.3",
ContainerPath: "/dev/dfl-port.3", ContainerPath: "/dev/dfl-port.3",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.4", dpapi.DeviceInfo{ expected.AddDevice(afMode+"-"+unhealthyAfuID, "dfl-port.3", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dfl-port.4", HostPath: "/dev/dfl-port.4",
ContainerPath: "/dev/dfl-port.4", ContainerPath: "/dev/dfl-port.4",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "dfl-port.4", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
result := getAfuTree(getDevicesDFL()) result := getAfuTree(getDevicesDFL())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {

View File

@ -82,10 +82,8 @@ func getRegionDevelTree(devices []device) dpapi.DeviceTree {
ContainerPath: region.devNode, ContainerPath: region.devNode,
Permissions: "rw", Permissions: "rw",
} }
regionTree.AddDevice(devType, region.id, dpapi.DeviceInfo{
State: health, regionTree.AddDevice(devType, region.id, dpapi.NewDeviceInfo(health, devNodes, nil, nil))
Nodes: devNodes,
})
} }
} }
@ -111,10 +109,7 @@ func getRegionTree(devices []device) dpapi.DeviceTree {
Permissions: "rw", Permissions: "rw",
} }
} }
regionTree.AddDevice(devType, region.id, dpapi.DeviceInfo{ regionTree.AddDevice(devType, region.id, dpapi.NewDeviceInfo(health, devNodes, nil, nil))
State: health,
Nodes: devNodes,
})
} }
} }
@ -133,16 +128,14 @@ func getAfuTree(devices []device) dpapi.DeviceTree {
health = pluginapi.Unhealthy health = pluginapi.Unhealthy
} }
devType := fmt.Sprintf("%s-%s", afMode, afu.afuID) devType := fmt.Sprintf("%s-%s", afMode, afu.afuID)
afuTree.AddDevice(devType, afu.id, dpapi.DeviceInfo{ devNodes := []pluginapi.DeviceSpec{
State: health, {
Nodes: []pluginapi.DeviceSpec{ HostPath: afu.devNode,
{ ContainerPath: afu.devNode,
HostPath: afu.devNode, Permissions: "rw",
ContainerPath: afu.devNode,
Permissions: "rw",
},
}, },
}) }
afuTree.AddDevice(devType, afu.id, dpapi.NewDeviceInfo(health, devNodes, nil, nil))
} }
} }
} }

View File

@ -114,51 +114,47 @@ func getDevicesOPAE() []device {
func TestGetRegionDevelTreeOPAE(t *testing.T) { func TestGetRegionDevelTreeOPAE(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.0", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/intel-fpga-port.0",
{ ContainerPath: "/dev/intel-fpga-port.0",
HostPath: "/dev/intel-fpga-port.0", Permissions: "rw",
ContainerPath: "/dev/intel-fpga-port.0",
Permissions: "rw",
},
{
HostPath: "/dev/intel-fpga-fme.0",
ContainerPath: "/dev/intel-fpga-fme.0",
Permissions: "rw",
},
}, },
}) {
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.1", dpapi.DeviceInfo{ HostPath: "/dev/intel-fpga-fme.0",
State: pluginapi.Healthy, ContainerPath: "/dev/intel-fpga-fme.0",
Nodes: []pluginapi.DeviceSpec{ Permissions: "rw",
{
HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1",
Permissions: "rw",
},
{
HostPath: "/dev/intel-fpga-fme.1",
ContainerPath: "/dev/intel-fpga-fme.1",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "intel-fpga-fme.2", dpapi.DeviceInfo{ expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.0", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.2", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.2", ContainerPath: "/dev/intel-fpga-port.1",
Permissions: "rw", Permissions: "rw",
},
{
HostPath: "/dev/intel-fpga-fme.2",
ContainerPath: "/dev/intel-fpga-fme.2",
Permissions: "rw",
},
}, },
}) {
HostPath: "/dev/intel-fpga-fme.1",
ContainerPath: "/dev/intel-fpga-fme.1",
Permissions: "rw",
},
}
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
nodes = []pluginapi.DeviceSpec{
{
HostPath: "/dev/intel-fpga-port.2",
ContainerPath: "/dev/intel-fpga-port.2",
Permissions: "rw",
},
{
HostPath: "/dev/intel-fpga-fme.2",
ContainerPath: "/dev/intel-fpga-fme.2",
Permissions: "rw",
},
}
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "intel-fpga-fme.2", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
result := getRegionDevelTree(getDevicesOPAE()) result := getRegionDevelTree(getDevicesOPAE())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
@ -168,36 +164,32 @@ func TestGetRegionDevelTreeOPAE(t *testing.T) {
func TestGetRegionTreeOPAE(t *testing.T) { func TestGetRegionTreeOPAE(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.0", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/intel-fpga-port.0",
{ ContainerPath: "/dev/intel-fpga-port.0",
HostPath: "/dev/intel-fpga-port.0", Permissions: "rw",
ContainerPath: "/dev/intel-fpga-port.0",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.1", dpapi.DeviceInfo{ expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.0", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.1", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1", ContainerPath: "/dev/intel-fpga-port.1",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "intel-fpga-fme.2", dpapi.DeviceInfo{ expected.AddDevice(regionMode+"-ce48969398f05f33946d560708be108a", "intel-fpga-fme.1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.2", HostPath: "/dev/intel-fpga-port.2",
ContainerPath: "/dev/intel-fpga-port.2", ContainerPath: "/dev/intel-fpga-port.2",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(regionMode+"-"+unhealthyInterfaceID, "intel-fpga-fme.2", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
result := getRegionTree(getDevicesOPAE()) result := getRegionTree(getDevicesOPAE())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {
@ -207,36 +199,32 @@ func TestGetRegionTreeOPAE(t *testing.T) {
func TestGetAfuTreeOPAE(t *testing.T) { func TestGetAfuTreeOPAE(t *testing.T) {
expected := dpapi.NewDeviceTree() expected := dpapi.NewDeviceTree()
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "intel-fpga-port.0", dpapi.DeviceInfo{ nodes := []pluginapi.DeviceSpec{
State: pluginapi.Healthy, {
Nodes: []pluginapi.DeviceSpec{ HostPath: "/dev/intel-fpga-port.0",
{ ContainerPath: "/dev/intel-fpga-port.0",
HostPath: "/dev/intel-fpga-port.0", Permissions: "rw",
ContainerPath: "/dev/intel-fpga-port.0",
Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "intel-fpga-port.1", dpapi.DeviceInfo{ expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "intel-fpga-port.0", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.1", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1", ContainerPath: "/dev/intel-fpga-port.1",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-"+unhealthyAfuID, "intel-fpga-port.2", dpapi.DeviceInfo{ expected.AddDevice(afMode+"-d8424dc4a4a3c413f89e433683f9040b", "intel-fpga-port.1", dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes = []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.2", HostPath: "/dev/intel-fpga-port.2",
ContainerPath: "/dev/intel-fpga-port.2", ContainerPath: "/dev/intel-fpga-port.2",
Permissions: "rw", Permissions: "rw",
},
}, },
}) }
expected.AddDevice(afMode+"-"+unhealthyAfuID, "intel-fpga-port.2", dpapi.NewDeviceInfo(pluginapi.Unhealthy, nodes, nil, nil))
result := getAfuTree(getDevicesOPAE()) result := getAfuTree(getDevicesOPAE())
if !reflect.DeepEqual(result, expected) { if !reflect.DeepEqual(result, expected) {

View File

@ -131,10 +131,7 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) {
devID := fmt.Sprintf("%s-%d", f.Name(), i) devID := fmt.Sprintf("%s-%d", f.Name(), i)
// Currently only one device type (i915) is supported. // Currently only one device type (i915) is supported.
// TODO: check model ID to differentiate device models. // TODO: check model ID to differentiate device models.
devTree.AddDevice(deviceType, devID, dpapi.DeviceInfo{ devTree.AddDevice(deviceType, devID, dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, nil, nil))
State: pluginapi.Healthy,
Nodes: nodes,
})
} }
} }
} }

View File

@ -28,7 +28,7 @@ import (
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
) )
const ( const (
@ -81,7 +81,7 @@ func newDevicePlugin(pciDriverDir, pciDeviceDir string, maxDevices int, kernelVf
} }
// Scan implements Scanner interface for vfio based QAT plugin. // Scan implements Scanner interface for vfio based QAT plugin.
func (dp *DevicePlugin) Scan(notifier deviceplugin.Notifier) error { func (dp *DevicePlugin) Scan(notifier dpapi.Notifier) error {
for { for {
devTree, err := dp.scan() devTree, err := dp.scan()
if err != nil { if err != nil {
@ -258,8 +258,8 @@ func (dp *DevicePlugin) PostAllocate(response *pluginapi.AllocateResponse) error
return nil return nil
} }
func (dp *DevicePlugin) scan() (deviceplugin.DeviceTree, error) { func (dp *DevicePlugin) scan() (dpapi.DeviceTree, error) {
devTree := deviceplugin.NewDeviceTree() devTree := dpapi.NewDeviceTree()
n := 0 n := 0
for _, driver := range append([]string{dp.dpdkDriver}, dp.kernelVfDrivers...) { for _, driver := range append([]string{dp.dpdkDriver}, dp.kernelVfDrivers...) {
files, err := ioutil.ReadDir(path.Join(dp.pciDriverDir, driver)) files, err := ioutil.ReadDir(path.Join(dp.pciDriverDir, driver))
@ -304,14 +304,10 @@ func (dp *DevicePlugin) scan() (deviceplugin.DeviceTree, error) {
return nil, err return nil, err
} }
devinfo := deviceplugin.DeviceInfo{ envs := map[string]string{
State: pluginapi.Healthy, fmt.Sprintf("%s%d", envVarPrefix, n): file.Name(),
Nodes: devNodes,
Mounts: devMounts,
Envs: map[string]string{
fmt.Sprintf("%s%d", envVarPrefix, n): file.Name(),
},
} }
devinfo := dpapi.NewDeviceInfo(pluginapi.Healthy, devNodes, devMounts, envs)
devTree.AddDevice("generic", vfpciaddr, devinfo) devTree.AddDevice("generic", vfpciaddr, devinfo)
} }

View File

@ -95,17 +95,15 @@ func getDevTree(sysfs string, qatDevs []device, config map[string]section) (dpap
devType = fmt.Sprintf("cy%d_dc%d", svalue.cryptoEngines, svalue.compressionEngines) devType = fmt.Sprintf("cy%d_dc%d", svalue.cryptoEngines, svalue.compressionEngines)
for _, ep := range svalue.endpoints { for _, ep := range svalue.endpoints {
for i := 0; i < ep.processes; i++ { for i := 0; i < ep.processes; i++ {
devTree.AddDevice(devType, fmt.Sprintf("%s_%s_%d", sname, ep.id, i), dpapi.DeviceInfo{ envs := map[string]string{
State: pluginapi.Healthy, fmt.Sprintf("QAT_SECTION_NAME_%s_%d", devType, uniqID): sname,
Nodes: devs, // This env variable may get overriden if a container requests more than one QAT process.
Envs: map[string]string{ // But we keep this code since the majority of pod workloads run only one QAT process.
fmt.Sprintf("QAT_SECTION_NAME_%s_%d", devType, uniqID): sname, // The rest should use QAT_SECTION_NAME_XXX variables.
// This env variable may get overriden if a container requests more than one QAT process. "QAT_SECTION_NAME": sname,
// But we keep this code since the majority of pod workloads run only one QAT process. }
// The rest should use QAT_SECTION_NAME_XXX variables. deviceInfo := dpapi.NewDeviceInfo(pluginapi.Healthy, devs, nil, envs)
"QAT_SECTION_NAME": sname, devTree.AddDevice(devType, fmt.Sprintf("%s_%s_%d", sname, ep.id, i), deviceInfo)
},
})
uniqID++ uniqID++
} }

View File

@ -17,10 +17,11 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"github.com/google/gousb"
"os" "os"
"time" "time"
"github.com/google/gousb"
"github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug" "github.com/intel/intel-device-plugins-for-kubernetes/pkg/debug"
dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin" dpapi "github.com/intel/intel-device-plugins-for-kubernetes/pkg/deviceplugin"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1" pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
@ -144,11 +145,7 @@ func (dp *devicePlugin) scan() (dpapi.DeviceTree, error) {
ContainerPath: hddlServicePath2, ContainerPath: hddlServicePath2,
}, },
} }
devTree.AddDevice(deviceType, devID, dpapi.DeviceInfo{ devTree.AddDevice(deviceType, devID, dpapi.NewDeviceInfo(pluginapi.Healthy, nodes, mounts, nil))
State: pluginapi.Healthy,
Nodes: nodes,
Mounts: mounts,
})
} }
} }

View File

@ -20,10 +20,27 @@ import (
// DeviceInfo contains information about device maintained by Device Plugin // DeviceInfo contains information about device maintained by Device Plugin
type DeviceInfo struct { type DeviceInfo struct {
State string state string
Nodes []pluginapi.DeviceSpec nodes []pluginapi.DeviceSpec
Mounts []pluginapi.Mount mounts []pluginapi.Mount
Envs map[string]string envs map[string]string
topology *pluginapi.TopologyInfo
}
// NewDeviceInfo makes DeviceInfo struct and adds topology information to it
func NewDeviceInfo(state string, nodes []pluginapi.DeviceSpec, mounts []pluginapi.Mount, envs map[string]string) DeviceInfo {
deviceInfo := DeviceInfo{
state: state,
nodes: nodes,
mounts: mounts,
envs: envs,
}
devPaths := []string{}
for _, node := range nodes {
devPaths = append(devPaths, node.HostPath)
}
return deviceInfo
} }
// DeviceTree contains a tree-like structure of device type -> device ID -> device info. // DeviceTree contains a tree-like structure of device type -> device ID -> device info.

View File

@ -43,8 +43,8 @@ func TestNotify(t *testing.T) {
newmap: map[string]map[string]DeviceInfo{ newmap: map[string]map[string]DeviceInfo{
"someDeviceType": { "someDeviceType": {
"intel-fpga-port.0": { "intel-fpga-port.0": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",
@ -61,8 +61,8 @@ func TestNotify(t *testing.T) {
oldmap: map[string]map[string]DeviceInfo{ oldmap: map[string]map[string]DeviceInfo{
"someDeviceType": { "someDeviceType": {
"intel-fpga-port.0": { "intel-fpga-port.0": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",
@ -75,8 +75,8 @@ func TestNotify(t *testing.T) {
newmap: map[string]map[string]DeviceInfo{ newmap: map[string]map[string]DeviceInfo{
"someDeviceType": { "someDeviceType": {
"intel-fpga-port.1": { "intel-fpga-port.1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.1", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1", ContainerPath: "/dev/intel-fpga-port.1",
@ -93,8 +93,8 @@ func TestNotify(t *testing.T) {
oldmap: map[string]map[string]DeviceInfo{ oldmap: map[string]map[string]DeviceInfo{
"someDeviceType": { "someDeviceType": {
"intel-fpga-port.0": { "intel-fpga-port.0": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",
@ -150,8 +150,8 @@ type devicePluginStub struct{}
func (*devicePluginStub) Scan(n Notifier) error { func (*devicePluginStub) Scan(n Notifier) error {
tree := NewDeviceTree() tree := NewDeviceTree()
tree.AddDevice("testdevice", "dev1", DeviceInfo{ tree.AddDevice("testdevice", "dev1", DeviceInfo{
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: make([]pluginapi.DeviceSpec, 0), nodes: make([]pluginapi.DeviceSpec, 0),
}) })
n.Notify(tree) n.Notify(tree)
return nil return nil
@ -179,8 +179,8 @@ func TestHandleUpdate(t *testing.T) {
Added: map[string]map[string]DeviceInfo{ Added: map[string]map[string]DeviceInfo{
"ce48969398f05f33946d560708be108a": { "ce48969398f05f33946d560708be108a": {
"intel-fpga-fme.0": { "intel-fpga-fme.0": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",
@ -194,8 +194,8 @@ func TestHandleUpdate(t *testing.T) {
}, },
}, },
"intel-fpga-fme.1": { "intel-fpga-fme.1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.1", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1", ContainerPath: "/dev/intel-fpga-port.1",
@ -222,8 +222,8 @@ func TestHandleUpdate(t *testing.T) {
Updated: map[string]map[string]DeviceInfo{ Updated: map[string]map[string]DeviceInfo{
"ce48969398f05f33946d560708be108a": { "ce48969398f05f33946d560708be108a": {
"intel-fpga-fme.1": { "intel-fpga-fme.1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.1", HostPath: "/dev/intel-fpga-port.1",
ContainerPath: "/dev/intel-fpga-port.1", ContainerPath: "/dev/intel-fpga-port.1",

View File

@ -83,7 +83,7 @@ func (srv *server) sendDevices(stream pluginapi.DevicePlugin_ListAndWatchServer)
for id, device := range srv.devices { for id, device := range srv.devices {
resp.Devices = append(resp.Devices, &pluginapi.Device{ resp.Devices = append(resp.Devices, &pluginapi.Device{
ID: id, ID: id,
Health: device.State, Health: device.state,
}) })
} }
debug.Print("Sending to kubelet", resp.Devices) debug.Print("Sending to kubelet", resp.Devices)
@ -120,16 +120,16 @@ func (srv *server) Allocate(ctx context.Context, rqt *pluginapi.AllocateRequest)
if !ok { if !ok {
return nil, errors.Errorf("Invalid allocation request with non-existing device %s", id) return nil, errors.Errorf("Invalid allocation request with non-existing device %s", id)
} }
if dev.State != pluginapi.Healthy { if dev.state != pluginapi.Healthy {
return nil, errors.Errorf("Invalid allocation request with unhealthy device %s", id) return nil, errors.Errorf("Invalid allocation request with unhealthy device %s", id)
} }
for i := range dev.Nodes { for i := range dev.nodes {
cresp.Devices = append(cresp.Devices, &dev.Nodes[i]) cresp.Devices = append(cresp.Devices, &dev.nodes[i])
} }
for i := range dev.Mounts { for i := range dev.mounts {
cresp.Mounts = append(cresp.Mounts, &dev.Mounts[i]) cresp.Mounts = append(cresp.Mounts, &dev.mounts[i])
} }
for key, value := range dev.Envs { for key, value := range dev.envs {
if cresp.Envs == nil { if cresp.Envs == nil {
cresp.Envs = make(map[string]string) cresp.Envs = make(map[string]string)
} }

View File

@ -120,10 +120,10 @@ func TestSetupAndServe(t *testing.T) {
devType: "testtype", devType: "testtype",
devices: map[string]DeviceInfo{ devices: map[string]DeviceInfo{
"dev1": { "dev1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
}, },
"dev2": { "dev2": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
}, },
}, },
updatesCh: make(chan map[string]DeviceInfo), updatesCh: make(chan map[string]DeviceInfo),
@ -247,8 +247,8 @@ func TestAllocate(t *testing.T) {
name: "Allocate unhealthy devices", name: "Allocate unhealthy devices",
devices: map[string]DeviceInfo{ devices: map[string]DeviceInfo{
"dev1": { "dev1": {
State: pluginapi.Unhealthy, state: pluginapi.Unhealthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dev1", HostPath: "/dev/dev1",
ContainerPath: "/dev/dev1", ContainerPath: "/dev/dev1",
@ -263,8 +263,8 @@ func TestAllocate(t *testing.T) {
name: "Allocate healthy device", name: "Allocate healthy device",
devices: map[string]DeviceInfo{ devices: map[string]DeviceInfo{
"dev1": { "dev1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dev1", HostPath: "/dev/dev1",
ContainerPath: "/dev/dev1", ContainerPath: "/dev/dev1",
@ -279,8 +279,8 @@ func TestAllocate(t *testing.T) {
name: "Allocate healthy device with postAllocate hook", name: "Allocate healthy device with postAllocate hook",
devices: map[string]DeviceInfo{ devices: map[string]DeviceInfo{
"dev1": { "dev1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dev1", HostPath: "/dev/dev1",
ContainerPath: "/dev/dev1", ContainerPath: "/dev/dev1",
@ -292,7 +292,7 @@ func TestAllocate(t *testing.T) {
Permissions: "rw", Permissions: "rw",
}, },
}, },
Mounts: []pluginapi.Mount{ mounts: []pluginapi.Mount{
{ {
HostPath: "/dev", HostPath: "/dev",
ContainerPath: "/dev", ContainerPath: "/dev",
@ -302,7 +302,7 @@ func TestAllocate(t *testing.T) {
ContainerPath: "/mnt", ContainerPath: "/mnt",
}, },
}, },
Envs: map[string]string{ envs: map[string]string{
"testname": "testvalue", "testname": "testvalue",
}, },
}, },
@ -316,8 +316,8 @@ func TestAllocate(t *testing.T) {
name: "Allocate healthy device with failing postAllocate hook", name: "Allocate healthy device with failing postAllocate hook",
devices: map[string]DeviceInfo{ devices: map[string]DeviceInfo{
"dev1": { "dev1": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/dev1", HostPath: "/dev/dev1",
ContainerPath: "/dev/dev1", ContainerPath: "/dev/dev1",
@ -418,8 +418,8 @@ func TestListAndWatch(t *testing.T) {
updates: []map[string]DeviceInfo{ updates: []map[string]DeviceInfo{
{ {
"fake_id": { "fake_id": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",
@ -435,8 +435,8 @@ func TestListAndWatch(t *testing.T) {
updates: []map[string]DeviceInfo{ updates: []map[string]DeviceInfo{
{ {
"fake_id": { "fake_id": {
State: pluginapi.Healthy, state: pluginapi.Healthy,
Nodes: []pluginapi.DeviceSpec{ nodes: []pluginapi.DeviceSpec{
{ {
HostPath: "/dev/intel-fpga-port.0", HostPath: "/dev/intel-fpga-port.0",
ContainerPath: "/dev/intel-fpga-port.0", ContainerPath: "/dev/intel-fpga-port.0",