Skip to content

Commit 6b2b489

Browse files
feat: work agent track availability (#689)
1 parent cbc5d7c commit 6b2b489

17 files changed

+1645
-233
lines changed

pkg/controllers/work/applied_work_syncer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,21 @@ func TestCalculateNewAppliedWork(t *testing.T) {
185185
}
186186
newRes, staleRes, err := r.generateDiff(context.Background(), &tt.inputWork, &tt.inputAppliedWork)
187187
if len(tt.expectedNewRes) != len(newRes) {
188-
t.Errorf("Testcase %s: get newRes contains different number of elements than the expected newRes.", testName)
188+
t.Errorf("Testcase %s: get newRes contains different number of elements than the want newRes.", testName)
189189
}
190190
for i := 0; i < len(newRes); i++ {
191191
diff := cmp.Diff(tt.expectedNewRes[i].WorkResourceIdentifier, newRes[i].WorkResourceIdentifier)
192192
if len(diff) != 0 {
193-
t.Errorf("Testcase %s: get newRes is different from the expected newRes, diff = %s", testName, diff)
193+
t.Errorf("Testcase %s: get newRes is different from the want newRes, diff = %s", testName, diff)
194194
}
195195
}
196196
if len(tt.expectedStaleRes) != len(staleRes) {
197-
t.Errorf("Testcase %s: get staleRes contains different number of elements than the expected staleRes.", testName)
197+
t.Errorf("Testcase %s: get staleRes contains different number of elements than the want staleRes.", testName)
198198
}
199199
for i := 0; i < len(staleRes); i++ {
200200
diff := cmp.Diff(tt.expectedStaleRes[i].WorkResourceIdentifier, staleRes[i].WorkResourceIdentifier)
201201
if len(diff) != 0 {
202-
t.Errorf("Testcase %s: get staleRes is different from the expected staleRes, diff = %s", testName, diff)
202+
t.Errorf("Testcase %s: get staleRes is different from the want staleRes, diff = %s", testName, diff)
203203
}
204204
}
205205
if tt.hasErr {

pkg/controllers/work/apply_controller.go

Lines changed: 377 additions & 138 deletions
Large diffs are not rendered by default.

pkg/controllers/work/apply_controller_helper_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package work
77

88
import (
99
"context"
10+
"fmt"
1011

1112
"github.com/google/go-cmp/cmp"
1213
. "github.com/onsi/ginkgo/v2"
@@ -20,6 +21,7 @@ import (
2021
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2122

2223
fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
24+
"go.goms.io/fleet/pkg/utils/condition"
2325
)
2426

2527
// createWorkWithManifest creates a work given a manifest
@@ -74,10 +76,36 @@ func waitForWorkToApply(workName, workNS string) *fleetv1beta1.Work {
7476
}
7577
applyCond := meta.FindStatusCondition(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeApplied)
7678
if applyCond == nil || applyCond.Status != metav1.ConditionTrue || applyCond.ObservedGeneration != resultWork.Generation {
79+
By(fmt.Sprintf("applyCond not true: %v", applyCond))
7780
return false
7881
}
7982
for _, manifestCondition := range resultWork.Status.ManifestConditions {
8083
if !meta.IsStatusConditionTrue(manifestCondition.Conditions, fleetv1beta1.WorkConditionTypeApplied) {
84+
By(fmt.Sprintf("manifest applyCond not true %v : %v", manifestCondition.Identifier, manifestCondition.Conditions))
85+
return false
86+
}
87+
}
88+
return true
89+
}, timeout, interval).Should(BeTrue())
90+
return &resultWork
91+
}
92+
93+
// waitForWorkToAvailable waits for a work to have an available condition to be true
94+
func waitForWorkToBeAvailable(workName, workNS string) *fleetv1beta1.Work {
95+
var resultWork fleetv1beta1.Work
96+
Eventually(func() bool {
97+
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workName, Namespace: workNS}, &resultWork)
98+
if err != nil {
99+
return false
100+
}
101+
availCond := meta.FindStatusCondition(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeAvailable)
102+
if !condition.IsConditionStatusTrue(availCond, resultWork.Generation) {
103+
By(fmt.Sprintf("availCond not true: %v", availCond))
104+
return false
105+
}
106+
for _, manifestCondition := range resultWork.Status.ManifestConditions {
107+
if !meta.IsStatusConditionTrue(manifestCondition.Conditions, fleetv1beta1.WorkConditionTypeAvailable) {
108+
By(fmt.Sprintf("manifest availCond not true %v : %v", manifestCondition.Identifier, manifestCondition.Conditions))
81109
return false
82110
}
83111
}

pkg/controllers/work/apply_controller_integration_test.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
4343

4444
fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
45+
"go.goms.io/fleet/test/utils/controller"
4546
)
4647

