Introduce RemoveCluster for ResourceBinding and ClusterResourceBinding
Signed-off-by: RainbowMango <qdurenhongcai@gmail.com>
This commit is contained in:
parent
dabdbea0fb
commit
dcbf8d2b96
|
@ -21,3 +21,22 @@ func (s *ResourceBindingSpec) AssignedReplicasForCluster(targetCluster string) i
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveCluster removes specific cluster from the target list.
|
||||||
|
// This function no-opts if cluster not exist.
|
||||||
|
func (s *ResourceBindingSpec) RemoveCluster(name string) {
|
||||||
|
var i int
|
||||||
|
|
||||||
|
for i = 0; i < len(s.Clusters); i++ {
|
||||||
|
if s.Clusters[i].Name == name {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// not found, do nothing
|
||||||
|
if i >= len(s.Clusters) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.Clusters = append(s.Clusters[:i], s.Clusters[i+1:]...)
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package v1alpha2
|
package v1alpha2
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestResourceBindingSpec_TargetContains(t *testing.T) {
|
func TestResourceBindingSpec_TargetContains(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
|
@ -76,3 +79,53 @@ func TestResourceBindingSpec_AssignedReplicasForCluster(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResourceBindingSpec_RemoveCluster(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
Name string
|
||||||
|
InputSpec ResourceBindingSpec
|
||||||
|
ClusterName string
|
||||||
|
ExpectSpec ResourceBindingSpec
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Name: "cluster not exist should do nothing",
|
||||||
|
InputSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}, {Name: "m3"}}},
|
||||||
|
ClusterName: "no-exist",
|
||||||
|
ExpectSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}, {Name: "m3"}}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "remove cluster from head",
|
||||||
|
InputSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}, {Name: "m3"}}},
|
||||||
|
ClusterName: "m1",
|
||||||
|
ExpectSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m2"}, {Name: "m3"}}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "remove cluster from middle",
|
||||||
|
InputSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}, {Name: "m3"}}},
|
||||||
|
ClusterName: "m2",
|
||||||
|
ExpectSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m3"}}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "remove cluster from tail",
|
||||||
|
InputSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}, {Name: "m3"}}},
|
||||||
|
ClusterName: "m3",
|
||||||
|
ExpectSpec: ResourceBindingSpec{Clusters: []TargetCluster{{Name: "m1"}, {Name: "m2"}}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "remove cluster from empty list",
|
||||||
|
InputSpec: ResourceBindingSpec{Clusters: []TargetCluster{}},
|
||||||
|
ClusterName: "na",
|
||||||
|
ExpectSpec: ResourceBindingSpec{Clusters: []TargetCluster{}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
tc := test
|
||||||
|
t.Run(tc.Name, func(t *testing.T) {
|
||||||
|
tc.InputSpec.RemoveCluster(tc.ClusterName)
|
||||||
|
if !reflect.DeepEqual(tc.InputSpec.Clusters, tc.ExpectSpec.Clusters) {
|
||||||
|
t.Fatalf("expect: %v, but got: %v", tc.ExpectSpec.Clusters, tc.InputSpec.Clusters)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -153,7 +153,7 @@ func (tc *NoExecuteTaintManager) syncBindingEviction(key util.QueueKey) error {
|
||||||
// Case 3: Tolerate forever, we do nothing.
|
// Case 3: Tolerate forever, we do nothing.
|
||||||
if needEviction || tolerationTime == 0 {
|
if needEviction || tolerationTime == 0 {
|
||||||
// update final result to evict the target cluster
|
// update final result to evict the target cluster
|
||||||
binding.Spec.Clusters = util.RemoveTargetCluster(binding.Spec.Clusters, cluster)
|
binding.Spec.RemoveCluster(cluster)
|
||||||
if err = tc.Update(context.TODO(), binding); err != nil {
|
if err = tc.Update(context.TODO(), binding); err != nil {
|
||||||
klog.ErrorS(err, "Failed to update binding", "binding", klog.KObj(binding))
|
klog.ErrorS(err, "Failed to update binding", "binding", klog.KObj(binding))
|
||||||
return err
|
return err
|
||||||
|
@ -198,7 +198,7 @@ func (tc *NoExecuteTaintManager) syncClusterBindingEviction(key util.QueueKey) e
|
||||||
// Case 3: Tolerate forever, we do nothing.
|
// Case 3: Tolerate forever, we do nothing.
|
||||||
if needEviction || tolerationTime == 0 {
|
if needEviction || tolerationTime == 0 {
|
||||||
// update final result to evict the target cluster
|
// update final result to evict the target cluster
|
||||||
binding.Spec.Clusters = util.RemoveTargetCluster(binding.Spec.Clusters, cluster)
|
binding.Spec.RemoveCluster(cluster)
|
||||||
if err = tc.Update(context.TODO(), binding); err != nil {
|
if err = tc.Update(context.TODO(), binding); err != nil {
|
||||||
klog.ErrorS(err, "Failed to update cluster binding", "binding", binding.Name)
|
klog.ErrorS(err, "Failed to update cluster binding", "binding", binding.Name)
|
||||||
return err
|
return err
|
||||||
|
|
|
@ -111,14 +111,3 @@ func MergeTargetClusters(old, new []workv1alpha2.TargetCluster) []workv1alpha2.T
|
||||||
}
|
}
|
||||||
return new
|
return new
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTargetCluster will delete a target cluster from cluster list.
|
|
||||||
func RemoveTargetCluster(clusters []workv1alpha2.TargetCluster, target string) []workv1alpha2.TargetCluster {
|
|
||||||
res := make([]workv1alpha2.TargetCluster, 0, len(clusters)-1)
|
|
||||||
for _, cluster := range clusters {
|
|
||||||
if cluster.Name != target {
|
|
||||||
res = append(res, cluster)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return res
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue