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

🌱 Update conditions.Set function to set LastTransitionTime only when status of condition changes #11176

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions util/conditions/setter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Setter interface {

// Set sets the given condition.
//
// NOTE: If a condition already exists, the LastTransitionTime is updated only if a change is detected
// in any of the following fields: Status, Reason, Severity and Message.
// 1. if the condition of the specified type already exists (all fields of the existing condition are updated to
// new condition, LastTransitionTime is set to current time if unset and new status differs from the old status)
// 2. if a condition of the specified type does not exist (LastTransitionTime is set to current time if unset, and newCondition is appended)
func Set(to Setter, condition *clusterv1.Condition) {
if to == nil || condition == nil {
return
Expand All @@ -52,11 +53,15 @@ func Set(to Setter, condition *clusterv1.Condition) {
if existingCondition.Type == condition.Type {
exists = true
if !HasSameState(&existingCondition, condition) {
condition.LastTransitionTime = metav1.NewTime(time.Now().UTC().Truncate(time.Second))
if existingCondition.Status != condition.Status {
if condition.LastTransitionTime.IsZero() {
condition.LastTransitionTime = metav1.NewTime(time.Now().UTC().Truncate(time.Second))
}
} else {
condition.LastTransitionTime = existingCondition.LastTransitionTime
}
conditions[i] = *condition
break
}
condition.LastTransitionTime = existingCondition.LastTransitionTime
break
}
}
Expand Down
31 changes: 31 additions & 0 deletions util/conditions/setter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,18 @@ func TestSet(t *testing.T) {

func TestSetLastTransitionTime(t *testing.T) {
x := metav1.Date(2012, time.January, 1, 12, 15, 30, 5e8, time.UTC)
y := metav1.Date(2012, time.January, 2, 12, 15, 30, 5e8, time.UTC)

foo := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message foo")
fooWithBarMessage := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message bar")
fooWithLastTransitionTime := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message foo")
fooWithLastTransitionTime.LastTransitionTime = x
fooWithLastTransitionTimeWithBarMessage := FalseCondition("foo", "reason foo", clusterv1.ConditionSeverityInfo, "message bar")
fooWithLastTransitionTimeWithBarMessage.LastTransitionTime = y

fooWithAnotherState := TrueCondition("foo")
fooWithAnotherStateWithLastTransitionTime := TrueCondition("foo")
fooWithAnotherStateWithLastTransitionTime.LastTransitionTime = y

tests := []struct {
name string
Expand Down Expand Up @@ -196,6 +203,30 @@ func TestSetLastTransitionTime(t *testing.T) {
g.Expect(lastTransitionTime).ToNot(Equal(x))
},
},
{
name: "Set a condition that already exists but with different state should preserve the last transition time if defined",
to: setterWithConditions(fooWithLastTransitionTime),
new: fooWithAnotherStateWithLastTransitionTime,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).To(Equal(y))
},
},
{
name: "Set a condition that already exists but with different Message should preserve the last transition time",
to: setterWithConditions(fooWithLastTransitionTime),
new: fooWithBarMessage,
Copy link
Member

Choose a reason for hiding this comment

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

What about adding also a test where the new condition has a last transition time time set (but it gets ignored)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a new test in separate commit, Please let me know if anything else needs to be added. Thank you.

LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).To(Equal(x))
},
},
{
name: "Set a condition that already exists, with different state but same Status should ignore the last transition even if defined",
to: setterWithConditions(fooWithLastTransitionTime),
new: fooWithLastTransitionTimeWithBarMessage,
LastTransitionTimeCheck: func(g *WithT, lastTransitionTime metav1.Time) {
g.Expect(lastTransitionTime).To(Equal(x))
},
},
}

for _, tt := range tests {
Expand Down