@@ -344,23 +344,36 @@ func (t *componentStatusTransformer) reconcileStatusCondition(transCtx *componen
344344
345345func (t * componentStatusTransformer ) reconcileAvailableCondition (transCtx * componentTransformContext ) error {
346346 policy := component .GetComponentAvailablePolicy (transCtx .CompDef )
347- if policy .WithPhases == nil {
347+ if policy .WithPhases == nil && policy . WithRole == nil {
348348 return nil
349349 }
350350
351351 var (
352- comp = transCtx .Component
352+ comp = transCtx .Component
353+ status , status1 , status2 metav1.ConditionStatus
354+ reason , reason1 , reason2 string
355+ message , message1 , message2 string
353356 )
354- status , reason , message := func () (metav1.ConditionStatus , string , string ) {
355- if comp .Status .Phase == "" {
356- return metav1 .ConditionUnknown , "Unknown" , "the component phase is unknown"
357- }
358- phases := sets .New [string ](strings .Split (strings .ToLower (* policy .WithPhases ), "," )... )
359- if phases .Has (strings .ToLower (string (comp .Status .Phase ))) {
360- return metav1 .ConditionTrue , "Available" , fmt .Sprintf ("the component phase is %s" , comp .Status .Phase )
357+ if policy .WithPhases != nil {
358+ status1 , reason1 , message1 = t .availableWithPhases (transCtx , comp , policy )
359+ }
360+ if policy .WithRole != nil {
361+ status2 , reason2 , message2 = t .availableWithRole (transCtx , comp , policy )
362+ }
363+
364+ // merge conditions
365+ switch {
366+ case policy .WithPhases != nil && policy .WithRole == nil :
367+ status , reason , message = status1 , reason1 , message1
368+ case policy .WithPhases == nil && policy .WithRole != nil :
369+ status , reason , message = status2 , reason2 , message2
370+ default : // both are not nil
371+ if status1 != metav1 .ConditionTrue {
372+ status , reason , message = status1 , reason1 , message1
373+ } else {
374+ status , reason , message = status2 , reason2 , message2
361375 }
362- return metav1 .ConditionFalse , "Unavailable" , fmt .Sprintf ("the component phase is %s" , comp .Status .Phase )
363- }()
376+ }
364377
365378 cond := metav1.Condition {
366379 Type : appsv1 .ConditionTypeAvailable ,
@@ -373,10 +386,40 @@ func (t *componentStatusTransformer) reconcileAvailableCondition(transCtx *compo
373386 if meta .SetStatusCondition (& comp .Status .Conditions , cond ) {
374387 transCtx .EventRecorder .Event (comp , corev1 .EventTypeNormal , reason , message )
375388 }
376-
377389 return nil
378390}
379391
392+ func (t * componentStatusTransformer ) availableWithPhases (_ * componentTransformContext ,
393+ comp * appsv1.Component , policy appsv1.ComponentAvailable ) (metav1.ConditionStatus , string , string ) {
394+ if comp .Status .Phase == "" {
395+ return metav1 .ConditionUnknown , "Unknown" , "the component phase is unknown"
396+ }
397+ phases := sets .New [string ](strings .Split (strings .ToLower (* policy .WithPhases ), "," )... )
398+ if phases .Has (strings .ToLower (string (comp .Status .Phase ))) {
399+ return metav1 .ConditionTrue , "Available" , fmt .Sprintf ("the component phase is %s" , comp .Status .Phase )
400+ }
401+ return metav1 .ConditionFalse , "Unavailable" , fmt .Sprintf ("the component phase is %s" , comp .Status .Phase )
402+ }
403+
404+ func (t * componentStatusTransformer ) availableWithRole (transCtx * componentTransformContext ,
405+ _ * appsv1.Component , policy appsv1.ComponentAvailable ) (metav1.ConditionStatus , string , string ) {
406+ var its * workloads.InstanceSet
407+ if transCtx .RunningWorkload != nil {
408+ its = transCtx .RunningWorkload .(* workloads.InstanceSet )
409+ }
410+ if its == nil {
411+ return metav1 .ConditionFalse , "Unavailable" , "the workload is not present"
412+ }
413+ for _ , member := range its .Status .MembersStatus {
414+ if member .ReplicaRole != nil {
415+ if strings .EqualFold (member .ReplicaRole .Name , * policy .WithRole ) {
416+ return metav1 .ConditionTrue , "Available" , fmt .Sprintf ("the role %s is present" , * policy .WithRole )
417+ }
418+ }
419+ }
420+ return metav1 .ConditionFalse , "Unavailable" , fmt .Sprintf ("the role %s is not present" , * policy .WithRole )
421+ }
422+
380423func getRunningVolumes (ctx context.Context , cli client.Client , synthesizedComp * component.SynthesizedComponent ,
381424 itsObj * workloads.InstanceSet , vctName string ) ([]* corev1.PersistentVolumeClaim , error ) {
382425 pvcs , err := component .ListOwnedPVCs (ctx , cli , synthesizedComp .Namespace , synthesizedComp .ClusterName , synthesizedComp .Name )
0 commit comments