Skip to content

Commit 4ad9733

Browse files
committed
added RWLock to node info
1 parent ac4d2da commit 4ad9733

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

pkg/k8s/node_info.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package k8s
22

3-
import v1 "k8s.io/api/core/v1"
3+
import (
4+
"sync"
5+
6+
v1 "k8s.io/api/core/v1"
7+
)
48

59
// NodeInfo provides an abstraction on top of node to pods mappings
610
// replaces scheduler cache.NodeInfo that was removed from public in an older version of kubernetes
711
// Maintains the same interface
12+
// NodeInfo this thread safe
813
type NodeInfo struct {
14+
lock sync.RWMutex
15+
916
node *v1.Node
1017
pods []*v1.Pod
1118
}
@@ -17,20 +24,32 @@ func NewNodeInfo() *NodeInfo {
1724

1825
// AddPod adds a pod to the list of pods for this node
1926
func (i *NodeInfo) AddPod(pod *v1.Pod) {
27+
i.lock.Lock()
28+
defer i.lock.Unlock()
29+
2030
i.pods = append(i.pods, pod)
2131
}
2232

2333
// Pods returns the list of pods for this node
2434
func (i *NodeInfo) Pods() []*v1.Pod {
35+
i.lock.RLock()
36+
defer i.lock.RUnlock()
37+
2538
return i.pods
2639
}
2740

2841
// SetNode sets the current node that the pods belong to
2942
func (i *NodeInfo) SetNode(node *v1.Node) {
43+
i.lock.Lock()
44+
defer i.lock.Unlock()
45+
3046
i.node = node
3147
}
3248

3349
// Node returns the current node for these pods
3450
func (i *NodeInfo) Node() *v1.Node {
51+
i.lock.RLock()
52+
defer i.lock.RUnlock()
53+
3554
return i.node
3655
}

0 commit comments

Comments
 (0)