mirror of https://github.com/k3s-io/k3s
Fix a bug in node tree when all nodes in a zone are removed
parent
521028ed52
commit
141b55abf5
|
@ -21,7 +21,6 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
"k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
|
||||||
utilnode "k8s.io/kubernetes/pkg/util/node"
|
utilnode "k8s.io/kubernetes/pkg/util/node"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
@ -33,7 +32,6 @@ type NodeTree struct {
|
||||||
tree map[string]*nodeArray // a map from zone (region-zone) to an array of nodes in the zone.
|
tree map[string]*nodeArray // a map from zone (region-zone) to an array of nodes in the zone.
|
||||||
zones []string // a list of all the zones in the tree (keys)
|
zones []string // a list of all the zones in the tree (keys)
|
||||||
zoneIndex int
|
zoneIndex int
|
||||||
exhaustedZones sets.String // set of zones that all of their nodes are returned by next()
|
|
||||||
NumNodes int
|
NumNodes int
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
@ -63,7 +61,6 @@ func (na *nodeArray) next() (nodeName string, exhausted bool) {
|
||||||
func newNodeTree(nodes []*v1.Node) *NodeTree {
|
func newNodeTree(nodes []*v1.Node) *NodeTree {
|
||||||
nt := &NodeTree{
|
nt := &NodeTree{
|
||||||
tree: make(map[string]*nodeArray),
|
tree: make(map[string]*nodeArray),
|
||||||
exhaustedZones: sets.NewString(),
|
|
||||||
}
|
}
|
||||||
for _, n := range nodes {
|
for _, n := range nodes {
|
||||||
nt.AddNode(n)
|
nt.AddNode(n)
|
||||||
|
@ -156,7 +153,7 @@ func (nt *NodeTree) resetExhausted() {
|
||||||
for _, na := range nt.tree {
|
for _, na := range nt.tree {
|
||||||
na.lastIndex = 0
|
na.lastIndex = 0
|
||||||
}
|
}
|
||||||
nt.exhaustedZones = sets.NewString()
|
nt.zoneIndex = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next returns the name of the next node. NodeTree iterates over zones and in each zone iterates
|
// Next returns the name of the next node. NodeTree iterates over zones and in each zone iterates
|
||||||
|
@ -167,18 +164,19 @@ func (nt *NodeTree) Next() string {
|
||||||
if len(nt.zones) == 0 {
|
if len(nt.zones) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
numExhaustedZones := 0
|
||||||
for {
|
for {
|
||||||
if nt.zoneIndex >= len(nt.zones) {
|
if nt.zoneIndex >= len(nt.zones) {
|
||||||
nt.zoneIndex = 0
|
nt.zoneIndex = 0
|
||||||
}
|
}
|
||||||
zone := nt.zones[nt.zoneIndex]
|
zone := nt.zones[nt.zoneIndex]
|
||||||
nt.zoneIndex++
|
nt.zoneIndex++
|
||||||
// We do not check the set of exhausted zones before calling next() on the zone. This ensures
|
// We do not check the exhausted zones before calling next() on the zone. This ensures
|
||||||
// that if more nodes are added to a zone after it is exhausted, we iterate over the new nodes.
|
// that if more nodes are added to a zone after it is exhausted, we iterate over the new nodes.
|
||||||
nodeName, exhausted := nt.tree[zone].next()
|
nodeName, exhausted := nt.tree[zone].next()
|
||||||
if exhausted {
|
if exhausted {
|
||||||
nt.exhaustedZones.Insert(zone)
|
numExhaustedZones++
|
||||||
if len(nt.exhaustedZones) == len(nt.zones) { // all zones are exhausted. we should reset.
|
if numExhaustedZones >= len(nt.zones) { // all zones are exhausted. we should reset.
|
||||||
nt.resetExhausted()
|
nt.resetExhausted()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -403,6 +403,13 @@ func TestNodeTreeMultiOperations(t *testing.T) {
|
||||||
operations: []string{"add", "add", "add", "add", "add", "next", "next", "next", "next", "add", "next", "next", "next"},
|
operations: []string{"add", "add", "add", "add", "add", "next", "next", "next", "next", "add", "next", "next", "next"},
|
||||||
expectedOutput: []string{"node-4", "node-5", "node-6", "node-7", "node-3", "node-8", "node-4"},
|
expectedOutput: []string{"node-4", "node-5", "node-6", "node-7", "node-3", "node-8", "node-4"},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "remove zone and add new to ensure exhausted is reset correctly",
|
||||||
|
nodesToAdd: append(allNodes[3:5], allNodes[6:8]...),
|
||||||
|
nodesToRemove: allNodes[3:5],
|
||||||
|
operations: []string{"add", "add", "next", "next", "remove", "add", "add", "next", "next", "remove", "next", "next"},
|
||||||
|
expectedOutput: []string{"node-3", "node-4", "node-6", "node-7", "node-6", "node-7"},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
Loading…
Reference in New Issue