Skip to content

Commit 527510d

Browse files
authored
fix(operator): Remove duplicate conditions from status (#13497)
1 parent ac284ca commit 527510d

File tree

2 files changed

+101
-30
lines changed

2 files changed

+101
-30
lines changed

operator/internal/status/conditions.go

+20-21
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,35 @@ package status
22

33
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
44

5+
type conditionKey struct {
6+
Type string
7+
Reason string
8+
}
9+
510
func mergeConditions(old, active []metav1.Condition, now metav1.Time) []metav1.Condition {
11+
conditions := map[conditionKey]bool{}
612
merged := make([]metav1.Condition, 0, len(old)+len(active))
7-
for len(old) > 0 {
8-
c := old[0]
9-
found := -1
10-
for i, ac := range active {
11-
if c.Type == ac.Type && c.Reason == ac.Reason {
12-
found = i
13-
break
14-
}
15-
}
13+
for _, c := range active {
14+
c.Status = metav1.ConditionTrue
15+
c.LastTransitionTime = now
16+
17+
merged = append(merged, c)
18+
conditions[conditionKey{Type: c.Type, Reason: c.Reason}] = true
19+
}
1620

17-
if found != -1 {
18-
c = active[found]
19-
active = append(active[:found], active[found+1:]...)
21+
for _, c := range old {
22+
if conditions[conditionKey{c.Type, c.Reason}] {
23+
continue
24+
}
2025

21-
c.Status = metav1.ConditionTrue
22-
} else {
26+
if c.Status != metav1.ConditionFalse {
27+
c.LastTransitionTime = now
2328
c.Status = metav1.ConditionFalse
2429
}
2530

26-
c.LastTransitionTime = now
2731
merged = append(merged, c)
28-
old = old[1:]
32+
conditions[conditionKey{c.Type, c.Reason}] = true
2933
}
3034

31-
for _, c := range active {
32-
c.Status = metav1.ConditionTrue
33-
c.LastTransitionTime = now
34-
merged = append(merged, c)
35-
}
3635
return merged
3736
}

operator/internal/status/conditions_test.go

+81-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
func TestMergeConditions(t *testing.T) {
14-
now := metav1.NewTime(time.Unix(0, 0))
14+
oldTime := metav1.NewTime(time.Unix(0, 0))
15+
now := metav1.NewTime(time.Unix(10, 0))
1516
tt := []struct {
1617
desc string
1718
old []metav1.Condition
@@ -37,25 +38,53 @@ func TestMergeConditions(t *testing.T) {
3738
{
3839
desc: "reset old condition",
3940
old: []metav1.Condition{
40-
conditionPending,
41+
{
42+
Type: conditionPending.Type,
43+
Status: metav1.ConditionTrue,
44+
LastTransitionTime: oldTime,
45+
Reason: conditionPending.Reason,
46+
Message: conditionPending.Message,
47+
},
4148
},
4249
active: []metav1.Condition{
4350
conditionReady,
4451
},
4552
wantMerged: []metav1.Condition{
53+
{
54+
Type: conditionReady.Type,
55+
Status: metav1.ConditionTrue,
56+
LastTransitionTime: now,
57+
Reason: conditionReady.Reason,
58+
Message: conditionReady.Message,
59+
},
4660
{
4761
Type: conditionPending.Type,
4862
Status: metav1.ConditionFalse,
4963
LastTransitionTime: now,
5064
Reason: conditionPending.Reason,
5165
Message: conditionPending.Message,
5266
},
67+
},
68+
},
69+
{
70+
desc: "keep transition time of old condition",
71+
old: []metav1.Condition{
5372
{
54-
Type: conditionReady.Type,
55-
Status: metav1.ConditionTrue,
56-
LastTransitionTime: now,
57-
Reason: conditionReady.Reason,
58-
Message: conditionReady.Message,
73+
Type: conditionPending.Type,
74+
Status: metav1.ConditionFalse,
75+
LastTransitionTime: oldTime,
76+
Reason: conditionPending.Reason,
77+
Message: conditionPending.Message,
78+
},
79+
},
80+
active: []metav1.Condition{},
81+
wantMerged: []metav1.Condition{
82+
{
83+
Type: conditionPending.Type,
84+
Status: metav1.ConditionFalse,
85+
LastTransitionTime: oldTime,
86+
Reason: conditionPending.Reason,
87+
Message: conditionPending.Message,
5988
},
6089
},
6190
},
@@ -72,7 +101,7 @@ func TestMergeConditions(t *testing.T) {
72101
{
73102
Type: conditionPending.Type,
74103
Status: metav1.ConditionFalse,
75-
LastTransitionTime: now,
104+
LastTransitionTime: oldTime,
76105
Reason: conditionPending.Reason,
77106
Message: conditionPending.Message,
78107
},
@@ -93,13 +122,56 @@ func TestMergeConditions(t *testing.T) {
93122
Reason: conditionReady.Reason,
94123
Message: conditionReady.Message,
95124
},
125+
{
126+
Type: string(lokiv1.ConditionWarning),
127+
Status: metav1.ConditionTrue,
128+
LastTransitionTime: now,
129+
Reason: "test-warning",
130+
Message: "test-warning-message",
131+
},
96132
{
97133
Type: conditionPending.Type,
98134
Status: metav1.ConditionFalse,
99-
LastTransitionTime: now,
135+
LastTransitionTime: oldTime,
100136
Reason: conditionPending.Reason,
101137
Message: conditionPending.Message,
102138
},
139+
},
140+
},
141+
{
142+
desc: "remove duplicates",
143+
old: []metav1.Condition{
144+
{
145+
Type: conditionReady.Type,
146+
Status: metav1.ConditionTrue,
147+
LastTransitionTime: now,
148+
Reason: conditionReady.Reason,
149+
Message: conditionReady.Message,
150+
},
151+
{
152+
Type: conditionReady.Type,
153+
Status: metav1.ConditionTrue,
154+
LastTransitionTime: now,
155+
Reason: conditionReady.Reason,
156+
Message: conditionReady.Message,
157+
},
158+
},
159+
active: []metav1.Condition{
160+
conditionReady,
161+
{
162+
Type: string(lokiv1.ConditionWarning),
163+
Reason: "test-warning",
164+
Message: "test-warning-message",
165+
},
166+
},
167+
wantMerged: []metav1.Condition{
168+
{
169+
Type: conditionReady.Type,
170+
Status: metav1.ConditionTrue,
171+
LastTransitionTime: now,
172+
Reason: conditionReady.Reason,
173+
Message: conditionReady.Message,
174+
},
103175
{
104176
Type: string(lokiv1.ConditionWarning),
105177
Status: metav1.ConditionTrue,

0 commit comments

Comments
 (0)