/
ghostmaster.go
64 lines (52 loc) · 1.93 KB
/
ghostmaster.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
package sanitycheck
import (
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/util/errors"
rapi "github.com/amadeusitgroup/redis-operator/pkg/api/redis/v1"
"github.com/amadeusitgroup/redis-operator/pkg/controller/pod"
"github.com/amadeusitgroup/redis-operator/pkg/redis"
)
// FixGhostMasterNodes used to removed gost redis nodes
func FixGhostMasterNodes(admin redis.AdminInterface, podControl pod.RedisClusterControlInteface, cluster *rapi.RedisCluster, info *redis.ClusterInfos) (bool, error) {
ghosts := listGhostMasterNodes(podControl, cluster, info)
var errs []error
doneAnAction := false
for _, nodeID := range ghosts {
doneAnAction = true
glog.Infof("forget ghost master nodes with no slot, id:%s", nodeID)
if err := admin.ForgetNode(nodeID); err != nil {
errs = append(errs, err)
}
}
return doneAnAction, errors.NewAggregate(errs)
}
func listGhostMasterNodes(podControl pod.RedisClusterControlInteface, cluster *rapi.RedisCluster, infos *redis.ClusterInfos) []string {
if infos == nil || infos.Infos == nil {
return []string{}
}
ghostNodes := make(map[string]*redis.Node) // map by id is used to dedouble Node from the different view
for _, nodeinfos := range infos.Infos {
for _, node := range nodeinfos.Friends.FilterByFunc(redis.IsMasterWithNoSlot) {
ghostNodes[node.ID] = node
}
}
currentPods, err := podControl.GetRedisClusterPods(cluster)
if err != nil {
glog.Errorf("unable to retrieve the Pod list, err:%v", err)
}
ghosts := []string{}
for id := range ghostNodes {
podExist := false
podReused := false
// Check if the Redis master nodes (with not slot) is still running in a Pod
// if not it will be added to the ghosts redis node slice
bomNode, _ := infos.GetNodes().GetNodeByID(id)
if bomNode != nil && bomNode.Pod != nil {
podExist, podReused = checkIfPodNameExistAndIsReused(bomNode, currentPods)
}
if !podExist || podReused {
ghosts = append(ghosts, id)
}
}
return ghosts
}