Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: node: cm: use maps.Clone instead of reinvent it #128679

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
}