diff --git a/pkg/kubelet/cm/cpumanager/containermap/container_map.go b/pkg/kubelet/cm/cpumanager/containermap/container_map.go index 90720a48cd9..c324e028b4f 100644 --- a/pkg/kubelet/cm/cpumanager/containermap/container_map.go +++ b/pkg/kubelet/cm/cpumanager/containermap/container_map.go @@ -39,11 +39,19 @@ func (cm ContainerMap) Add(podUID, containerName, containerID string) { }{podUID, containerName} } -// Remove removes a mapping of (containerID)->(*v1.Pod, *.v1.Container) from the ContainerMap -func (cm ContainerMap) Remove(containerID string) { +// RemoveByContainerID removes a mapping of (containerID)->(podUID, containerName) from the ContainerMap +func (cm ContainerMap) RemoveByContainerID(containerID string) { delete(cm, containerID) } +// RemoveByContainerRef removes a mapping of (containerID)->(podUID, containerName) from the ContainerMap +func (cm ContainerMap) RemoveByContainerRef(podUID, containerName string) { + containerID, err := cm.GetContainerID(podUID, containerName) + if err == nil { + cm.RemoveByContainerID(containerID) + } +} + // GetContainerID retrieves a ContainerID from the ContainerMap func (cm ContainerMap) GetContainerID(podUID, containerName string) (string, error) { for key, val := range cm { diff --git a/pkg/kubelet/cm/cpumanager/containermap/container_map_test.go b/pkg/kubelet/cm/cpumanager/containermap/container_map_test.go index 64d632ac44b..8e58e2f2901 100644 --- a/pkg/kubelet/cm/cpumanager/containermap/container_map_test.go +++ b/pkg/kubelet/cm/cpumanager/containermap/container_map_test.go @@ -63,11 +63,19 @@ func TestContainerMap(t *testing.T) { // Remove all entries from the containerMap, checking proper removal of // each along the way. for i := range tc.containerNames { - cm.Remove(tc.containerIDs[i]) + cm.RemoveByContainerID(tc.containerIDs[i]) containerID, err := cm.GetContainerID(tc.podUID, tc.containerNames[i]) if err == nil { t.Errorf("unexpected retrieval of containerID after removal: %v", containerID) } + + cm.Add(tc.podUID, tc.containerNames[i], tc.containerIDs[i]) + + cm.RemoveByContainerRef(tc.podUID, tc.containerNames[i]) + podUID, containerName, err := cm.GetContainerRef(tc.containerIDs[i]) + if err == nil { + t.Errorf("unexpected retrieval of container reference after removal: (%v, %v)", podUID, containerName) + } } // Verify containerMap now empty. diff --git a/pkg/kubelet/cm/cpumanager/policy_static.go b/pkg/kubelet/cm/cpumanager/policy_static.go index ba2c5e7fd4f..bfab188713e 100644 --- a/pkg/kubelet/cm/cpumanager/policy_static.go +++ b/pkg/kubelet/cm/cpumanager/policy_static.go @@ -243,7 +243,7 @@ func (p *staticPolicy) RemoveContainer(s state.State, containerID string) (rerr // remove containerID from the containerMap. defer func() { if rerr == nil { - p.containerMap.Remove(containerID) + p.containerMap.RemoveByContainerID(containerID) } }()