Repository: dragonflyoss/Dragonfly2
Pull request #1181 (2022-03-23 07:38:57)

feat: dfdaemon match scheduler with case insensitive
Signed-off-by: Gaius <gaius.qi@gmail.com>

<!--- Provide a general summary of your changes in the Title above -->

## Description
- Dfdaemon match scheduler with case insensitive
<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## Screenshots (if appropriate)

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation Update (if none of the other choices apply)

## Checklist

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [x] Code compiles correctly.
- [x] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.



Patch:
diff --git a/manager/router/router.go b/manager/router/router.go
index 30de995b..c267b2f2 100644
--- a/manager/router/router.go
+++ b/manager/router/router.go
@@ -197,10 +197,10 @@ func Init(cfg *config.Config, logDir string, service service.Service, enforcer *
 
 	// Config
 	config := apiv1.Group("/configs")
-	config.POST("", h.CreateConfig)
-	config.DELETE(":id", h.DestroyConfig)
-	config.PATCH(":id", h.UpdateConfig)
-	config.GET(":id", h.GetConfig)
+	config.POST("", h.CreateConfig, jwt.MiddlewareFunc(), rbac)
+	config.DELETE(":id", h.DestroyConfig, jwt.MiddlewareFunc(), rbac)
+	config.PATCH(":id", h.UpdateConfig, jwt.MiddlewareFunc(), rbac)
+	config.GET(":id", h.GetConfig, jwt.MiddlewareFunc(), rbac)
 	config.GET("", h.GetConfigs)
 
 	// Job
diff --git a/manager/searcher/searcher.go b/manager/searcher/searcher.go
index aeb5bced..690f89fa 100644
--- a/manager/searcher/searcher.go
+++ b/manager/searcher/searcher.go
@@ -166,7 +166,7 @@ func FilterSchedulerClusters(conditions map[string]string, schedulerClusters []m
 		// scheduler cluster SecurityRules also exists,
 		// then security_domain and SecurityRules are equal to match.
 		for _, securityRule := range schedulerCluster.SecurityGroup.SecurityRules {
-			if securityRule.Domain == securityDomain {
+			if strings.EqualFold(securityRule.Domain, securityDomain) {
 				clusters = append(clusters, schedulerCluster)
 			}
 		}
@@ -203,7 +203,7 @@ func calculateIDCAffinityScore(dst, src string) float64 {
 		return minScore
 	}
 
-	if dst == src {
+	if strings.EqualFold(dst, src) {
 		return maxScore
 	}
 
@@ -212,7 +212,7 @@ func calculateIDCAffinityScore(dst, src string) float64 {
 	// it gets the max score of idc.
 	srcElements := strings.Split(src, "|")
 	for _, srcElement := range srcElements {
-		if dst == srcElement {
+		if strings.EqualFold(dst, srcElement) {
 			return maxScore
 		}
 	}
@@ -226,7 +226,7 @@ func calculateMultiElementAffinityScore(dst, src string) float64 {
 		return minScore
 	}
 
-	if dst == src {
+	if strings.EqualFold(dst, src) {
 		return maxScore
 	}
 
@@ -242,7 +242,7 @@ func calculateMultiElementAffinityScore(dst, src string) float64 {
 	}
 
 	for i := 0; i < elementLen; i++ {
-		if dstElements[i] != srcElements[i] {
+		if !strings.EqualFold(dstElements[i], srcElements[i]) {
 			break
 		}
 		score++


Test patch:
diff --git a/manager/searcher/searcher_test.go b/manager/searcher/searcher_test.go
index 70d892ce..a49bc5a6 100644
--- a/manager/searcher/searcher_test.go
+++ b/manager/searcher/searcher_test.go
@@ -616,6 +616,115 @@ func TestSchedulerCluster(t *testing.T) {
 				assert.Equal(len(data), 4)
 			},
 		},
+		{
+			name: "match according to all conditions with the case insensitive",
+			schedulerClusters: []model.SchedulerCluster{
+				{
+					Name: "foo",
+					Scopes: map[string]interface{}{
+						"idc":      "IDC-1",
+						"location": "LOCATION-2",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-1",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "foo",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "bar",
+					Scopes: map[string]interface{}{
+						"idc":      "IDC-1",
+						"location": "LOCATION-1",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-1",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bar",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "baz",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-1|LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1",
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "baz",
+							State:    "active",
+						},
+					},
+				},
+				{
+					Name: "bax",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1|NET_TOPOLOGY-2",
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bax",
+							State:    "active",
+						},
+					},
+					IsDefault: true,
+				},
+				{
+					Name: "bac",
+					Scopes: map[string]interface{}{
+						"idc":          "IDC-1",
+						"location":     "LOCATION-2",
+						"net_topology": "NET_TOPOLOGY-1|NET_TOPOLOGY-2",
+					},
+					SecurityGroup: model.SecurityGroup{
+						SecurityRules: []model.SecurityRule{
+							{
+								Domain: "DOMAIN-2",
+							},
+						},
+					},
+					Schedulers: []model.Scheduler{
+						{
+							HostName: "bac",
+							State:    "active",
+						},
+					},
+				},
+			},
+			conditions: map[string]string{
+				"security_domain": "domain-1",
+				"idc":             "idc-1|idc-2",
+				"location":        "location-1|location-2",
+				"net_topology":    "net_topology-1|net_topology-1",
+			},
+			expect: func(t *testing.T, data []model.SchedulerCluster, err error) {
+				assert := assert.New(t)
+				assert.Equal(data[0].Name, "bar")
+				assert.Equal(data[1].Name, "foo")
+				assert.Equal(data[2].Name, "baz")
+				assert.Equal(data[3].Name, "bax")
+				assert.Equal(len(data), 4)
+			},
+		},
 	}
 
 	for _, tc := range tests {