-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
74 lines (61 loc) · 1.74 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import "sync"
type LabelState map[string]bool
type NodeLabelMapType map[string]LabelState
type NodeLabelMap struct {
m NodeLabelMapType
sync.RWMutex
}
func NewNodeLabelMap() *NodeLabelMap {
return &NodeLabelMap{
m: make(NodeLabelMapType),
}
}
// Disable any previously set labels, the reconciler will use this to clear stale node labels
func (n *NodeLabelMap) ResetLabels(nodeName string) {
for label := range n.m[nodeName] {
n.m[nodeName][label] = false
}
}
// Add a node to the node map
func (n *NodeLabelMap) Add(nodeName string) {
if _, exists := n.m[nodeName]; !exists {
n.m[nodeName] = make(LabelState)
}
}
// Remove a node from the node map
func (n *NodeLabelMap) Remove(nodeName string) {
delete(n.m, nodeName)
}
// Identify labels to set. The map of possible labels must be iterated over with the read lock held,
// as it may changed by the filesystem watchers.
func (n *NodeLabelMap) SetPossible(nodeName string, nodeLabels map[string]string) {
possibleLabelLock.RLock()
for label := range nodeLabels {
for labelKey, fileLabels := range possibleLabelMap {
for _, fileLabel := range fileLabels {
if fileLabel == label {
n.m[nodeName][labelKey] = true
break
}
}
}
}
possibleLabelLock.RUnlock()
}
func (n *NodeLabelMap) AddLabelToNode(nodeName string, label string) {
n.m[nodeName][label] = true
}
func (n *NodeLabelMap) RemoveLabelFromNode(nodeName string, label string) {
delete(n.m[nodeName], label)
}
func (n *NodeLabelMap) GetLabels(nodeName string) LabelState {
return n.m[nodeName]
}
// Determine if there are valid labels set for a given node
func (n *NodeLabelMap) Valid(nodeName string) bool {
if _, exists := n.m[nodeName]; exists {
return len(n.m[nodeName]) > 0
}
return false
}