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"
)
type pluginMode int
type pluginMode string
const (
wrongMode pluginMode = iota
afMode
regionMode
afMode pluginMode = "af"
regionMode pluginMode = "region"
)
func parseMode(input string) pluginMode {
switch input {
case "af":
return afMode
case "region":
return regionMode
}
return wrongMode
func isValidPluginMode(m pluginMode) bool {
return m == afMode || m == regionMode
}
// deviceManager manages Intel FPGA devices.
@ -122,7 +114,7 @@ func discoverFPGAs(sysfsDir string, devfsDir string, mode pluginMode) (map[strin
if err != nil {
return nil, err
}
interfaceId = strings.TrimSpace(string(data))
interfaceId = fmt.Sprintf("%s-%s", mode, strings.TrimSpace(string(data)))
fpgaNodes[interfaceId] = append(fpgaNodes[interfaceId], name)
}
}
@ -143,7 +135,7 @@ func discoverFPGAs(sysfsDir string, devfsDir string, mode pluginMode) (map[strin
if err != nil {
return nil, err
}
afuID := strings.TrimSpace(string(data))
afuID := fmt.Sprintf("%s-%s", mode, strings.TrimSpace(string(data)))
fpgaNodes[afuID] = append(fpgaNodes[afuID], name)
default:
glog.Fatal("Unsupported mode")
@ -229,16 +221,16 @@ func (dm *deviceManager) PreStartContainer(ctx context.Context, rqt *pluginapi.P
func main() {
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()
mode := parseMode(modeStr)
if mode == wrongMode {
mode := pluginMode(modeStr)
if !isValidPluginMode(mode) {
glog.Error("Wrong mode: ", modeStr)
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)
if err != nil {

View File

@ -116,7 +116,7 @@ func TestDiscoverFPGAs(t *testing.T) {
"intel-fpga-port.2", "intel-fpga-fme.2",
},
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{
State: "Healthy",
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{
State: "Healthy",
Nodes: []string{
@ -187,7 +187,7 @@ func TestDiscoverFPGAs(t *testing.T) {
"intel-fpga-port.2", "intel-fpga-fme.2",
},
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{
State: "Healthy",
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{
State: "Healthy",
Nodes: []string{
@ -336,8 +336,8 @@ func TestListAndWatch(t *testing.T) {
},
}
resourceName := resourceNamePrefix + "-" + afuID
testDM := newDeviceManager(resourceName, afuID, tmpdir, afMode)
resourceName := fmt.Sprintf("%s%s-%s", resourceNamePrefix, afMode, afuID)
testDM := newDeviceManager(resourceName, fmt.Sprintf("%s-%s", afMode, afuID), tmpdir, afMode)
if testDM == nil {
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 {
input string
output pluginMode
input pluginMode
output bool
}{
{
input: "af",
output: afMode,
input: afMode,
output: true,
},
{
input: "region",
output: regionMode,
input: regionMode,
output: true,
},
{
input: "unparsable",
output: wrongMode,
input: pluginMode("unparsable"),
output: false,
},
}
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)
}
}