diff --git a/pkg/device/amd/device.go b/pkg/device/amd/device.go index 787210b8bf..a4f6d04d20 100644 --- a/pkg/device/amd/device.go +++ b/pkg/device/amd/device.go @@ -19,7 +19,6 @@ package amd import ( "flag" "fmt" - "slices" "strings" "github.com/Project-HAMi/HAMi/pkg/device" @@ -135,25 +134,6 @@ func (dev *AMDDevices) checkType(n device.ContainerDeviceRequest) (bool, bool, b return false, false, false } -func (dev *AMDDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[AMDUseUUID] - if ok { - klog.V(5).Infof("check uuid for AMD GPU user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[AMDNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for AMD GPU no-use uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *AMDDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return true, true } @@ -224,7 +204,7 @@ func (amddevice *AMDDevices) Fit(devices []*device.DeviceUsage, request device.C klog.V(5).InfoS(common.CardTypeMismatch, "pod", klog.KObj(pod), "device", dev.ID, dev.Type, k.Type) continue } - if !amddevice.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, AMDUseUUID, AMDNoUseUUID, amddevice.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/amd/device_test.go b/pkg/device/amd/device_test.go index f776dc05e2..aadeb397df 100644 --- a/pkg/device/amd/device_test.go +++ b/pkg/device/amd/device_test.go @@ -265,96 +265,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "no annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AMDUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: true, - }, - { - name: "use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AMDUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: false, - }, - { - name: "no use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AMDNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "no use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AMDNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := AMDDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/ascend/device.go b/pkg/device/ascend/device.go index 6f571d10b8..e90a643853 100644 --- a/pkg/device/ascend/device.go +++ b/pkg/device/ascend/device.go @@ -21,7 +21,6 @@ import ( "errors" "flag" "fmt" - "slices" "sort" "strconv" "strings" @@ -221,25 +220,6 @@ func (dev *Devices) checkType(annos map[string]string, d device.DeviceUsage, n d return false, false, false } -func (dev *Devices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[dev.useUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for ascend user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[dev.noUseUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for ascend not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *Devices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return device.CheckHealth(devType, n) } @@ -376,7 +356,7 @@ func (npu *Devices) Fit(devices []*device.DeviceUsage, request device.ContainerD prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !npu.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, npu.useUUIDAnno, npu.noUseUUIDAnno, npu.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/ascend/device_test.go b/pkg/device/ascend/device_test.go index ca82678540..76ca0b0da3 100644 --- a/pkg/device/ascend/device_test.go +++ b/pkg/device/ascend/device_test.go @@ -399,99 +399,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - dev := Devices{ - useUUIDAnno: "hami.io/use-Ascend910A-uuid", - noUseUUIDAnno: "hami.io/no-use-Ascend910A-uuid", - } - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "don't set GPUUseUUID,GPUNoUseUUID and annotation", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "set GPUUseUUID,don't set GPUNoUseUUID,annotation and device match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - dev.useUUIDAnno: "test123,111", - }, - d: device.DeviceUsage{ - ID: "test123", - }, - }, - want: true, - }, - { - name: "don't set GPUUseUUID, set GPUNoUseUUID,annotation and device match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - dev.noUseUUIDAnno: "test123,222", - }, - d: device.DeviceUsage{ - ID: "test123", - }, - }, - want: false, - }, - { - name: "set GPUUseUUID, don't set GPUNoUseUUID,annotation and device not match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - dev.useUUIDAnno: "test123,222", - }, - d: device.DeviceUsage{ - ID: "test456", - }, - }, - want: false, - }, - { - name: "don't set GPUUseUUID, set GPUNoUseUUID,annotation and device not match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - dev.noUseUUIDAnno: "test123,222", - }, - d: device.DeviceUsage{ - ID: "test456", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_CheckHealth(t *testing.T) { dev := Devices{} tests := []struct { diff --git a/pkg/device/awsneuron/device.go b/pkg/device/awsneuron/device.go index 93dcd0a1b6..ee00dc1f1f 100644 --- a/pkg/device/awsneuron/device.go +++ b/pkg/device/awsneuron/device.go @@ -19,7 +19,6 @@ package awsneuron import ( "flag" "fmt" - "slices" "strconv" "strings" "time" @@ -192,25 +191,6 @@ func (dev *AWSNeuronDevices) checkType(n device.ContainerDeviceRequest) (bool, b return false, false, false } -func (dev *AWSNeuronDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[AWSNeuronUseUUID] - if ok { - klog.V(5).Infof("check uuid for AWSNeuron user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[AWSNeuronNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for AWSNeuron no-use uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *AWSNeuronDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return true, true } @@ -433,7 +413,7 @@ func (neuron *AWSNeuronDevices) Fit(devices []*device.DeviceUsage, request devic klog.V(5).InfoS(common.CardTypeMismatch, "pod", klog.KObj(pod), "device", dev.ID, dev.Type, k.Type) continue } - if !neuron.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, AWSNeuronUseUUID, AWSNeuronNoUseUUID, neuron.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/awsneuron/device_test.go b/pkg/device/awsneuron/device_test.go index dff441023b..7940db762c 100644 --- a/pkg/device/awsneuron/device_test.go +++ b/pkg/device/awsneuron/device_test.go @@ -341,96 +341,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "no annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AWSNeuronUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: true, - }, - { - name: "use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AWSNeuronUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: false, - }, - { - name: "no use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AWSNeuronNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "no use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - AWSNeuronNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := AWSNeuronDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/cambricon/device.go b/pkg/device/cambricon/device.go index 7a9f6e0afb..0c49fde05e 100644 --- a/pkg/device/cambricon/device.go +++ b/pkg/device/cambricon/device.go @@ -22,7 +22,6 @@ import ( "flag" "fmt" "math/rand" - "slices" "strings" "time" @@ -225,25 +224,6 @@ func (dev *CambriconDevices) checkType(annos map[string]string, d device.DeviceU return false, false, false } -func (dev *CambriconDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[MLUUseUUID] - if ok { - klog.V(5).Infof("check uuid for mlu user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[MLUNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for mlu not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *CambriconDevices) GenerateResourceRequests(ctr *corev1.Container) device.ContainerDeviceRequest { klog.Info("Start to count mlu devices for container ", ctr.Name) mluResourceCount := corev1.ResourceName(MLUResourceCount) @@ -356,7 +336,7 @@ func (cam *CambriconDevices) Fit(devices []*device.DeviceUsage, request device.C prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !cam.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, MLUUseUUID, MLUNoUseUUID, cam.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/cambricon/device_test.go b/pkg/device/cambricon/device_test.go index c4fcea33a1..e94d452035 100644 --- a/pkg/device/cambricon/device_test.go +++ b/pkg/device/cambricon/device_test.go @@ -201,95 +201,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "don't set UserUUID,NoUserUUID and annotation", - args: struct { - annos map[string]string - d device.DeviceUsage - }{annos: map[string]string{}, - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "set UserUUID and annotation, don't set NoUserUUID", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "cambricon.com/use-gpuuuid": "test123,111", - }, - d: device.DeviceUsage{ - ID: "test123", - }, - }, - want: true, - }, - { - name: "don't set UserUUID, set NoUserUUID and annotation", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "cambricon.com/nouse-gpuuuid": "test123,111", - }, - d: device.DeviceUsage{ - ID: "test123", - }, - }, - want: false, - }, - { - name: "set UserUUID, don't set NoUserUUID,annotation and device not match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "cambricon.com/nouse-gpuuuid": "test123,111", - }, - d: device.DeviceUsage{ - ID: "test456", - }, - }, - want: true, - }, - { - name: "don't set UserUUID,set NoUserUUID,annotation and device not match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "cambricon.com/use-gpuuuid": "test123,111", - }, - d: device.DeviceUsage{ - ID: "test456", - }, - }, - want: false, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := CambriconDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/devices.go b/pkg/device/devices.go index 8acbd6dc32..8c7de81204 100644 --- a/pkg/device/devices.go +++ b/pkg/device/devices.go @@ -20,6 +20,7 @@ import ( "encoding/json" "errors" "fmt" + "slices" "strconv" "strings" "time" @@ -511,3 +512,22 @@ func Resourcereqs(pod *corev1.Pod) (counts PodDeviceRequests) { } return counts } + +func CheckUUID(annos map[string]string, id, useKey, noUseKey, deviceType string) bool { + userUUID, ok := annos[useKey] + if ok { + klog.V(5).Infof("check uuid for %s user uuid [%s], device id is %s", deviceType, userUUID, id) + // use , symbol to connect multiple uuid + userUUIDs := strings.Split(userUUID, ",") + return slices.Contains(userUUIDs, id) + } + + noUserUUID, ok := annos[noUseKey] + if ok { + klog.V(5).Infof("check uuid for %s not user uuid [%s], device id is %s", deviceType, noUserUUID, id) + // use , symbol to connect multiple uuid + noUserUUIDs := strings.Split(noUserUUID, ",") + return !slices.Contains(noUserUUIDs, id) + } + return true +} diff --git a/pkg/device/devices_test.go b/pkg/device/devices_test.go index 64b622c9cd..d728c6366d 100644 --- a/pkg/device/devices_test.go +++ b/pkg/device/devices_test.go @@ -801,3 +801,60 @@ func TestEncodeContainerDeviceType(t *testing.T) { }) } } + +func TestCheckUUID(t *testing.T) { + GPUUseUUID := "hami.io/gpu-use-uuid" + GPUNoUseUUID := "hami.io/gpu-no-use-uuid" + tests := []struct { + name string + annos map[string]string + id string + want bool + }{ + { + name: "don't set GPUUseUUID and GPUNoUseUUID annotation", + annos: make(map[string]string), + id: "abc", + want: true, + }, + { + name: "use set GPUUseUUID don't set GPUNoUseUUID annotation,device match", + annos: map[string]string{ + GPUUseUUID: "abc,123", + }, + id: "abc", + want: true, + }, + { + name: "use set GPUUseUUID don't set GPUNoUseUUID annotation,device don't match", + annos: map[string]string{ + GPUUseUUID: "abc,123", + }, + id: "1abc", + want: false, + }, + { + name: "use don't set GPUUseUUID set GPUNoUseUUID annotation,device match", + annos: map[string]string{ + GPUNoUseUUID: "abc,123", + }, + id: "abc", + want: false, + }, + { + name: "use don't set GPUUseUUID set GPUNoUseUUID annotation,device don't match", + annos: map[string]string{ + GPUNoUseUUID: "abc,123", + }, + id: "1abc", + want: true, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + got := CheckUUID(test.annos, test.id, GPUUseUUID, GPUNoUseUUID, "NVIDIA") + assert.Equal(t, test.want, got) + }) + } +} diff --git a/pkg/device/enflame/device.go b/pkg/device/enflame/device.go index abf857fb6b..a72294daaf 100644 --- a/pkg/device/enflame/device.go +++ b/pkg/device/enflame/device.go @@ -18,7 +18,6 @@ package enflame import ( "fmt" - "slices" "strconv" "strings" "time" @@ -162,25 +161,6 @@ func (dev *EnflameDevices) checkType(annos map[string]string, d device.DeviceUsa return false, false, false } -func (dev *EnflameDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[EnflameUseUUID] - if ok { - klog.V(5).Infof("check uuid for Enflame user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[EnflameNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for Enflame not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *EnflameDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return true, true } @@ -254,7 +234,7 @@ func (enf *EnflameDevices) Fit(devices []*device.DeviceUsage, request device.Con prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !enf.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, EnflameUseUUID, EnflameNoUseUUID, enf.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/enflame/device_test.go b/pkg/device/enflame/device_test.go index 21546496e5..714a40c631 100644 --- a/pkg/device/enflame/device_test.go +++ b/pkg/device/enflame/device_test.go @@ -275,98 +275,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "useid is same as the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "enflame.com/use-gpuuuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: true, - }, - { - name: "useid is different from the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "enflame.com/use-gpuuuid": "test2", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "no annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{ - ID: "test3", - }, - }, - want: true, - }, - { - name: "nouseid is same as the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "enflame.com/nouse-gpuuuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "nouseid is different from the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "enflame.com/nouse-gpuuuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := EnflameDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/hygon/device.go b/pkg/device/hygon/device.go index f4027fd0db..2d02b27684 100644 --- a/pkg/device/hygon/device.go +++ b/pkg/device/hygon/device.go @@ -19,7 +19,6 @@ package hygon import ( "errors" "flag" - "slices" "strings" "github.com/Project-HAMi/HAMi/pkg/device" @@ -192,25 +191,6 @@ func (dev *DCUDevices) checkType(annos map[string]string, d device.DeviceUsage, return false, false, false } -func (dev *DCUDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[DCUUseUUID] - if ok { - klog.V(5).Infof("check uuid for dcu user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[DCUNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for dcu not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *DCUDevices) GenerateResourceRequests(ctr *corev1.Container) device.ContainerDeviceRequest { klog.Info("Start to count dcu devices for container ", ctr.Name) dcuResourceCount := corev1.ResourceName(HygonResourceCount) @@ -318,7 +298,7 @@ func (dcu *DCUDevices) Fit(devices []*device.DeviceUsage, request device.Contain prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !dcu.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, DCUUseUUID, DCUNoUseUUID, dcu.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/hygon/device_test.go b/pkg/device/hygon/device_test.go index 50c566904a..0323278bd3 100644 --- a/pkg/device/hygon/device_test.go +++ b/pkg/device/hygon/device_test.go @@ -432,98 +432,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "device id the same as the dcu in use uuid", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hygon.com/use-gpuuuid": "123", - }, - d: device.DeviceUsage{ - ID: "123", - }, - }, - want: true, - }, - { - name: "device id the different from the dcu in use uuid", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hygon.com/use-gpuuuid": "123", - }, - d: device.DeviceUsage{ - ID: "456", - }, - }, - want: false, - }, - { - name: "no dcu in use uuid annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{ - ID: "456", - }, - }, - want: true, - }, - { - name: "device id the same as the dcu no use uuid", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hygon.com/nouse-gpuuuid": "123", - }, - d: device.DeviceUsage{ - ID: "123", - }, - }, - want: false, - }, - { - name: "device id the different from the dcu no use uuid", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hygon.com/nouse-gpuuuid": "123", - }, - d: device.DeviceUsage{ - ID: "456", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := DCUDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_PatchAnnotations(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/iluvatar/device.go b/pkg/device/iluvatar/device.go index 7e5ddb96d8..07f2889bb7 100644 --- a/pkg/device/iluvatar/device.go +++ b/pkg/device/iluvatar/device.go @@ -20,7 +20,6 @@ import ( "errors" "flag" "fmt" - "slices" "strings" "github.com/Project-HAMi/HAMi/pkg/device" @@ -189,25 +188,6 @@ func (dev *IluvatarDevices) checkType(annos map[string]string, d device.DeviceUs return false, false, false } -func (dev *IluvatarDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[dev.useUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for Iluvatar user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[dev.noUseUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for Iluvatar not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *IluvatarDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return device.CheckHealth(devType, n) } @@ -302,7 +282,7 @@ func (ilu *IluvatarDevices) Fit(devices []*device.DeviceUsage, request device.Co prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !ilu.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, ilu.useUUIDAnno, ilu.noUseUUIDAnno, ilu.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/iluvatar/device_test.go b/pkg/device/iluvatar/device_test.go index 05ddde5221..758676e937 100644 --- a/pkg/device/iluvatar/device_test.go +++ b/pkg/device/iluvatar/device_test.go @@ -314,108 +314,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "useid is same as the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hami.io/use-MR-V100-uuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: true, - }, - { - name: "useid is different from the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hami.io/use-MR-V100-uuid": "test2", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "no annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{ - ID: "test3", - }, - }, - want: true, - }, - { - name: "nouseid is same as the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hami.io/no-use-MR-V100-uuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "nouseid is different from the device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - "hami.io/no-use-MR-V100-uuid": "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := IluvatarDevices{ - config: IluvatarConfig{ - CommonWord: "MR-V100", - ChipName: "MR-V100", - ResourceCountName: "iluvatar.ai/MR-V100-vgpu", - ResourceMemoryName: "iluvatar.ai/MR-V100.vMem", - ResourceCoreName: "iluvatar.ai/MR-V100.vCore", - }, - useUUIDAnno: "hami.io/use-MR-V100-uuid", - noUseUUIDAnno: "hami.io/no-use-MR-V100-uuid", - } - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/kunlun/device.go b/pkg/device/kunlun/device.go index 8c096fe1a4..b08e2b14fd 100644 --- a/pkg/device/kunlun/device.go +++ b/pkg/device/kunlun/device.go @@ -18,7 +18,6 @@ package kunlun import ( "fmt" - "slices" "strings" "github.com/Project-HAMi/HAMi/pkg/device" @@ -125,25 +124,6 @@ func (dev *KunlunDevices) CheckType(annos map[string]string, d device.DeviceUsag return false, false } -func (dev *KunlunDevices) CheckUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[KunlunUseUUID] - if ok { - klog.V(5).Infof("check uuid for Kunlun user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[KunlunNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for Kunlun not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *KunlunDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return true, true } diff --git a/pkg/device/kunlun/vdevice.go b/pkg/device/kunlun/vdevice.go index 708eedc685..b3ea86550a 100644 --- a/pkg/device/kunlun/vdevice.go +++ b/pkg/device/kunlun/vdevice.go @@ -19,8 +19,6 @@ package kunlun import ( "errors" "fmt" - "slices" - "strings" "github.com/Project-HAMi/HAMi/pkg/device" "github.com/Project-HAMi/HAMi/pkg/device/common" @@ -167,25 +165,6 @@ func (dev *KunlunVDevices) CheckType(annos map[string]string, d device.DeviceUsa return false, false } -func (dev *KunlunVDevices) CheckUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[UseUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for xpu user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[NoUseUUIDAnno] - if ok { - klog.V(5).Infof("check uuid for xpu not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *KunlunVDevices) GenerateResourceRequests(ctr *corev1.Container) device.ContainerDeviceRequest { xpuResourceCount := corev1.ResourceName(KunlunResourceVCount) xpuResourceMem := corev1.ResourceName(KunlunResourceVMemory) diff --git a/pkg/device/metax/sdevice.go b/pkg/device/metax/sdevice.go index 1e86817d9f..e1cc53efaa 100644 --- a/pkg/device/metax/sdevice.go +++ b/pkg/device/metax/sdevice.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "maps" - "slices" "sort" "strconv" "strings" @@ -207,34 +206,6 @@ func (sdev *MetaxSDevices) checkType(annos map[string]string, d device.DeviceUsa return strings.Compare(n.Type, MetaxSGPUDevice) == 0 } -func (sdev *MetaxSDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - useUUIDAnno, ok := annos[MetaxUseUUID] - if ok { - klog.V(5).Infof("check UUID for metax, useUUID[%s], deviceID[%s]", useUUIDAnno, d.ID) - - useUUIDs := strings.Split(useUUIDAnno, ",") - if slices.Contains(useUUIDs, d.ID) { - klog.V(5).Infof("check UUID pass, the deviceID[%s]", d.ID) - return true - } - return false - } - - noUseUUIDAnno, ok := annos[MetaxNoUseUUID] - if ok { - klog.V(5).Infof("check UUID for metax, nouseUUID[%s], deviceID[%s]", noUseUUIDAnno, d.ID) - - noUseUUIDs := strings.Split(noUseUUIDAnno, ",") - if slices.Contains(noUseUUIDs, d.ID) { - klog.V(5).Infof("check UUID failed to pass, the deviceID[%s]", d.ID) - return false - } - return true - } - - return true -} - func (sdev *MetaxSDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { devices, _ := sdev.getMetaxSDevices(*n) @@ -370,7 +341,7 @@ func (mats *MetaxSDevices) Fit(devices []*device.DeviceUsage, request device.Con } } - if !mats.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, MetaxUseUUID, MetaxNoUseUUID, mats.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/mthreads/device.go b/pkg/device/mthreads/device.go index 9d8ce01bd4..86d9a6baf1 100644 --- a/pkg/device/mthreads/device.go +++ b/pkg/device/mthreads/device.go @@ -179,25 +179,6 @@ func (dev *MthreadsDevices) checkType(annos map[string]string, d device.DeviceUs return false, false, false } -func (dev *MthreadsDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[MthreadsUseUUID] - if ok { - klog.V(5).Infof("check uuid for Mthreads user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[MthreadsNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for Mthreads not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - return true -} - func (dev *MthreadsDevices) CheckHealth(devType string, n *corev1.Node) (bool, bool) { return true, true } @@ -316,7 +297,7 @@ func (mth *MthreadsDevices) Fit(devices []*device.DeviceUsage, request device.Co prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !mth.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, MthreadsUseUUID, MthreadsNoUseUUID, mth.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/mthreads/device_test.go b/pkg/device/mthreads/device_test.go index cc23cde34e..6b395799d6 100644 --- a/pkg/device/mthreads/device_test.go +++ b/pkg/device/mthreads/device_test.go @@ -329,96 +329,6 @@ func Test_checkType(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "no annos", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{}, - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - MthreadsUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: true, - }, - { - name: "use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - MthreadsUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: false, - }, - { - name: "no use id the same as device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - MthreadsNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test1", - }, - }, - want: false, - }, - { - name: "no use id the different from device id", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - MthreadsNoUseUUID: "test1", - }, - d: device.DeviceUsage{ - ID: "test2", - }, - }, - want: true, - }, - } - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - dev := MthreadsDevices{} - result := dev.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, result, test.want) - }) - } -} - func Test_GenerateResourceRequests(t *testing.T) { tests := []struct { name string diff --git a/pkg/device/nvidia/device.go b/pkg/device/nvidia/device.go index a5bee12a95..f2355eca28 100644 --- a/pkg/device/nvidia/device.go +++ b/pkg/device/nvidia/device.go @@ -501,26 +501,6 @@ func (dev *NvidiaGPUDevices) checkType(annos map[string]string, d device.DeviceU return false, false } -func (dev *NvidiaGPUDevices) checkUUID(annos map[string]string, d device.DeviceUsage) bool { - userUUID, ok := annos[GPUUseUUID] - if ok { - klog.V(5).Infof("check uuid for nvidia user uuid [%s], device id is %s", userUUID, d.ID) - // use , symbol to connect multiple uuid - userUUIDs := strings.Split(userUUID, ",") - return slices.Contains(userUUIDs, d.ID) - } - - noUserUUID, ok := annos[GPUNoUseUUID] - if ok { - klog.V(5).Infof("check uuid for nvidia not user uuid [%s], device id is %s", noUserUUID, d.ID) - // use , symbol to connect multiple uuid - noUserUUIDs := strings.Split(noUserUUID, ",") - return !slices.Contains(noUserUUIDs, d.ID) - } - - return true -} - func (dev *NvidiaGPUDevices) PatchAnnotations(pod *corev1.Pod, annoinput *map[string]string, pd device.PodDevices) map[string]string { devlist, ok := pd[NvidiaGPUDevice] if ok && len(devlist) > 0 { @@ -778,7 +758,7 @@ func (nv *NvidiaGPUDevices) Fit(devices []*device.DeviceUsage, request device.Co prevnuma = dev.Numa tmpDevs = make(map[string]device.ContainerDevices) } - if !nv.checkUUID(pod.GetAnnotations(), *dev) { + if !device.CheckUUID(pod.GetAnnotations(), dev.ID, GPUUseUUID, GPUNoUseUUID, nv.CommonWord()) { reason[common.CardUUIDMismatch]++ klog.V(5).InfoS(common.CardUUIDMismatch, "pod", klog.KObj(pod), "device", dev.ID, "current device info is:", *dev) continue diff --git a/pkg/device/nvidia/device_test.go b/pkg/device/nvidia/device_test.go index afb102e211..d57b9845cc 100644 --- a/pkg/device/nvidia/device_test.go +++ b/pkg/device/nvidia/device_test.go @@ -285,105 +285,6 @@ func TestMutateAdmissionDefaultsExclusiveCore(t *testing.T) { } } -func Test_checkUUID(t *testing.T) { - gpuDevices := &NvidiaGPUDevices{ - config: NvidiaConfig{ - ResourceCountName: "nvidia.com/gpu", - ResourceMemoryName: "nvidia.com/gpumem", - ResourceMemoryPercentageName: "nvidia.com/gpumem-percentage", - ResourceCoreName: "nvidia.com/gpucores", - DefaultGPUNum: int32(1), - }, - } - tests := []struct { - name string - args struct { - annos map[string]string - d device.DeviceUsage - } - want bool - }{ - { - name: "don't set GPUUseUUID and GPUNoUseUUID annotation", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: make(map[string]string), - d: device.DeviceUsage{}, - }, - want: true, - }, - { - name: "use set GPUUseUUID don't set GPUNoUseUUID annotation,device match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - GPUUseUUID: "abc,123", - }, - d: device.DeviceUsage{ - ID: "abc", - }, - }, - want: true, - }, - { - name: "use set GPUUseUUID don't set GPUNoUseUUID annotation,device don't match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - GPUUseUUID: "abc,123", - }, - d: device.DeviceUsage{ - ID: "1abc", - }, - }, - want: false, - }, - { - name: "use don't set GPUUseUUID set GPUNoUseUUID annotation,device match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - GPUNoUseUUID: "abc,123", - }, - d: device.DeviceUsage{ - ID: "abc", - }, - }, - want: false, - }, - { - name: "use don't set GPUUseUUID set GPUNoUseUUID annotation,device don't match", - args: struct { - annos map[string]string - d device.DeviceUsage - }{ - annos: map[string]string{ - GPUNoUseUUID: "abc,123", - }, - d: device.DeviceUsage{ - ID: "1abc", - }, - }, - want: true, - }, - } - - for _, test := range tests { - t.Run(test.name, func(t *testing.T) { - got := gpuDevices.checkUUID(test.args.annos, test.args.d) - assert.Equal(t, test.want, got) - }) - } -} - func Test_checkType(t *testing.T) { gpuDevices := &NvidiaGPUDevices{ config: NvidiaConfig{