-
Notifications
You must be signed in to change notification settings - Fork 34
/
setup_loadbalancers.go
269 lines (224 loc) · 11 KB
/
setup_loadbalancers.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package usecases
import (
"fmt"
"os"
"path/filepath"
"github.com/rs/zerolog"
"github.com/berops/claudie/internal/templateUtils"
commonUtils "github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
"github.com/berops/claudie/services/ansibler/server/utils"
"github.com/berops/claudie/services/ansibler/templates"
)
const (
// nodeExporterPlaybookFileName defines name for node exporter playbook.
nodeExporterPlaybookFileName = "node-exporter.yml"
// nginxPlaybookName defines name for nginx playbook.
nginxPlaybookName = "nginx.yml"
// nginxConfigName defines name for nginx config.
nginxConfigName = "lb.conf"
)
func (u *Usecases) SetUpLoadbalancers(request *pb.SetUpLBRequest) (*pb.SetUpLBResponse, error) {
logger := commonUtils.CreateLoggerWithProjectAndClusterName(request.ProjectName, commonUtils.GetClusterID(request.Desired.ClusterInfo))
logger.Info().Msgf("Setting up the loadbalancers")
currentLBClusters := make(map[string]*pb.LBcluster)
for _, lbCluster := range request.CurrentLbs {
currentLBClusters[lbCluster.ClusterInfo.Name] = lbCluster
}
lbClustersInfo := &utils.LBClustersInfo{
FirstRun: request.FirstRun,
TargetK8sNodepool: request.Desired.ClusterInfo.NodePools,
TargetK8sNodepoolKey: request.Desired.ClusterInfo.PrivateKey,
PreviousAPIEndpointLB: request.PreviousAPIEndpoint,
ClusterID: commonUtils.GetClusterID(request.Desired.ClusterInfo),
}
for _, lbCluster := range request.DesiredLbs {
lbClustersInfo.LbClusters = append(lbClustersInfo.LbClusters, &utils.LBClusterData{
DesiredLbCluster: lbCluster,
// if there is a value in the map it will return it, otherwise nil is returned.
CurrentLbCluster: currentLBClusters[lbCluster.ClusterInfo.Name],
})
}
if err := setUpLoadbalancers(request.Desired.ClusterInfo.Name, lbClustersInfo, logger, u.SpawnProcessLimit); err != nil {
logger.Err(err).Msgf("Error encountered while setting up the loadbalancers")
return nil, fmt.Errorf("error encountered while setting up the loadbalancers for cluster %s project %s : %w", request.Desired.ClusterInfo.Name, request.ProjectName, err)
}
logger.Info().Msgf("Loadbalancers were successfully set up")
return &pb.SetUpLBResponse{Desired: request.Desired, CurrentLbs: request.CurrentLbs, DesiredLbs: request.DesiredLbs}, nil
}
// setUpLoadbalancers sets up the loadbalancers along with DNS and verifies their configuration
func setUpLoadbalancers(clusterName string, lbClustersInfo *utils.LBClustersInfo, logger zerolog.Logger, spawnProcessLimit chan struct{}) error {
clusterBaseDirectory := filepath.Join(baseDirectory, outputDirectory, fmt.Sprintf("%s-%s-lbs", clusterName, commonUtils.CreateHash(commonUtils.HashLength)))
if err := utils.GenerateLBBaseFiles(clusterBaseDirectory, lbClustersInfo); err != nil {
return fmt.Errorf("error encountered while generating base files for %s : %w", clusterName, err)
}
err := commonUtils.ConcurrentExec(lbClustersInfo.LbClusters,
func(_ int, lbCluster *utils.LBClusterData) error {
var (
loggerPrefix = "LB-cluster"
lbClusterId = commonUtils.GetClusterID(lbCluster.DesiredLbCluster.ClusterInfo)
)
logger.Info().Str(loggerPrefix, lbClusterId).Msg("Setting up the loadbalancer cluster")
// Create the directory where files will be generated
clusterDirectory := filepath.Join(clusterBaseDirectory, lbClusterId)
if err := commonUtils.CreateDirectory(clusterDirectory); err != nil {
return fmt.Errorf("failed to create directory %s : %w", clusterDirectory, err)
}
// Generate SSH key
if err := commonUtils.CreateKeyFile(lbCluster.DesiredLbCluster.ClusterInfo.PrivateKey, clusterDirectory, fmt.Sprintf("key.%s", sshPrivateKeyFileExtension)); err != nil {
return fmt.Errorf("failed to create key file for %s : %w", lbCluster.DesiredLbCluster.ClusterInfo.Name, err)
}
if err := commonUtils.CreateKeysForStaticNodepools(commonUtils.GetCommonStaticNodePools(lbCluster.DesiredLbCluster.ClusterInfo.NodePools), clusterDirectory); err != nil {
return fmt.Errorf("failed to create key file(s) for static nodes : %w", err)
}
if err := setUpNodeExporter(lbCluster.DesiredLbCluster, clusterDirectory, spawnProcessLimit); err != nil {
return err
}
if err := setUpNginx(lbCluster.DesiredLbCluster, lbClustersInfo.TargetK8sNodepool, clusterDirectory, spawnProcessLimit); err != nil {
return err
}
logger.Info().Str(loggerPrefix, lbClusterId).Msg("Loadbalancer cluster successfully set up")
return nil
},
)
if err != nil {
return fmt.Errorf("error while setting up the loadbalancers for cluster %s : %w", clusterName, err)
}
var desiredApiServerTypeLBCluster *utils.LBClusterData
for _, lbClusterInfo := range lbClustersInfo.LbClusters {
if commonUtils.HasAPIServerRole(lbClusterInfo.DesiredLbCluster.Roles) {
desiredApiServerTypeLBCluster = lbClusterInfo
}
}
// If we didn't find any Api server type LB cluster in the desired state,
// it's possible that we've changed the role from an API server to some other role.
// This won't be caught by the above check.
// So we have to do an additional check for the desiredApiServerTypeLBCluster using the current state.
if desiredApiServerTypeLBCluster == nil {
desiredApiServerTypeLBCluster = utils.FindCurrentAPIServerTypeLBCluster(lbClustersInfo.LbClusters)
}
if err := utils.HandleAPIEndpointChange(desiredApiServerTypeLBCluster, lbClustersInfo, clusterBaseDirectory, spawnProcessLimit); err != nil {
return fmt.Errorf("failed to find a candidate for the Api Server: %w", err)
}
return os.RemoveAll(clusterBaseDirectory)
}
// setUpNodeExporter sets up node-exporter on each node of the LB cluster.
// Returns error if not successful, nil otherwise.
func setUpNodeExporter(lbCluster *pb.LBcluster, clusterDirectory string, spawnProcessLimit chan struct{}) error {
var playbookParameters = utils.LBPlaybookParameters{Loadbalancer: lbCluster.ClusterInfo.Name}
// Generate node-exporter Ansible playbook from template
template, err := templateUtils.LoadTemplate(templates.NodeExporterPlaybookTemplate)
if err != nil {
return fmt.Errorf("error while loading %s template for node_exporter playbook : %w", lbCluster.ClusterInfo.Name, err)
}
tpl := templateUtils.Templates{Directory: clusterDirectory}
if err := tpl.Generate(template, nodeExporterPlaybookFileName, playbookParameters); err != nil {
return fmt.Errorf("error while generating %s for %s : %w", nodeExporterPlaybookFileName, lbCluster.ClusterInfo.Name, err)
}
// Run the Ansible playbook
ansible := utils.Ansible{
Directory: clusterDirectory,
Playbook: nodeExporterPlaybookFileName,
Inventory: filepath.Join("..", utils.InventoryFileName),
SpawnProcessLimit: spawnProcessLimit,
}
if err = ansible.RunAnsiblePlaybook(fmt.Sprintf("LB - %s-%s", lbCluster.ClusterInfo.Name, lbCluster.ClusterInfo.Hash)); err != nil {
return fmt.Errorf("error while running ansible for %s : %w", lbCluster.ClusterInfo.Name, err)
}
return nil
}
// setUpNginx sets up the nginx loadbalancer based on the input manifest specification.
// Return error if not successful, nil otherwise
func setUpNginx(lbCluster *pb.LBcluster, targetK8sNodepool []*pb.NodePool, clusterDirectory string, spawnProcessLimit chan struct{}) error {
lbClusterRolesInfo := targetPools(lbCluster, targetK8sNodepool)
// Generate the nginx config file
nginxConfTemplate, err := templateUtils.LoadTemplate(templates.NginxConfigTemplate)
tpl := templateUtils.Templates{Directory: clusterDirectory}
if err != nil {
return fmt.Errorf("error while loading nginx config template : %w", err)
}
nginxPlaybookTemplate, err := templateUtils.LoadTemplate(templates.NginxPlaybookTemplate)
if err != nil {
return fmt.Errorf("error while loading nginx playbook template : %w", err)
}
if err := tpl.Generate(nginxConfTemplate, nginxConfigName, utils.NginxConfigTemplateParameters{Roles: lbClusterRolesInfo}); err != nil {
return fmt.Errorf("error while generating %s for %s : %w", nginxConfigName, lbCluster.ClusterInfo.Name, err)
}
if err := tpl.Generate(nginxPlaybookTemplate, nginxPlaybookName, utils.LBPlaybookParameters{Loadbalancer: lbCluster.ClusterInfo.Name}); err != nil {
return fmt.Errorf("error while generating %s for %s : %w", nginxPlaybookName, lbCluster.ClusterInfo.Name, err)
}
ansible := utils.Ansible{
Playbook: nginxPlaybookName,
Inventory: filepath.Join("..", utils.InventoryFileName),
Directory: clusterDirectory,
SpawnProcessLimit: spawnProcessLimit,
}
err = ansible.RunAnsiblePlaybook(fmt.Sprintf("LB - %s-%s", lbCluster.ClusterInfo.Name, lbCluster.ClusterInfo.Hash))
if err != nil {
return fmt.Errorf("error while running ansible for %s : %w", lbCluster.ClusterInfo.Name, err)
}
return nil
}
func targetPools(lbCluster *pb.LBcluster, targetK8sNodepool []*pb.NodePool) []utils.LBClusterRolesInfo {
//TODO: remove in favor of targetNodepools
targetControlNodes, targetComputeNodes := splitNodesByType(targetK8sNodepool)
var lbClusterRolesInfo []utils.LBClusterRolesInfo
for _, role := range lbCluster.Roles {
if len(role.TargetPools) == 0 {
// TODO: remove in favor of targetNodepools
target := assignTarget(targetControlNodes, targetComputeNodes, role.Target)
lbClusterRolesInfo = append(lbClusterRolesInfo, utils.LBClusterRolesInfo{Role: role, TargetNodes: target})
} else {
lbClusterRolesInfo = append(lbClusterRolesInfo, utils.LBClusterRolesInfo{
Role: role,
TargetNodes: targetNodes(role.TargetPools, targetK8sNodepool),
})
}
}
return lbClusterRolesInfo
}
func targetNodes(targetPools []string, targetk8sPools []*pb.NodePool) (nodes []*pb.Node) {
var pools []*pb.NodePool
for _, target := range targetPools {
for _, np := range targetk8sPools {
if np.GetDynamicNodePool() != nil {
if name, _ := commonUtils.GetNameAndHashFromNodepool(target, np.Name); name != "" {
pools = append(pools, np)
}
} else if np.GetStaticNodePool() != nil {
if target == np.Name {
pools = append(pools, np)
}
}
}
}
for _, np := range pools {
nodes = append(nodes, np.Nodes...)
}
return
}
// splitNodesByType returns two slices of *pb.Node, one for control nodes and one for compute nodes.
func splitNodesByType(nodepools []*pb.NodePool) (controlNodes, computeNodes []*pb.Node) {
for _, nodepool := range nodepools {
for _, node := range nodepool.Nodes {
if node.NodeType == pb.NodeType_apiEndpoint || node.NodeType == pb.NodeType_master {
controlNodes = append(controlNodes, node)
} else {
computeNodes = append(computeNodes, node)
}
}
}
return controlNodes, computeNodes
}
// assignTarget returns a target nodes for pb.Target.
// If no target matches the pb.Target enum, returns nil
func assignTarget(targetControlNodes, targetComputeNodes []*pb.Node, target pb.Target) (targetNodes []*pb.Node) {
if target == pb.Target_k8sAllNodes {
targetNodes = append(targetControlNodes, targetComputeNodes...)
} else if target == pb.Target_k8sControlPlane {
targetNodes = targetControlNodes
} else if target == pb.Target_k8sComputePlane {
targetNodes = targetComputeNodes
}
return targetNodes
}