-
Notifications
You must be signed in to change notification settings - Fork 41
/
k8s_cluster.go
160 lines (138 loc) · 4.94 KB
/
k8s_cluster.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package utils
import (
"fmt"
"strings"
"github.com/berops/claudie/internal/manifest"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
)
// CreateK8sCluster reads the unmarshalled manifest and creates desired state for Kubernetes clusters.
// Returns slice of *pb.K8Scluster if successful, nil otherwise
func CreateK8sCluster(unmarshalledManifest *manifest.Manifest) ([]*pb.K8Scluster, error) {
var clusters []*pb.K8Scluster
// Loop through clusters mentioned in the manifest
for _, cluster := range unmarshalledManifest.Kubernetes.Clusters {
// Generate variables
newCluster := &pb.K8Scluster{
ClusterInfo: &pb.ClusterInfo{
Name: strings.ToLower(cluster.Name),
Hash: utils.CreateHash(utils.HashLength),
},
Kubernetes: cluster.Version,
Network: cluster.Network,
}
// create node-pools
controlNodePools, err := unmarshalledManifest.CreateNodepools(cluster.Pools.Control, true)
if err != nil {
return nil, fmt.Errorf("error while creating control nodepool for %s : %w", cluster.Name, err)
}
computeNodePools, err := unmarshalledManifest.CreateNodepools(cluster.Pools.Compute, false)
if err != nil {
return nil, fmt.Errorf("error while creating compute nodepool for %s : %w", cluster.Name, err)
}
newCluster.ClusterInfo.NodePools = append(controlNodePools, computeNodePools...)
clusters = append(clusters, newCluster)
}
return clusters, nil
}
// UpdateK8sClusters updates the desired state of the kubernetes clusters based on the current state
// returns error if failed, nil otherwise
func UpdateK8sClusters(newConfig *pb.Config) error {
clusterDesired:
for _, clusterDesired := range newConfig.DesiredState.Clusters {
for _, clusterCurrent := range newConfig.CurrentState.Clusters {
// Found current cluster with matching name
if clusterDesired.ClusterInfo.Name == clusterCurrent.ClusterInfo.Name {
updateClusterInfo(clusterDesired.ClusterInfo, clusterCurrent.ClusterInfo)
if clusterCurrent.Kubeconfig != "" {
clusterDesired.Kubeconfig = clusterCurrent.Kubeconfig
}
// Skip the checks bellow
continue clusterDesired
}
}
// No current cluster found with matching name, create keys
if clusterDesired.ClusterInfo.PublicKey == "" {
err := createSSHKeyPair(clusterDesired.ClusterInfo)
if err != nil {
return fmt.Errorf("error encountered while creating desired state for %s : %w", clusterDesired.ClusterInfo.Name, err)
}
}
}
return nil
}
// CopyLbNodePoolNamesFromCurrentState copies the generated hash from an existing reference in the current state to the desired state.
func CopyLbNodePoolNamesFromCurrentState(used map[string]struct{}, nodepool string, current, desired []*pb.LBcluster) {
for _, desired := range desired {
references := FindNodePoolReferences(nodepool, desired.GetClusterInfo().GetNodePools())
switch {
case len(references) > 1:
panic("unexpected nodepool reference count")
case len(references) == 0:
continue
}
ref := references[0]
for _, current := range current {
if desired.ClusterInfo.Name != current.ClusterInfo.Name {
continue
}
for _, np := range current.GetClusterInfo().GetNodePools() {
if len(np.Name) != len(nodepool)+utils.HashLength+1 {
continue
}
idx := strings.LastIndex(np.Name, "-")
name := np.Name[:idx]
hash := np.Name[idx+1:]
if name != nodepool {
continue
}
used[hash] = struct{}{}
ref.Name += fmt.Sprintf("-%s", hash)
break
}
}
}
}
// CopyK8sNodePoolsNamesFromCurrentState copies the generated hash from an existing reference in the current state to the desired state.
func CopyK8sNodePoolsNamesFromCurrentState(used map[string]struct{}, nodepool string, current, desired *pb.K8Scluster) {
references := FindNodePoolReferences(nodepool, desired.GetClusterInfo().GetNodePools())
switch {
case len(references) == 0:
return
case len(references) > 2:
panic("unexpected nodepool reference count")
}
// to avoid extra code for special cases where there is just 1 reference, append a nil.
references = append(references, []*pb.NodePool{nil}...)
control, compute := references[0], references[1]
if !references[0].IsControl {
control, compute = compute, control
}
for _, np := range current.GetClusterInfo().GetNodePools() {
if len(np.Name) != len(nodepool)+utils.HashLength+1 {
continue
}
idx := strings.LastIndex(np.Name, "-")
name := np.Name[:idx]
hash := np.Name[idx+1:]
if name != nodepool {
continue
}
used[hash] = struct{}{}
if np.IsControl && control != nil {
control.Name += fmt.Sprintf("-%s", hash)
} else if !np.IsControl && compute != nil {
compute.Name += fmt.Sprintf("-%s", hash)
}
}
}
// FindNodePoolReferences find all nodepools that share the given name.
func FindNodePoolReferences(name string, nodePools []*pb.NodePool) []*pb.NodePool {
var references []*pb.NodePool
for _, np := range nodePools {
if np.Name == name {
references = append(references, np)
}
}
return references
}