Skip to content

Commit

Permalink
Updated the logic
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelawyu committed Jun 26, 2023
1 parent a681896 commit 791e2d1
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 141 deletions.
5 changes: 5 additions & 0 deletions apis/placement/v1beta1/binding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type ResourceBindingSpec struct {
// it points to the name of the leading snapshot of the index group.
ResourceSnapshotName string `json:"resourceSnapshotName"`

// PolicySnapshtName is the name of the scheduling policy snapshot that this resource binding
// points to; more specifically, the scheduler creates this bindings in accordance with this
// scheduling policy snapshot.
PolicySnapshotName string `json:"policySnapshotName"`

// TargetCluster is the name of the cluster that the scheduler assigns the resources to.
TargetCluster string `json:"targetCluster"`

Expand Down
2 changes: 1 addition & 1 deletion apis/placement/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ spec:
- reason
- selected
type: object
policySnapshotName:
description: PolicySnapshtName is the name of the scheduling policy
snapshot that this resource binding points to; more specifically,
the scheduler creates this bindings in accordance with this scheduling
policy snapshot.
type: string
resourceSnapshotName:
description: ResourceSnapshotName is the name of the resource snapshot
that this resource binding points to. If the resources are divided
Expand All @@ -105,6 +111,7 @@ spec:
type: string
required:
- clusterDecision
- policySnapshotName
- resourceSnapshotName
- state
- targetCluster
Expand Down
210 changes: 119 additions & 91 deletions pkg/scheduler/framework/framework.go

Large diffs are not rendered by default.

72 changes: 59 additions & 13 deletions pkg/scheduler/framework/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
const (
CRPName = "test-placement"
policyName = "test-policy"
altPolicyName = "another-test-policy"
bindingName = "test-binding"
altBindingName = "another-test-binding"
clusterName = "bravelion"
Expand Down Expand Up @@ -142,6 +143,12 @@ func TestCollectBindings(t *testing.T) {
func TestClassifyBindings(t *testing.T) {
now := metav1.Now()

policy := &fleetv1beta1.ClusterPolicySnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
},
}

clusterName1 := "cluster-1"
clusterName2 := "cluster-2"
clusterName3 := "cluster-3"
Expand Down Expand Up @@ -202,35 +209,49 @@ func TestClassifyBindings(t *testing.T) {
Name: "binding-4",
},
Spec: fleetv1beta1.ResourceBindingSpec{
State: fleetv1beta1.BindingStateActive,
TargetCluster: clusterName3,
State: fleetv1beta1.BindingStateActive,
TargetCluster: clusterName3,
PolicySnapshotName: altPolicyName,
},
}
assocaitedWithDisappearedClusterBinding := fleetv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding-5",
},
Spec: fleetv1beta1.ResourceBindingSpec{
State: fleetv1beta1.BindingStateCreating,
TargetCluster: clusterName4,
State: fleetv1beta1.BindingStateCreating,
TargetCluster: clusterName4,
PolicySnapshotName: policyName,
},
}
activeBinding := fleetv1beta1.ClusterResourceBinding{
obsoleteBinding := fleetv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding-6",
},
Spec: fleetv1beta1.ResourceBindingSpec{
State: fleetv1beta1.BindingStateActive,
TargetCluster: clusterName1,
State: fleetv1beta1.BindingStateActive,
TargetCluster: clusterName1,
PolicySnapshotName: altPolicyName,
},
}
creatingBinding := fleetv1beta1.ClusterResourceBinding{
activeBinding := fleetv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding-7",
},
Spec: fleetv1beta1.ResourceBindingSpec{
State: fleetv1beta1.BindingStateCreating,
TargetCluster: clusterName2,
State: fleetv1beta1.BindingStateActive,
TargetCluster: clusterName1,
PolicySnapshotName: policyName,
},
}
creatingBinding := fleetv1beta1.ClusterResourceBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "binding-8",
},
Spec: fleetv1beta1.ResourceBindingSpec{
State: fleetv1beta1.BindingStateCreating,
TargetCluster: clusterName2,
PolicySnapshotName: policyName,
},
}

Expand All @@ -240,15 +261,17 @@ func TestClassifyBindings(t *testing.T) {
deletingBinding,
associatedWithLeavingClusterBinding,
assocaitedWithDisappearedClusterBinding,
obsoleteBinding,
activeBinding,
creatingBinding,
}
wantActive := []*fleetv1beta1.ClusterResourceBinding{&activeBinding}
wantCreating := []*fleetv1beta1.ClusterResourceBinding{&creatingBinding}
wantObsolete := []*fleetv1beta1.ClusterResourceBinding{&obsoleteBinding}
wantDeleted := []*fleetv1beta1.ClusterResourceBinding{&markedForDeletionWithFinalizerBinding}
wantObsolete := []*fleetv1beta1.ClusterResourceBinding{&associatedWithLeavingClusterBinding, &assocaitedWithDisappearedClusterBinding}
wantDangling := []*fleetv1beta1.ClusterResourceBinding{&associatedWithLeavingClusterBinding, &assocaitedWithDisappearedClusterBinding}

active, creating, obsolete, deleted := classifyBindings(bindings, clusters)
active, creating, obsolete, dangling, deleted := classifyBindings(policy, bindings, clusters)
if !cmp.Equal(active, wantActive) {
t.Errorf("classifyBindings() active = %v, want %v", active, wantActive)
}
Expand All @@ -261,6 +284,10 @@ func TestClassifyBindings(t *testing.T) {
t.Errorf("classifyBindings() obsolete = %v, want %v", obsolete, wantObsolete)
}

if !cmp.Equal(dangling, wantDangling) {
t.Errorf("classifyBindings() dangling = %v, want %v", dangling, wantDangling)
}

if !cmp.Equal(deleted, wantDeleted) {
t.Errorf("classifyBindings() deleted = %v, want %v", deleted, wantDeleted)
}
Expand Down Expand Up @@ -307,6 +334,7 @@ func TestShouldDownscale(t *testing.T) {
policy *fleetv1beta1.ClusterPolicySnapshot
desired int
present int
obsolete int
wantAct bool
wantCount int
}{
Expand Down Expand Up @@ -355,11 +383,29 @@ func TestShouldDownscale(t *testing.T) {
wantAct: true,
wantCount: 1,
},
{
name: "should downscale (obsolete bindings)",
policy: &fleetv1beta1.ClusterPolicySnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: policyName,
},
Spec: fleetv1beta1.SchedulingPolicySnapshotSpec{
Policy: &fleetv1beta1.PlacementPolicy{
PlacementType: fleetv1beta1.PickNPlacementType,
},
},
},
desired: 1,
present: 1,
obsolete: 1,
wantAct: true,
wantCount: 0,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
act, count := shouldDownscale(tc.policy, tc.desired, tc.present)
act, count := shouldDownscale(tc.policy, tc.desired, tc.present, tc.obsolete)
if act != tc.wantAct || count != tc.wantCount {
t.Fatalf("shouldDownscale() = %v, %v, want %v, %v", act, count, tc.wantAct, tc.wantCount)
}
Expand Down
Loading

0 comments on commit 791e2d1

Please sign in to comment.