Skip to content

Commit

Permalink
predicates: notice source changing to Ready=True
Browse files Browse the repository at this point in the history
This ensure that when a chart object has a temporary `Ready=False`
state, the predicate will notice the change to `Ready=True` and
cause an enqueue request.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Nov 22, 2023
1 parent c520a3e commit 1b7c764
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
19 changes: 17 additions & 2 deletions internal/predicates/source_predicate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ limitations under the License.
package predicates

import (
"github.com/fluxcd/pkg/runtime/conditions"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"

sourcev1 "github.com/fluxcd/source-controller/api/v1"
)

// SourceRevisionChangePredicate detects revision changes to the v1beta2.Artifact
// of a v1beta2.Source object.
// SourceRevisionChangePredicate detects revision changes to the v1.Artifact of
// a v1.Source object.
type SourceRevisionChangePredicate struct {
predicate.Funcs
}
Expand Down Expand Up @@ -53,6 +54,20 @@ func (SourceRevisionChangePredicate) Update(e event.UpdateEvent) bool {
return true
}

oldConditions, ok := e.ObjectOld.(conditions.Getter)
if !ok {
return false
}

newConditions, ok := e.ObjectNew.(conditions.Getter)
if !ok {
return false
}

if !conditions.IsReady(oldConditions) && conditions.IsReady(newConditions) {
return true
}

return false
}

Expand Down
58 changes: 57 additions & 1 deletion internal/predicates/source_predicate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ import (
"time"

"github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"

"github.com/fluxcd/pkg/apis/meta"

sourcev1 "github.com/fluxcd/source-controller/api/v1"
)

Expand All @@ -46,6 +49,54 @@ func TestSourceRevisionChangePredicate_Update(t *testing.T) {
{name: "old with artifact", old: sourceA, new: emptySource, want: false},
{name: "old not a source", old: notASource, new: sourceA, want: false},
{name: "new not a source", old: sourceA, new: notASource, want: false},
{
name: "old not ready and new ready",
old: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
},
new: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
},
want: true,
},
{
name: "old ready and new not ready",
old: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
},
new: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
},
want: false,
},
{
name: "old not ready and new not ready",
old: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
},
new: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
},
want: false,
},
{
name: "old ready and new ready",
old: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
},
new: &sourceMock{
revision: "revision-a",
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
},
want: false,
},
{name: "old nil", old: nil, new: sourceA, want: false},
{name: "new nil", old: sourceA, new: nil, want: false},
}
Expand All @@ -65,7 +116,8 @@ func TestSourceRevisionChangePredicate_Update(t *testing.T) {

type sourceMock struct {
unstructured.Unstructured
revision string
revision string
conditions []metav1.Condition
}

func (m sourceMock) GetRequeueAfter() time.Duration {
Expand All @@ -80,3 +132,7 @@ func (m *sourceMock) GetArtifact() *sourcev1.Artifact {
}
return nil
}

func (m *sourceMock) GetConditions() []metav1.Condition {
return m.conditions
}

0 comments on commit 1b7c764

Please sign in to comment.