Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions pkg/kubelet/cm/containermap/container_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package containermap

import (
"fmt"
"maps"
)

// cmItem (ContainerMap ITEM) is a pair podUID, containerName
Expand All @@ -36,11 +37,7 @@ func NewContainerMap() ContainerMap {

// Clone creates a deep copy of the ContainerMap
func (cm ContainerMap) Clone() ContainerMap {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personally I'd drop this function entirely as it just uses a stdlib function anyway

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it. I'd argue that if we bothered to create a type around essentially a map, we should not break the abstraction, and if so we need the trivial Clone method.

Other (more far-fetching?) options:

  • dissolve ContainerMap and make use an actual map + helper functions
  • make ContainerMap more opaque, fully wrap it into a struct, to open the option in the future to add locks (spoiler: node: cm: don't share containerMap instances between managers #128657 was caused largely because I incorrectly remembered ContainerMap is more than such a thin wrapper. I confused it with pod_devices)

I'm open to conversation here.

ret := make(ContainerMap, len(cm))
for key, val := range cm {
ret[key] = val
}
return ret
return maps.Clone(cm)
}

// Add adds a mapping of (containerID)->(podUID, containerName) to the ContainerMap
Expand Down
32 changes: 0 additions & 32 deletions pkg/kubelet/cm/containermap/container_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ import (
"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) {
cm := NewContainerMap()
// add random fake data
Expand Down Expand Up @@ -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
}
7 changes: 2 additions & 5 deletions pkg/kubelet/cm/cpumanager/cpu_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cpumanager

import (
"fmt"
"maps"
"math"
"sort"

Expand All @@ -39,11 +40,7 @@ const (
type mapIntInt map[int]int

func (m mapIntInt) Clone() mapIntInt {
cp := make(mapIntInt, len(m))
for k, v := range m {
cp[k] = v
}
return cp
return maps.Clone(m)
}

func (m mapIntInt) Keys() []int {
Expand Down
6 changes: 2 additions & 4 deletions pkg/kubelet/cm/devicemanager/pod_devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package devicemanager

import (
"maps"
"sync"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -429,10 +430,7 @@ func NewResourceDeviceInstances() ResourceDeviceInstances {
func (rdev ResourceDeviceInstances) Clone() ResourceDeviceInstances {
clone := NewResourceDeviceInstances()
for resourceName, resourceDevs := range rdev {
clone[resourceName] = make(map[string]pluginapi.Device)
for devID, dev := range resourceDevs {
clone[resourceName][devID] = dev
}
clone[resourceName] = maps.Clone(resourceDevs)
}
return clone
}
Expand Down