Skip to content

Commit 074533a

Browse files
authored
Merge pull request #11990 from fabriziopandini/set-merge-operation
🌱 Set merge operation
2 parents 8933ace + 9f706af commit 074533a

File tree

5 files changed

+23
-19
lines changed

5 files changed

+23
-19
lines changed

internal/controllers/cluster/cluster_controller_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ type clusterConditionCustomMergeStrategy struct {
10251025
negativePolarityConditionTypes []string
10261026
}
10271027

1028-
func (c clusterConditionCustomMergeStrategy) Merge(conditions []v1beta2conditions.ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
1028+
func (c clusterConditionCustomMergeStrategy) Merge(operation v1beta2conditions.MergeOperation, conditions []v1beta2conditions.ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
10291029
return v1beta2conditions.DefaultMergeStrategy(v1beta2conditions.GetPriorityFunc(
10301030
func(condition metav1.Condition) v1beta2conditions.MergePriority {
10311031
// While cluster is deleting, treat unknown conditions from external objects as info (it is ok that those objects have been deleted at this stage).
@@ -1050,7 +1050,7 @@ func (c clusterConditionCustomMergeStrategy) Merge(conditions []v1beta2condition
10501050
clusterv1.ClusterAvailableUnknownV1Beta2Reason,
10511051
clusterv1.ClusterAvailableV1Beta2Reason,
10521052
)),
1053-
).Merge(conditions, conditionTypes)
1053+
).Merge(operation, conditions, conditionTypes)
10541054
}
10551055

10561056
func setAvailableCondition(ctx context.Context, cluster *clusterv1.Cluster, clusterClass *clusterv1.ClusterClass) {

internal/controllers/machine/machine_controller_status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ type machineConditionCustomMergeStrategy struct {
496496
negativePolarityConditionTypes []string
497497
}
498498

499-
func (c machineConditionCustomMergeStrategy) Merge(conditions []v1beta2conditions.ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
499+
func (c machineConditionCustomMergeStrategy) Merge(operation v1beta2conditions.MergeOperation, conditions []v1beta2conditions.ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
500500
return v1beta2conditions.DefaultMergeStrategy(
501501
// While machine is deleting, treat unknown conditions from external objects as info (it is ok that those objects have been deleted at this stage).
502502
v1beta2conditions.GetPriorityFunc(func(condition metav1.Condition) v1beta2conditions.MergePriority {
@@ -522,7 +522,7 @@ func (c machineConditionCustomMergeStrategy) Merge(conditions []v1beta2condition
522522
clusterv1.MachineReadyUnknownV1Beta2Reason,
523523
clusterv1.MachineReadyV1Beta2Reason,
524524
)),
525-
).Merge(conditions, conditionTypes)
525+
).Merge(operation, conditions, conditionTypes)
526526
}
527527

528528
// transformControlPlaneAndEtcdConditions Group readiness gates for control plane conditions when they have the same messages.

util/conditions/v1beta2/aggregate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func NewAggregateCondition[T Getter](sourceObjs []T, sourceConditionType string,
110110
}
111111
}
112112

113-
status, reason, message, err := aggregateOpt.mergeStrategy.Merge(conditionsInScope, []string{sourceConditionType})
113+
status, reason, message, err := aggregateOpt.mergeStrategy.Merge(AggregateMergeOperation, conditionsInScope, []string{sourceConditionType})
114114
if err != nil {
115115
return nil, err
116116
}

util/conditions/v1beta2/merge_strategies.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ func (o ConditionOwnerInfo) String() string {
5151
return fmt.Sprintf("%s %s", o.Kind, o.Name)
5252
}
5353

54+
// MergeOperation defines merge operations.
55+
type MergeOperation string
56+
57+
const (
58+
// SummaryMergeOperation defines a merge operation of type Summary.
59+
// Summary should merge different conditions from the same object.
60+
SummaryMergeOperation MergeOperation = "Summary"
61+
62+
// AggregateMergeOperation defines a merge operation of type Aggregate.
63+
// Aggregate should merge the same condition across many objects.
64+
AggregateMergeOperation MergeOperation = "Aggregate"
65+
)
66+
5467
// MergeStrategy defines a strategy used to merge conditions during the aggregate or summary operation.
5568
type MergeStrategy interface {
5669
// Merge passed in conditions.
@@ -59,7 +72,7 @@ type MergeStrategy interface {
5972
// Conditions passed in must be of the given conditionTypes (other condition types must be discarded).
6073
//
6174
// The list of conditionTypes has an implicit order; it is up to the implementation of merge to use this info or not.
62-
Merge(conditions []ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error)
75+
Merge(operation MergeOperation, conditions []ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error)
6376
}
6477

6578
// DefaultMergeStrategyOption is some configuration that modifies the DefaultMergeStrategy behaviour.
@@ -199,7 +212,7 @@ type defaultMergeStrategy struct {
199212
// - issues: conditions with positive polarity (normal True) and status False or conditions with negative polarity (normal False) and status True.
200213
// - unknown: conditions with status unknown.
201214
// - info: conditions with positive polarity (normal True) and status True or conditions with negative polarity (normal False) and status False.
202-
func (d *defaultMergeStrategy) Merge(conditions []ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
215+
func (d *defaultMergeStrategy) Merge(operation MergeOperation, conditions []ConditionWithOwnerInfo, conditionTypes []string) (status metav1.ConditionStatus, reason, message string, err error) {
203216
if len(conditions) == 0 {
204217
return "", "", "", errors.New("can't merge an empty list of conditions")
205218
}
@@ -208,15 +221,6 @@ func (d *defaultMergeStrategy) Merge(conditions []ConditionWithOwnerInfo, condit
208221
return "", "", "", errors.New("can't merge without a getPriority func")
209222
}
210223

211-
// Infer which operation is calling this func, so it is possible to use different strategies for computing the message for the target condition.
212-
// - When merge should consider a single condition type, we can assume this func is called within an aggregate operation
213-
// (Aggregate should merge the same condition across many objects)
214-
isAggregateOperation := len(conditionTypes) == 1
215-
216-
// - Otherwise we can assume this func is called within a summary operation
217-
// (Summary should merge different conditions from the same object)
218-
isSummaryOperation := !isAggregateOperation
219-
220224
// sortConditions the relevance defined by the users (the order of condition types), LastTransition time (older first).
221225
sortConditions(conditions, conditionTypes)
222226

@@ -265,7 +269,7 @@ func (d *defaultMergeStrategy) Merge(conditions []ConditionWithOwnerInfo, condit
265269
//
266270
// When including messages from conditions, they are sorted by issue/unknown and by the implicit order of condition types
267271
// provided by the user (it is considered as order of relevance).
268-
if isSummaryOperation {
272+
if operation == SummaryMergeOperation {
269273
message = summaryMessage(conditions, d, status)
270274
}
271275

@@ -291,7 +295,7 @@ func (d *defaultMergeStrategy) Merge(conditions []ConditionWithOwnerInfo, condit
291295
//
292296
// e.g. ...; 2 more Objects with issues; 1 more Objects with unknown status
293297
//
294-
if isAggregateOperation {
298+
if operation == AggregateMergeOperation {
295299
n := 3
296300
messages := []string{}
297301

util/conditions/v1beta2/summary.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func NewSummaryCondition(sourceObj Getter, targetConditionType string, opts ...S
137137
return nil, errors.New("summary can't be performed when the list of conditions to be summarized is empty")
138138
}
139139

140-
status, reason, message, err := summarizeOpt.mergeStrategy.Merge(conditionsInScope, summarizeOpt.conditionTypes)
140+
status, reason, message, err := summarizeOpt.mergeStrategy.Merge(SummaryMergeOperation, conditionsInScope, summarizeOpt.conditionTypes)
141141
if err != nil {
142142
return nil, err
143143
}

0 commit comments

Comments
 (0)