Skip to content

Commit

Permalink
[conditions] Reset conditions on Init()
Browse files Browse the repository at this point in the history
Recently the operators changed to use CreateServiceReadyCondition
instead of ExposeServiceReadyCondition. An update of an existing
environment would result in the ExposeServiceReadyCondition to be
never removed from the conditions list of a CR because the list
is not reset on Init().

This adds a Reset() method and a call right at the start of Init().

Signed-off-by: Martin Schuppert <[email protected]>
  • Loading branch information
stuggi committed Dec 13, 2024
1 parent 41a4a62 commit fb21c47
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions modules/common/condition/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
// Optional conditions list can be passed as parameter which allows to initialize
// additional conditions at the beginning.
func (conditions *Conditions) Init(cl *Conditions) {
conditions.Reset()
conditions.Set(UnknownCondition(ReadyCondition, RequestedReason, ReadyInitMessage))

// add all optional conditions if no not nil
Expand Down Expand Up @@ -102,6 +103,11 @@ func (conditions *Conditions) Remove(t Type) {
*conditions = newConditions
}

// Reset - removes all conditions
func (conditions *Conditions) Reset() {
*conditions = Conditions{}
}

// Get returns the condition with the given type, if the condition does not exists,
// it returns nil.
func (conditions *Conditions) Get(t Type) *Condition {
Expand Down
37 changes: 36 additions & 1 deletion modules/common/condition/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func TestInit(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

conditions := Conditions{}
someCondition := TrueCondition("foo", "to be removed on Init()")
conditions := Conditions{
*someCondition,
}

conditions.Init(&tt.conditions)
g.Expect(conditions).To(haveSameConditionsOf(tt.want))
Expand Down Expand Up @@ -228,6 +231,38 @@ func TestRemove(t *testing.T) {
}
}

func TestReset(t *testing.T) {
tests := []struct {
name string
conditions Conditions
expected Conditions
}{
{
name: "empty",
conditions: Conditions{},
expected: Conditions{},
},
{
name: "present",
conditions: CreateList(
unknownReady,
unknownA,
unknownB,
),
expected: Conditions{},
},
}

g := NewWithT(t)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.conditions.Reset()
g.Expect(tt.expected).To(haveSameConditionsOf(tt.conditions))
})
}
}

func TestHasSameState(t *testing.T) {
g := NewWithT(t)

Expand Down

0 comments on commit fb21c47

Please sign in to comment.