From 6ccfad91acce544fa40733eb9c2b65d9a1e745be Mon Sep 17 00:00:00 2001 From: SpiritZhou Date: Tue, 18 Jun 2024 23:33:29 +0800 Subject: [PATCH] Refactor Selenium Scaler (#5877) Co-authored-by: Tom Kerkhove --- pkg/scalers/selenium_grid_scaler.go | 90 ++++++---------------- pkg/scalers/selenium_grid_scaler_test.go | 98 ++++++++++++------------ 2 files changed, 74 insertions(+), 114 deletions(-) diff --git a/pkg/scalers/selenium_grid_scaler.go b/pkg/scalers/selenium_grid_scaler.go index 486e40a678f..4e1f75667ad 100644 --- a/pkg/scalers/selenium_grid_scaler.go +++ b/pkg/scalers/selenium_grid_scaler.go @@ -9,7 +9,6 @@ import ( "io" "math" "net/http" - "strconv" "strings" "github.com/go-logr/logr" @@ -28,15 +27,17 @@ type seleniumGridScaler struct { } type seleniumGridScalerMetadata struct { - url string - browserName string - sessionBrowserName string - targetValue int64 - activationThreshold int64 - browserVersion string - unsafeSsl bool - triggerIndex int - platformName string + triggerIndex int + + URL string `keda:"name=url, order=triggerMetadata;authParams"` + BrowserName string `keda:"name=browserName, order=triggerMetadata"` + SessionBrowserName string `keda:"name=sessionBrowserName, order=triggerMetadata, optional"` + ActivationThreshold int64 `keda:"name=activationThreshold, order=triggerMetadata, optional"` + BrowserVersion string `keda:"name=browserVersion, order=triggerMetadata, optional, default=latest"` + UnsafeSsl bool `keda:"name=unsafeSsl, order=triggerMetadata, optional, default=false"` + PlatformName string `keda:"name=platformName, order=triggerMetadata, optional, default=linux"` + + TargetValue int64 } type seleniumResponse struct { @@ -89,7 +90,7 @@ func NewSeleniumGridScaler(config *scalersconfig.ScalerConfig) (Scaler, error) { return nil, fmt.Errorf("error parsing selenium grid metadata: %w", err) } - httpClient := kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, meta.unsafeSsl) + httpClient := kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, meta.UnsafeSsl) return &seleniumGridScaler{ metricType: metricType, @@ -100,61 +101,20 @@ func NewSeleniumGridScaler(config *scalersconfig.ScalerConfig) (Scaler, error) { } func parseSeleniumGridScalerMetadata(config *scalersconfig.ScalerConfig) (*seleniumGridScalerMetadata, error) { - meta := seleniumGridScalerMetadata{ - targetValue: 1, - } - - if val, ok := config.AuthParams["url"]; ok { - meta.url = val - } else if val, ok := config.TriggerMetadata["url"]; ok { - meta.url = val - } else { - return nil, fmt.Errorf("no selenium grid url given in metadata") - } - - if val, ok := config.TriggerMetadata["browserName"]; ok { - meta.browserName = val - } else { - return nil, fmt.Errorf("no browser name given in metadata") - } - - if val, ok := config.TriggerMetadata["sessionBrowserName"]; ok { - meta.sessionBrowserName = val - } else { - meta.sessionBrowserName = meta.browserName + meta := &seleniumGridScalerMetadata{ + TargetValue: 1, } - meta.activationThreshold = 0 - if val, ok := config.TriggerMetadata["activationThreshold"]; ok { - activationThreshold, err := strconv.ParseInt(val, 10, 64) - if err != nil { - return nil, fmt.Errorf("error parsing unsafeSsl: %w", err) - } - meta.activationThreshold = activationThreshold - } - - if val, ok := config.TriggerMetadata["browserVersion"]; ok && val != "" { - meta.browserVersion = val - } else { - meta.browserVersion = DefaultBrowserVersion + if err := config.TypedConfig(meta); err != nil { + return nil, fmt.Errorf("error parsing prometheus metadata: %w", err) } - if val, ok := config.TriggerMetadata["unsafeSsl"]; ok { - parsedVal, err := strconv.ParseBool(val) - if err != nil { - return nil, fmt.Errorf("error parsing unsafeSsl: %w", err) - } - meta.unsafeSsl = parsedVal - } + meta.triggerIndex = config.TriggerIndex - if val, ok := config.TriggerMetadata["platformName"]; ok && val != "" { - meta.platformName = val - } else { - meta.platformName = DefaultPlatformName + if meta.SessionBrowserName == "" { + meta.SessionBrowserName = meta.BrowserName } - - meta.triggerIndex = config.TriggerIndex - return &meta, nil + return meta, nil } // No cleanup required for selenium grid scaler @@ -173,16 +133,16 @@ func (s *seleniumGridScaler) GetMetricsAndActivity(ctx context.Context, metricNa metric := GenerateMetricInMili(metricName, float64(sessions)) - return []external_metrics.ExternalMetricValue{metric}, sessions > s.metadata.activationThreshold, nil + return []external_metrics.ExternalMetricValue{metric}, sessions > s.metadata.ActivationThreshold, nil } func (s *seleniumGridScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec { - metricName := kedautil.NormalizeString(fmt.Sprintf("seleniumgrid-%s", s.metadata.browserName)) + metricName := kedautil.NormalizeString(fmt.Sprintf("seleniumgrid-%s", s.metadata.BrowserName)) externalMetric := &v2.ExternalMetricSource{ Metric: v2.MetricIdentifier{ Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, metricName), }, - Target: GetMetricTarget(s.metricType, s.metadata.targetValue), + Target: GetMetricTarget(s.metricType, s.metadata.TargetValue), } metricSpec := v2.MetricSpec{ External: externalMetric, Type: externalMetricType, @@ -199,7 +159,7 @@ func (s *seleniumGridScaler) getSessionsCount(ctx context.Context, logger logr.L return -1, err } - req, err := http.NewRequestWithContext(ctx, "POST", s.metadata.url, bytes.NewBuffer(body)) + req, err := http.NewRequestWithContext(ctx, "POST", s.metadata.URL, bytes.NewBuffer(body)) if err != nil { return -1, err } @@ -219,7 +179,7 @@ func (s *seleniumGridScaler) getSessionsCount(ctx context.Context, logger logr.L if err != nil { return -1, err } - v, err := getCountFromSeleniumResponse(b, s.metadata.browserName, s.metadata.browserVersion, s.metadata.sessionBrowserName, s.metadata.platformName, logger) + v, err := getCountFromSeleniumResponse(b, s.metadata.BrowserName, s.metadata.BrowserVersion, s.metadata.SessionBrowserName, s.metadata.PlatformName, logger) if err != nil { return -1, err } diff --git a/pkg/scalers/selenium_grid_scaler_test.go b/pkg/scalers/selenium_grid_scaler_test.go index 4a2fb412d52..95e24743c1e 100644 --- a/pkg/scalers/selenium_grid_scaler_test.go +++ b/pkg/scalers/selenium_grid_scaler_test.go @@ -558,12 +558,12 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "chrome", - sessionBrowserName: "chrome", - targetValue: 1, - browserVersion: "latest", - platformName: "linux", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "chrome", + SessionBrowserName: "chrome", + TargetValue: 1, + BrowserVersion: "latest", + PlatformName: "linux", }, }, { @@ -579,12 +579,12 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "MicrosoftEdge", - sessionBrowserName: "msedge", - targetValue: 1, - browserVersion: "latest", - platformName: "linux", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "MicrosoftEdge", + SessionBrowserName: "msedge", + TargetValue: 1, + BrowserVersion: "latest", + PlatformName: "linux", }, }, { @@ -602,12 +602,12 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://user:password@selenium-hub:4444/graphql", - browserName: "MicrosoftEdge", - sessionBrowserName: "msedge", - targetValue: 1, - browserVersion: "latest", - platformName: "linux", + URL: "http://user:password@selenium-hub:4444/graphql", + BrowserName: "MicrosoftEdge", + SessionBrowserName: "msedge", + TargetValue: 1, + BrowserVersion: "latest", + PlatformName: "linux", }, }, { @@ -624,13 +624,13 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "chrome", - sessionBrowserName: "chrome", - targetValue: 1, - browserVersion: "91.0", - unsafeSsl: false, - platformName: "linux", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "chrome", + SessionBrowserName: "chrome", + TargetValue: 1, + BrowserVersion: "91.0", + UnsafeSsl: false, + PlatformName: "linux", }, }, { @@ -648,14 +648,14 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "chrome", - sessionBrowserName: "chrome", - targetValue: 1, - activationThreshold: 10, - browserVersion: "91.0", - unsafeSsl: true, - platformName: "linux", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "chrome", + SessionBrowserName: "chrome", + TargetValue: 1, + ActivationThreshold: 10, + BrowserVersion: "91.0", + UnsafeSsl: true, + PlatformName: "linux", }, }, { @@ -688,14 +688,14 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "chrome", - sessionBrowserName: "chrome", - targetValue: 1, - activationThreshold: 10, - browserVersion: "91.0", - unsafeSsl: true, - platformName: "linux", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "chrome", + SessionBrowserName: "chrome", + TargetValue: 1, + ActivationThreshold: 10, + BrowserVersion: "91.0", + UnsafeSsl: true, + PlatformName: "linux", }, }, { @@ -714,14 +714,14 @@ func Test_parseSeleniumGridScalerMetadata(t *testing.T) { }, wantErr: false, want: &seleniumGridScalerMetadata{ - url: "http://selenium-hub:4444/graphql", - browserName: "chrome", - sessionBrowserName: "chrome", - targetValue: 1, - activationThreshold: 10, - browserVersion: "91.0", - unsafeSsl: true, - platformName: "Windows 11", + URL: "http://selenium-hub:4444/graphql", + BrowserName: "chrome", + SessionBrowserName: "chrome", + TargetValue: 1, + ActivationThreshold: 10, + BrowserVersion: "91.0", + UnsafeSsl: true, + PlatformName: "Windows 11", }, }, }