Add mutex to Topology Manager Add/RemoveContainer

This was exposed as a potential bug during e2e test debugging of this
PR.
This commit is contained in:
nolancon 2020-02-14 03:38:39 +00:00
parent 1e613e5a4c
commit e8538d9b76

View File

@ -18,6 +18,7 @@ package topologymanager
import ( import (
"fmt" "fmt"
"sync"
"k8s.io/api/core/v1" "k8s.io/api/core/v1"
"k8s.io/klog" "k8s.io/klog"
@ -54,6 +55,7 @@ type Manager interface {
} }
type manager struct { type manager struct {
mutex sync.Mutex
//The list of components registered with the Manager //The list of components registered with the Manager
hintProviders []HintProvider hintProviders []HintProvider
//Mapping of a Pods mapping of Containers and their TopologyHints //Mapping of a Pods mapping of Containers and their TopologyHints
@ -189,13 +191,18 @@ func (m *manager) AddHintProvider(h HintProvider) {
} }
func (m *manager) AddContainer(pod *v1.Pod, containerID string) error { func (m *manager) AddContainer(pod *v1.Pod, containerID string) error {
m.mutex.Lock()
defer m.mutex.Unlock()
m.podMap[containerID] = string(pod.UID) m.podMap[containerID] = string(pod.UID)
return nil return nil
} }
func (m *manager) RemoveContainer(containerID string) error { func (m *manager) RemoveContainer(containerID string) error {
klog.Infof("[topologymanager] RemoveContainer - Container ID: %v", containerID) m.mutex.Lock()
defer m.mutex.Unlock()
klog.Infof("[topologymanager] RemoveContainer - Container ID: %v", containerID)
podUIDString := m.podMap[containerID] podUIDString := m.podMap[containerID]
delete(m.podMap, containerID) delete(m.podMap, containerID)
if _, exists := m.podTopologyHints[podUIDString]; exists { if _, exists := m.podTopologyHints[podUIDString]; exists {