Skip to content

Commit

Permalink
feat: enable new condition only
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhiying Lin committed Apr 27, 2024
1 parent a0c2dd1 commit 0c0db6f
Show file tree
Hide file tree
Showing 26 changed files with 1,078 additions and 3,534 deletions.
8 changes: 0 additions & 8 deletions apis/placement/v1beta1/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,6 @@ const (
// - "Unknown" means it is unknown.
ResourceBindingOverridden ResourceBindingConditionType = "Overridden"

// ResourceBindingBound indicates the bound condition of the given resources.
// Its condition status can be one of the following:
// - "True" means the corresponding work CR is created in the target cluster's namespace.
// - "False" means the corresponding work CR is not created yet.
// - "Unknown" means it is unknown.
// TODO, will be replaced by "WorkSynchronized"
ResourceBindingBound ResourceBindingConditionType = "Bound"

// ResourceBindingWorkSynchronized indicates the work synchronized condition of the given resources.
// Its condition status can be one of the following:
// - "True" means all corresponding works are created or updated in the target cluster's namespace.
Expand Down
14 changes: 12 additions & 2 deletions cmd/hubagent/workload/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
fleetv1alpha1 "go.goms.io/fleet/apis/v1alpha1"
"go.goms.io/fleet/cmd/hubagent/options"
"go.goms.io/fleet/pkg/controllers/clusterresourcebindingwatcher"
"go.goms.io/fleet/pkg/controllers/clusterresourceplacement"
"go.goms.io/fleet/pkg/controllers/clusterresourceplacementwatcher"
"go.goms.io/fleet/pkg/controllers/clusterschedulingpolicysnapshot"
Expand Down Expand Up @@ -194,12 +195,21 @@ func SetupControllers(ctx context.Context, wg *sync.WaitGroup, mgr ctrl.Manager,
return err
}

klog.Info("Setting up clusterSchedulingPolicySnapshot controller")
klog.Info("Setting up clusterResourceBinding watcher")
if err := (&clusterresourcebindingwatcher.Reconciler{
PlacementController: clusterResourcePlacementControllerV1Beta1,
Client: mgr.GetClient(),
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "Unable to set up the clusterResourceBinding watcher")
return err
}

klog.Info("Setting up clusterSchedulingPolicySnapshot watcher")
if err := (&clusterschedulingpolicysnapshot.Reconciler{
Client: mgr.GetClient(),
PlacementController: clusterResourcePlacementControllerV1Beta1,
}).SetupWithManager(mgr); err != nil {
klog.ErrorS(err, "Unable to set up the clusterResourcePlacement watcher")
klog.ErrorS(err, "Unable to set up the clusterSchedulingPolicySnapshot watcher")
return err
}

Expand Down
245 changes: 26 additions & 219 deletions pkg/controllers/clusterresourceplacement/controller.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (

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

const (
member1Name = "member-1"
member2Name = "member-2"

eventuallyTimeout = time.Minute * 2
consistentlyDuration = time.Second * 10
interval = time.Millisecond * 250
eventuallyTimeout = time.Second * 10
interval = time.Millisecond * 250
)

var (
Expand Down Expand Up @@ -154,7 +154,7 @@ func retrieveAndValidateCRPDeletion(crp *placementv1beta1.ClusterResourcePlaceme
}, eventuallyTimeout, interval).Should(BeTrue(), "Get() clusterResourcePlacement, want not found")
}

func createClusterResourceBinding(cluster string, policySnapshot *placementv1beta1.ClusterSchedulingPolicySnapshot, resourceSnapshot *placementv1beta1.ClusterResourceSnapshot) {
func createOverriddenClusterResourceBinding(cluster string, policySnapshot *placementv1beta1.ClusterSchedulingPolicySnapshot, resourceSnapshot *placementv1beta1.ClusterResourceSnapshot) *placementv1beta1.ClusterResourceBinding {
binding := &placementv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding-" + cluster,
Expand All @@ -170,36 +170,34 @@ func createClusterResourceBinding(cluster string, policySnapshot *placementv1bet
},
}
Expect(k8sClient.Create(ctx, binding)).Should(Succeed(), "Failed to create clusterResourceBinding")
condition := metav1.Condition{
cond := metav1.Condition{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceBindingBound),
Reason: "resourceBindingTrue",
Type: string(placementv1beta1.ResourceBindingRolloutStarted),
Reason: condition.RolloutStartedReason,
ObservedGeneration: binding.Generation,
}
meta.SetStatusCondition(&binding.Status.Conditions, condition)
meta.SetStatusCondition(&binding.Status.Conditions, cond)
cond = metav1.Condition{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceBindingOverridden),
Reason: condition.OverriddenSucceededReason,
ObservedGeneration: binding.Generation,
}
meta.SetStatusCondition(&binding.Status.Conditions, cond)
Expect(k8sClient.Status().Update(ctx, binding)).Should(Succeed(), "Failed to update the binding status")
return binding
}

