Merge pull request #128679 from ffromani/unshare-containermap-among-managers-cleanup

node: cm: use maps.Clone instead of reinvent it
This commit is contained in:
Kubernetes Prow Robot 2025-01-30 13:05:25 -08:00 committed by GitHub
commit 4e54a67d57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 6 additions and 46 deletions

View File

@ -18,6 +18,7 @@ package containermap
import ( import (
"fmt" "fmt"
"maps"
) )
// cmItem (ContainerMap ITEM) is a pair podUID, containerName // cmItem (ContainerMap ITEM) is a pair podUID, containerName
@ -36,11 +37,7 @@ func NewContainerMap() ContainerMap {
// Clone creates a deep copy of the ContainerMap // Clone creates a deep copy of the ContainerMap
func (cm ContainerMap) Clone() ContainerMap { func (cm ContainerMap) Clone() ContainerMap {
ret := make(ContainerMap, len(cm)) return maps.Clone(cm)
for key, val := range cm {
ret[key] = val
}
return ret
} }
// Add adds a mapping of (containerID)->(podUID, containerName) to the ContainerMap // Add adds a mapping of (containerID)->(podUID, containerName) to the ContainerMap

View File

@ -20,22 +20,6 @@ import (
"testing" "testing"
) )
func TestContainerMapCloneEqual(t *testing.T) {
cm := NewContainerMap()
// add random fake data
cm.Add("fakePodUID-1", "fakeContainerName-a1", "fakeContainerID-A")
cm.Add("fakePodUID-2", "fakeContainerName-b2", "fakeContainerID-B")
cm.Add("fakePodUID-2", "fakeContainerName-c2", "fakeContainerID-C")
cm.Add("fakePodUID-3", "fakeContainerName-d3", "fakeContainerID-D")
cm.Add("fakePodUID-3", "fakeContainerName-e3", "fakeContainerID-E")
cm.Add("fakePodUID-3", "fakeContainerName-f3", "fakeContainerID-F")
cloned := cm.Clone()
if !areEqual(cm, cloned) {
t.Fatalf("clone %+#v different from original %+#v", cloned, cm)
}
}
func TestContainerMapCloneUnshared(t *testing.T) { func TestContainerMapCloneUnshared(t *testing.T) {
cm := NewContainerMap() cm := NewContainerMap()
// add random fake data // add random fake data
@ -143,19 +127,3 @@ func TestContainerMap(t *testing.T) {
} }
} }
} }
func areEqual(cm1, cm2 ContainerMap) bool {
if len(cm1) != len(cm2) {
return false
}
for key1, item1 := range cm1 {
item2, ok := cm2[key1]
if !ok {
return false
}
if item1 != item2 {
return false
}
}
return true
}

View File

@ -18,6 +18,7 @@ package cpumanager
import ( import (
"fmt" "fmt"
"maps"
"math" "math"
"sort" "sort"
@ -39,11 +40,7 @@ const (
type mapIntInt map[int]int type mapIntInt map[int]int
func (m mapIntInt) Clone() mapIntInt { func (m mapIntInt) Clone() mapIntInt {
cp := make(mapIntInt, len(m)) return maps.Clone(m)
for k, v := range m {
cp[k] = v
}
return cp
} }
func (m mapIntInt) Keys() []int { func (m mapIntInt) Keys() []int {

View File

@ -17,6 +17,7 @@ limitations under the License.
package devicemanager package devicemanager
import ( import (
"maps"
"sync" "sync"
"k8s.io/klog/v2" "k8s.io/klog/v2"
@ -429,10 +430,7 @@ func NewResourceDeviceInstances() ResourceDeviceInstances {
func (rdev ResourceDeviceInstances) Clone() ResourceDeviceInstances { func (rdev ResourceDeviceInstances) Clone() ResourceDeviceInstances {
clone := NewResourceDeviceInstances() clone := NewResourceDeviceInstances()
for resourceName, resourceDevs := range rdev { for resourceName, resourceDevs := range rdev {
clone[resourceName] = make(map[string]pluginapi.Device) clone[resourceName] = maps.Clone(resourceDevs)
for devID, dev := range resourceDevs {
clone[resourceName][devID] = dev
}
} }
return clone return clone
} }