Skip to content

Commit 1b7c764

Browse files
committed
predicates: notice source changing to Ready=True
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>
1 parent c520a3e commit 1b7c764

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

internal/predicates/source_predicate.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ limitations under the License.
1717
package predicates
1818

1919
import (
20+
"github.com/fluxcd/pkg/runtime/conditions"
2021
"sigs.k8s.io/controller-runtime/pkg/event"
2122
"sigs.k8s.io/controller-runtime/pkg/predicate"
2223

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

26-
// SourceRevisionChangePredicate detects revision changes to the v1beta2.Artifact
27-
// of a v1beta2.Source object.
27+
// SourceRevisionChangePredicate detects revision changes to the v1.Artifact of
28+
// a v1.Source object.
2829
type SourceRevisionChangePredicate struct {
2930
predicate.Funcs
3031
}
@@ -53,6 +54,20 @@ func (SourceRevisionChangePredicate) Update(e event.UpdateEvent) bool {
5354
return true
5455
}
5556

57+
oldConditions, ok := e.ObjectOld.(conditions.Getter)
58+
if !ok {
59+
return false
60+
}
61+
62+
newConditions, ok := e.ObjectNew.(conditions.Getter)
63+
if !ok {
64+
return false
65+
}
66+
67+
if !conditions.IsReady(oldConditions) && conditions.IsReady(newConditions) {
68+
return true
69+
}
70+
5671
return false
5772
}
5873

internal/predicates/source_predicate_test.go

+57-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ import (
2121
"time"
2222

2323
"github.com/onsi/gomega"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2526
"sigs.k8s.io/controller-runtime/pkg/client"
2627
"sigs.k8s.io/controller-runtime/pkg/event"
2728

29+
"github.com/fluxcd/pkg/apis/meta"
30+
2831
sourcev1 "github.com/fluxcd/source-controller/api/v1"
2932
)
3033

@@ -46,6 +49,54 @@ func TestSourceRevisionChangePredicate_Update(t *testing.T) {
4649
{name: "old with artifact", old: sourceA, new: emptySource, want: false},
4750
{name: "old not a source", old: notASource, new: sourceA, want: false},
4851
{name: "new not a source", old: sourceA, new: notASource, want: false},
52+
{
53+
name: "old not ready and new ready",
54+
old: &sourceMock{
55+
revision: "revision-a",
56+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
57+
},
58+
new: &sourceMock{
59+
revision: "revision-a",
60+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
61+
},
62+
want: true,
63+
},
64+
{
65+
name: "old ready and new not ready",
66+
old: &sourceMock{
67+
revision: "revision-a",
68+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
69+
},
70+
new: &sourceMock{
71+
revision: "revision-a",
72+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
73+
},
74+
want: false,
75+
},
76+
{
77+
name: "old not ready and new not ready",
78+
old: &sourceMock{
79+
revision: "revision-a",
80+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
81+
},
82+
new: &sourceMock{
83+
revision: "revision-a",
84+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionFalse}},
85+
},
86+
want: false,
87+
},
88+
{
89+
name: "old ready and new ready",
90+
old: &sourceMock{
91+
revision: "revision-a",
92+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
93+
},
94+
new: &sourceMock{
95+
revision: "revision-a",
96+
conditions: []metav1.Condition{{Type: meta.ReadyCondition, Status: metav1.ConditionTrue}},
97+
},
98+
want: false,
99+
},
49100
{name: "old nil", old: nil, new: sourceA, want: false},
50101
{name: "new nil", old: sourceA, new: nil, want: false},
51102
}
@@ -65,7 +116,8 @@ func TestSourceRevisionChangePredicate_Update(t *testing.T) {
65116

66117
type sourceMock struct {
67118
unstructured.Unstructured
68-
revision string
119+
revision string
120+
conditions []metav1.Condition
69121
}
70122

71123
func (m sourceMock) GetRequeueAfter() time.Duration {
@@ -80,3 +132,7 @@ func (m *sourceMock) GetArtifact() *sourcev1.Artifact {
80132
}
81133
return nil
82134
}
135+
136+
func (m *sourceMock) GetConditions() []metav1.Condition {
137+
return m.conditions
138+
}

0 commit comments

Comments
 (0)