Skip to content

Commit fe84992

Browse files
authored
Merge pull request kubernetes#120542 from alvaroaleman/condition-change
Apimeta Set/RemoveStatusCondition: Indicate change
2 parents 5f8fc30 + 5d56f7c commit fe84992

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed

staging/src/k8s.io/apimachinery/pkg/api/meta/conditions.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ import (
2222
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2323
)
2424

25-
// SetStatusCondition sets the corresponding condition in conditions to newCondition.
25+
// SetStatusCondition sets the corresponding condition in conditions to newCondition and returns true
26+
// if the conditions are changed by this call.
2627
// conditions must be non-nil.
2728
// 1. if the condition of the specified type already exists (all fields of the existing condition are updated to
2829
// newCondition, LastTransitionTime is set to now if the new status differs from the old status)
2930
// 2. if a condition of the specified type does not exist (LastTransitionTime is set to now() if unset, and newCondition is appended)
30-
func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Condition) {
31+
func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Condition) (changed bool) {
3132
if conditions == nil {
32-
return
33+
return false
3334
}
3435
existingCondition := FindStatusCondition(*conditions, newCondition.Type)
3536
if existingCondition == nil {
3637
if newCondition.LastTransitionTime.IsZero() {
3738
newCondition.LastTransitionTime = metav1.NewTime(time.Now())
3839
}
3940
*conditions = append(*conditions, newCondition)
40-
return
41+
return true
4142
}
4243

4344
if existingCondition.Status != newCondition.Status {
@@ -47,18 +48,31 @@ func SetStatusCondition(conditions *[]metav1.Condition, newCondition metav1.Cond
4748
} else {
4849
existingCondition.LastTransitionTime = metav1.NewTime(time.Now())
4950
}
51+
changed = true
5052
}
5153

52-
existingCondition.Reason = newCondition.Reason
53-
existingCondition.Message = newCondition.Message
54-
existingCondition.ObservedGeneration = newCondition.ObservedGeneration
54+
if existingCondition.Reason != newCondition.Reason {
55+
existingCondition.Reason = newCondition.Reason
56+
changed = true
57+
}
58+
if existingCondition.Message != newCondition.Message {
59+
existingCondition.Message = newCondition.Message
60+
changed = true
61+
}
62+
if existingCondition.ObservedGeneration != newCondition.ObservedGeneration {
63+
existingCondition.ObservedGeneration = newCondition.ObservedGeneration
64+
changed = true
65+
}
66+
67+
return changed
5568
}
5669

57-
// RemoveStatusCondition removes the corresponding conditionType from conditions.
70+
// RemoveStatusCondition removes the corresponding conditionType from conditions if present. Returns
71+
// true if it was present and got removed.
5872
// conditions must be non-nil.
59-
func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string) {
73+
func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string) (removed bool) {
6074
if conditions == nil || len(*conditions) == 0 {
61-
return
75+
return false
6276
}
6377
newConditions := make([]metav1.Condition, 0, len(*conditions)-1)
6478
for _, condition := range *conditions {
@@ -67,7 +81,10 @@ func RemoveStatusCondition(conditions *[]metav1.Condition, conditionType string)
6781
}
6882
}
6983

84+
removed = len(*conditions) != len(newConditions)
7085
*conditions = newConditions
86+
87+
return removed
7188
}
7289

7390
// FindStatusCondition finds the conditionType in conditions.

staging/src/k8s.io/apimachinery/pkg/api/meta/conditions_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ func TestSetStatusCondition(t *testing.T) {
2929
oneHourAfter := time.Now().Add(1 * time.Hour)
3030

3131
tests := []struct {
32-
name string
33-
conditions []metav1.Condition
34-
toAdd metav1.Condition
35-
expected []metav1.Condition
32+
name string
33+
conditions []metav1.Condition
34+
toAdd metav1.Condition
35+
expectChanged bool
36+
expected []metav1.Condition
3637
}{
3738
{
3839
name: "should-add",
3940
conditions: []metav1.Condition{
4041
{Type: "first"},
4142
{Type: "third"},
4243
},
43-
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message"},
44+
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message"},
45+
expectChanged: true,
4446
expected: []metav1.Condition{
4547
{Type: "first"},
4648
{Type: "third"},
@@ -54,7 +56,8 @@ func TestSetStatusCondition(t *testing.T) {
5456
{Type: "second", Status: metav1.ConditionFalse},
5557
{Type: "third"},
5658
},
57-
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message"},
59+
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message"},
60+
expectChanged: true,
5861
expected: []metav1.Condition{
5962
{Type: "first"},
6063
{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message"},
@@ -68,18 +71,36 @@ func TestSetStatusCondition(t *testing.T) {
6871
{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}},
6972
{Type: "third"},
7073
},
71-
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourAfter}, Reason: "reason", Message: "message", ObservedGeneration: 3},
74+
toAdd: metav1.Condition{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourAfter}, Reason: "reason", Message: "message", ObservedGeneration: 3},
75+
expectChanged: true,
7276
expected: []metav1.Condition{
7377
{Type: "first"},
7478
{Type: "second", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}, Reason: "reason", Message: "message", ObservedGeneration: 3},
7579
{Type: "third"},
7680
},
7781
},
82+
{
83+
name: "nothing changes",
84+
conditions: []metav1.Condition{{
85+
Type: "type",
86+
Status: metav1.ConditionTrue,
87+
LastTransitionTime: metav1.Time{Time: oneHourBefore},
88+
}},
89+
toAdd: metav1.Condition{Type: "type", Status: metav1.ConditionTrue, LastTransitionTime: metav1.Time{Time: oneHourBefore}},
90+
expected: []metav1.Condition{{
91+
Type: "type",
92+
Status: metav1.ConditionTrue,
93+
LastTransitionTime: metav1.Time{Time: oneHourBefore},
94+
}},
95+
},
7896
}
7997

8098
for _, test := range tests {
8199
t.Run(test.name, func(t *testing.T) {
82-
SetStatusCondition(&test.conditions, test.toAdd)
100+
changed := SetStatusCondition(&test.conditions, test.toAdd)
101+
if test.expectChanged != changed {
102+
t.Errorf("expectChanged=%t != changed=%t", test.expectChanged, changed)
103+
}
83104
if !reflect.DeepEqual(test.conditions, test.expected) {
84105
t.Error(test.conditions)
85106
}
@@ -92,6 +113,7 @@ func TestRemoveStatusCondition(t *testing.T) {
92113
name string
93114
conditions []metav1.Condition
94115
conditionType string
116+
expectRemoval bool
95117
expected []metav1.Condition
96118
}{
97119
{
@@ -102,6 +124,7 @@ func TestRemoveStatusCondition(t *testing.T) {
102124
{Type: "third"},
103125
},
104126
conditionType: "second",
127+
expectRemoval: true,
105128
expected: []metav1.Condition{
106129
{Type: "first"},
107130
{Type: "third"},
@@ -131,7 +154,10 @@ func TestRemoveStatusCondition(t *testing.T) {
131154

132155
for _, test := range tests {
133156
t.Run(test.name, func(t *testing.T) {
134-
RemoveStatusCondition(&test.conditions, test.conditionType)
157+
removed := RemoveStatusCondition(&test.conditions, test.conditionType)
158+
if test.expectRemoval != removed {
159+
t.Errorf("expectRemoval=%t != removal=%t", test.expectRemoval, removed)
160+
}
135161
if !reflect.DeepEqual(test.conditions, test.expected) {
136162
t.Error(test.conditions)
137163
}

0 commit comments

Comments
 (0)