forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide Azure Data Explorer Scaler (kedacore#1488)
Signed-off-by: Yarden Siboni <[email protected]>
- Loading branch information
Showing
9 changed files
with
824 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
Copyright 2021 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"strconv" | ||
|
||
"github.com/Azure/azure-kusto-go/kusto" | ||
"github.com/Azure/azure-kusto-go/kusto/data/table" | ||
"github.com/Azure/azure-kusto-go/kusto/unsafe" | ||
"github.com/Azure/go-autorest/autorest/azure/auth" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type DataExplorerMetadata struct { | ||
ClientId string | ||
ClientSecret string | ||
DatabaseName string | ||
Endpoint string | ||
MetricName string | ||
PodIdentity string | ||
Query string | ||
TenantId string | ||
Threshold int64 | ||
} | ||
|
||
var azureDataExplorerLogger = logf.Log.WithName("azure_data_explorer_scaler") | ||
|
||
func CreateAzureDataExplorerClient(metadata *DataExplorerMetadata) (*kusto.Client, error) { | ||
authConfig, err := getDataExplorerAuthConfig(metadata) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get data explorer auth config: %v", err) | ||
} | ||
authorizer := kusto.Authorization{Config: *authConfig} | ||
|
||
client, err := kusto.New(metadata.Endpoint, authorizer) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create kusto client: %v", err) | ||
} | ||
|
||
return client, nil | ||
} | ||
|
||
func getDataExplorerAuthConfig(metadata *DataExplorerMetadata) (*auth.AuthorizerConfig, error) { | ||
var authConfig auth.AuthorizerConfig | ||
|
||
if metadata.PodIdentity != "" { | ||
authConfig = auth.NewMSIConfig() | ||
azureDataExplorerLogger.Info("Creating Azure Data Explorer Client using Pod Identity") | ||
} else if metadata.ClientId != "" && metadata.ClientSecret != "" && metadata.TenantId != "" { | ||
authConfig = auth.NewClientCredentialsConfig(metadata.ClientId, metadata.ClientSecret, metadata.TenantId) | ||
azureDataExplorerLogger.Info("Creating Azure Data Explorer Client using clientId, clientSecret and tenantId") | ||
} else { | ||
return nil, fmt.Errorf("missing credentials. please reconfigure your scaled object metadata") | ||
} | ||
|
||
return &authConfig, nil | ||
} | ||
|
||
func GetAzureDataExplorerMetricValue(ctx context.Context, client *kusto.Client, db string, query string) (int64, error) { | ||
azureDataExplorerLogger.Info("Querying Azure Data Explorer", "db", db, "query", query) | ||
|
||
iter, err := client.Query(ctx, db, kusto.NewStmt("", kusto.UnsafeStmt(unsafe.Stmt{true, false})).UnsafeAdd(query)) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to get azure data explorer metric result from query %s: %v", query, err) | ||
} | ||
defer iter.Stop() | ||
|
||
// Get the first (and only) row. | ||
row, err := iter.Next() | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to get query %s result: %v", query, err) | ||
} | ||
|
||
if !row.ColumnTypes[0].Type.Valid() { | ||
return -1, fmt.Errorf("column type %s is not valid", row.ColumnTypes[0].Type) | ||
} | ||
|
||
// Return error if there is more than one row. | ||
_, err = iter.Next() | ||
if err != io.EOF { | ||
return -1, fmt.Errorf("query %s result had more than a single result row", query) | ||
} | ||
|
||
metricValue, err := extractDataExplorerMetricValue(row) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to extract value from query %s: %v", query, err) | ||
} | ||
|
||
return metricValue, nil | ||
} | ||
|
||
func extractDataExplorerMetricValue(row *table.Row) (int64, error) { | ||
if row == nil || len(row.ColumnTypes) == 0 { | ||
return -1, fmt.Errorf("query has no results") | ||
} | ||
|
||
dataType := row.ColumnTypes[0].Type | ||
value := row.Values[0].String() | ||
azureDataExplorerLogger.Info("Query Result", "value", value, "dataType", dataType) | ||
|
||
// Query result validation. | ||
if dataType == "real" || dataType == "int" || dataType == "long" { | ||
convertedValue, err := strconv.Atoi(value) | ||
if err != nil { | ||
return -1, fmt.Errorf("failed to convert result %s to int", value) | ||
} | ||
if convertedValue < 0 { | ||
return -1, fmt.Errorf("query result must be >= 0 but recieved: %s", value) | ||
} | ||
return int64(convertedValue), nil | ||
} | ||
|
||
return -1, fmt.Errorf("failed to extract metric value from Data Explorer request") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
Copyright 2021 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Azure/azure-kusto-go/kusto/data/errors" | ||
"github.com/Azure/azure-kusto-go/kusto/data/table" | ||
"github.com/Azure/azure-kusto-go/kusto/data/types" | ||
"github.com/Azure/azure-kusto-go/kusto/data/value" | ||
) | ||
|
||
type testExtractDataExplorerMetricValue struct { | ||
testRow *table.Row | ||
isError bool | ||
} | ||
|
||
type testGetDataExplorerAuthConfig struct { | ||
testMetadata *DataExplorerMetadata | ||
isError bool | ||
} | ||
|
||
type testGetDataExplorerEndpoint struct { | ||
testMetadata *DataExplorerMetadata | ||
isError bool | ||
} | ||
|
||
var ( | ||
clientId = "test_client_id" | ||
clusterName = "test_cluster_name" | ||
region = "test_region" | ||
rowName = "result" | ||
rowType types.Column = "long" | ||
rowValue int64 = 3 | ||
podIdentity = "Azure" | ||
secret = "test_secret" | ||
tenantId = "test_tenant_id" | ||
) | ||
|
||
var testExtractDataExplorerMetricValues = []testExtractDataExplorerMetricValue{ | ||
// pass | ||
{testRow: &table.Row{ColumnTypes: table.Columns{{rowName, rowType}}, Values: value.Values{value.Long{Value: rowValue, Valid: true}}, Op: errors.OpQuery}, isError: false}, | ||
// nil row - fail | ||
{testRow: nil, isError: true}, | ||
// Empty row - fail | ||
{testRow: &table.Row{}, isError: true}, | ||
// Metric value is not bigger than 0 - fail | ||
{testRow: &table.Row{ColumnTypes: table.Columns{{rowName, rowType}}, Values: value.Values{value.Long{Value: -1, Valid: true}}, Op: errors.OpQuery}, isError: true}, | ||
// Metric result is invalid - fail | ||
{testRow: &table.Row{ColumnTypes: table.Columns{{rowName, rowType}}, Values: value.Values{value.String{Value: "invalid", Valid: true}}, Op: errors.OpQuery}, isError: true}, | ||
// Metric Type is not valid - fail | ||
{testRow: &table.Row{ColumnTypes: table.Columns{{rowName, "String"}}, Values: value.Values{value.Long{Value: rowValue, Valid: true}}, Op: errors.OpQuery}, isError: true}, | ||
} | ||
|
||
var testGetDataExplorerAuthConfigs = []testGetDataExplorerAuthConfig{ | ||
// Auth with aad app - pass | ||
{testMetadata: &DataExplorerMetadata{ClientId: clientId, ClientSecret: secret, TenantId: tenantId}, isError: false}, | ||
// Auth with podIdentity - pass | ||
{testMetadata: &DataExplorerMetadata{PodIdentity: podIdentity}, isError: false}, | ||
// Empty metadata - fail | ||
{testMetadata: &DataExplorerMetadata{}, isError: true}, | ||
// Empty tenantId - fail | ||
{testMetadata: &DataExplorerMetadata{ClientId: clientId, ClientSecret: secret}, isError: true}, | ||
// Empty clientId - fail | ||
{testMetadata: &DataExplorerMetadata{ClientSecret: secret, TenantId: tenantId}, isError: true}, | ||
// Empty clientSecret - fail | ||
{testMetadata: &DataExplorerMetadata{ClientId: clientId, TenantId: tenantId}, isError: true}, | ||
} | ||
|
||
func TestExtractDataExplorerMetricValue(t *testing.T) { | ||
for _, testData := range testExtractDataExplorerMetricValues { | ||
_, err := extractDataExplorerMetricValue(testData.testRow) | ||
if err != nil && !testData.isError { | ||
t.Error("Expected success but got error", err) | ||
} | ||
if testData.isError && err == nil { | ||
t.Error("Expected error but got success") | ||
} | ||
} | ||
} | ||
|
||
func TestGetDataExplorerAuthConfig(t *testing.T) { | ||
for _, testData := range testGetDataExplorerAuthConfigs { | ||
_, err := getDataExplorerAuthConfig(testData.testMetadata) | ||
if err != nil && !testData.isError { | ||
t.Error("Expected success but got error", err) | ||
} | ||
if testData.isError && err == nil { | ||
t.Error("Expected error but got success") | ||
} | ||
} | ||
} |
Oops, something went wrong.