@@ -31,6 +31,7 @@ import (
3131 "k8s.io/apimachinery/pkg/api/meta"
3232 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333 "k8s.io/apimachinery/pkg/util/sets"
34+ "k8s.io/client-go/tools/record"
3435 "sigs.k8s.io/controller-runtime/pkg/client"
3536
3637 appsv1 "github.com/apecloud/kubeblocks/apis/apps/v1"
@@ -304,169 +305,142 @@ func (t *componentStatusTransformer) updateComponentStatus(transCtx *componentTr
304305}
305306
306307func (t * componentStatusTransformer ) reconcileStatusCondition (transCtx * componentTransformContext ) error {
307- t .reconcileAvailableCondition (transCtx )
308- err1 := t .reconcileHasFailureCondition (transCtx )
309- err2 := t .reconcileUpdatingCondition (transCtx )
310- t .reconcileWorkloadRunningCondition (transCtx )
311- return errors .Join (err1 , err2 )
308+ err1 := t .reconcileAvailableCondition (transCtx )
309+ err2 := t .reconcileHasFailureCondition (transCtx )
310+ err3 := t .reconcileUpdatingCondition (transCtx )
311+ err4 := t .reconcileWorkloadRunningCondition (transCtx )
312+ return errors .Join (err1 , err2 , err3 , err4 )
312313}
313314
314- func (t * componentStatusTransformer ) reconcileHasFailureCondition (transCtx * componentTransformContext ) error {
315- status := metav1 .ConditionFalse
316- reason := "NoFailure"
317- message := ""
318-
319- hasFailedPod , messages := t .hasFailedPod ()
320-
321- if hasFailedPod {
322- status = metav1 .ConditionTrue
323- reason = "PodFailure"
324- message = "component has failed pod(s)"
325- for _ , msg := range messages {
326- message += "; " + msg
327- }
328- }
329-
330- _ , hasFailedScaleOut , err := t .hasScaleOutRunning (transCtx )
315+ func (t * componentStatusTransformer ) checkNSetCondition (
316+ eventRecorder record.EventRecorder ,
317+ conditionType string ,
318+ checker func () (err error , status metav1.ConditionStatus , reason , message string ),
319+ ) error {
320+ err , status , reason , message := checker ()
331321 if err != nil {
332322 return err
333323 }
334-
335- if hasFailedScaleOut {
336- status = metav1 .ConditionTrue
337- reason = "ScaleOutFailure"
338- message = "component scale out has failure"
339- }
340-
341324 cond := metav1.Condition {
342- Type : appsv1 . ConditionTypeHasFailure ,
325+ Type : conditionType ,
343326 Status : status ,
344327 ObservedGeneration : t .comp .Generation ,
345328 Reason : reason ,
346329 Message : message ,
347330 }
348331 if meta .SetStatusCondition (& t .comp .Status .Conditions , cond ) {
349- transCtx . EventRecorder .Event (t .comp , corev1 .EventTypeNormal , reason , message )
332+ eventRecorder .Event (t .comp , corev1 .EventTypeNormal , reason , message )
350333 }
351334 return nil
352335}
353336
354- func (t * componentStatusTransformer ) reconcileUpdatingCondition (transCtx * componentTransformContext ) error {
355- status := metav1 .ConditionFalse
356- reason := "NotUpdating"
357- message := ""
358-
359- // check if the component scale out failed
360- hasRunningScaleOut , _ , err := t .hasScaleOutRunning (transCtx )
361- if err != nil {
362- return err
363- }
364- if hasRunningScaleOut {
365- status = metav1 .ConditionTrue
366- reason = "ScaleOutRunning"
367- message = "component scale out is running"
368- }
337+ func (t * componentStatusTransformer ) reconcileHasFailureCondition (transCtx * componentTransformContext ) error {
338+ return t .checkNSetCondition (
339+ transCtx .EventRecorder ,
340+ appsv1 .ConditionTypeHasFailure ,
341+ func () (err error , status metav1.ConditionStatus , reason string , message string ) {
342+ hasFailedPod , messages := t .hasFailedPod ()
343+ if hasFailedPod {
344+ message = "component has failed pod(s)"
345+ for _ , msg := range messages {
346+ message += "; " + msg
347+ }
348+ return nil , metav1 .ConditionTrue , "PodFailure" , message
349+ }
369350
370- // check if the volume expansion is running
371- hasRunningVolumeExpansion := t . hasVolumeExpansionRunning ()
372- if hasRunningVolumeExpansion {
373- status = metav1 . ConditionTrue
374- reason = "VolumeExpansionRunning"
375- message = " component volume expansion is running "
376- }
351+ _ , hasFailedScaleOut , err := t . hasScaleOutRunning ( transCtx )
352+ if err != nil {
353+ return err , "" , "" , ""
354+ }
355+ if hasFailedScaleOut {
356+ return nil , metav1 . ConditionTrue , "ScaleOutFailure" , " component scale out has failure "
357+ }
377358
378- cond := metav1.Condition {
379- Type : appsv1 .ConditionTypeUpdating ,
380- Status : status ,
381- ObservedGeneration : t .comp .Generation ,
382- Reason : reason ,
383- Message : message ,
384- }
385- if meta .SetStatusCondition (& t .comp .Status .Conditions , cond ) {
386- transCtx .EventRecorder .Event (t .comp , corev1 .EventTypeNormal , reason , message )
387- }
388- return nil
359+ return nil , metav1 .ConditionFalse , "NoFailure" , ""
360+ },
361+ )
389362}
390363
391- func (t * componentStatusTransformer ) reconcileWorkloadRunningCondition (transCtx * componentTransformContext ) {
392- status := metav1 .ConditionTrue
393- reason := "WorkloadRunning"
394- message := ""
395- comp := t .comp
364+ func (t * componentStatusTransformer ) reconcileUpdatingCondition (transCtx * componentTransformContext ) error {
365+ return t .checkNSetCondition (
366+ transCtx .EventRecorder ,
367+ appsv1 .ConditionTypeUpdating ,
368+ func () (err error , status metav1.ConditionStatus , reason string , message string ) {
369+ hasRunningScaleOut , _ , err := t .hasScaleOutRunning (transCtx )
370+ if err != nil {
371+ return err , "" , "" , ""
372+ }
373+ if hasRunningScaleOut {
374+ status = metav1 .ConditionTrue
375+ reason = "ScaleOutRunning"
376+ message = "component scale out is running"
377+ return nil , status , reason , message
378+ }
396379
397- switch {
398- case t .runningITS == nil :
399- status = metav1 .ConditionFalse
400- reason = "WorkloadNotExist"
401- message = "waiting for workload to be created"
402- case ! t .isWorkloadUpdated ():
403- status = metav1 .ConditionFalse
404- reason = "WorkloadNotUpdated"
405- message = "observed workload's generation not matching component's"
406- case ! t .runningITS .IsInstanceSetReady ():
407- status = metav1 .ConditionFalse
408- reason = "WorkloadNotReady"
409- message = "workload not ready"
410- }
380+ hasRunningVolumeExpansion := t .hasVolumeExpansionRunning ()
381+ if hasRunningVolumeExpansion {
382+ status = metav1 .ConditionTrue
383+ reason = "VolumeExpansionRunning"
384+ message = "component volume expansion is running"
385+ return nil , status , reason , message
386+ }
411387
412- cond := metav1.Condition {
413- Type : appsv1 .ConditionTypeWorkloadRunning ,
414- Status : status ,
415- ObservedGeneration : comp .Generation ,
416- Reason : reason ,
417- Message : message ,
418- }
419- if meta .SetStatusCondition (& comp .Status .Conditions , cond ) {
420- transCtx .EventRecorder .Event (comp , corev1 .EventTypeNormal , reason , message )
421- }
388+ return nil , metav1 .ConditionFalse , "NotUpdating" , ""
389+ },
390+ )
422391}
423392
424- func (t * componentStatusTransformer ) reconcileAvailableCondition (transCtx * componentTransformContext ) {
393+ func (t * componentStatusTransformer ) reconcileWorkloadRunningCondition (transCtx * componentTransformContext ) error {
394+ return t .checkNSetCondition (
395+ transCtx .EventRecorder ,
396+ appsv1 .ConditionTypeWorkloadRunning ,
397+ func () (err error , status metav1.ConditionStatus , reason string , message string ) {
398+ status = metav1 .ConditionTrue
399+ reason = "WorkloadRunning"
400+ message = ""
401+
402+ switch {
403+ case t .runningITS == nil :
404+ status = metav1 .ConditionFalse
405+ reason = "WorkloadNotExist"
406+ message = "waiting for workload to be created"
407+ case ! t .isWorkloadUpdated ():
408+ status = metav1 .ConditionFalse
409+ reason = "WorkloadNotUpdated"
410+ message = "observed workload's generation not matching component's"
411+ case ! t .runningITS .IsInstanceSetReady ():
412+ status = metav1 .ConditionFalse
413+ reason = "WorkloadNotReady"
414+ message = "workload not ready"
415+ }
416+
417+ return nil , status , reason , message
418+ },
419+ )
420+ }
421+
422+ func (t * componentStatusTransformer ) reconcileAvailableCondition (transCtx * componentTransformContext ) error {
425423 policy := component .GetComponentAvailablePolicy (transCtx .CompDef )
426424 if policy .WithPhases == nil && policy .WithRole == nil {
427- return
425+ return nil
428426 }
429427
430- var (
431- comp = transCtx .Component
432- reason string
433- message string
434- )
435- status := metav1 .ConditionTrue
436- if policy .WithPhases != nil {
437- status1 , message1 := t .availableWithPhases (transCtx , comp , policy )
438- if status1 != metav1 .ConditionTrue {
439- status = status1
440- reason = "PhaseCheckFail"
441- }
442- message += fmt .Sprintf ("phase check: %s. " , message1 )
443- }
444- if policy .WithRole != nil {
445- status2 , message2 := t .availableWithRole (transCtx , comp , policy )
446- if status2 != metav1 .ConditionTrue {
447- status = status2
448- if reason != "" {
449- reason = "PhaseAndRoleCheckFail"
450- } else {
451- reason = "RoleCheckFail"
428+ return t .checkNSetCondition (
429+ transCtx .EventRecorder ,
430+ appsv1 .ConditionTypeAvailable ,
431+ func () (err error , status metav1.ConditionStatus , reason string , message string ) {
432+ if policy .WithPhases != nil {
433+ status , message := t .availableWithPhases (transCtx , transCtx .Component , policy )
434+ return nil , status , "PhaseCheckFail" , message
435+ }
436+ if policy .WithRole != nil {
437+ status , message := t .availableWithRole (transCtx , transCtx .Component , policy )
438+ return nil , status , "RoleCheckFail" , message
452439 }
453- }
454- message += fmt .Sprintf ("role check: %s. " , message2 )
455- }
456- if reason == "" {
457- reason = "Available"
458- }
459440
460- cond := metav1.Condition {
461- Type : appsv1 .ConditionTypeAvailable ,
462- Status : status ,
463- ObservedGeneration : comp .Generation ,
464- Reason : reason ,
465- Message : message ,
466- }
467- if meta .SetStatusCondition (& comp .Status .Conditions , cond ) {
468- transCtx .EventRecorder .Event (comp , corev1 .EventTypeNormal , reason , message )
469- }
441+ return nil , metav1 .ConditionTrue , "Available" , ""
442+ },
443+ )
470444}
471445
472446func (t * componentStatusTransformer ) availableWithPhases (_ * componentTransformContext ,
0 commit comments