Skip to content

Commit 2af207a

Browse files
committed
Update KEDA resource
Signed-off-by: Viet Nguyen Duc <[email protected]>
1 parent 8549f5d commit 2af207a

File tree

2 files changed

+163
-20
lines changed

2 files changed

+163
-20
lines changed

.keda/scalers/selenium_grid_scaler.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ var FunctionCapabilitiesPrefixes = []string{EnableManagedDownloadsCapability}
109109

110110
// Follow pattern in https://github.com./SeleniumHQ/selenium/blob/trunk/java/src/org/openqa/selenium/grid/data/DefaultSlotMatcher.java
111111
func filterCapabilities(capabilities map[string]interface{}) map[string]interface{} {
112-
filteredCapabilities := make(map[string]interface{})
112+
filteredCapabilities := map[string]interface{}{}
113113

114114
for key, value := range capabilities {
115115
retain := true
@@ -308,12 +308,28 @@ func countMatchingSessions(sessions Sessions, browserName string, browserVersion
308308
return matchingSessions
309309
}
310310

311+
func managedDownloadsEnabled(stereotype map[string]interface{}, capabilities map[string]interface{}) bool {
312+
// First lets check if user wanted a Node with managed downloads enabled
313+
value1, ok1 := capabilities[EnableManagedDownloadsCapability]
314+
if !ok1 || !value1.(bool) {
315+
// User didn't ask. So lets move on to the next matching criteria
316+
return true
317+
}
318+
// User wants managed downloads enabled to be done on this Node, let's check the stereotype
319+
value2, ok2 := stereotype[EnableManagedDownloadsCapability]
320+
// Try to match what the user requested
321+
return ok2 && value2.(bool)
322+
}
323+
311324
func extensionCapabilitiesMatch(stereotype map[string]interface{}, capabilities map[string]interface{}) bool {
312325
capabilities = filterCapabilities(capabilities)
313326
if len(capabilities) == 0 {
314327
return true
315328
}
316329
for key, value := range capabilities {
330+
if key == EnableManagedDownloadsCapability {
331+
continue
332+
}
317333
if stereotypeValue, ok := stereotype[key]; !ok || stereotypeValue != value {
318334
return false
319335
}
@@ -338,7 +354,7 @@ func checkRequestCapabilitiesMatch(request map[string]interface{}, browserName s
338354
platformNameMatch := (_platformName == "" || strings.EqualFold("any", _platformName) || strings.EqualFold(platformName, _platformName)) &&
339355
(platformName == "" || platformName == "any" || strings.EqualFold(platformName, _platformName))
340356

341-
return browserNameMatch && browserVersionMatch && platformNameMatch && extensionCapabilitiesMatch(request, capabilities)
357+
return browserNameMatch && browserVersionMatch && platformNameMatch && managedDownloadsEnabled(capabilities, request) && extensionCapabilitiesMatch(request, capabilities)
342358
}
343359

344360
// This function checks if Node stereotypes or ongoing sessions match the scaler metadata
@@ -359,7 +375,7 @@ func checkStereotypeCapabilitiesMatch(capability map[string]interface{}, browser
359375
platformNameMatch := (_platformVersion == "" || strings.EqualFold("any", _platformVersion) || strings.EqualFold(platformName, _platformVersion)) &&
360376
(platformName == "" || platformName == "any" || strings.EqualFold(platformName, _platformVersion))
361377

362-
return browserNameMatch && browserVersionMatch && platformNameMatch && extensionCapabilitiesMatch(capability, capabilities)
378+
return browserNameMatch && browserVersionMatch && platformNameMatch && managedDownloadsEnabled(capabilities, capability) && extensionCapabilitiesMatch(capability, capabilities)
363379
}
364380

365381
func checkNodeReservedSlots(reservedNodes []ReservedNodes, nodeID string, availableSlots int64) int64 {
@@ -397,7 +413,8 @@ func getCountFromSeleniumResponse(b []byte, browserName string, browserVersion s
397413
capabilities, err := parseCapabilitiesToMap(_capabilities)
398414
if err != nil {
399415
logger.Error(err, fmt.Sprintf("Error when unmarshaling trigger metadata 'capabilities': %s", err))
400-
} else if enableManagedDownloads {
416+
}
417+
if enableManagedDownloads {
401418
capabilities[EnableManagedDownloadsCapability] = true
402419
}
403420

.keda/scalers/selenium_grid_scaler_test.go

+142-16
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
100100
}
101101
}
102102
`),
103-
browserName: "chrome",
104-
sessionBrowserName: "chrome",
105-
browserVersion: "",
106-
platformName: "linux",
103+
browserName: "chrome",
104+
sessionBrowserName: "chrome",
105+
browserVersion: "",
106+
enableManagedDownloads: true,
107+
platformName: "linux",
107108
},
108109
wantNewRequestNodes: 4,
109110
wantOnGoingSessions: 0,
@@ -276,10 +277,11 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
276277
}
277278
}
278279
`),
279-
browserName: "firefox",
280-
sessionBrowserName: "firefox",
281-
browserVersion: "",
282-
platformName: "linux",
280+
browserName: "firefox",
281+
sessionBrowserName: "firefox",
282+
browserVersion: "",
283+
enableManagedDownloads: true,
284+
platformName: "linux",
283285
},
284286
wantNewRequestNodes: 0,
285287
wantOnGoingSessions: 4,
@@ -326,10 +328,11 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
326328
}
327329
}
328330
`),
329-
browserName: "chrome",
330-
sessionBrowserName: "chrome",
331-
browserVersion: "",
332-
platformName: "linux",
331+
browserName: "chrome",
332+
sessionBrowserName: "chrome",
333+
browserVersion: "",
334+
enableManagedDownloads: true,
335+
platformName: "linux",
333336
},
334337
wantNewRequestNodes: 1,
335338
wantOnGoingSessions: 0,
@@ -1909,7 +1912,7 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
19091912
wantErr: false,
19101913
},
19111914
{
1912-
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_nodeMaxSessions_2_and_3_requests_match_should_return_count_as_1_and_1_ongoing_session",
1915+
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_nodeMaxSessions_2_and_4_requests_match_should_return_count_as_2_and_1_ongoing_session",
19131916
args: args{
19141917
b: []byte(`{
19151918
"data": {
@@ -1957,10 +1960,62 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
19571960
nodeMaxSessions: 2,
19581961
enableManagedDownloads: true,
19591962
},
1960-
wantNewRequestNodes: 1,
1963+
wantNewRequestNodes: 2,
19611964
wantOnGoingSessions: 1,
19621965
wantErr: false,
19631966
},
1967+
{
1968+
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_nodeMaxSessions_2_disable_managed_downloads_and_1_requests_match_should_return_count_as_1_and_0_ongoing_session",
1969+
args: args{
1970+
b: []byte(`{
1971+
"data": {
1972+
"grid": {
1973+
"sessionCount": 1,
1974+
"maxSession": 2,
1975+
"totalSlots": 2
1976+
},
1977+
"nodesInfo": {
1978+
"nodes": [
1979+
{
1980+
"id": "node-1",
1981+
"status": "UP",
1982+
"sessionCount": 1,
1983+
"maxSession": 1,
1984+
"slotCount": 1,
1985+
"stereotypes": "[{\"slots\": 1, \"stereotype\": {\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"se:downloadsEnabled\": true}}]",
1986+
"sessions": [
1987+
{
1988+
"id": "session-1",
1989+
"capabilities": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"se:downloadsEnabled\": true}",
1990+
"slot": {
1991+
"id": "9ce1edba-72fb-465e-b311-ee473d8d7b64",
1992+
"stereotype": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"se:downloadsEnabled\": true}"
1993+
}
1994+
}
1995+
]
1996+
}
1997+
]
1998+
},
1999+
"sessionsInfo": {
2000+
"sessionQueueRequests": [
2001+
"{\n \"browserName\": \"chrome\",\n \"platformName\": \"linux\"\n}",
2002+
"{\n \"browserName\": \"chrome\",\n \"se:downloadsEnabled\": true,\n \"platformName\": \"linux\"\n}",
2003+
"{\n \"browserName\": \"chrome\",\n \"se:downloadsEnabled\": true,\n \"platformName\": \"linux\"\n}",
2004+
"{\n \"browserName\": \"chrome\",\n \"se:downloadsEnabled\": true,\n \"platformName\": \"linux\"\n}",
2005+
"{\n \"browserName\": \"chrome\",\n \"platformName\": \"Windows 11\"\n}"]
2006+
}
2007+
}
2008+
}`),
2009+
browserName: "chrome",
2010+
sessionBrowserName: "chrome",
2011+
browserVersion: "",
2012+
platformName: "linux",
2013+
nodeMaxSessions: 2,
2014+
},
2015+
wantNewRequestNodes: 1,
2016+
wantOnGoingSessions: 0,
2017+
wantErr: false,
2018+
},
19642019
{
19652020
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_extra_capabilities_and_2_requests_match_should_return_count_as_2_and_ongoing_1",
19662021
args: args{
@@ -2015,7 +2070,7 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
20152070
wantErr: false,
20162071
},
20172072
{
2018-
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_extra_capabilities_and_mangaged_downloads_and_1_request_match_should_return_count_as_1_and_ongoing_1",
2073+
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_extra_capabilities_and_mangaged_downloads_and_1_request_match_should_return_count_as_2_and_ongoing_1",
20192074
args: args{
20202075
b: []byte(`{
20212076
"data": {
@@ -2064,10 +2119,81 @@ func Test_getCountFromSeleniumResponse(t *testing.T) {
20642119
enableManagedDownloads: true,
20652120
capabilities: "{\"myApp:version\": \"beta\", \"myApp:scope\": \"internal\"}",
20662121
},
2067-
wantNewRequestNodes: 1,
2122+
wantNewRequestNodes: 2,
20682123
wantOnGoingSessions: 1,
20692124
wantErr: false,
20702125
},
2126+
{
2127+
name: "4_sessions_requests_with_matching_browserName_and_platformName_when_set_extra_capabilities_and_mangaged_downloads_and_4_request_match_should_return_count_as_4_and_ongoing_2",
2128+
args: args{
2129+
b: []byte(`{
2130+
"data": {
2131+
"grid": {
2132+
"sessionCount": 1,
2133+
"maxSession": 1,
2134+
"totalSlots": 1
2135+
},
2136+
"nodesInfo": {
2137+
"nodes": [
2138+
{
2139+
"id": "node-1",
2140+
"status": "UP",
2141+
"sessionCount": 1,
2142+
"maxSession": 1,
2143+
"slotCount": 1,
2144+
"stereotypes": "[{\"slots\": 1, \"stereotype\": {\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\", \"se:downloadsEnabled\": true}}]",
2145+
"sessions": [
2146+
{
2147+
"id": "session-1",
2148+
"capabilities": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\", \"se:downloadsEnabled\": true}",
2149+
"slot": {
2150+
"id": "9ce1edba-72fb-465e-b311-ee473d8d7b64",
2151+
"stereotype": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\", \"se:downloadsEnabled\": true}"
2152+
}
2153+
}
2154+
]
2155+
},
2156+
{
2157+
"id": "node-2",
2158+
"status": "UP",
2159+
"sessionCount": 1,
2160+
"maxSession": 1,
2161+
"slotCount": 1,
2162+
"stereotypes": "[{\"slots\": 1, \"stereotype\": {\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\"}}]",
2163+
"sessions": [
2164+
{
2165+
"id": "session-1",
2166+
"capabilities": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\"}",
2167+
"slot": {
2168+
"id": "9ce1edba-72fb-465e-b311-ee473d8d7b64",
2169+
"stereotype": "{\"browserName\": \"chrome\", \"browserVersion\": \"\", \"platformName\": \"linux\", \"myApp:version\": \"beta\", \"myApp:scope\": \"internal\"}"
2170+
}
2171+
}
2172+
]
2173+
}
2174+
]
2175+
},
2176+
"sessionsInfo": {
2177+
"sessionQueueRequests": [
2178+
"{\n \"browserName\": \"chrome\",\n \"platformName\": \"linux\"\n}",
2179+
"{\n \"browserName\": \"chrome\",\n \"myApp:version\": \"beta\",\n \"platformName\": \"linux\"\n}",
2180+
"{\n \"browserName\": \"chrome\",\n \"myApp:version\": \"beta\",\n \"myApp:scope\": \"internal\",\n \"platformName\": \"linux\"\n}",
2181+
"{\n \"browserName\": \"chrome\",\n \"myApp:version\": \"beta\",\n \"myApp:scope\": \"internal\",\n \"platformName\": \"linux\"\n}",
2182+
"{\n \"browserName\": \"chrome\",\n \"platformName\": \"Windows 11\"\n}"]
2183+
}
2184+
}
2185+
}`),
2186+
browserName: "chrome",
2187+
sessionBrowserName: "chrome",
2188+
browserVersion: "",
2189+
platformName: "linux",
2190+
nodeMaxSessions: 1,
2191+
enableManagedDownloads: true,
2192+
},
2193+
wantNewRequestNodes: 4,
2194+
wantOnGoingSessions: 2,
2195+
wantErr: false,
2196+
},
20712197
{
20722198
name: "Given_2_requests_include_1_without_browserVersion_When_scaler_metadata_explicit_name_version_platform_Then_scaler_should_scale_up_for_1_request_has_browserVersion_and_return_0_ongoing_sessions",
20732199
args: args{

0 commit comments

Comments
 (0)