Skip to content

Commit 17fce08

Browse files
committed
Update managed reconciler to store observedGeneration on conditions and also update that controllers unit tests
Signed-off-by: Scott Nichols <[email protected]>
1 parent db9545b commit 17fce08

File tree

4 files changed

+262
-184
lines changed

4 files changed

+262
-184
lines changed

apis/common/v1/condition.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ type Condition struct {
9696
}
9797

9898
// Equal returns true if the condition is identical to the supplied condition,
99-
// ignoring the LastTransitionTime and ObservedGeneration.
99+
// ignoring the LastTransitionTime.
100100
func (c Condition) Equal(other Condition) bool {
101101
return c.Type == other.Type &&
102102
c.Status == other.Status &&
103103
c.Reason == other.Reason &&
104-
c.Message == other.Message
104+
c.Message == other.Message &&
105+
c.ObservedGeneration == other.ObservedGeneration
105106
}
106107

107108
// WithMessage returns a condition by adding the provided message to existing
@@ -167,28 +168,24 @@ func (s *ConditionedStatus) GetCondition(ct ConditionType) Condition {
167168
// SetConditions sets the supplied conditions, replacing any existing conditions
168169
// of the same type. This is a no-op if all supplied conditions are identical,
169170
// ignoring the last transition time, to those already set.
170-
// Observed generation is updated if higher than the existing one.
171171
func (s *ConditionedStatus) SetConditions(c ...Condition) {
172-
for _, new := range c {
172+
for _, cond := range c {
173173
exists := false
174174
for i, existing := range s.Conditions {
175-
if existing.Type != new.Type {
175+
if existing.Type != cond.Type {
176176
continue
177177
}
178178

179-
if existing.Equal(new) {
179+
if existing.Equal(cond) {
180180
exists = true
181-
if existing.ObservedGeneration < new.ObservedGeneration {
182-
existing.ObservedGeneration = new.ObservedGeneration
183-
}
184181
continue
185182
}
186183

187-
s.Conditions[i] = new
184+
s.Conditions[i] = cond
188185
exists = true
189186
}
190187
if !exists {
191-
s.Conditions = append(s.Conditions, new)
188+
s.Conditions = append(s.Conditions, cond)
192189
}
193190
}
194191
}

apis/common/v1/condition_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ func TestConditionEqual(t *testing.T) {
3232
b Condition
3333
want bool
3434
}{
35+
"Identical": {
36+
a: Condition{
37+
Type: TypeReady,
38+
Status: corev1.ConditionTrue,
39+
LastTransitionTime: metav1.Now(),
40+
Reason: ReasonCreating,
41+
Message: "UnitTest",
42+
ObservedGeneration: 1,
43+
},
44+
b: Condition{
45+
Type: TypeReady,
46+
Status: corev1.ConditionTrue,
47+
LastTransitionTime: metav1.Now(),
48+
Reason: ReasonCreating,
49+
Message: "UnitTest",
50+
ObservedGeneration: 1,
51+
},
52+
want: true,
53+
},
3554
"IdenticalIgnoringTimestamp": {
3655
a: Condition{Type: TypeReady, LastTransitionTime: metav1.Now()},
3756
b: Condition{Type: TypeReady, LastTransitionTime: metav1.Now()},
@@ -57,6 +76,11 @@ func TestConditionEqual(t *testing.T) {
5776
b: Condition{Message: "uncool"},
5877
want: false,
5978
},
79+
"DifferentObservedGeneration": {
80+
a: Condition{ObservedGeneration: 1},
81+
b: Condition{},
82+
want: false,
83+
},
6084
"CheckReconcilePaused": {
6185
a: ReconcilePaused(),
6286
b: Condition{
@@ -139,6 +163,11 @@ func TestSetConditions(t *testing.T) {
139163
c: []Condition{Available()},
140164
want: NewConditionedStatus(Available()),
141165
},
166+
"ObservedGenerationIsUpdated": {
167+
cs: NewConditionedStatus(Available().WithObservedGeneration(1)),
168+
c: []Condition{Available().WithObservedGeneration(2)},
169+
want: NewConditionedStatus(Available().WithObservedGeneration(2)),
170+
},
142171
"TypeIsDifferent": {
143172
cs: NewConditionedStatus(Creating()),
144173
c: []Condition{Available()},

0 commit comments

Comments
 (0)