Skip to content

Commit

Permalink
feat: work agent track availability (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanzhang-oss authored Mar 13, 2024
1 parent cbc5d7c commit 6b2b489
Show file tree
Hide file tree
Showing 17 changed files with 1,645 additions and 233 deletions.
8 changes: 4 additions & 4 deletions pkg/controllers/work/applied_work_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ func TestCalculateNewAppliedWork(t *testing.T) {
}
newRes, staleRes, err := r.generateDiff(context.Background(), &tt.inputWork, &tt.inputAppliedWork)
if len(tt.expectedNewRes) != len(newRes) {
t.Errorf("Testcase %s: get newRes contains different number of elements than the expected newRes.", testName)
t.Errorf("Testcase %s: get newRes contains different number of elements than the want newRes.", testName)
}
for i := 0; i < len(newRes); i++ {
diff := cmp.Diff(tt.expectedNewRes[i].WorkResourceIdentifier, newRes[i].WorkResourceIdentifier)
if len(diff) != 0 {
t.Errorf("Testcase %s: get newRes is different from the expected newRes, diff = %s", testName, diff)
t.Errorf("Testcase %s: get newRes is different from the want newRes, diff = %s", testName, diff)
}
}
if len(tt.expectedStaleRes) != len(staleRes) {
t.Errorf("Testcase %s: get staleRes contains different number of elements than the expected staleRes.", testName)
t.Errorf("Testcase %s: get staleRes contains different number of elements than the want staleRes.", testName)
}
for i := 0; i < len(staleRes); i++ {
diff := cmp.Diff(tt.expectedStaleRes[i].WorkResourceIdentifier, staleRes[i].WorkResourceIdentifier)
if len(diff) != 0 {
t.Errorf("Testcase %s: get staleRes is different from the expected staleRes, diff = %s", testName, diff)
t.Errorf("Testcase %s: get staleRes is different from the want staleRes, diff = %s", testName, diff)
}
}
if tt.hasErr {
Expand Down
515 changes: 377 additions & 138 deletions pkg/controllers/work/apply_controller.go

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions pkg/controllers/work/apply_controller_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package work

import (
"context"
"fmt"

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

fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
"go.goms.io/fleet/pkg/utils/condition"
)

// createWorkWithManifest creates a work given a manifest
Expand Down Expand Up @@ -74,10 +76,36 @@ func waitForWorkToApply(workName, workNS string) *fleetv1beta1.Work {
}
applyCond := meta.FindStatusCondition(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeApplied)
if applyCond == nil || applyCond.Status != metav1.ConditionTrue || applyCond.ObservedGeneration != resultWork.Generation {
By(fmt.Sprintf("applyCond not true: %v", applyCond))
return false
}
for _, manifestCondition := range resultWork.Status.ManifestConditions {
if !meta.IsStatusConditionTrue(manifestCondition.Conditions, fleetv1beta1.WorkConditionTypeApplied) {
By(fmt.Sprintf("manifest applyCond not true %v : %v", manifestCondition.Identifier, manifestCondition.Conditions))
return false
}
}
return true
}, timeout, interval).Should(BeTrue())
return &resultWork
}

// waitForWorkToAvailable waits for a work to have an available condition to be true
func waitForWorkToBeAvailable(workName, workNS string) *fleetv1beta1.Work {
var resultWork fleetv1beta1.Work
Eventually(func() bool {
err := k8sClient.Get(context.Background(), types.NamespacedName{Name: workName, Namespace: workNS}, &resultWork)
if err != nil {
return false
}
availCond := meta.FindStatusCondition(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeAvailable)
if !condition.IsConditionStatusTrue(availCond, resultWork.Generation) {
By(fmt.Sprintf("availCond not true: %v", availCond))
return false
}
for _, manifestCondition := range resultWork.Status.ManifestConditions {
if !meta.IsStatusConditionTrue(manifestCondition.Conditions, fleetv1beta1.WorkConditionTypeAvailable) {
By(fmt.Sprintf("manifest availCond not true %v : %v", manifestCondition.Identifier, manifestCondition.Conditions))
return false
}
}
Expand Down
38 changes: 31 additions & 7 deletions pkg/controllers/work/apply_controller_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

fleetv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
"go.goms.io/fleet/test/utils/controller"
)

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

resultWork := waitForWorkToApply(work.GetName(), work.GetNamespace())
resultWork := waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())
Expect(len(resultWork.Status.ManifestConditions)).Should(Equal(1))
Expect(meta.IsStatusConditionTrue(resultWork.Status.Conditions, fleetv1beta1.WorkConditionTypeApplied)).Should(BeTrue())
Expect(meta.IsStatusConditionTrue(resultWork.Status.ManifestConditions[0].Conditions, fleetv1beta1.WorkConditionTypeApplied)).Should(BeTrue())
expectedResourceID := fleetv1beta1.WorkResourceIdentifier{
Ordinal: 0,
Group: "",
Expand All @@ -109,13 +108,38 @@ var _ = Describe("Work Controller", func() {
Name: cm.Name,
}
Expect(cmp.Diff(resultWork.Status.ManifestConditions[0].Identifier, expectedResourceID)).Should(BeEmpty())
expected := []metav1.Condition{
{
Type: fleetv1beta1.WorkConditionTypeApplied,
Status: metav1.ConditionTrue,
Reason: ManifestAlreadyUpToDateReason,
},
{
Type: fleetv1beta1.WorkConditionTypeAvailable,
Status: metav1.ConditionTrue,
Reason: string(manifestNotTrackableAction),
},
}
Expect(controller.CompareConditions(expected, resultWork.Status.ManifestConditions[0].Conditions)).Should(BeEmpty())
expected = []metav1.Condition{
{
Type: fleetv1beta1.WorkConditionTypeApplied,
Status: metav1.ConditionTrue,
Reason: workAppliedCompletedReason,
},
{
Type: fleetv1beta1.WorkConditionTypeAvailable,
Status: metav1.ConditionTrue,
Reason: workNotTrackableReason,
},
}
Expect(controller.CompareConditions(expected, resultWork.Status.Conditions)).Should(BeEmpty())

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

})

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

By("wait for the work to be applied")
waitForWorkToApply(work.GetName(), work.GetNamespace())
By("wait for the work to be available")
waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())

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

By("wait for the work to be applied")
waitForWorkToApply(work.GetName(), work.GetNamespace())
waitForWorkToBeAvailable(work.GetName(), work.GetNamespace())

By("Check applied CloneSet")
var appliedCloneSet kruisev1alpha1.CloneSet
Expand Down
Loading

0 comments on commit 6b2b489

Please sign in to comment.