Repository: kubean-io/kubean
Pull request #983 (2023-10-10 07:10:18)

add BackUp Weight for ClusterOperation
<!--  

Thanks for sending a pull request!  Here are some tips for you:

* make sure your commit is signed off

-->

#### What type of PR is this?

<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind enhancement
/kind feature

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind design
/kind regression
-->

#### What this PR does / why we need it:

* sort clusterOperations by eliminate-score anno and then createTime 

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:


ErikJiang: /lgtm

Patch:
diff --git a/pkg/controllers/cluster/controller.go b/pkg/controllers/cluster/controller.go
index 00eff6a21..a67df56d7 100644
--- a/pkg/controllers/cluster/controller.go
+++ b/pkg/controllers/cluster/controller.go
@@ -31,6 +31,7 @@ const (
 	KubeanConfigMapName                  = "kubean-config"
 	DefaultClusterOperationsBackEndLimit = 30
 	MaxClusterOperationsBackEndLimit     = 200
+	EliminateScoreAnno                   = "kubean.io/eliminate-score"
 )
 
 type Controller struct {
@@ -106,11 +107,22 @@ func (c *Controller) UpdateStatus(cluster *clusterv1alpha1.Cluster) error {
 	return nil
 }
 
-// SortClusterOperationsByCreation operations from large to small by creation timestamp.
+func (c *Controller) GetEliminateScoreValue(operation clusteroperationv1alpha1.ClusterOperation) int {
+	value, err := strconv.Atoi(operation.Annotations[EliminateScoreAnno])
+	if err != nil {
+		return 0
+	}
+	return value
+}
+
+// SortClusterOperationsByCreation sort operations order by EliminateScore ascend , createTime desc.
 func (c *Controller) SortClusterOperationsByCreation(operations []clusteroperationv1alpha1.ClusterOperation) {
 	sort.Slice(operations, func(i, j int) bool {
 		return operations[i].CreationTimestamp.After(operations[j].CreationTimestamp.Time)
 	})
+	sort.Slice(operations, func(i, j int) bool {
+		return c.GetEliminateScoreValue(operations[i]) < c.GetEliminateScoreValue(operations[j])
+	})
 }
 
 // CleanExcessClusterOps clean up excess ClusterOperation.


Test patch:
diff --git a/pkg/controllers/cluster/controller_test.go b/pkg/controllers/cluster/controller_test.go
index 08ef7f552..5fad4eb3b 100644
--- a/pkg/controllers/cluster/controller_test.go
+++ b/pkg/controllers/cluster/controller_test.go
@@ -267,6 +267,31 @@ func TestSortClusterOperationsByCreation(t *testing.T) {
 				{ObjectMeta: metav1.ObjectMeta{CreationTimestamp: metav1.Unix(1, 0)}},
 			},
 		},
+		{
+			name: "eliminate-score",
+			args: []clusteroperationv1alpha1.ClusterOperation{
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(2, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(1, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(3, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(300, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(100, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(200, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(12, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(11, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(13, 0)}},
+			},
+			want: []clusteroperationv1alpha1.ClusterOperation{
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(300, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(200, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "2"}, CreationTimestamp: metav1.Unix(100, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(3, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(2, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "100"}, CreationTimestamp: metav1.Unix(1, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(13, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(12, 0)}},
+				{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{EliminateScoreAnno: "101"}, CreationTimestamp: metav1.Unix(11, 0)}},
+			},
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {