Skip to content

Commit

Permalink
feat(util): enable usage of ContainerDevice Idx
Browse files Browse the repository at this point in the history
- Completed TODO: current Idx cannot use, because EncodeContainerDevices method

- Updated test cases to include `index` in the `want` structure.
- Ensured better coverage of scenarios involving `index` handling.

chore: update .gitignore to ignore build and test output

- Added `_output/` directory to `.gitignore` to ignore generated build artifacts.
- Added `coverage.out` to `.gitignore` to ignore test coverage reports.

Signed-off-by: elrondwong <[email protected]>
  • Loading branch information
elrondwong committed Dec 26, 2024
1 parent 311d18b commit 89f62f5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ libvgpu.so
.idea
vendor
license
vgpuvalidator
vgpuvalidator
_output/
coverage.out
4 changes: 2 additions & 2 deletions pkg/device/ascend/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ func Test_PatchAnnotations(t *testing.T) {
},
},
want: map[string]string{
util.InRequestDevices[dev.config.CommonWord]: "device-0,Ascend,8738,1:;",
util.SupportDevices[dev.config.CommonWord]: "device-0,Ascend,8738,1:;",
util.InRequestDevices[dev.config.CommonWord]: "device-0,Ascend,8738,1,0:;",
util.SupportDevices[dev.config.CommonWord]: "device-0,Ascend,8738,1,0:;",
"predicate-time": strconv.FormatInt(time.Now().Unix(), 10),
"huawei.com/Ascend910A": "[{\"UUID\":\"device-0\",\"temp\":\"vir08\"}]",
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/device/iluvatar/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func TestPatchAnnotations(t *testing.T) {
},
},
expected: map[string]string{
util.InRequestDevices[IluvatarGPUDevice]: "k8s-gpu-iluvatar-0,Iluvatar,0,0:;",
util.SupportDevices[IluvatarGPUDevice]: "k8s-gpu-iluvatar-0,Iluvatar,0,0:;",
util.InRequestDevices[IluvatarGPUDevice]: "k8s-gpu-iluvatar-0,Iluvatar,0,0,0:;",
util.SupportDevices[IluvatarGPUDevice]: "k8s-gpu-iluvatar-0,Iluvatar,0,0,0:;",
"iluvatar.ai/gpu-assigned": "false",
"iluvatar.ai/predicate-time": strconv.FormatInt(time.Now().UnixNano(), 10),
IluvatarDeviceSelection + "0": "0",
Expand Down
4 changes: 2 additions & 2 deletions pkg/device/nvidia/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ func Test_PatchAnnotations(t *testing.T) {
},
},
want: map[string]string{
util.InRequestDevices[NvidiaGPUDevice]: "nvidia-device-0,NVIDIA,2000,1:;",
util.SupportDevices[NvidiaGPUDevice]: "nvidia-device-0,NVIDIA,2000,1:;",
util.InRequestDevices[NvidiaGPUDevice]: "nvidia-device-0,NVIDIA,2000,1,0:;",
util.SupportDevices[NvidiaGPUDevice]: "nvidia-device-0,NVIDIA,2000,1,0:;",
},
},
{
Expand Down
1 change: 0 additions & 1 deletion pkg/util/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ var (
)

type ContainerDevice struct {
// TODO current Idx cannot use, because EncodeContainerDevices method not encode this filed.
Idx int
UUID string
Type string
Expand Down
37 changes: 23 additions & 14 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ func UnMarshalNodeDevices(str string) ([]*DeviceInfo, error) {
func EncodeContainerDevices(cd ContainerDevices) string {
tmp := ""
for _, val := range cd {
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," + strconv.Itoa(int(val.Usedcores)) + OneContainerMultiDeviceSplitSymbol
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," +
strconv.Itoa(int(val.Usedcores)) + "," + strconv.Itoa(val.Idx) + OneContainerMultiDeviceSplitSymbol
}
klog.Infof("Encoded container Devices: %s", tmp)
return tmp
Expand All @@ -218,7 +219,8 @@ func EncodeContainerDeviceType(cd ContainerDevices, t string) string {
tmp := ""
for _, val := range cd {
if strings.Compare(val.Type, t) == 0 {
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," + strconv.Itoa(int(val.Usedcores))
tmp += val.UUID + "," + val.Type + "," + strconv.Itoa(int(val.Usedmem)) + "," +
strconv.Itoa(int(val.Usedcores)) + "," + strconv.Itoa(val.Idx)

Check warning on line 223 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L222-L223

Added lines #L222 - L223 were not covered by tests
}
tmp += OneContainerMultiDeviceSplitSymbol
}
Expand Down Expand Up @@ -258,20 +260,27 @@ func DecodeContainerDevices(str string) (ContainerDevices, error) {
return ContainerDevices{}, nil
}
for _, val := range cd {
if strings.Contains(val, ",") {
//fmt.Println("cd is ", val)
tmpstr := strings.Split(val, ",")
if len(tmpstr) < 4 {
return ContainerDevices{}, fmt.Errorf("pod annotation format error; information missing, please do not use nodeName field in task")
if !strings.Contains(val, ",") {
continue
}
tmpstr := strings.Split(val, ",")
if len(tmpstr) < 4 {
return ContainerDevices{}, fmt.Errorf("pod annotation format error; information missing, please do not use nodeName field in task")
}

Check warning on line 269 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L268-L269

Added lines #L268 - L269 were not covered by tests
tmpdev.UUID = tmpstr[0]
tmpdev.Type = tmpstr[1]
devmem, _ := strconv.ParseInt(tmpstr[2], 10, 32)
tmpdev.Usedmem = int32(devmem)
devcores, _ := strconv.ParseInt(tmpstr[3], 10, 32)
tmpdev.Usedcores = int32(devcores)
if len(tmpstr) == 5 {
idx, err := strconv.Atoi(tmpstr[4])
if err != nil {
return ContainerDevices{}, fmt.Errorf("invalid index value in pod annotation: %v", err)

Check warning on line 279 in pkg/util/util.go

View check run for this annotation

Codecov / codecov/patch

pkg/util/util.go#L279

Added line #L279 was not covered by tests
}
tmpdev.UUID = tmpstr[0]
tmpdev.Type = tmpstr[1]
devmem, _ := strconv.ParseInt(tmpstr[2], 10, 32)
tmpdev.Usedmem = int32(devmem)
devcores, _ := strconv.ParseInt(tmpstr[3], 10, 32)
tmpdev.Usedcores = int32(devcores)
contdev = append(contdev, tmpdev)
tmpdev.Idx = idx
}
contdev = append(contdev, tmpdev)
}
klog.V(5).Infof("Finished decoding container devices. Total devices: %d", len(contdev))
return contdev, nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ func TestExtractMigTemplatesFromUUID(t *testing.T) {
}

func TestEmptyContainerDevicesCoding(t *testing.T) {
cd1 := ContainerDevices{}
cd1 := ContainerDevices{
ContainerDevice{0, "UUID1", "Type1", 1000, 30},
ContainerDevice{1, "UUID2", "Type1", 1000, 30},
}
s := EncodeContainerDevices(cd1)
fmt.Println(s)
cd2, _ := DecodeContainerDevices(s)
Expand Down

0 comments on commit 89f62f5

Please sign in to comment.