-
Notifications
You must be signed in to change notification settings - Fork 38
/
utils.go
101 lines (94 loc) · 3.46 KB
/
utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package utils
import (
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
cbox "github.com/berops/claudie/services/context-box/client"
"github.com/rs/zerolog/log"
)
// SaveConfigWithWorkflowError saves config with workflow states
func SaveConfigWithWorkflowError(config *pb.Config, c pb.ContextBoxServiceClient, clusterView *utils.ClusterView) error {
config.State = clusterView.ClusterWorkflows
return cbox.SaveConfigBuilder(c, &pb.SaveConfigRequest{Config: config})
}
// updateNodePoolInfo updates the nodepool metadata and node private IPs between stages of the cluster build.
func UpdateNodePoolInfo(src []*pb.NodePool, dst []*pb.NodePool) {
src:
for _, npSrc := range src {
for _, npDst := range dst {
if npSrc.Name == npDst.Name {
srcNodes := getNodeMap(npSrc.Nodes)
dstNodes := getNodeMap(npDst.Nodes)
for dstName, dstNode := range dstNodes {
if srcNode, ok := srcNodes[dstName]; ok {
dstNode.Private = srcNode.Private
}
}
if npSrc.GetDynamicNodePool() != nil && npDst.GetDynamicNodePool() != nil {
npDst.GetDynamicNodePool().Metadata = npSrc.GetDynamicNodePool().Metadata
}
continue src
}
}
}
}
// getNodeMap returns a map of nodes, where each key is node name.
func getNodeMap(nodes []*pb.Node) map[string]*pb.Node {
m := make(map[string]*pb.Node, len(nodes))
for _, node := range nodes {
m[node.Name] = node
}
return m
}
// separateNodepools creates two slices of node names, one for master and one for worker nodes
func SeparateNodepools(clusterNodes map[string]int32, currentClusterInfo, desiredClusterInfo *pb.ClusterInfo) (master []string, worker []string) {
for _, nodepool := range currentClusterInfo.NodePools {
var names = make([]string, 0, len(nodepool.Nodes))
if np := nodepool.GetDynamicNodePool(); np != nil {
if count, ok := clusterNodes[nodepool.Name]; ok && count > 0 {
names = getDynamicNodeNames(nodepool, int(count))
}
} else if np := nodepool.GetStaticNodePool(); np != nil {
if count, ok := clusterNodes[nodepool.Name]; ok && count > 0 {
names = getStaticNodeNames(nodepool, desiredClusterInfo)
}
}
if nodepool.IsControl {
master = append(master, names...)
} else {
worker = append(worker, names...)
}
}
return master, worker
}
// getDynamicNodeNames returns slice of length count with names of the nodes from specified nodepool
// nodes chosen are from the last element in Nodes slice, up to the first one
func getDynamicNodeNames(np *pb.NodePool, count int) (names []string) {
for i := len(np.GetNodes()) - 1; i >= len(np.GetNodes())-count; i-- {
names = append(names, np.GetNodes()[i].GetName())
log.Debug().Msgf("Choosing node %s for deletion", np.GetNodes()[i].GetName())
}
return names
}
// getStaticNodeNames returns slice of length count with names of the nodes from specified nodepool
// nodes chosen are from the last element in Nodes slice, up to the first one
func getStaticNodeNames(np *pb.NodePool, desiredCluster *pb.ClusterInfo) (names []string) {
// Find desired nodes for node pool.
desired := make(map[string]struct{})
for _, n := range desiredCluster.NodePools {
if n.Name == np.Name {
for _, node := range n.Nodes {
desired[node.Name] = struct{}{}
}
}
}
// Find deleted nodes
if n := np.GetStaticNodePool(); n != nil {
for _, node := range np.Nodes {
if _, ok := desired[node.Name]; !ok {
// Append name as it is not defined in desired state.
names = append(names, node.Name)
}
}
}
return names
}