|
| 1 | +package conditions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "slices" |
| 6 | + "strings" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + |
| 10 | + workapiv1 "open-cluster-management.io/api/work/v1" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + // conditionRuleReasonPrefix is used to identify conditions that were generated by condition rules |
| 15 | + conditionRuleReasonPrefix = "ConditionRule" |
| 16 | +) |
| 17 | + |
| 18 | +// IsConditionGeneratedByConditionRule determines if a condition was created by condition rules |
| 19 | +func IsConditionGeneratedByConditionRule(condition metav1.Condition) bool { |
| 20 | + return strings.HasPrefix(condition.Reason, conditionRuleReasonPrefix) |
| 21 | +} |
| 22 | + |
| 23 | +// PruneConditionsGeneratedByConditionRules removes old conditions that were generated by a ConditionRule |
| 24 | +// but have not been updated since that latest ManifestWork generation. Removing a ConditionRule will |
| 25 | +// always update the ManifestWork generation, so any conditions generated by conditions rules from a |
| 26 | +// previous generation must have been from a deleted rule. |
| 27 | +func PruneConditionsGeneratedByConditionRules(conditions *[]metav1.Condition, workGeneration int64) { |
| 28 | + *conditions = slices.DeleteFunc(*conditions, func(c metav1.Condition) bool { |
| 29 | + return IsConditionGeneratedByConditionRule(c) && c.ObservedGeneration != workGeneration |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +// AggregateManifestConditions collects conditions from manifests and combines them into top level |
| 34 | +// conditions for the ManifestWork. Only the Available condition and conditions generated by condition rules |
| 35 | +// are aggregated. |
| 36 | +func AggregateManifestConditions(generation int64, manifests []workapiv1.ManifestCondition) []metav1.Condition { |
| 37 | + aggregated := map[string]aggregateCondition{ |
| 38 | + // Always set Available condition, even if there are no manifests |
| 39 | + workapiv1.ManifestAvailable: {}, |
| 40 | + } |
| 41 | + |
| 42 | + // Collect conditions by type |
| 43 | + for _, manifest := range manifests { |
| 44 | + for _, condition := range manifest.Conditions { |
| 45 | + if condition.Type == workapiv1.ManifestAvailable || IsConditionGeneratedByConditionRule(condition) { |
| 46 | + aggCondition := aggregated[condition.Type] |
| 47 | + switch condition.Status { |
| 48 | + case metav1.ConditionFalse: |
| 49 | + aggCondition.numFalse++ |
| 50 | + case metav1.ConditionTrue: |
| 51 | + aggCondition.numTrue++ |
| 52 | + case metav1.ConditionUnknown: |
| 53 | + aggCondition.numUnknown++ |
| 54 | + } |
| 55 | + |
| 56 | + if condition.Message != "" { |
| 57 | + aggCondition.message = condition.Message |
| 58 | + } |
| 59 | + |
| 60 | + aggregated[condition.Type] = aggCondition |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Create aggregate conditions |
| 66 | + conditions := make([]metav1.Condition, 0, len(aggregated)) |
| 67 | + for conditionType, aggCondition := range aggregated { |
| 68 | + var condition metav1.Condition |
| 69 | + switch conditionType { |
| 70 | + case workapiv1.ManifestAvailable: |
| 71 | + condition = newAggregateAvailableCondition(aggCondition, generation, len(manifests)) |
| 72 | + default: |
| 73 | + condition = newAggregateConditionFromRules(conditionType, generation, aggCondition) |
| 74 | + } |
| 75 | + |
| 76 | + conditions = append(conditions, condition) |
| 77 | + } |
| 78 | + |
| 79 | + // Ensure consistent ordering of aggregated conditions |
| 80 | + slices.SortFunc(conditions, func(a, b metav1.Condition) int { |
| 81 | + return strings.Compare(a.Type, b.Type) |
| 82 | + }) |
| 83 | + return conditions |
| 84 | +} |
| 85 | + |
| 86 | +type aggregateCondition struct { |
| 87 | + numTrue, numFalse, numUnknown int |
| 88 | + message string |
| 89 | +} |
| 90 | + |
| 91 | +func newAggregateAvailableCondition(aggCondition aggregateCondition, generation int64, total int) metav1.Condition { |
| 92 | + switch { |
| 93 | + case aggCondition.numFalse > 0: |
| 94 | + return metav1.Condition{ |
| 95 | + Type: workapiv1.WorkAvailable, |
| 96 | + Status: metav1.ConditionFalse, |
| 97 | + Reason: "ResourcesNotAvailable", |
| 98 | + ObservedGeneration: generation, |
| 99 | + Message: fmt.Sprintf("%d of %d resources are not available", aggCondition.numFalse, total), |
| 100 | + } |
| 101 | + case aggCondition.numUnknown > 0: |
| 102 | + return metav1.Condition{ |
| 103 | + Type: workapiv1.WorkAvailable, |
| 104 | + Status: metav1.ConditionUnknown, |
| 105 | + Reason: "ResourcesStatusUnknown", |
| 106 | + ObservedGeneration: generation, |
| 107 | + Message: fmt.Sprintf("%d of %d resources have unknown status", aggCondition.numUnknown, total), |
| 108 | + } |
| 109 | + case aggCondition.numTrue == 0: |
| 110 | + return metav1.Condition{ |
| 111 | + Type: workapiv1.WorkAvailable, |
| 112 | + Status: metav1.ConditionUnknown, |
| 113 | + Reason: "ResourcesStatusUnknown", |
| 114 | + ObservedGeneration: generation, |
| 115 | + Message: "cannot get any available resource ", |
| 116 | + } |
| 117 | + default: |
| 118 | + return metav1.Condition{ |
| 119 | + Type: workapiv1.WorkAvailable, |
| 120 | + Status: metav1.ConditionTrue, |
| 121 | + Reason: "ResourcesAvailable", |
| 122 | + ObservedGeneration: generation, |
| 123 | + Message: "All resources are available", |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func newAggregateConditionFromRules(conditionType string, generation int64, agg aggregateCondition) metav1.Condition { |
| 129 | + var status metav1.ConditionStatus |
| 130 | + switch { |
| 131 | + case agg.numFalse > 0: |
| 132 | + status = metav1.ConditionFalse |
| 133 | + case agg.numUnknown > 0 || agg.numTrue == 0: |
| 134 | + status = metav1.ConditionUnknown |
| 135 | + default: |
| 136 | + status = metav1.ConditionTrue |
| 137 | + } |
| 138 | + |
| 139 | + // Use manifest condition message if there is only one, otherwise use generic message |
| 140 | + numTotal := agg.numFalse + agg.numTrue + agg.numUnknown |
| 141 | + var message string |
| 142 | + if numTotal == 1 && agg.message != "" { |
| 143 | + message = agg.message |
| 144 | + } else { |
| 145 | + message = fmt.Sprintf("Aggregated from %d manifest conditions", numTotal) |
| 146 | + } |
| 147 | + |
| 148 | + return metav1.Condition{ |
| 149 | + Type: conditionType, |
| 150 | + Status: status, |
| 151 | + Reason: "ConditionRulesAggregated", |
| 152 | + ObservedGeneration: generation, |
| 153 | + Message: message, |
| 154 | + } |
| 155 | +} |
0 commit comments