Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-public cloud support for Azure Monitor scaler. #2837

Merged
merged 2 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
- **Azure Application Insights Scaler:** Provide support for non-public clouds ([#2735](https://github.com/kedacore/keda/issues/2735))
- **Azure Event Hub Scaler:** Improve logging when blob container not found ([#2363](https://github.com/kedacore/keda/issues/2363)
- **Azure Event Hub Scaler:** Provide support for non-public clouds ([#1915](https://github.com/kedacore/keda/issues/1915))
- **Azure Monitor Scaler:** Provide support for non-public clouds ([#1917](https://github.com/kedacore/keda/issues/1917))
- **Azure Queue:** Don't call Azure queue GetProperties API unnecessarily ([#2613](https://github.com/kedacore/keda/pull/2613))
- **Datadog Scaler:** Validate query to contain `{` to prevent panic on invalid query ([#2625](https://github.com/kedacore/keda/issues/2625))
- **Datadog Scaler:** Several improvements, including a new optional parameter `metricUnavailableValue` to fill data when no Datadog metric was returned ([#2657](https://github.com/kedacore/keda/issues/2657))
Expand Down
41 changes: 25 additions & 16 deletions pkg/scalers/azure/azure_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ type azureExternalMetricRequest struct {

// MonitorInfo to create metric request
type MonitorInfo struct {
ResourceURI string
TenantID string
SubscriptionID string
ResourceGroupName string
Name string
Namespace string
Filter string
AggregationInterval string
AggregationType string
ClientID string
ClientPassword string
ResourceURI string
TenantID string
SubscriptionID string
ResourceGroupName string
Name string
Namespace string
Filter string
AggregationInterval string
AggregationType string
ClientID string
ClientPassword string
AzureResourceManagerEndpoint string
ActiveDirectoryEndpoint string
}

var azureMonitorLog = logf.Log.WithName("azure_monitor_scaler")
Expand All @@ -82,14 +84,21 @@ func GetAzureMetricValue(ctx context.Context, info MonitorInfo, podIdentity keda
}

func createMetricsClient(info MonitorInfo, podIdentityEnabled bool) insights.MetricsClient {
client := insights.NewMetricsClient(info.SubscriptionID)
var config auth.AuthorizerConfig
client := insights.NewMetricsClientWithBaseURI(info.AzureResourceManagerEndpoint, info.SubscriptionID)
var authConfig auth.AuthorizerConfig
if podIdentityEnabled {
config = auth.NewMSIConfig()
config := auth.NewMSIConfig()
config.Resource = info.AzureResourceManagerEndpoint

authConfig = config
} else {
config = auth.NewClientCredentialsConfig(info.ClientID, info.ClientPassword, info.TenantID)
config := auth.NewClientCredentialsConfig(info.ClientID, info.ClientPassword, info.TenantID)
config.Resource = info.AzureResourceManagerEndpoint
config.AADEndpoint = info.ActiveDirectoryEndpoint

authConfig = config
}
authorizer, _ := config.Authorizer()
authorizer, _ := authConfig.Authorizer()
client.Authorizer = authorizer

return client
Expand Down
19 changes: 19 additions & 0 deletions pkg/scalers/azure_monitor_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strconv"
"strings"

az "github.com/Azure/go-autorest/autorest/azure"
v2beta2 "k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -148,6 +149,24 @@ func parseAzureMonitorMetadata(config *ScalerConfig) (*azureMonitorMetadata, err

meta.scalerIndex = config.ScalerIndex

azureResourceManagerEndpointProvider := func(env az.Environment) (string, error) {
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
return env.ResourceManagerEndpoint, nil
}
azureResourceManagerEndpoint, err := azure.ParseEnvironmentProperty(config.TriggerMetadata, "azureResourceManagerEndpoint", azureResourceManagerEndpointProvider)
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
meta.azureMonitorInfo.AzureResourceManagerEndpoint = azureResourceManagerEndpoint

activeDirectoryEndpointProvider := func(env az.Environment) (string, error) {
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
return env.ActiveDirectoryEndpoint, nil
}
activeDirectoryEndpoint, err := azure.ParseEnvironmentProperty(config.TriggerMetadata, "activeDirectoryEndpoint", activeDirectoryEndpointProvider)
if err != nil {
return nil, err
}
meta.azureMonitorInfo.ActiveDirectoryEndpoint = activeDirectoryEndpoint

return &meta, nil
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/scalers/azure_monitor_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
)

const (
testAzureResourceManagerEndpoint = "testAzureResourceManagerEndpoint"
testActiveDirectoryEndpoint = "testActiveDirectoryEndpoint"
)

type parseAzMonitorMetadataTestData struct {
metadata map[string]string
isError bool
Expand Down Expand Up @@ -79,6 +84,17 @@ var testParseAzMonitorMetadata = []parseAzMonitorMetadataTestData{
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "targetValue": "5"}, false, map[string]string{}, map[string]string{}, kedav1alpha1.PodIdentityProviderAzure},
// wrong podIdentity
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "targetValue": "5"}, true, map[string]string{}, map[string]string{}, kedav1alpha1.PodIdentityProvider("notAzure")},
// known azure cloud
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "activeDirectoryClientId": "CLIENT_ID", "activeDirectoryClientPasswordFromEnv": "CLIENT_PASSWORD", "targetValue": "5", "metricNamespace": "namespace", "cloud": "azureChinaCloud"}, false, testAzMonitorResolvedEnv, map[string]string{}, ""},
// private cloud
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "activeDirectoryClientId": "CLIENT_ID", "activeDirectoryClientPasswordFromEnv": "CLIENT_PASSWORD", "targetValue": "5", "metricNamespace": "namespace", "cloud": "private",
"azureResourceManagerEndpoint": testAzureResourceManagerEndpoint, "activeDirectoryEndpoint": testActiveDirectoryEndpoint}, false, testAzMonitorResolvedEnv, map[string]string{}, ""},
// private cloud with missing resource manager endpoint
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "activeDirectoryClientId": "CLIENT_ID", "activeDirectoryClientPasswordFromEnv": "CLIENT_PASSWORD", "targetValue": "5", "metricNamespace": "namespace", "cloud": "private",
"activeDirectoryEndpoint": testActiveDirectoryEndpoint}, true, testAzMonitorResolvedEnv, map[string]string{}, ""},
// private cloud with missing active directory endpoint
{map[string]string{"resourceURI": "test/resource/uri", "tenantId": "123", "subscriptionId": "456", "resourceGroupName": "test", "metricName": "metric", "metricAggregationInterval": "0:15:0", "metricAggregationType": "Average", "activeDirectoryClientId": "CLIENT_ID", "activeDirectoryClientPasswordFromEnv": "CLIENT_PASSWORD", "targetValue": "5", "metricNamespace": "namespace", "cloud": "private",
"azureResourceManagerEndpoint": testAzureResourceManagerEndpoint}, true, testAzMonitorResolvedEnv, map[string]string{}, ""},
}

var azMonitorMetricIdentifiers = []azMonitorMetricIdentifier{
Expand Down