-
Notifications
You must be signed in to change notification settings - Fork 165
/
ops_progress_util.go
615 lines (573 loc) · 24.6 KB
/
ops_progress_util.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*
Copyright (C) 2022-2023 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package operations
import (
"fmt"
"time"
"golang.org/x/exp/slices"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
appsv1alpha1 "github.com/apecloud/kubeblocks/apis/apps/v1alpha1"
"github.com/apecloud/kubeblocks/controllers/apps/components"
"github.com/apecloud/kubeblocks/internal/constant"
intctrlutil "github.com/apecloud/kubeblocks/internal/controllerutil"
)
// getProgressObjectKey gets progress object key from the client.Object.
func getProgressObjectKey(kind, name string) string {
return fmt.Sprintf("%s/%s", kind, name)
}
// isCompletedProgressStatus checks the progress detail with final state, either Failed or Succeed.
func isCompletedProgressStatus(status appsv1alpha1.ProgressStatus) bool {
return slices.Contains([]appsv1alpha1.ProgressStatus{appsv1alpha1.SucceedProgressStatus,
appsv1alpha1.FailedProgressStatus}, status)
}
// setComponentStatusProgressDetail sets the corresponding progressDetail in progressDetails to newProgressDetail.
// progressDetails must be non-nil.
// 1. the startTime and endTime will be filled automatically.
// 2. if the progressDetail of the specified objectKey does not exist, it will be appended to the progressDetails.
func setComponentStatusProgressDetail(
recorder record.EventRecorder,
opsRequest *appsv1alpha1.OpsRequest,
progressDetails *[]appsv1alpha1.ProgressStatusDetail,
newProgressDetail appsv1alpha1.ProgressStatusDetail) {
if progressDetails == nil {
return
}
existingProgressDetail := findStatusProgressDetail(*progressDetails, newProgressDetail.ObjectKey)
if existingProgressDetail == nil {
updateProgressDetailTime(&newProgressDetail)
*progressDetails = append(*progressDetails, newProgressDetail)
sendProgressDetailEvent(recorder, opsRequest, newProgressDetail)
return
}
if existingProgressDetail.Status == newProgressDetail.Status &&
existingProgressDetail.Message == newProgressDetail.Message {
return
}
// if existing progress detail is 'Failed' and new progress detail is not 'Succeed', ignores the new one.
if existingProgressDetail.Status == appsv1alpha1.FailedProgressStatus &&
newProgressDetail.Status != appsv1alpha1.SucceedProgressStatus {
return
}
existingProgressDetail.Status = newProgressDetail.Status
existingProgressDetail.Message = newProgressDetail.Message
updateProgressDetailTime(existingProgressDetail)
sendProgressDetailEvent(recorder, opsRequest, newProgressDetail)
}
// findStatusProgressDetail finds the progressDetail of the specified objectKey in progressDetails.
func findStatusProgressDetail(progressDetails []appsv1alpha1.ProgressStatusDetail,
objectKey string) *appsv1alpha1.ProgressStatusDetail {
for i := range progressDetails {
if progressDetails[i].ObjectKey == objectKey {
return &progressDetails[i]
}
}
return nil
}
// getProgressDetailEventType gets the event type with progressDetail status.
func getProgressDetailEventType(status appsv1alpha1.ProgressStatus) string {
if status == appsv1alpha1.FailedProgressStatus {
return corev1.EventTypeWarning
}
return corev1.EventTypeNormal
}
// getProgressDetailEventReason gets the event reason with progressDetail status.
func getProgressDetailEventReason(status appsv1alpha1.ProgressStatus) string {
switch status {
case appsv1alpha1.SucceedProgressStatus:
return "Succeed"
case appsv1alpha1.ProcessingProgressStatus:
return "Processing"
case appsv1alpha1.FailedProgressStatus:
return "Failed"
}
return ""
}
// sendProgressDetailEvent sends the progress detail changed events.
func sendProgressDetailEvent(recorder record.EventRecorder,
opsRequest *appsv1alpha1.OpsRequest,
progressDetail appsv1alpha1.ProgressStatusDetail) {
status := progressDetail.Status
if status == appsv1alpha1.PendingProgressStatus {
return
}
recorder.Event(opsRequest, getProgressDetailEventType(status),
getProgressDetailEventReason(status), progressDetail.Message)
}
// updateProgressDetailTime updates the progressDetail startTime or endTime according to the status.
func updateProgressDetailTime(progressDetail *appsv1alpha1.ProgressStatusDetail) {
if progressDetail.Status == appsv1alpha1.ProcessingProgressStatus &&
progressDetail.StartTime.IsZero() {
progressDetail.StartTime = metav1.NewTime(time.Now())
}
if isCompletedProgressStatus(progressDetail.Status) &&
progressDetail.EndTime.IsZero() {
progressDetail.EndTime = metav1.NewTime(time.Now())
}
}
// convertPodObjectKeyMap converts the object key map from the pod list.
func convertPodObjectKeyMap(podList *corev1.PodList) map[string]struct{} {
podObjectKeyMap := map[string]struct{}{}
for _, v := range podList.Items {
objectKey := getProgressObjectKey(v.Kind, v.Name)
podObjectKeyMap[objectKey] = struct{}{}
}
return podObjectKeyMap
}
// removeStatelessExpiredPods if the object of progressDetail is not existing in k8s cluster, it indicates the pod is deleted.
// For example, a replicaSet may attempt to create a pod multiple times till it succeeds.
// so some pod may be expired, we should clear them.
func removeStatelessExpiredPods(podList *corev1.PodList,
progressDetails []appsv1alpha1.ProgressStatusDetail) []appsv1alpha1.ProgressStatusDetail {
podObjectKeyMap := convertPodObjectKeyMap(podList)
newProgressDetails := make([]appsv1alpha1.ProgressStatusDetail, 0)
for _, v := range progressDetails {
if _, ok := podObjectKeyMap[v.ObjectKey]; ok {
newProgressDetails = append(newProgressDetails, v)
}
}
return newProgressDetails
}
// handleComponentStatusProgress handles the component status progressDetails.
// if all the pods of the component are affected, use this function to reconcile the progressDetails.
func handleComponentStatusProgress(
reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus) (expectProgressCount int32, completedCount int32, err error) {
var (
podList *corev1.PodList
clusterComponentDef = pgRes.clusterComponentDef
clusterComponent = pgRes.clusterComponent
)
if clusterComponent == nil || clusterComponentDef == nil {
return
}
if podList, err = components.GetComponentPodList(reqCtx.Ctx, cli, *opsRes.Cluster, clusterComponent.Name); err != nil {
return
}
switch clusterComponentDef.WorkloadType {
case appsv1alpha1.Stateless:
completedCount, err = handleStatelessProgress(reqCtx, cli, opsRes, podList, pgRes, compStatus)
default:
completedCount, err = handleStatefulSetProgress(reqCtx, cli, opsRes, podList, pgRes, compStatus)
}
expectReplicas := clusterComponent.Replicas
if opsRes.OpsRequest.Status.Phase == appsv1alpha1.OpsCancellingPhase {
// only rollback the actual re-created pod during cancelling.
expectReplicas = int32(len(compStatus.ProgressDetails))
}
return expectReplicas, completedCount, err
}
// handleStatelessProgress handles the stateless component progressDetails.
// For stateless component changes, it applies the Deployment updating policy.
func handleStatelessProgress(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
podList *corev1.PodList,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus) (int32, error) {
if compStatus.Phase == appsv1alpha1.RunningClusterCompPhase && pgRes.clusterComponent.Replicas != int32(len(podList.Items)) {
return 0, intctrlutil.NewError(intctrlutil.ErrorWaitCacheRefresh, "wait for the pods of deployment to be synchronized")
}
minReadySeconds, err := components.GetComponentDeployMinReadySeconds(reqCtx.Ctx, cli, *opsRes.Cluster, pgRes.clusterComponent.Name)
if err != nil {
return 0, err
}
completedCount := handleRollingUpdateProgress(opsRes, podList, pgRes, compStatus, minReadySeconds)
compStatus.ProgressDetails = removeStatelessExpiredPods(podList, compStatus.ProgressDetails)
return completedCount, nil
}
// handleStatefulSetProgress handles the component progressDetails which using statefulSet workloads.
func handleStatefulSetProgress(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
podList *corev1.PodList,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus) (int32, error) {
minReadySeconds, err := components.GetComponentStsMinReadySeconds(reqCtx.Ctx, cli, *opsRes.Cluster, pgRes.clusterComponent.Name)
if err != nil {
return 0, err
}
return handleRollingUpdateProgress(opsRes, podList, pgRes, compStatus, minReadySeconds), nil
}
// handleRollingUpdateProgress handles the component progressDetails during rolling update.
func handleRollingUpdateProgress(
opsRes *OpsResource,
podList *corev1.PodList,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
minReadySeconds int32) int32 {
if opsRes.OpsRequest.Status.Phase == appsv1alpha1.OpsCancellingPhase {
return handleCancelProgressForPodsRollingUpdate(opsRes, podList, pgRes, compStatus, minReadySeconds)
}
return handleProgressForPodsRollingUpdate(opsRes, podList, pgRes, compStatus, minReadySeconds)
}
// handleProgressForPodsRollingUpdate handles the progress of pods during rolling update.
func handleProgressForPodsRollingUpdate(
opsRes *OpsResource,
podList *corev1.PodList,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
minReadySeconds int32) int32 {
workloadType := pgRes.clusterComponentDef.WorkloadType
opsRequest := opsRes.OpsRequest
opsStartTime := opsRequest.Status.StartTimestamp
var completedCount int32
for _, v := range podList.Items {
objectKey := getProgressObjectKey(v.Kind, v.Name)
progressDetail := appsv1alpha1.ProgressStatusDetail{ObjectKey: objectKey}
if podProcessedSuccessful(workloadType, opsStartTime, &v, minReadySeconds, compStatus.Phase, pgRes.opsIsCompleted) {
completedCount += 1
handleSucceedProgressDetail(opsRes, pgRes, compStatus, progressDetail)
continue
}
if podIsPendingDuringOperation(opsStartTime, &v) {
handlePendingProgressDetail(opsRes, compStatus, progressDetail)
continue
}
completedCount += handleFailedOrProcessingProgressDetail(opsRes, pgRes, compStatus, progressDetail, &v)
}
return completedCount
}
// handleCancelProgressForPodsRollingUpdate handles the cancel progress of pods during rolling update.
func handleCancelProgressForPodsRollingUpdate(
opsRes *OpsResource,
podList *corev1.PodList,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
minReadySeconds int32) int32 {
var newProgressDetails []appsv1alpha1.ProgressStatusDetail
for _, v := range compStatus.ProgressDetails {
// remove the pending progressDetail
if v.Status != appsv1alpha1.PendingProgressStatus {
newProgressDetails = append(newProgressDetails, v)
}
}
compStatus.ProgressDetails = newProgressDetails
opsCancelTime := opsRes.OpsRequest.Status.CancelTimestamp
workloadType := pgRes.clusterComponentDef.WorkloadType
pgRes.opsMessageKey = fmt.Sprintf("%s with rollback", pgRes.opsMessageKey)
var completedCount int32
for _, pod := range podList.Items {
objectKey := getProgressObjectKey(pod.Kind, pod.Name)
progressDetail := appsv1alpha1.ProgressStatusDetail{ObjectKey: objectKey}
if !pod.CreationTimestamp.Before(&opsCancelTime) &&
components.PodIsAvailable(workloadType, &pod, minReadySeconds) {
completedCount += 1
handleSucceedProgressDetail(opsRes, pgRes, compStatus, progressDetail)
continue
}
if podIsPendingDuringOperation(opsCancelTime, &pod) {
continue
}
completedCount += handleFailedOrProcessingProgressDetail(opsRes, pgRes, compStatus, progressDetail, &pod)
}
return completedCount
}
// handlePendingProgressDetail handles the pending progressDetail and sets it to progressDetails.
func handlePendingProgressDetail(opsRes *OpsResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
progressDetail appsv1alpha1.ProgressStatusDetail,
) {
progressDetail.Status = appsv1alpha1.PendingProgressStatus
setComponentStatusProgressDetail(opsRes.Recorder, opsRes.OpsRequest,
&compStatus.ProgressDetails, progressDetail)
}
// handleSucceedProgressDetail handles the successful progressDetail and sets it to progressDetails.
func handleSucceedProgressDetail(opsRes *OpsResource,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
progressDetail appsv1alpha1.ProgressStatusDetail,
) {
progressDetail.SetStatusAndMessage(appsv1alpha1.SucceedProgressStatus,
getProgressSucceedMessage(pgRes.opsMessageKey, progressDetail.ObjectKey, pgRes.clusterComponent.Name))
setComponentStatusProgressDetail(opsRes.Recorder, opsRes.OpsRequest,
&compStatus.ProgressDetails, progressDetail)
}
// handleFailedOrProcessingProgressDetail handles failed or processing progressDetail and sets it to progressDetails.
func handleFailedOrProcessingProgressDetail(opsRes *OpsResource,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
progressDetail appsv1alpha1.ProgressStatusDetail,
pod *corev1.Pod) (completedCount int32) {
componentName := pgRes.clusterComponent.Name
opsStartTime := opsRes.OpsRequest.Status.StartTimestamp
if podIsFailedDuringOperation(opsStartTime, pod, compStatus.Phase, pgRes.opsIsCompleted) {
podMessage := getFailedPodMessage(opsRes.Cluster, componentName, pod)
// if the pod is not failed, return
if len(podMessage) == 0 {
return
}
message := getProgressFailedMessage(pgRes.opsMessageKey, progressDetail.ObjectKey, componentName, podMessage)
progressDetail.SetStatusAndMessage(appsv1alpha1.FailedProgressStatus, message)
completedCount = 1
} else {
progressDetail.SetStatusAndMessage(appsv1alpha1.ProcessingProgressStatus,
getProgressProcessingMessage(pgRes.opsMessageKey, progressDetail.ObjectKey, componentName))
}
setComponentStatusProgressDetail(opsRes.Recorder, opsRes.OpsRequest,
&compStatus.ProgressDetails, progressDetail)
return completedCount
}
// podIsPendingDuringOperation checks if pod is pending during the component's operation.
func podIsPendingDuringOperation(opsStartTime metav1.Time, pod *corev1.Pod) bool {
return pod.CreationTimestamp.Before(&opsStartTime) && pod.DeletionTimestamp.IsZero()
}
// podIsFailedDuringOperation checks if pod is failed during operation.
func podIsFailedDuringOperation(
opsStartTime metav1.Time,
pod *corev1.Pod,
componentPhase appsv1alpha1.ClusterComponentPhase,
opsIsCompleted bool) bool {
if !components.IsFailedOrAbnormal(componentPhase) {
return false
}
return !pod.CreationTimestamp.Before(&opsStartTime) || opsIsCompleted
}
// podProcessedSuccessful checks if the pod has been processed successfully:
// 1. the pod is recreated after OpsRequest.status.startTime and pod is available.
// 2. the component is running and pod is available.
func podProcessedSuccessful(workloadType appsv1alpha1.WorkloadType,
opsStartTime metav1.Time,
pod *corev1.Pod,
minReadySeconds int32,
componentPhase appsv1alpha1.ClusterComponentPhase,
opsIsCompleted bool) bool {
if !components.PodIsAvailable(workloadType, pod, minReadySeconds) {
return false
}
return (opsIsCompleted && componentPhase == appsv1alpha1.RunningClusterCompPhase) || !pod.CreationTimestamp.Before(&opsStartTime)
}
func getProgressProcessingMessage(opsMessageKey, objectKey, componentName string) string {
return fmt.Sprintf("Start to %s: %s in Component: %s", opsMessageKey, objectKey, componentName)
}
func getProgressSucceedMessage(opsMessageKey, objectKey, componentName string) string {
return fmt.Sprintf("Successfully %s: %s in Component: %s", opsMessageKey, objectKey, componentName)
}
func getProgressFailedMessage(opsMessageKey, objectKey, componentName, podMessage string) string {
return fmt.Sprintf("Failed to %s: %s in Component: %s, message: %s", opsMessageKey, objectKey, componentName, podMessage)
}
// getFailedPodMessage gets the failed pod message from cluster component status
func getFailedPodMessage(cluster *appsv1alpha1.Cluster, componentName string, pod *corev1.Pod) string {
clusterCompStatus := cluster.Status.Components[componentName]
return clusterCompStatus.GetObjectMessage(pod.Kind, pod.Name)
}
func getComponentLastReplicas(opsRequest *appsv1alpha1.OpsRequest, componentName string) *int32 {
lastCompConfiguration := opsRequest.Status.LastConfiguration.Components[componentName]
if lastCompConfiguration.Replicas == nil {
return nil
}
if lastPods, ok := lastCompConfiguration.TargetResources[appsv1alpha1.PodsCompResourceKey]; ok {
lastActualComponents := int32(len(lastPods))
// may the actual pods not equals the component replicas
if lastActualComponents < *lastCompConfiguration.Replicas {
return &lastActualComponents
}
}
return lastCompConfiguration.Replicas
}
// handleComponentProgressDetails handles the component progressDetails when scale the replicas.
// @return expectProgressCount,
// @return completedCount
// @return error
func handleComponentProgressForScalingReplicas(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes progressResource,
compStatus *appsv1alpha1.OpsRequestComponentStatus,
getExpectReplicas func(opsRequest *appsv1alpha1.OpsRequest, componentName string) *int32) (int32, int32, error) {
var (
podList *corev1.PodList
clusterComponent = pgRes.clusterComponent
opsRequest = opsRes.OpsRequest
err error
)
if clusterComponent == nil || pgRes.clusterComponentDef == nil {
return 0, 0, nil
}
expectReplicas := getExpectReplicas(opsRequest, clusterComponent.Name)
if expectReplicas == nil {
return 0, 0, nil
}
lastComponentReplicas := getComponentLastReplicas(opsRequest, clusterComponent.Name)
if lastComponentReplicas == nil {
return 0, 0, nil
}
// if replicas are not changed, return
if *lastComponentReplicas == *expectReplicas {
return 0, 0, nil
}
if podList, err = components.GetComponentPodList(reqCtx.Ctx, cli, *opsRes.Cluster, clusterComponent.Name); err != nil {
return 0, 0, err
}
actualPodsLen := int32(len(podList.Items))
if compStatus.Phase == appsv1alpha1.RunningClusterCompPhase && pgRes.clusterComponent.Replicas != actualPodsLen {
return 0, 0, intctrlutil.NewError(intctrlutil.ErrorWaitCacheRefresh, "wait for the pods of component to be synchronized")
}
if opsRequest.Status.Phase == appsv1alpha1.OpsCancellingPhase {
expectReplicas = opsRequest.Status.LastConfiguration.Components[clusterComponent.Name].Replicas
}
var (
isScaleOut bool
expectProgressCount int32
completedCount int32
dValue = *expectReplicas - *lastComponentReplicas
)
if dValue > 0 {
expectProgressCount = dValue
isScaleOut = true
} else {
expectProgressCount = dValue * -1
}
if isScaleOut {
completedCount, err = handleScaleOutProgress(reqCtx, cli, opsRes, pgRes, podList, compStatus)
// if the workload type is Stateless, remove the progressDetails of the expired pods.
// because ReplicaSet may attempt to create a pod multiple times till it succeeds when scale out the replicas.
if pgRes.clusterComponentDef.WorkloadType == appsv1alpha1.Stateless {
compStatus.ProgressDetails = removeStatelessExpiredPods(podList, compStatus.ProgressDetails)
}
} else {
completedCount, err = handleScaleDownProgress(reqCtx, cli, opsRes, pgRes, podList, compStatus)
}
return getFinalExpectCount(compStatus, expectProgressCount), completedCount, err
}
// handleScaleOutProgress handles the progressDetails of scaled out replicas.
func handleScaleOutProgress(reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes progressResource,
podList *corev1.PodList,
compStatus *appsv1alpha1.OpsRequestComponentStatus) (int32, error) {
var componentName = pgRes.clusterComponent.Name
var workloadType = pgRes.clusterComponentDef.WorkloadType
minReadySeconds, err := components.GetComponentWorkloadMinReadySeconds(reqCtx.Ctx, cli, *opsRes.Cluster, workloadType, componentName)
if err != nil {
return 0, err
}
var completedCount int32
for _, v := range podList.Items {
// only focus on the newly created pod when scaling out the replicas.
if v.CreationTimestamp.Before(&opsRes.OpsRequest.Status.StartTimestamp) {
continue
}
objectKey := getProgressObjectKey(v.Kind, v.Name)
progressDetail := appsv1alpha1.ProgressStatusDetail{ObjectKey: objectKey}
pgRes.opsMessageKey = "create"
if components.PodIsAvailable(workloadType, &v, minReadySeconds) {
completedCount += 1
handleSucceedProgressDetail(opsRes, pgRes, compStatus, progressDetail)
continue
}
completedCount += handleFailedOrProcessingProgressDetail(opsRes, pgRes, compStatus, progressDetail, &v)
}
return completedCount, nil
}
// handleScaleDownProgress handles the progressDetails of scaled down replicas.
func handleScaleDownProgress(
reqCtx intctrlutil.RequestCtx,
cli client.Client,
opsRes *OpsResource,
pgRes progressResource,
podList *corev1.PodList,
compStatus *appsv1alpha1.OpsRequestComponentStatus) (completedCount int32, err error) {
podMap := map[string]corev1.Pod{}
// record the deleting pod progressDetail
for _, v := range podList.Items {
objectKey := getProgressObjectKey(constant.PodKind, v.Name)
podMap[objectKey] = v
if v.DeletionTimestamp.IsZero() {
continue
}
setComponentStatusProgressDetail(opsRes.Recorder, opsRes.OpsRequest,
&compStatus.ProgressDetails, appsv1alpha1.ProgressStatusDetail{
ObjectKey: objectKey,
Status: appsv1alpha1.ProcessingProgressStatus,
Message: fmt.Sprintf("Start to delete pod: %s in Component: %s", objectKey, pgRes.clusterComponent.Name),
})
}
var workloadType = pgRes.clusterComponentDef.WorkloadType
var componentName = pgRes.clusterComponent.Name
minReadySeconds, err := components.GetComponentStsMinReadySeconds(reqCtx.Ctx, cli, *opsRes.Cluster, componentName)
if err != nil {
return 0, err
}
handleDeletionSuccessful := func(objectKey string) {
// if the pod is not in the podList, it means the pod has been deleted.
progressDetail := appsv1alpha1.ProgressStatusDetail{
ObjectKey: objectKey,
Status: appsv1alpha1.SucceedProgressStatus,
Message: fmt.Sprintf("Successfully delete pod: %s in Component: %s", objectKey, pgRes.clusterComponent.Name),
}
completedCount += 1
setComponentStatusProgressDetail(opsRes.Recorder, opsRes.OpsRequest,
&compStatus.ProgressDetails, progressDetail)
}
handleProgressDetails := func() {
for _, progressDetail := range compStatus.ProgressDetails {
if isCompletedProgressStatus(progressDetail.Status) {
completedCount += 1
continue
}
// if pod not exists, means successful deletion.
pod, ok := podMap[progressDetail.ObjectKey]
if !ok {
handleDeletionSuccessful(progressDetail.ObjectKey)
continue
}
// handle the re-created pods if these pods are failed before doing horizontal scaling.
pgRes.opsMessageKey = "re-create"
if components.PodIsAvailable(workloadType, &pod, minReadySeconds) {
completedCount += 1
handleSucceedProgressDetail(opsRes, pgRes, compStatus, progressDetail)
continue
}
if pod.DeletionTimestamp.IsZero() {
completedCount += handleFailedOrProcessingProgressDetail(opsRes, pgRes, compStatus, progressDetail, &pod)
}
}
}
handleDeletedPodNotInProgressDetails := func() {
// pod may not be recorded in the progressDetails if deleted quickly or due to unknown reasons, but it has actually been deleted.
// compare with the last pods and current pods to check if pod is deleted.
lastComponentPodNames := getTargetResourcesOfLastComponent(opsRes.OpsRequest.Status.LastConfiguration, componentName, appsv1alpha1.PodsCompResourceKey)
for _, v := range lastComponentPodNames {
objectKey := getProgressObjectKey(constant.PodKind, v)
progressDetail := findStatusProgressDetail(compStatus.ProgressDetails, objectKey)
// if recorded in progressDetails, continue
if progressDetail != nil {
continue
}
if _, ok := podMap[objectKey]; ok {
continue
}
handleDeletionSuccessful(objectKey)
}
}
handleProgressDetails()
handleDeletedPodNotInProgressDetails()
return completedCount, nil
}
// getFinalExpectCount gets the number of pods which has been processed by controller.
func getFinalExpectCount(compStatus *appsv1alpha1.OpsRequestComponentStatus, expectProgressCount int32) int32 {
progressDetailsLen := int32(len(compStatus.ProgressDetails))
if progressDetailsLen > expectProgressCount {
expectProgressCount = progressDetailsLen
}
return expectProgressCount
}