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

Enable support for non-public clouds in Application Insights scaler. #2818

Merged
merged 4 commits into from
Mar 25, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
### Improvements

- **General:** Synchronize HPA annotations from ScaledObject ([#2659](https://github.com/kedacore/keda/pull/2659))
- **Azure Event Hub Scaler:** Improve logging when blob container not found ([#2363]https://github.com/kedacore/keda/issues/2363)
- **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 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))
Expand Down
33 changes: 21 additions & 12 deletions pkg/scalers/azure/azure_app_insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ import (
)

const (
appInsightsResource = "https://api.applicationinsights.io"
DefaultAppInsightsResourceURL = "https://api.applicationinsights.io"
)

var AppInsightsResourceURLInCloud = map[string]string{
"AZUREPUBLICCLOUD": "https://api.applicationinsights.io",
"AZUREUSGOVERNMENTCLOUD": "https://api.applicationinsights.us",
"AZURECHINACLOUD": "https://api.applicationinsights.azure.cn",
}

type AppInsightsInfo struct {
ApplicationInsightsID string
TenantID string
MetricID string
AggregationTimespan string
AggregationType string
Filter string
ClientID string
ClientPassword string
ApplicationInsightsID string
TenantID string
MetricID string
AggregationTimespan string
AggregationType string
Filter string
ClientID string
ClientPassword string
AppInsightsResourceURL string
ActiveDirectoryEndpoint string
}

type ApplicationInsightsMetric struct {
Expand Down Expand Up @@ -55,12 +63,13 @@ func toISO8601(time string) (string, error) {
func getAuthConfig(info AppInsightsInfo, podIdentity kedav1alpha1.PodIdentityProvider) auth.AuthorizerConfig {
if podIdentity == "" || podIdentity == kedav1alpha1.PodIdentityProviderNone {
config := auth.NewClientCredentialsConfig(info.ClientID, info.ClientPassword, info.TenantID)
config.Resource = appInsightsResource
config.Resource = info.AppInsightsResourceURL
config.AADEndpoint = info.ActiveDirectoryEndpoint
return config
}

config := auth.NewMSIConfig()
config.Resource = appInsightsResource
config.Resource = info.AppInsightsResourceURL
return config
}

Expand Down Expand Up @@ -115,7 +124,7 @@ func GetAzureAppInsightsMetricValue(ctx context.Context, info AppInsightsInfo, p
}

req, err := autorest.Prepare(&http.Request{},
autorest.WithBaseURL(appInsightsResource),
autorest.WithBaseURL(info.AppInsightsResourceURL),
autorest.WithPath("v1/apps"),
autorest.WithPath(info.ApplicationInsightsID),
autorest.WithPath("metrics"),
Expand Down
26 changes: 26 additions & 0 deletions pkg/scalers/azure_app_insights_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,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 @@ -97,6 +98,31 @@ func parseAzureAppInsightsMetadata(config *ScalerConfig) (*azureAppInsightsMetad
meta.azureAppInsightsInfo.Filter = ""
}

meta.azureAppInsightsInfo.AppInsightsResourceURL = azure.DefaultAppInsightsResourceURL

if cloud, ok := config.TriggerMetadata["cloud"]; ok {
if strings.EqualFold(cloud, azure.PrivateCloud) {
if resource, ok := config.TriggerMetadata["appInsightsResourceURL"]; ok && resource != "" {
meta.azureAppInsightsInfo.AppInsightsResourceURL = resource
} else {
return nil, fmt.Errorf("appInsightsResourceURL must be provided for %s cloud type", azure.PrivateCloud)
}
} else if resource, ok := azure.AppInsightsResourceURLInCloud[strings.ToUpper(cloud)]; ok {
meta.azureAppInsightsInfo.AppInsightsResourceURL = resource
} else {
return nil, fmt.Errorf("there is no cloud environment matching the name %s", cloud)
}
}

activeDirectoryEndpointProvider := func(env az.Environment) (string, error) {
return env.ActiveDirectoryEndpoint, nil
}
activeDirectoryEndpoint, err := azure.ParseEnvironmentProperty(config.TriggerMetadata, "activeDirectoryEndpoint", activeDirectoryEndpointProvider)
if err != nil {
return nil, err
}
meta.azureAppInsightsInfo.ActiveDirectoryEndpoint = activeDirectoryEndpoint

// Required authentication parameters below

val, err = getParameterFromConfig(config, azureAppInsightsAppIDName, true)
Expand Down
50 changes: 50 additions & 0 deletions pkg/scalers/azure_app_insights_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,56 @@ var azureAppInsightsScalerData = []azureAppInsightsScalerTestData{
"AD_CLIENT_ID": "5678", "AD_CLIENT_PASSWORD": "pw", "APP_INSIGHTS_ID": "1234", "TENANT_ID": "1234",
},
}},
{name: "known Azure Cloud", isError: false, config: ScalerConfig{
TriggerMetadata: map[string]string{
"metricAggregationTimespan": "00:01", "metricAggregationType": "count", "metricId": "unittest/test", "targetValue": "10",
"applicationInsightsId": "appinsightid", "tenantId": "tenantid",
"cloud": "azureChinaCloud",
},
AuthParams: map[string]string{
"tenantId": "tenantId", "activeDirectoryClientId": "adClientId", "activeDirectoryClientPassword": "adClientPassword",
},
}},
{name: "private cloud", isError: false, config: ScalerConfig{
TriggerMetadata: map[string]string{
"metricAggregationTimespan": "00:01", "metricAggregationType": "count", "metricId": "unittest/test", "targetValue": "10",
"applicationInsightsId": "appinsightid", "tenantId": "tenantid",
"cloud": "private", "appInsightsResourceURL": "appInsightsResourceURL", "activeDirectoryEndpoint": "adEndpoint",
},
AuthParams: map[string]string{
"tenantId": "tenantId", "activeDirectoryClientId": "adClientId", "activeDirectoryClientPassword": "adClientPassword",
},
}},
{name: "private cloud - missing app insights resource URL", isError: true, config: ScalerConfig{
TriggerMetadata: map[string]string{
"metricAggregationTimespan": "00:01", "metricAggregationType": "count", "metricId": "unittest/test", "targetValue": "10",
"applicationInsightsId": "appinsightid", "tenantId": "tenantid",
"cloud": "private", "activeDirectoryEndpoint": "adEndpoint",
},
AuthParams: map[string]string{
"tenantId": "tenantId", "activeDirectoryClientId": "adClientId", "activeDirectoryClientPassword": "adClientPassword",
},
}},
{name: "private cloud - missing active directory endpoint", isError: true, config: ScalerConfig{
TriggerMetadata: map[string]string{
"metricAggregationTimespan": "00:01", "metricAggregationType": "count", "metricId": "unittest/test", "targetValue": "10",
"applicationInsightsId": "appinsightid", "tenantId": "tenantid",
"cloud": "private", "appInsightsResourceURL": "appInsightsResourceURL",
},
AuthParams: map[string]string{
"tenantId": "tenantId", "activeDirectoryClientId": "adClientId", "activeDirectoryClientPassword": "adClientPassword",
},
}},
{name: "unsupported cloud", isError: true, config: ScalerConfig{
TriggerMetadata: map[string]string{
"metricAggregationTimespan": "00:01", "metricAggregationType": "count", "metricId": "unittest/test", "targetValue": "10",
"applicationInsightsId": "appinsightid", "tenantId": "tenantid",
"cloud": "azureGermanCloud",
},
AuthParams: map[string]string{
"tenantId": "tenantId", "activeDirectoryClientId": "adClientId", "activeDirectoryClientPassword": "adClientPassword",
},
}},
}

func TestNewAzureAppInsightsScaler(t *testing.T) {
Expand Down