Skip to content

Commit 0a6e981

Browse files
Add SetUnknownWithReason for holding terminal conditions in unknown states (#64)
1 parent 05ffed2 commit 0a6e981

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

status/condition_set.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (c ConditionSet) List() []Condition {
7777
return c.object.GetConditions()
7878
}
7979

80-
// GetCondition finds and returns the Condition that matches the ConditionType
80+
// Get finds and returns the Condition that matches the ConditionType
8181
// previously set on Conditions.
8282
func (c ConditionSet) Get(t string) *Condition {
8383
if c.object == nil {
@@ -89,7 +89,7 @@ func (c ConditionSet) Get(t string) *Condition {
8989
return nil
9090
}
9191

92-
// True returns true if all condition types are true.
92+
// IsTrue returns true if all condition types are true.
9393
func (c ConditionSet) IsTrue(conditionTypes ...string) bool {
9494
for _, conditionType := range conditionTypes {
9595
if !c.Get(conditionType).IsTrue() {
@@ -126,7 +126,7 @@ func (c ConditionSet) Set(condition Condition) (modified bool) {
126126
return true
127127
}
128128

129-
// RemoveCondition removes the non normal condition that matches the ConditionType
129+
// Clear removes the abnormal condition that matches the ConditionType
130130
// Not implemented for normal conditions
131131
func (c ConditionSet) Clear(t string) error {
132132
var conditions []Condition
@@ -155,13 +155,13 @@ func (c ConditionSet) Clear(t string) error {
155155
return nil
156156
}
157157

158-
// SetTrue sets the status of t to true with the reason, and then marks the root condition to
158+
// SetTrue sets the status of conditionType to true with the reason, and then marks the root condition to
159159
// true if all other dependents are also true.
160160
func (c ConditionSet) SetTrue(conditionType string) (modified bool) {
161161
return c.SetTrueWithReason(conditionType, conditionType, "")
162162
}
163163

164-
// SetTrueWithReason sets the status of t to true with the reason, and then marks the root condition to
164+
// SetTrueWithReason sets the status of conditionType to true with the reason, and then marks the root condition to
165165
// true if all other dependents are also true.
166166
func (c ConditionSet) SetTrueWithReason(conditionType string, reason, message string) (modified bool) {
167167
return c.Set(Condition{
@@ -174,19 +174,24 @@ func (c ConditionSet) SetTrueWithReason(conditionType string, reason, message st
174174

175175
// SetUnknown sets the status of conditionType to Unknown and also sets the root condition
176176
// to Unknown if no other dependent condition is in an error state.
177-
func (r ConditionSet) SetUnknown(conditionType string) (modified bool) {
178-
// set the specified condition
179-
return r.Set(Condition{
177+
func (c ConditionSet) SetUnknown(conditionType string) (modified bool) {
178+
return c.SetUnknownWithReason(conditionType, "AwaitingReconciliation", "object is awaiting reconciliation")
179+
}
180+
181+
// SetUnknownWithReason sets the status of conditionType to Unknown with the reason, and also sets the root condition
182+
// to Unknown if no other dependent condition is in an error state.
183+
func (c ConditionSet) SetUnknownWithReason(conditionType string, reason, message string) (modified bool) {
184+
return c.Set(Condition{
180185
Type: conditionType,
181186
Status: metav1.ConditionUnknown,
182-
Reason: "AwaitingReconciliation",
183-
Message: "object is awaiting reconciliation",
187+
Reason: reason,
188+
Message: message,
184189
})
185190
}
186191

187-
// SetFalse sets the status of t and the root condition to False.
188-
func (r ConditionSet) SetFalse(conditionType string, reason, message string) (modified bool) {
189-
return r.Set(Condition{
192+
// SetFalse sets the status of conditionType and the root condition to False.
193+
func (c ConditionSet) SetFalse(conditionType string, reason, message string) (modified bool) {
194+
return c.Set(Condition{
190195
Type: conditionType,
191196
Status: metav1.ConditionFalse,
192197
Reason: reason,
@@ -195,15 +200,15 @@ func (r ConditionSet) SetFalse(conditionType string, reason, message string) (mo
195200
}
196201

197202
// recomputeRootCondition marks the root condition to true if all other dependents are also true.
198-
func (r ConditionSet) recomputeRootCondition(conditionType string) {
199-
if conditionType == r.root {
203+
func (c ConditionSet) recomputeRootCondition(conditionType string) {
204+
if conditionType == c.root {
200205
return
201206
}
202-
if conditions := r.findUnhealthyDependents(); len(conditions) == 0 {
203-
r.SetTrue(r.root)
207+
if conditions := c.findUnhealthyDependents(); len(conditions) == 0 {
208+
c.SetTrue(c.root)
204209
} else {
205-
r.Set(Condition{
206-
Type: r.root,
210+
c.Set(Condition{
211+
Type: c.root,
207212
// The root condition is no longer unknown as soon as any are false
208213
Status: lo.Ternary(
209214
lo.ContainsBy(conditions, func(condition Condition) bool { return condition.IsFalse() }),

status/condition_set_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,19 @@ var _ = Describe("Conditions", func() {
1717
Expect(conditions.Get(ConditionTypeFoo).GetStatus()).To(Equal(metav1.ConditionUnknown))
1818
Expect(conditions.Get(ConditionTypeBar).GetStatus()).To(Equal(metav1.ConditionUnknown))
1919
Expect(conditions.Root().GetStatus()).To(Equal(metav1.ConditionUnknown))
20+
// Update the condition to unknown with reason
21+
Expect(conditions.SetUnknownWithReason(ConditionTypeFoo, "reason", "message")).To(BeTrue())
22+
fooCondition := conditions.Get(ConditionTypeFoo)
23+
Expect(fooCondition.Type).To(Equal(ConditionTypeFoo))
24+
Expect(fooCondition.Status).To(Equal(metav1.ConditionUnknown))
25+
Expect(fooCondition.Reason).To(Equal("reason")) // default to type
26+
Expect(fooCondition.Message).To(Equal("message")) // default to type
27+
Expect(fooCondition.LastTransitionTime.UnixNano()).To(BeNumerically(">", 0))
28+
Expect(conditions.Root().GetStatus()).To(Equal(metav1.ConditionUnknown))
29+
time.Sleep(1 * time.Nanosecond)
2030
// Update the condition to true
2131
Expect(conditions.SetTrue(ConditionTypeFoo)).To(BeTrue())
22-
fooCondition := conditions.Get(ConditionTypeFoo)
32+
fooCondition = conditions.Get(ConditionTypeFoo)
2333
Expect(fooCondition.Type).To(Equal(ConditionTypeFoo))
2434
Expect(fooCondition.Status).To(Equal(metav1.ConditionTrue))
2535
Expect(fooCondition.Reason).To(Equal(ConditionTypeFoo)) // default to type

0 commit comments

Comments
 (0)