@@ -11,48 +11,78 @@ import (
11
11
12
12
// conditionMergeState indicates whether you want to merge all Falses or merge all Trues. For instance, Failures merge
13
13
// on true, but Available merges on false. Thing of it like an anti-default.
14
- func unionCondition (conditionType string , conditionMergeState operatorv1.ConditionStatus , allConditions ... operatorv1.OperatorCondition ) configv1.ClusterOperatorStatusCondition {
15
- var interestingConditions []operatorv1.OperatorCondition
14
+ func unionCondition (conditionType string , defaultConditionState operatorv1.ConditionStatus , allConditions ... operatorv1.OperatorCondition ) configv1.ClusterOperatorStatusCondition {
15
+ var oppositeConditionStatus operatorv1.ConditionStatus
16
+ if defaultConditionState == operatorv1 .ConditionTrue {
17
+ oppositeConditionStatus = operatorv1 .ConditionFalse
18
+ } else {
19
+ oppositeConditionStatus = operatorv1 .ConditionTrue
20
+ }
21
+
22
+ interestingConditions := []operatorv1.OperatorCondition {}
23
+ badConditions := []operatorv1.OperatorCondition {}
16
24
for _ , condition := range allConditions {
17
- if strings .HasSuffix (condition .Type , conditionType ) && condition . Status == conditionMergeState {
25
+ if strings .HasSuffix (condition .Type , conditionType ) {
18
26
interestingConditions = append (interestingConditions , condition )
27
+
28
+ if condition .Status == oppositeConditionStatus {
29
+ badConditions = append (badConditions , condition )
30
+ }
19
31
}
20
32
}
21
33
22
34
unionedCondition := operatorv1.OperatorCondition {Type : conditionType , Status : operatorv1 .ConditionUnknown }
23
- if len (interestingConditions ) > 0 {
24
- unionedCondition .Status = conditionMergeState
25
- var messages []string
26
- latestTransitionTime := metav1.Time {}
27
- for _ , condition := range interestingConditions {
28
- if latestTransitionTime .Before (& condition .LastTransitionTime ) {
29
- latestTransitionTime = condition .LastTransitionTime
30
- }
35
+ if len (interestingConditions ) == 0 {
36
+ unionedCondition .Status = operatorv1 .ConditionUnknown
37
+ unionedCondition .Reason = "NoData"
38
+ return OperatorConditionToClusterOperatorCondition (unionedCondition )
39
+ }
31
40
32
- if len (condition .Message ) == 0 {
33
- continue
34
- }
35
- for _ , message := range strings .Split (condition .Message , "\n " ) {
36
- messages = append (messages , fmt .Sprintf ("%s: %s" , condition .Type , message ))
37
- }
41
+ if len (badConditions ) == 0 {
42
+ unionedCondition .Status = defaultConditionState
43
+ unionedCondition .Message = unionMessage (interestingConditions )
44
+ unionedCondition .Reason = "AsExpected"
45
+ unionedCondition .LastTransitionTime = latestTransitionTime (interestingConditions )
46
+
47
+ return OperatorConditionToClusterOperatorCondition (unionedCondition )
48
+ }
49
+
50
+ // at this point we have bad conditions
51
+ unionedCondition .Status = oppositeConditionStatus
52
+ unionedCondition .Message = unionMessage (badConditions )
53
+ unionedCondition .Reason = unionReason (badConditions )
54
+ unionedCondition .LastTransitionTime = latestTransitionTime (badConditions )
55
+
56
+ return OperatorConditionToClusterOperatorCondition (unionedCondition )
57
+ }
58
+
59
+ func latestTransitionTime (conditions []operatorv1.OperatorCondition ) metav1.Time {
60
+ latestTransitionTime := metav1.Time {}
61
+ for _ , condition := range conditions {
62
+ if latestTransitionTime .Before (& condition .LastTransitionTime ) {
63
+ latestTransitionTime = condition .LastTransitionTime
38
64
}
39
- if len (messages ) > 0 {
40
- unionedCondition .Message = strings .Join (messages , "\n " )
65
+ }
66
+ return latestTransitionTime
67
+ }
68
+
69
+ func unionMessage (conditions []operatorv1.OperatorCondition ) string {
70
+ messages := []string {}
71
+ for _ , condition := range conditions {
72
+ if len (condition .Message ) == 0 {
73
+ continue
41
74
}
42
- if len (interestingConditions ) == 1 {
43
- unionedCondition .Reason = interestingConditions [0 ].Type
44
- } else {
45
- unionedCondition .Reason = "MultipleConditionsMatching"
75
+ for _ , message := range strings .Split (condition .Message , "\n " ) {
76
+ messages = append (messages , fmt .Sprintf ("%s: %s" , condition .Type , message ))
46
77
}
47
- unionedCondition .LastTransitionTime = latestTransitionTime
78
+ }
79
+ return strings .Join (messages , "\n " )
80
+ }
48
81
82
+ func unionReason (conditions []operatorv1.OperatorCondition ) string {
83
+ if len (conditions ) == 1 {
84
+ return conditions [0 ].Type
49
85
} else {
50
- if conditionMergeState == operatorv1 .ConditionTrue {
51
- unionedCondition .Status = operatorv1 .ConditionFalse
52
- } else {
53
- unionedCondition .Status = operatorv1 .ConditionTrue
54
- }
86
+ return "MultipleConditionsMatching"
55
87
}
56
-
57
- return OperatorConditionToClusterOperatorCondition (unionedCondition )
58
88
}
0 commit comments