fpga: add prefix to FPGA resource name

Added mode ("af" or "region") prefix to the resource name to
distingush between announced functions and regions, e.g.
 intel.com/fpga-af-f7df405cbd7acf7222f144b0b93acd18
 intel.com/fpga-region-ce48969398f05f33946d560708be108a
This commit is contained in:
Ed Bartosh 2018-05-23 16:59:44 +03:00
parent 6c91dbd8f5
commit 8a8971ed5c
2 changed files with 27 additions and 35 deletions

View File

@ -46,23 +46,15 @@ const (
resourceNamePrefix = "intel.com/fpga" resourceNamePrefix = "intel.com/fpga"
) )
type pluginMode int type pluginMode string
const ( const (
wrongMode pluginMode = iota afMode pluginMode = "af"
afMode regionMode pluginMode = "region"
regionMode
) )
func parseMode(input string) pluginMode { func isValidPluginMode(m pluginMode) bool {
switch input { return m == afMode || m == regionMode
case "af":
return afMode
case "region":
return regionMode
}
return wrongMode
} }
// deviceManager manages Intel FPGA devices. // deviceManager manages Intel FPGA devices.
@ -122,7 +114,7 @@ func discoverFPGAs(sysfsDir string, devfsDir string, mode pluginMode) (map[strin
if err != nil { if err != nil {
return nil, err return nil, err
} }
interfaceId = strings.TrimSpace(string(data)) interfaceId = fmt.Sprintf("%s-%s", mode, strings.TrimSpace(string(data)))
fpgaNodes[interfaceId] = append(fpgaNodes[interfaceId], name) fpgaNodes[interfaceId] = append(fpgaNodes[interfaceId], name)
} }
} }
@ -143,7 +135,7 @@ func discoverFPGAs(sysfsDir string, devfsDir string, mode pluginMode) (map[strin
if err != nil { if err != nil {
return nil, err return nil, err
} }
afuID := strings.TrimSpace(string(data)) afuID := fmt.Sprintf("%s-%s", mode, strings.TrimSpace(string(data)))
fpgaNodes[afuID] = append(fpgaNodes[afuID], name) fpgaNodes[afuID] = append(fpgaNodes[afuID], name)
default: default:
glog.Fatal("Unsupported mode") glog.Fatal("Unsupported mode")
@ -229,16 +221,16 @@ func (dm *deviceManager) PreStartContainer(ctx context.Context, rqt *pluginapi.P
func main() { func main() {
var modeStr string var modeStr string
flag.StringVar(&modeStr, "mode", "af", "device plugin mode: 'af' (default) or 'region'") flag.StringVar(&modeStr, "mode", string(afMode), fmt.Sprintf("device plugin mode: '%s' (default) or '%s'", afMode, regionMode))
flag.Parse() flag.Parse()
mode := parseMode(modeStr) mode := pluginMode(modeStr)
if mode == wrongMode { if !isValidPluginMode(mode) {
glog.Error("Wrong mode: ", modeStr) glog.Error("Wrong mode: ", modeStr)
os.Exit(1) os.Exit(1)
} }
fmt.Println("FPGA device plugin started in", modeStr, "mode") fmt.Println("FPGA device plugin started in", mode, "mode")
devs, err := discoverFPGAs(sysfsDirectory, devfsDirectory, mode) devs, err := discoverFPGAs(sysfsDirectory, devfsDirectory, mode)
if err != nil { if err != nil {

View File

@ -116,7 +116,7 @@ func TestDiscoverFPGAs(t *testing.T) {
"intel-fpga-port.2", "intel-fpga-fme.2", "intel-fpga-port.2", "intel-fpga-fme.2",
}, },
expectedResult: map[string]map[string]deviceplugin.DeviceInfo{ expectedResult: map[string]map[string]deviceplugin.DeviceInfo{
"d8424dc4a4a3c413f89e433683f9040b": map[string]deviceplugin.DeviceInfo{ fmt.Sprintf("%s-d8424dc4a4a3c413f89e433683f9040b", afMode): map[string]deviceplugin.DeviceInfo{
"intel-fpga-dev.0": deviceplugin.DeviceInfo{ "intel-fpga-dev.0": deviceplugin.DeviceInfo{
State: "Healthy", State: "Healthy",
Nodes: []string{ Nodes: []string{
@ -130,7 +130,7 @@ func TestDiscoverFPGAs(t *testing.T) {
}, },
}, },
}, },
"47595d0fae972fbed0c51b4a41c7a349": map[string]deviceplugin.DeviceInfo{ fmt.Sprintf("%s-47595d0fae972fbed0c51b4a41c7a349", afMode): map[string]deviceplugin.DeviceInfo{
"intel-fpga-dev.2": deviceplugin.DeviceInfo{ "intel-fpga-dev.2": deviceplugin.DeviceInfo{
State: "Healthy", State: "Healthy",
Nodes: []string{ Nodes: []string{
@ -187,7 +187,7 @@ func TestDiscoverFPGAs(t *testing.T) {
"intel-fpga-port.2", "intel-fpga-fme.2", "intel-fpga-port.2", "intel-fpga-fme.2",
}, },
expectedResult: map[string]map[string]deviceplugin.DeviceInfo{ expectedResult: map[string]map[string]deviceplugin.DeviceInfo{
"ce48969398f05f33946d560708be108a": map[string]deviceplugin.DeviceInfo{ fmt.Sprintf("%s-ce48969398f05f33946d560708be108a", regionMode): map[string]deviceplugin.DeviceInfo{
"intel-fpga-dev.0": deviceplugin.DeviceInfo{ "intel-fpga-dev.0": deviceplugin.DeviceInfo{
State: "Healthy", State: "Healthy",
Nodes: []string{ Nodes: []string{
@ -203,7 +203,7 @@ func TestDiscoverFPGAs(t *testing.T) {
}, },
}, },
}, },
"fd967345645f05f338462a0748be0091": map[string]deviceplugin.DeviceInfo{ fmt.Sprintf("%s-fd967345645f05f338462a0748be0091", regionMode): map[string]deviceplugin.DeviceInfo{
"intel-fpga-dev.2": deviceplugin.DeviceInfo{ "intel-fpga-dev.2": deviceplugin.DeviceInfo{
State: "Healthy", State: "Healthy",
Nodes: []string{ Nodes: []string{
@ -336,8 +336,8 @@ func TestListAndWatch(t *testing.T) {
}, },
} }
resourceName := resourceNamePrefix + "-" + afuID resourceName := fmt.Sprintf("%s%s-%s", resourceNamePrefix, afMode, afuID)
testDM := newDeviceManager(resourceName, afuID, tmpdir, afMode) testDM := newDeviceManager(resourceName, fmt.Sprintf("%s-%s", afMode, afuID), tmpdir, afMode)
if testDM == nil { if testDM == nil {
t.Fatal("Failed to create a deviceManager") t.Fatal("Failed to create a deviceManager")
} }
@ -410,27 +410,27 @@ func TestAllocate(t *testing.T) {
} }
} }
func TestParseMode(t *testing.T) { func TestisValidPluginMode(t *testing.T) {
tcases := []struct { tcases := []struct {
input string input pluginMode
output pluginMode output bool
}{ }{
{ {
input: "af", input: afMode,
output: afMode, output: true,
}, },
{ {
input: "region", input: regionMode,
output: regionMode, output: true,
}, },
{ {
input: "unparsable", input: pluginMode("unparsable"),
output: wrongMode, output: false,
}, },
} }
for _, tcase := range tcases { for _, tcase := range tcases {
if parseMode(tcase.input) != tcase.output { if isValidPluginMode(tcase.input) != tcase.output {
t.Error("Wrong output", tcase.output, "for the given input", tcase.input) t.Error("Wrong output", tcase.output, "for the given input", tcase.input)
} }
} }