diff --git a/pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go b/pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go index fd3d927db9..115a900c54 100644 --- a/pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go +++ b/pkg/device-plugin/nvidiadevice/nvinternal/plugin/register.go @@ -162,6 +162,7 @@ func (plugin *NvidiaDevicePlugin) getAPIDevices() *[]*api.DeviceInfo { } res = append(res, &api.DeviceInfo{ ID: UUID, + Index: idx, Count: int32(plugin.schedulerConfig.DeviceSplitCount), Devmem: registeredmem, Devcore: int32(plugin.schedulerConfig.DeviceCoreScaling * 100), diff --git a/pkg/util/util.go b/pkg/util/util.go index 5f9c392a3a..9abf76d418 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -147,6 +147,24 @@ func DecodeNodeDevices(str string) ([]*api.DeviceInfo, error) { Health: health, } retval = append(retval, &i) + } else if len(items) == 8 { + count, _ := strconv.ParseInt(items[1], 10, 32) + devmem, _ := strconv.ParseInt(items[2], 10, 32) + devcore, _ := strconv.ParseInt(items[3], 10, 32) + health, _ := strconv.ParseBool(items[6]) + numa, _ := strconv.Atoi(items[5]) + index, _ := strconv.Atoi(items[7]) + i := api.DeviceInfo{ + ID: items[0], + Count: int32(count), + Devmem: int32(devmem), + Devcore: int32(devcore), + Type: items[4], + Numa: numa, + Health: health, + Index: index, + } + retval = append(retval, &i) } else { return []*api.DeviceInfo{}, errors.New("node annotations not decode successfully") } @@ -156,10 +174,27 @@ func DecodeNodeDevices(str string) ([]*api.DeviceInfo, error) { } func EncodeNodeDevices(dlist []*api.DeviceInfo) string { - tmp := "" + builder := strings.Builder{} for _, val := range dlist { - tmp += val.ID + "," + strconv.FormatInt(int64(val.Count), 10) + "," + strconv.Itoa(int(val.Devmem)) + "," + strconv.Itoa(int(val.Devcore)) + "," + val.Type + "," + strconv.Itoa(val.Numa) + "," + strconv.FormatBool(val.Health) + OneContainerMultiDeviceSplitSymbol - } + builder.WriteString(val.ID) + builder.WriteString(",") + builder.WriteString(strconv.FormatInt(int64(val.Count), 10)) + builder.WriteString(",") + builder.WriteString(strconv.Itoa(int(val.Devmem))) + builder.WriteString(",") + builder.WriteString(strconv.Itoa(int(val.Devcore))) + builder.WriteString(",") + builder.WriteString(val.Type) + builder.WriteString(",") + builder.WriteString(strconv.Itoa(val.Numa)) + builder.WriteString(",") + builder.WriteString(strconv.FormatBool(val.Health)) + builder.WriteString(",") + builder.WriteString(strconv.Itoa(val.Index)) + builder.WriteString(OneContainerMultiDeviceSplitSymbol) + //tmp += val.ID + "," + strconv.FormatInt(int64(val.Count), 10) + "," + strconv.Itoa(int(val.Devmem)) + "," + strconv.Itoa(int(val.Devcore)) + "," + val.Type + "," + strconv.Itoa(val.Numa) + "," + strconv.FormatBool(val.Health) + "," + strconv.Itoa(val.Index) + OneContainerMultiDeviceSplitSymbol + } + tmp := builder.String() klog.Infof("Encoded node Devices: %s", tmp) return tmp } diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go index 0c4b680905..80dcdcf5e2 100644 --- a/pkg/util/util_test.go +++ b/pkg/util/util_test.go @@ -17,6 +17,7 @@ limitations under the License. package util import ( + "errors" "fmt" "testing" @@ -282,3 +283,128 @@ func TestUnMarshalNodeDevices(t *testing.T) { }) } } + +func Test_DecodeNodeDevices(t *testing.T) { + tests := []struct { + name string + args string + want struct { + di []*api.DeviceInfo + err error + } + }{ + { + name: "args is invalid", + args: "a", + want: struct { + di []*api.DeviceInfo + err error + }{ + di: []*api.DeviceInfo{}, + err: errors.New("node annotations not decode successfully"), + }, + }, + { + name: "str is old format", + args: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4,10,7680,100,NVIDIA-Tesla P4,0,true:", + want: struct { + di []*api.DeviceInfo + err error + }{ + di: []*api.DeviceInfo{ + { + ID: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4", + Index: 0, + Count: 10, + Devmem: 7680, + Devcore: 100, + Type: "NVIDIA-Tesla P4", + Numa: 0, + Health: true, + }, + }, + err: nil, + }, + }, + { + name: "str is new format", + args: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4,10,7680,100,NVIDIA-Tesla P4,0,true,1:", + want: struct { + di []*api.DeviceInfo + err error + }{ + di: []*api.DeviceInfo{ + { + ID: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4", + Index: 1, + Count: 10, + Devmem: 7680, + Devcore: 100, + Type: "NVIDIA-Tesla P4", + Numa: 0, + Health: true, + }, + }, + err: nil, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got, err := DecodeNodeDevices(test.args) + assert.DeepEqual(t, test.want.di, got) + if err != nil { + assert.DeepEqual(t, test.want.err.Error(), err.Error()) + } + }) + } +} + +func Test_EncodeNodeDevices(t *testing.T) { + tests := []struct { + name string + args []*api.DeviceInfo + want string + }{ + { + name: "old format", + args: []*api.DeviceInfo{ + { + ID: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4", + Index: 0, + Count: 10, + Devmem: 7680, + Devcore: 100, + Type: "NVIDIA-Tesla P4", + Numa: 0, + Health: true, + }, + }, + want: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4,10,7680,100,NVIDIA-Tesla P4,0,true,0:", + }, + { + name: "test two", + args: []*api.DeviceInfo{ + { + ID: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4", + Index: 1, + Count: 10, + Devmem: 7680, + Devcore: 100, + Type: "NVIDIA-Tesla P4", + Numa: 0, + Health: true, + }, + }, + want: "GPU-ebe7c3f7-303d-558d-435e-99a160631fe4,10,7680,100,NVIDIA-Tesla P4,0,true,1:", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := EncodeNodeDevices(test.args) + assert.DeepEqual(t, test.want, got) + }) + } +}