Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(operator): Remove duplicate conditions from status #13497

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions operator/internal/status/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@ package status

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

type conditionKey struct {
Type string
Reason string
}

func mergeConditions(old, active []metav1.Condition, now metav1.Time) []metav1.Condition {
conditions := map[conditionKey]bool{}
merged := make([]metav1.Condition, 0, len(old)+len(active))
for len(old) > 0 {
c := old[0]
found := -1
for i, ac := range active {
if c.Type == ac.Type && c.Reason == ac.Reason {
found = i
break
}
}
for _, c := range active {
c.Status = metav1.ConditionTrue
c.LastTransitionTime = now

merged = append(merged, c)
conditions[conditionKey{Type: c.Type, Reason: c.Reason}] = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just to match the usage below

Suggested change
conditions[conditionKey{Type: c.Type, Reason: c.Reason}] = true
conditions[conditionKey{c.Type, c.Reason}] = true

}

if found != -1 {
c = active[found]
active = append(active[:found], active[found+1:]...)
for _, c := range old {
if conditions[conditionKey{c.Type, c.Reason}] {
continue
}

c.Status = metav1.ConditionTrue
} else {
if c.Status != metav1.ConditionFalse {
c.LastTransitionTime = now
c.Status = metav1.ConditionFalse
}

c.LastTransitionTime = now
merged = append(merged, c)
old = old[1:]
conditions[conditionKey{c.Type, c.Reason}] = true
}

for _, c := range active {
c.Status = metav1.ConditionTrue
c.LastTransitionTime = now
merged = append(merged, c)
}
return merged
}
90 changes: 81 additions & 9 deletions operator/internal/status/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

func TestMergeConditions(t *testing.T) {
now := metav1.NewTime(time.Unix(0, 0))
oldTime := metav1.NewTime(time.Unix(0, 0))
now := metav1.NewTime(time.Unix(10, 0))
tt := []struct {
desc string
old []metav1.Condition
Expand All @@ -37,25 +38,53 @@ func TestMergeConditions(t *testing.T) {
{
desc: "reset old condition",
old: []metav1.Condition{
conditionPending,
{
Type: conditionPending.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: oldTime,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
},
active: []metav1.Condition{
conditionReady,
},
wantMerged: []metav1.Condition{
{
Type: conditionReady.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: conditionReady.Reason,
Message: conditionReady.Message,
},
{
Type: conditionPending.Type,
Status: metav1.ConditionFalse,
LastTransitionTime: now,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
},
},
{
desc: "keep transition time of old condition",
old: []metav1.Condition{
{
Type: conditionReady.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: conditionReady.Reason,
Message: conditionReady.Message,
Type: conditionPending.Type,
Status: metav1.ConditionFalse,
LastTransitionTime: oldTime,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
},
active: []metav1.Condition{},
wantMerged: []metav1.Condition{
{
Type: conditionPending.Type,
Status: metav1.ConditionFalse,
LastTransitionTime: oldTime,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
},
},
Expand All @@ -72,7 +101,7 @@ func TestMergeConditions(t *testing.T) {
{
Type: conditionPending.Type,
Status: metav1.ConditionFalse,
LastTransitionTime: now,
LastTransitionTime: oldTime,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
Expand All @@ -93,13 +122,56 @@ func TestMergeConditions(t *testing.T) {
Reason: conditionReady.Reason,
Message: conditionReady.Message,
},
{
Type: string(lokiv1.ConditionWarning),
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: "test-warning",
Message: "test-warning-message",
},
{
Type: conditionPending.Type,
Status: metav1.ConditionFalse,
LastTransitionTime: now,
LastTransitionTime: oldTime,
Reason: conditionPending.Reason,
Message: conditionPending.Message,
},
},
},
{
desc: "remove duplicates",
old: []metav1.Condition{
{
Type: conditionReady.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: conditionReady.Reason,
Message: conditionReady.Message,
},
{
Type: conditionReady.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: conditionReady.Reason,
Message: conditionReady.Message,
},
},
active: []metav1.Condition{
conditionReady,
{
Type: string(lokiv1.ConditionWarning),
Reason: "test-warning",
Message: "test-warning-message",
},
},
wantMerged: []metav1.Condition{
{
Type: conditionReady.Type,
Status: metav1.ConditionTrue,
LastTransitionTime: now,
Reason: conditionReady.Reason,
Message: conditionReady.Message,
},
{
Type: string(lokiv1.ConditionWarning),
Status: metav1.ConditionTrue,
Expand Down
Loading