4748
const timeout = time.Second * 10
@@ -95,10 +96,8 @@ var _ = Describe("Work Controller", func() {
9596
err := k8sClient.Create(context.Background(), work)
9697
Expect(err).ToNot(HaveOccurred())
9798

98-
resultWork := waitForWorkToApply(work.GetName(), work.GetNamespace())
99+
resultWork := waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())
99100
Expect(len(resultWork.Status.ManifestConditions)).Should(Equal(1))
100-
Expect(meta.IsStatusConditionTrue(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeApplied)).Should(BeTrue())
101-
Expect(meta.IsStatusConditionTrue(resultWork.Status.ManifestConditions[0].Conditions, fleetv1beta1.WorkConditionTypeApplied)).Should(BeTrue())
102101
expectedResourceID := fleetv1beta1.WorkResourceIdentifier{
103102
Ordinal: 0,
104103
Group: "",
@@ -109,13 +108,38 @@ var _ = Describe("Work Controller", func() {
109108
Name: cm.Name,
110109
}
111110
Expect(cmp.Diff(resultWork.Status.ManifestConditions[0].Identifier, expectedResourceID)).Should(BeEmpty())
111+
expected := []metav1.Condition{
112+
{
113+
Type: fleetv1beta1.WorkConditionTypeApplied,
114+
Status: metav1.ConditionTrue,
115+
Reason: ManifestAlreadyUpToDateReason,
116+
},
117+
{
118+
Type: fleetv1beta1.WorkConditionTypeAvailable,
119+
Status: metav1.ConditionTrue,
120+
Reason: string(manifestNotTrackableAction),
121+
},
122+
}
123+
Expect(controller.CompareConditions(expected, resultWork.Status.ManifestConditions[0].Conditions)).Should(BeEmpty())
124+
expected = []metav1.Condition{
125+
{
126+
Type: fleetv1beta1.WorkConditionTypeApplied,
127+
Status: metav1.ConditionTrue,
128+
Reason: workAppliedCompletedReason,
129+
},
130+
{
131+
Type: fleetv1beta1.WorkConditionTypeAvailable,
132+
Status: metav1.ConditionTrue,
133+
Reason: workNotTrackableReason,
134+
},
135+
}
136+
Expect(controller.CompareConditions(expected, resultWork.Status.Conditions)).Should(BeEmpty())
112137

113138
By("Check applied config map")
114139
var configMap corev1.ConfigMap
115140
Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: cmName, Namespace: cmNamespace}, &configMap)).Should(Succeed())
116141
Expect(cmp.Diff(configMap.Labels, cm.Labels)).Should(BeEmpty())
117142
Expect(cmp.Diff(configMap.Data, cm.Data)).Should(BeEmpty())
118-
119143
})
120144

121145
It("Should apply the same manifest in two work properly", func() {
@@ -208,8 +232,8 @@ var _ = Describe("Work Controller", func() {
208232
work = createWorkWithManifest(workNamespace, cm)
209233
Expect(k8sClient.Create(context.Background(), work)).ToNot(HaveOccurred())
210234

211-
By("wait for the work to be applied")
212-
waitForWorkToApply(work.GetName(), work.GetNamespace())
235+
By("wait for the work to be available")
236+
waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())
213237

214238
By("Check applied config map")
215239
verifyAppliedConfigMap(cm)
@@ -371,7 +395,7 @@ var _ = Describe("Work Controller", func() {
371395
Expect(err).ToNot(HaveOccurred())
372396

373397
By("wait for the work to be applied")
374-
waitForWorkToApply(work.GetName(), work.GetNamespace())
398+
waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())
375399

376400
By("Check applied CloneSet")
377401
var appliedCloneSet kruisev1alpha1.CloneSet

0 commit comments

Comments
 (0)