func createApplySucceededWork(resourceSnapshot *placementv1beta1.ClusterResourceSnapshot, namespace string) {
work := &placementv1beta1.Work{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: "work-1",
Labels: map[string]string{
placementv1beta1.CRPTrackingLabel: resourceSnapshot.Labels[placementv1beta1.CRPTrackingLabel],
placementv1beta1.ParentResourceSnapshotIndexLabel: resourceSnapshot.Labels[placementv1beta1.ResourceIndexLabel],
},
},
}
Expect(k8sClient.Create(ctx, work)).Should(Succeed(), "Failed to create work")
condition := metav1.Condition{
Type: placementv1beta1.WorkConditionTypeApplied,
func createSynchronizedClusterResourceBinding(cluster string, policySnapshot *placementv1beta1.ClusterSchedulingPolicySnapshot, resourceSnapshot *placementv1beta1.ClusterResourceSnapshot) {
binding := createOverriddenClusterResourceBinding(cluster, policySnapshot, resourceSnapshot)
cond := metav1.Condition{
Status: metav1.ConditionTrue,
ObservedGeneration: work.Generation,
Reason: "Success",
Type: string(placementv1beta1.ResourceBindingWorkSynchronized),
Reason: condition.WorkSynchronizedReason,
ObservedGeneration: binding.Generation,
}
meta.SetStatusCondition(&work.Status.Conditions, condition)
Expect(k8sClient.Status().Update(ctx, work)).Should(Succeed(), "Failed to update work status as applied")
meta.SetStatusCondition(&binding.Status.Conditions, cond)
Expect(k8sClient.Status().Update(ctx, binding)).Should(Succeed(), "Failed to update the binding status")
}

var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Expand Down Expand Up @@ -298,21 +296,11 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Status: placementv1beta1.ClusterResourcePlacementStatus{
ObservedResourceIndex: "0",
Conditions: []metav1.Condition{
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementAppliedConditionType),
Reason: ApplyPendingReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: SchedulingUnknownReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementSynchronizedConditionType),
Reason: SynchronizePendingReason,
},
},
},
}
Expand All @@ -337,7 +325,7 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
gotPolicySnapshot.Status.ObservedCRPGeneration = gotCRP.Generation
Expect(k8sClient.Status().Update(ctx, gotPolicySnapshot)).Should(Succeed(), "Failed to update the policy snapshot status")

By("By validating the CRP status")
By("By validating the CRP status has only scheduling condition")
wantCRP := &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Expand All @@ -347,21 +335,11 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Status: placementv1beta1.ClusterResourcePlacementStatus{
ObservedResourceIndex: "0",
Conditions: []metav1.Condition{
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementAppliedConditionType),
Reason: ApplySucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementSynchronizedConditionType),
Reason: SynchronizeSucceededReason,
},
},
},
}
Expand Down Expand Up @@ -393,10 +371,7 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Expect(k8sClient.Status().Update(ctx, gotPolicySnapshot)).Should(Succeed(), "Failed to update the policy snapshot status")

By("By creating clusterResourceBinding on member-1")
createClusterResourceBinding(member1Name, gotPolicySnapshot, gotResourceSnapshot)

By("By creating appliedSuccess works on member-1")
createApplySucceededWork(gotResourceSnapshot, member1Namespace)
createOverriddenClusterResourceBinding(member1Name, gotPolicySnapshot, gotResourceSnapshot)

