-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcustomcluster_scale.go
More file actions
367 lines (305 loc) · 15.2 KB
/
customcluster_scale.go
File metadata and controls
367 lines (305 loc) · 15.2 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
Copyright Kurator Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package clusteroperator
import (
"context"
"fmt"
"regexp"
"strings"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apiserver/pkg/storage/names"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"kurator.dev/kurator/pkg/apis/infra/v1alpha1"
)
// reconcileScaleUp is responsible for handling the customCluster reconciliation process when worker nodes need to be scaled up.
func (r *CustomClusterController) reconcileScaleUp(ctx context.Context, customCluster *v1alpha1.CustomCluster, scaleUpWorkerNodes []NodeInfo, kcp *controlplanev1.KubeadmControlPlane) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
// Create a temporary configmap that represents the desired state to create the scaleUp pod.
if _, err := r.ensureScaleUpHostsCreated(ctx, customCluster, scaleUpWorkerNodes); err != nil {
log.Error(err, "failed to ensure that scaleUp configmap is created ", "name", customCluster.Name, "namespace", customCluster.Namespace)
return ctrl.Result{}, err
}
// Create the scaleUp pod.
workerPod, err1 := r.ensureWorkerPodCreated(ctx, customCluster, CustomClusterScaleUpAction, KubesprayScaleUpCMD, generateScaleUpHostsName(customCluster), generateClusterConfigName(customCluster), kcp.Spec.Version)
if err1 != nil {
conditions.MarkFalse(customCluster, v1alpha1.ScaledUpCondition, v1alpha1.FailedCreateScaleUpWorker,
clusterv1.ConditionSeverityWarning, "scale up worker is failed to create %s/%s.", customCluster.Namespace, customCluster.Name)
log.Error(err1, "failed to ensure that scaleUp WorkerPod is created ", "name", customCluster.Name, "namespace", customCluster.Namespace)
return ctrl.Result{}, err1
}
// Check the current customCluster status.
if customCluster.Status.Phase != v1alpha1.ScalingUpPhase {
log.Info("phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ScalingUpPhase)
customCluster.Status.Phase = v1alpha1.ScalingUpPhase
}
// Determine the progress of scaling based on the status of the workerPod.
if workerPod.Status.Phase == corev1.PodSucceeded {
// Update cluster nodes to ensure that the current cluster-host represents the cluster after the scaling.
if err := r.updateClusterNodes(ctx, customCluster, scaleUpWorkerNodes); err != nil {
log.Error(err, "failed to update cluster nodes")
return ctrl.Result{}, err
}
// The scale up process is completed by restoring the workerPod's status to "provisioned".
log.Info("phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ProvisionedPhase)
customCluster.Status.Phase = v1alpha1.ProvisionedPhase
// Delete the temporary scaleUp cm.
if err := r.ensureConfigMapDeleted(ctx, generateScaleUpHostsKey(customCluster)); err != nil {
log.Error(err, "failed to delete scaleUp configmap", "configmap", generateScaleUpHostsKey(customCluster))
return ctrl.Result{}, err
}
// Delete the scaleUp worker.
if err := r.ensureWorkerPodDeleted(ctx, customCluster, CustomClusterScaleUpAction); err != nil {
log.Error(err, "failed to delete scaleUp worker pod")
return ctrl.Result{}, err
}
conditions.MarkTrue(customCluster, v1alpha1.ScaledUpCondition)
return ctrl.Result{}, nil
}
// When scaleUp worker pod runs failed, the status of customCluster will change into "provisioned". Deleting this error one will trigger the creation of a new scaleUp worker pod.
if workerPod.Status.Phase == corev1.PodFailed {
log.Info("scale up failed, phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ProvisionedPhase)
customCluster.Status.Phase = v1alpha1.ProvisionedPhase
conditions.MarkFalse(customCluster, v1alpha1.ScaledUpCondition, v1alpha1.ScaleUpWorkerRunFailedReason,
clusterv1.ConditionSeverityWarning, "scale up worker run failed %s/%s", customCluster.Namespace, customCluster.Name)
return ctrl.Result{}, nil
}
return ctrl.Result{}, nil
}
// reconcileScaleDown is responsible for handling the customCluster reconciliation process when worker nodes need to be scaled down.
func (r *CustomClusterController) reconcileScaleDown(ctx context.Context, customCluster *v1alpha1.CustomCluster, customMachine *v1alpha1.CustomMachine, scaleDownWorkerNodes []NodeInfo, kcp *controlplanev1.KubeadmControlPlane) (ctrl.Result, error) {
log := ctrl.LoggerFrom(ctx)
// Checks whether the worker node for scaling down already exists. If it does not exist, the function creates it.
workerPod, err1 := r.ensureWorkerPodCreated(ctx, customCluster, CustomClusterScaleDownAction, generateScaleDownManageCMD(scaleDownWorkerNodes), generateClusterHostsName(customCluster), generateClusterConfigName(customCluster), kcp.Spec.Version)
if err1 != nil {
conditions.MarkFalse(customCluster, v1alpha1.ScaledDownCondition, v1alpha1.FailedCreateScaleDownWorker,
clusterv1.ConditionSeverityWarning, "scale down worker is failed to create %s/%s.", customCluster.Namespace, customCluster.Name)
log.Error(err1, "failed to ensure that scaleDown WorkerPod is created", "name", customCluster.Name, "namespace", customCluster.Namespace)
return ctrl.Result{}, err1
}
// Check the current customCluster status.
if customCluster.Status.Phase != v1alpha1.ScalingDownPhase {
log.Info("phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ScalingDownPhase)
customCluster.Status.Phase = v1alpha1.ScalingDownPhase
}
// Determine the progress of scaling based on the status of the workerPod.
if workerPod.Status.Phase == corev1.PodSucceeded {
// Recreate the cluster-hosts to ensure that the current cluster-host represents the cluster after the deletion.
if _, err := r.recreateClusterHosts(ctx, customCluster, customMachine); err != nil {
log.Error(err, "failed to recreate configmap cluster-hosts when scale down")
return ctrl.Result{}, err
}
// The scale down process is completed by restoring the workerPod's status to "provisioned".
log.Info("phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ProvisionedPhase)
customCluster.Status.Phase = v1alpha1.ProvisionedPhase
// Delete the scaleDown worker
if err := r.ensureWorkerPodDeleted(ctx, customCluster, CustomClusterScaleDownAction); err != nil {
log.Error(err, "failed to delete scaleDown worker pod", "worker", customCluster.Name)
return ctrl.Result{}, err
}
conditions.MarkTrue(customCluster, v1alpha1.ScaledDownCondition)
return ctrl.Result{}, nil
}
// When scaleDown worker pod runs failed, the status of customCluster will change into "provisioned". Deleting this error one will trigger the creation of a new scaleDown worker pod.
if workerPod.Status.Phase == corev1.PodFailed {
log.Info("scale down failed, phase changes", "prevPhase", customCluster.Status.Phase, "currentPhase", v1alpha1.ProvisionedPhase)
customCluster.Status.Phase = v1alpha1.ProvisionedPhase
conditions.MarkFalse(customCluster, v1alpha1.ScaledDownCondition, v1alpha1.ScaleDownWorkerRunFailedReason,
clusterv1.ConditionSeverityWarning, "scale down worker run failed %s/%s", customCluster.Namespace, customCluster.Name)
return ctrl.Result{}, nil
}
return ctrl.Result{}, nil
}
// createScaleUpConfigMap create temporary cluster-hosts configmap for scaling.
func (r *CustomClusterController) createScaleUpConfigMap(ctx context.Context, customCluster *v1alpha1.CustomCluster, scaleUpWorkerNodes []NodeInfo) (*corev1.ConfigMap, error) {
// Get current cm.
curCM := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, generateClusterHostsKey(customCluster), curCM); err != nil {
return nil, err
}
newCM := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: generateScaleUpHostsName(customCluster),
Namespace: customCluster.Namespace,
},
Data: map[string]string{ClusterHostsName: strings.TrimSpace(getScaleUpConfigMapData(curCM.Data[ClusterHostsName], scaleUpWorkerNodes))},
}
if err := r.Client.Create(ctx, newCM); err != nil {
return nil, err
}
return newCM, nil
}
// ensureScaleUpHostsCreated ensure that the temporary cluster-hosts configmap for scaling up is created.
func (r *CustomClusterController) ensureScaleUpHostsCreated(ctx context.Context, customCluster *v1alpha1.CustomCluster, scaleUpWorkerNodes []NodeInfo) (*corev1.ConfigMap, error) {
cmKey := generateScaleUpHostsKey(customCluster)
cm := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, cmKey, cm); err != nil {
if apierrors.IsNotFound(err) {
return r.createScaleUpConfigMap(ctx, customCluster, scaleUpWorkerNodes)
}
return nil, err
}
return cm, nil
}
// findScaleUpWorkerNodes find the workerNodes which need to be scale up.
func findScaleUpWorkerNodes(provisionedWorkerNodes, curWorkerNodes []NodeInfo) []NodeInfo {
return findAdditionalWorkerNodes(provisionedWorkerNodes, curWorkerNodes)
}
// findScaleDownWorkerNodes find the workerNodes which need to be scale down.
func findScaleDownWorkerNodes(provisionedWorkerNodes, curWorkerNodes []NodeInfo) []NodeInfo {
return findAdditionalWorkerNodes(curWorkerNodes, provisionedWorkerNodes)
}
// findAdditionalWorkerNodes find additional workers in secondWorkersNodes than firstWorkerNodes.
func findAdditionalWorkerNodes(firstWorkerNodes, secondWorkersNodes []NodeInfo) []NodeInfo {
var additionalWorkers []NodeInfo
if len(secondWorkersNodes) == 0 {
return nil
}
if len(firstWorkerNodes) == 0 {
additionalWorkers = append(additionalWorkers, secondWorkersNodes...)
return additionalWorkers
}
var set = make(map[string]struct{})
for _, firstNode := range firstWorkerNodes {
set[firstNode.NodeName] = struct{}{}
}
for _, secondNode := range secondWorkersNodes {
if _, ok := set[secondNode.NodeName]; !ok {
additionalWorkers = append(additionalWorkers, secondNode)
}
}
return additionalWorkers
}
func generateScaleUpHostsKey(customCluster *v1alpha1.CustomCluster) client.ObjectKey {
return client.ObjectKey{
Namespace: customCluster.Namespace,
Name: generateScaleUpHostsName(customCluster),
}
}
func generateScaleUpHostsName(customCluster *v1alpha1.CustomCluster) string {
scaleUpHostName := customCluster.Name + "-" + ClusterHostsName + "-scale-up"
scaleUpHostName = names.SimpleNameGenerator.GenerateName(scaleUpHostName)
return scaleUpHostName
}
// generateScaleDownManageCMD generate a kubespray cmd to delete the node from the list of nodesNeedDelete.
func generateScaleDownManageCMD(nodesNeedDelete []NodeInfo) customClusterManageCMD {
if len(nodesNeedDelete) == 0 {
return ""
}
cmd := string(KubesprayScaleDownCMDPrefix) + " --extra-vars \"node=" + nodesNeedDelete[0].NodeName
if len(nodesNeedDelete) == 1 {
return customClusterManageCMD(cmd + "\" ")
}
for i := 1; i < len(nodesNeedDelete); i++ {
cmd = cmd + "," + nodesNeedDelete[i].NodeName
}
return customClusterManageCMD(cmd + "\" ")
}
// updateClusterNodes update the cluster nodes info in configmap.
func (r *CustomClusterController) updateClusterNodes(ctx context.Context, customCluster *v1alpha1.CustomCluster, scaleUpWorkerNodes []NodeInfo) error {
// Get cm.
cm := &corev1.ConfigMap{}
if err := r.Client.Get(ctx, generateClusterHostsKey(customCluster), cm); err != nil {
return err
}
// Add new nodes on the original data, instead of directly modifying it to the desired state (read from customMachine).
// This is the basis for the automatic execution of scaleDown after scaleUp is completed when both "scaleUpWorkerNodes" and "scaleDownWorkerNodes" are not nil.
cm.Data[ClusterHostsName] = getScaleUpConfigMapData(cm.Data[ClusterHostsName], scaleUpWorkerNodes)
// Update cm.
if err := r.Client.Update(ctx, cm); err != nil {
return err
}
return nil
}
// getScaleUpConfigMapData return a string of the configmap data that adds the scaleUp nodes to the original data.
func getScaleUpConfigMapData(data string, scaleUpWorkerNodes []NodeInfo) string {
sep := regexp.MustCompile(`\[kube_control_plane]|\[k8s-cluster:children]`)
dateParts := sep.Split(data, -1)
nodeAndIP := "\n"
nodeName := "\n"
for _, node := range scaleUpWorkerNodes {
nodeAndIP = nodeAndIP + fmt.Sprintf("%s ansible_host=%s ip=%s\n", node.NodeName, node.PublicIP, node.PrivateIP)
nodeName = nodeName + fmt.Sprintf("%s\n", node.NodeName)
}
ans := fmt.Sprintf("%s%s[kube_control_plane]%s%s[k8s-cluster:children]%s", dateParts[0], nodeAndIP, dateParts[1], nodeName, dateParts[2])
return ans
}
// getWorkerNodeInfoFromClusterHost get the provisioned workerNode info on VMs from the cluster-host configmap.
func getWorkerNodeInfoFromClusterHosts(clusterHost *corev1.ConfigMap) []NodeInfo {
var workerNodes []NodeInfo
var allNodes = make(map[string]NodeInfo)
clusterHostDate := clusterHost.Data[ClusterHostsName]
clusterHostDate = strings.TrimSpace(clusterHostDate)
// The regexp string depend on the template text which the function "CreateClusterHosts" use.
sep := regexp.MustCompile(`\[all]|\[kube_control_plane]|\[kube_node]|\[k8s-cluster:children]`)
clusterHostDateArr := sep.Split(clusterHostDate, -1)
allNodesStr := clusterHostDateArr[1]
workerNodesStr := clusterHostDateArr[3]
zp := regexp.MustCompile(`[\t\n\f\r]`)
allNodeArr := zp.Split(allNodesStr, -1)
workerNodesArr := zp.Split(workerNodesStr, -1)
// Get all nodes' info.
for _, nodeStr := range allNodeArr {
if len(nodeStr) == 0 {
continue
}
nodeStr = strings.TrimSpace(nodeStr)
curName, cruNodeInfo := getNodeInfoFromNodeStr(nodeStr)
// Deduplication.
allNodes[curName] = cruNodeInfo
}
// Choose workerNode from all node.
for _, workerNodeName := range workerNodesArr {
if len(workerNodeName) == 0 {
continue
}
workerNodeName = strings.TrimSpace(workerNodeName)
workerNodes = append(workerNodes, allNodes[workerNodeName])
}
return workerNodes
}
func getNodeInfoFromNodeStr(nodeStr string) (hostName string, nodeInfo NodeInfo) {
nodeStr = strings.TrimSpace(nodeStr)
// The sepStr depend on the template text which the function "CreateClusterHosts" use.
sepStr := regexp.MustCompile(` ansible_host=| ip=`)
strArr := sepStr.Split(nodeStr, -1)
hostName = strArr[0]
publicIP := strArr[1]
privateIP := strArr[2]
return hostName, NodeInfo{
NodeName: hostName,
PublicIP: publicIP,
PrivateIP: privateIP,
}
}
func getWorkerNodesFromCustomMachine(customMachine *v1alpha1.CustomMachine) []NodeInfo {
var workerNodes []NodeInfo
for i := 0; i < len(customMachine.Spec.Nodes); i++ {
curNode := NodeInfo{
NodeName: customMachine.Spec.Nodes[i].HostName,
PublicIP: customMachine.Spec.Nodes[i].PublicIP,
PrivateIP: customMachine.Spec.Nodes[i].PrivateIP,
}
workerNodes = append(workerNodes, curNode)
}
return workerNodes
}