diff --git a/CHANGELOG.md b/CHANGELOG.md index 310cfc2905c..06c9061905f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - Escape `queueName` and `vhostName` in RabbitMQ Scaler before use them in query string (bug fix) ([#2055](https://github.com/kedacore/keda/pull/2055)) - TriggerAuthentication/Vault: add support for HashiCorp Vault namespace (Vault Enterprise) ([#2085](https://github.com/kedacore/keda/pull/2085)) - Add custom http timeout in RabbitMQ Scaler ([#2086](https://github.com/kedacore/keda/pull/2086)) +- Artemis Scaler parses out broker config parameters in case `restAPITemplate` is given ([#2104](https://github.com/kedacore/keda/pull/2104)) - Add support to get connection data from Trigger Authorization in MongoDB Scaler ([#2115](https://github.com/kedacore/keda/pull/2115)) - Add support to get connection data from Trigger Authorization in MySQL Scaler ([#2113](https://github.com/kedacore/keda/pull/2113)) - Add support to get connection data from Trigger Authorization in MSSQL Scaler ([#2112](https://github.com/kedacore/keda/pull/2112)) diff --git a/pkg/scalers/artemis_scaler.go b/pkg/scalers/artemis_scaler.go index e4e53a122b8..f52b500bd66 100644 --- a/pkg/scalers/artemis_scaler.go +++ b/pkg/scalers/artemis_scaler.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strconv" "strings" @@ -79,14 +80,32 @@ func parseArtemisMetadata(config *ScalerConfig) (*artemisMetadata, error) { if val, ok := config.TriggerMetadata["restApiTemplate"]; ok && val != "" { meta.restAPITemplate = config.TriggerMetadata["restApiTemplate"] + var err error + if meta, err = getAPIParameters(meta); err != nil { + return nil, fmt.Errorf("can't parse restApiTemplate : %s ", err) + } } else { meta.restAPITemplate = defaultRestAPITemplate - } + if config.TriggerMetadata["managementEndpoint"] == "" { + return nil, errors.New("no management endpoint given") + } + meta.managementEndpoint = config.TriggerMetadata["managementEndpoint"] + + if config.TriggerMetadata["queueName"] == "" { + return nil, errors.New("no queue name given") + } + meta.queueName = config.TriggerMetadata["queueName"] + + if config.TriggerMetadata["brokerName"] == "" { + return nil, errors.New("no broker name given") + } + meta.brokerName = config.TriggerMetadata["brokerName"] - if config.TriggerMetadata["managementEndpoint"] == "" { - return nil, errors.New("no management endpoint given") + if config.TriggerMetadata["brokerAddress"] == "" { + return nil, errors.New("no broker address given") + } + meta.brokerAddress = config.TriggerMetadata["brokerAddress"] } - meta.managementEndpoint = config.TriggerMetadata["managementEndpoint"] if val, ok := config.TriggerMetadata["corsHeader"]; ok && val != "" { meta.corsHeader = config.TriggerMetadata["corsHeader"] @@ -94,21 +113,6 @@ func parseArtemisMetadata(config *ScalerConfig) (*artemisMetadata, error) { meta.corsHeader = fmt.Sprintf(defaultCorsHeader, meta.managementEndpoint) } - if config.TriggerMetadata["queueName"] == "" { - return nil, errors.New("no queue name given") - } - meta.queueName = config.TriggerMetadata["queueName"] - - if config.TriggerMetadata["brokerName"] == "" { - return nil, errors.New("no broker name given") - } - meta.brokerName = config.TriggerMetadata["brokerName"] - - if config.TriggerMetadata["brokerAddress"] == "" { - return nil, errors.New("no broker address given") - } - meta.brokerAddress = config.TriggerMetadata["brokerAddress"] - if val, ok := config.TriggerMetadata["queueLength"]; ok { queueLength, err := strconv.Atoi(val) if err != nil { @@ -163,6 +167,38 @@ func (s *artemisScaler) IsActive(ctx context.Context) (bool, error) { return messages > 0, nil } +// getAPIParameters parse restAPITemplate to provide managementEndpoint , brokerName, brokerAddress, queueName +func getAPIParameters(meta artemisMetadata) (artemisMetadata, error) { + u, err := url.ParseRequestURI(meta.restAPITemplate) + if err != nil { + return meta, fmt.Errorf("unable to parse the artemis restAPITemplate: %s", err) + } + meta.managementEndpoint = u.Host + splitURL := strings.Split(strings.Split(u.RawPath, ":")[1], "/")[0] // This returns : broker="<>",component=addresses,address="<>",subcomponent=queues,routing-type="anycast",queue="<>" + replacer := strings.NewReplacer(",", "&", "\"\"", "") + v, err := url.ParseQuery(replacer.Replace(splitURL)) // This returns a map with key: string types and element type [] string. : map[address:["<>"] broker:["<>"] component:[addresses] queue:["<>"] routing-type:["anycast"] subcomponent:[queues]] + if err != nil { + return meta, fmt.Errorf("unable to parse the artemis restAPITemplate: %s", err) + } + + if len(v["address"][0]) == 0 { + return meta, errors.New("no brokerAddress given") + } + meta.brokerAddress = v["address"][0] + + if len(v["queue"][0]) == 0 { + return meta, errors.New("no queueName is given") + } + meta.queueName = v["queue"][0] + + if len(v["broker"][0]) == 0 { + return meta, fmt.Errorf("no brokerName given: %s", meta.restAPITemplate) + } + meta.brokerName = v["broker"][0] + + return meta, nil +} + func (s *artemisScaler) getMonitoringEndpoint() string { replacer := strings.NewReplacer("<>", s.metadata.managementEndpoint, "<>", s.metadata.queueName, diff --git a/pkg/scalers/artemis_scaler_test.go b/pkg/scalers/artemis_scaler_test.go index 47d30681608..cba1022fd15 100644 --- a/pkg/scalers/artemis_scaler_test.go +++ b/pkg/scalers/artemis_scaler_test.go @@ -53,6 +53,9 @@ var testArtemisMetadata = []parseArtemisMetadataTestData{ // Missing password, should fail {map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test", "username": "myUserName", "password": ""}, true}, {map[string]string{"managementEndpoint": "localhost:8161", "queueName": "queue1", "brokerName": "broker-activemq", "brokerAddress": "test", "username": "myUserName", "password": "myPassword"}, false}, + {map[string]string{"restApiTemplate": "http://localhost:8161/console/jolokia/read/org.apache.activemq.artemis:broker=\"broker-activemq\",component=addresses,address=\"test\",subcomponent=queues,routing-type=\"anycast\",queue=\"queue1\"/MessageCount", "username": "myUserName", "password": "myPassword"}, false}, + // Missing brokername , should fail + {map[string]string{"restApiTemplate": "http://localhost:8161/console/jolokia/read/org.apache.activemq.artemis:broker=\"\",component=addresses,address=\"test\",subcomponent=queues,routing-type=\"anycast\",queue=\"queue1\"/MessageCount", "username": "myUserName", "password": "myPassword"}, true}, } var artemisMetricIdentifiers = []artemisMetricIdentifier{