By("By validating the CRP status")
wantCRP := &placementv1beta1.ClusterResourcePlacement{
Expand All @@ -408,20 +383,15 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Status: placementv1beta1.ClusterResourcePlacementStatus{
ObservedResourceIndex: "0",
Conditions: []metav1.Condition{
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementAppliedConditionType),
Reason: ApplyPendingReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
},
{
Status: metav1.ConditionFalse,
Type: string(placementv1beta1.ClusterResourcePlacementSynchronizedConditionType),
Reason: SynchronizePendingReason,
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementRolloutStartedConditionType),
Reason: condition.RolloutStartedUnknownReason,
},
},
PlacementStatuses: []placementv1beta1.ResourcePlacementStatus{
Expand All @@ -430,38 +400,38 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Conditions: []metav1.Condition{
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourcesAppliedConditionType),
Reason: ResourceApplySucceededReason,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: condition.ScheduleSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
Type: string(placementv1beta1.ResourceRolloutStartedConditionType),
Reason: condition.RolloutStartedReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceOverriddenConditionType),
Reason: condition.OverriddenSucceededReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ResourceWorkSynchronizedConditionType),
Reason: WorkSynchronizeSucceededReason,
Reason: condition.WorkSynchronizedUnknownReason,
},
},
},
{
ClusterName: member2Name,
Conditions: []metav1.Condition{
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ResourcesAppliedConditionType),
Reason: ResourceApplyPendingReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
Reason: condition.ScheduleSucceededReason,
},
{
Status: metav1.ConditionFalse,
Type: string(placementv1beta1.ResourceWorkSynchronizedConditionType),
Reason: WorkSynchronizePendingReason,
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ResourceRolloutStartedConditionType),
Reason: condition.RolloutStartedUnknownReason,
},
},
},
Expand All @@ -470,11 +440,9 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
}
retrieveAndValidateClusterResourcePlacement(testName, wantCRP)

By("By creating clusterResourceBinding on member-1")
createClusterResourceBinding(member2Name, gotPolicySnapshot, gotResourceSnapshot)
By("By creating a synchronized clusterResourceBinding on member-2")
createSynchronizedClusterResourceBinding(member2Name, gotPolicySnapshot, gotResourceSnapshot)

By("By creating appliedSuccess works on member-2")
createApplySucceededWork(gotResourceSnapshot, member2Namespace)
wantCRP = &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: testName,
Expand All @@ -486,38 +454,49 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Conditions: []metav1.Condition{
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementAppliedConditionType),
Reason: ApplySucceededReason,
Type: string(placementv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
Type: string(placementv1beta1.ClusterResourcePlacementRolloutStartedConditionType),
Reason: condition.RolloutStartedReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ClusterResourcePlacementSynchronizedConditionType),
Reason: SynchronizeSucceededReason,
Type: string(placementv1beta1.ClusterResourcePlacementOverriddenConditionType),
Reason: condition.OverrideNotSpecifiedReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ClusterResourcePlacementWorkSynchronizedConditionType),
Reason: condition.WorkSynchronizedUnknownReason,
},
},
PlacementStatuses: []placementv1beta1.ResourcePlacementStatus{
{

ClusterName: member1Name,
Conditions: []metav1.Condition{
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourcesAppliedConditionType),
Reason: ResourceApplySucceededReason,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: condition.ScheduleSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
Type: string(placementv1beta1.ResourceRolloutStartedConditionType),
Reason: condition.RolloutStartedReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceOverriddenConditionType),
Reason: condition.OverriddenSucceededReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ResourceWorkSynchronizedConditionType),
Reason: WorkSynchronizeSucceededReason,
Reason: condition.WorkSynchronizedUnknownReason,
},
},
},
Expand All @@ -526,18 +505,28 @@ var _ = Describe("Test ClusterResourcePlacement Controller", func() {
Conditions: []metav1.Condition{
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourcesAppliedConditionType),
Reason: ResourceApplySucceededReason,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: condition.ScheduleSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceScheduledConditionType),
Reason: ResourceScheduleSucceededReason,
Type: string(placementv1beta1.ResourceRolloutStartedConditionType),
Reason: condition.RolloutStartedReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceOverriddenConditionType),
Reason: condition.OverriddenSucceededReason,
},
{
Status: metav1.ConditionTrue,
Type: string(placementv1beta1.ResourceWorkSynchronizedConditionType),
Reason: WorkSynchronizeSucceededReason,
Reason: condition.WorkSynchronizedReason,
},
{
Status: metav1.ConditionUnknown,
Type: string(placementv1beta1.ResourcesAppliedConditionType),
Reason: condition.ApplyPendingReason,
},
},
},
Expand Down
Loading

0 comments on commit 0c0db6f

Please sign in to comment.