Skip to content

Commit 6494b2d

Browse files
Refactor condition helpers for Mirror, Summary and Aggregate
1 parent 88d43aa commit 6494b2d

File tree

5 files changed

+83
-17
lines changed

5 files changed

+83
-17
lines changed

util/conditions/getter.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ func GetLastTransitionTime(from Getter, t clusterv1.ConditionType) *metav1.Time
114114
return nil
115115
}
116116

117-
// Summary returns a Ready condition with the summary of all the conditions existing
117+
// summary returns a Ready condition with the summary of all the conditions existing
118118
// on an object. If the object does not have other conditions, no summary condition is generated.
119-
func Summary(from Getter, options ...MergeOption) *clusterv1.Condition {
119+
func summary(from Getter, options ...MergeOption) *clusterv1.Condition {
120120
conditions := from.GetConditions()
121121

122122
conditionsInScope := make([]localizedCondition, 0, len(conditions))
@@ -137,11 +137,47 @@ func Summary(from Getter, options ...MergeOption) *clusterv1.Condition {
137137
return merge(conditionsInScope, clusterv1.ReadyCondition, mergeOpt)
138138
}
139139

140-
// Mirror mirrors the Ready condition from a dependent object into the target condition;
140+
// mirrorOptions allows to set options for the mirror operation.
141+
type mirrorOptions struct {
142+
fallbackTo *bool
143+
fallbackReason string
144+
fallbackSeverity clusterv1.ConditionSeverity
145+
fallbackMessage string
146+
}
147+
148+
// MirrorOptions defines an option for mirroring conditions.
149+
type MirrorOptions func(*mirrorOptions)
150+
151+
// WithFallbackValue specify a fallback value to use in case the mirrored condition does not exists;
152+
// in case the fallbackValue is false, given values for reason, severity and message will be used.
153+
func WithFallbackValue(fallbackValue bool, reason string, severity clusterv1.ConditionSeverity, message string) MirrorOptions {
154+
return func(c *mirrorOptions) {
155+
c.fallbackTo = &fallbackValue
156+
c.fallbackReason = reason
157+
c.fallbackSeverity = severity
158+
c.fallbackMessage = message
159+
}
160+
}
161+
162+
// mirror mirrors the Ready condition from a dependent object into the target condition;
141163
// if the Ready condition does not exists in the source object, no target conditions is generated.
142-
func Mirror(from Getter, targetCondition clusterv1.ConditionType) *clusterv1.Condition {
164+
func mirror(from Getter, targetCondition clusterv1.ConditionType, options ...MirrorOptions) *clusterv1.Condition {
165+
mirrorOpt := &mirrorOptions{}
166+
for _, o := range options {
167+
o(mirrorOpt)
168+
}
169+
143170
condition := Get(from, clusterv1.ReadyCondition)
144171

172+
if mirrorOpt.fallbackTo != nil && condition == nil {
173+
switch *mirrorOpt.fallbackTo {
174+
case true:
175+
condition = TrueCondition(targetCondition)
176+
case false:
177+
condition = FalseCondition(targetCondition, mirrorOpt.fallbackReason, mirrorOpt.fallbackSeverity, mirrorOpt.fallbackMessage)
178+
}
179+
}
180+
145181
if condition != nil {
146182
condition.Type = targetCondition
147183
}
@@ -152,7 +188,7 @@ func Mirror(from Getter, targetCondition clusterv1.ConditionType) *clusterv1.Con
152188
// Aggregates all the the Ready condition from a list of dependent objects into the target object;
153189
// if the Ready condition does not exists in one of the source object, the object is excluded from
154190
// the aggregation; if none of the source object have ready condition, no target conditions is generated.
155-
func Aggregate(from []Getter, targetCondition clusterv1.ConditionType, options ...MergeOption) *clusterv1.Condition {
191+
func aggregate(from []Getter, targetCondition clusterv1.ConditionType, options ...MergeOption) *clusterv1.Condition {
156192
conditionsInScope := make([]localizedCondition, 0, len(from))
157193
for i := range from {
158194
condition := Get(from[i], clusterv1.ReadyCondition)

util/conditions/getter_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestMirror(t *testing.T) {
120120
t.Run(tt.name, func(t *testing.T) {
121121
g := NewWithT(t)
122122

123-
got := Mirror(tt.from, tt.t)
123+
got := mirror(tt.from, tt.t)
124124
if tt.want == nil {
125125
g.Expect(got).To(BeNil())
126126
return
@@ -161,7 +161,7 @@ func TestSummary(t *testing.T) {
161161
t.Run(tt.name, func(t *testing.T) {
162162
g := NewWithT(t)
163163

164-
got := Summary(tt.from)
164+
got := summary(tt.from)
165165
if tt.want == nil {
166166
g.Expect(got).To(BeNil())
167167
return
@@ -205,7 +205,7 @@ func TestAggregate(t *testing.T) {
205205
t.Run(tt.name, func(t *testing.T) {
206206
g := NewWithT(t)
207207

208-
got := Aggregate(tt.from, tt.t)
208+
got := aggregate(tt.from, tt.t)
209209
if tt.want == nil {
210210
g.Expect(got).To(BeNil())
211211
return

util/conditions/merge_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ func TestMergeRespectPriority(t *testing.T) {
8989
want *clusterv1.Condition
9090
}{
9191
{
92-
name: "Aggregate nil list return nil",
92+
name: "aggregate nil list return nil",
9393
conditions: nil,
9494
want: nil,
9595
},
9696
{
97-
name: "Aggregate empty list return nil",
97+
name: "aggregate empty list return nil",
9898
conditions: []*clusterv1.Condition{},
9999
want: nil,
100100
},

util/conditions/setter.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ func MarkFalse(to Setter, t clusterv1.ConditionType, reason string, severity clu
116116
// SetSummary sets a Ready condition with the summary of all the conditions existing
117117
// on an object. If the object does not have other conditions, no summary condition is generated.
118118
func SetSummary(to Setter, options ...MergeOption) {
119-
Set(to, Summary(to, options...))
119+
Set(to, summary(to, options...))
120120
}
121121

122-
// SetMirrorCondition creates a new condition by mirroring the the Ready condition from a dependent object;
122+
// SetMirror creates a new condition by mirroring the the Ready condition from a dependent object;
123123
// if the Ready condition does not exists in the source object, no target conditions is generated.
124-
func SetMirrorCondition(to Setter, targetCondition clusterv1.ConditionType, from Getter) {
125-
Set(to, Mirror(from, targetCondition))
124+
func SetMirror(to Setter, targetCondition clusterv1.ConditionType, from Getter, options ...MirrorOptions) {
125+
Set(to, mirror(from, targetCondition, options...))
126126
}
127127

128-
// SetAggregateCondition creates a new condition with the aggregation of all the the Ready condition
128+
// SetAggregate creates a new condition with the aggregation of all the the Ready condition
129129
// from a list of dependent objects; if the Ready condition does not exists in one of the source object,
130130
// the object is excluded from the aggregation; if none of the source object have ready condition,
131131
// no target conditions is generated.
132-
func SetAggregateCondition(to Setter, targetCondition clusterv1.ConditionType, from []Getter, options ...MergeOption) {
133-
Set(to, Aggregate(from, targetCondition, options...))
132+
func SetAggregate(to Setter, targetCondition clusterv1.ConditionType, from []Getter, options ...MergeOption) {
133+
Set(to, aggregate(from, targetCondition, options...))
134134
}
135135

136136
// Delete deletes the condition with the given type.

util/conditions/setter_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,36 @@ func TestMarkMethods(t *testing.T) {
216216
}))
217217
}
218218

219+
func TestSetSummary(t *testing.T) {
220+
g := NewWithT(t)
221+
target := setterWithConditions(TrueCondition("foo"))
222+
223+
SetSummary(target)
224+
225+
g.Expect(Has(target, clusterv1.ReadyCondition)).To(BeTrue())
226+
}
227+
228+
func TestSetMirror(t *testing.T) {
229+
g := NewWithT(t)
230+
source := getterWithConditions(TrueCondition(clusterv1.ReadyCondition))
231+
target := setterWithConditions()
232+
233+
SetMirror(target, "foo", source)
234+
235+
g.Expect(Has(target, "foo")).To(BeTrue())
236+
}
237+
238+
func TestSetAggregate(t *testing.T) {
239+
g := NewWithT(t)
240+
source1 := getterWithConditions(TrueCondition(clusterv1.ReadyCondition))
241+
source2 := getterWithConditions(TrueCondition(clusterv1.ReadyCondition))
242+
target := setterWithConditions()
243+
244+
SetAggregate(target, "foo", []Getter{source1, source2})
245+
246+
g.Expect(Has(target, "foo")).To(BeTrue())
247+
}
248+
219249
func setterWithConditions(conditions ...*clusterv1.Condition) Setter {
220250
obj := &clusterv1.Cluster{}
221251
obj.SetConditions(conditionList(conditions...))

0 commit comments

Comments
 (0)