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 1 commit
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
34 changes: 22 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,27 @@ 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
Cloud string
JorTurFer marked this conversation as resolved.
Show resolved Hide resolved
AppInsightsResourceURL string
ActiveDirectoryEndpoint string
}

type ApplicationInsightsMetric struct {
Expand Down Expand Up @@ -55,12 +64,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 +125,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
28 changes: 28 additions & 0 deletions pkg/scalers/azure_app_insights_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"

az "github.com/Azure/go-autorest/autorest/azure"
zroubalik marked this conversation as resolved.
Show resolved Hide resolved
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
"github.com/kedacore/keda/v2/pkg/scalers/azure"
kedautil "github.com/kedacore/keda/v2/pkg/util"
Expand Down Expand Up @@ -97,6 +98,33 @@ func parseAzureAppInsightsMetadata(config *ScalerConfig) (*azureAppInsightsMetad
meta.azureAppInsightsInfo.Filter = ""
}

meta.azureAppInsightsInfo.Cloud = azure.DefaultCloud
meta.azureAppInsightsInfo.AppInsightsResourceURL = azure.DefaultAppInsightsResourceURL

if cloud, ok := config.TriggerMetadata["cloud"]; ok {
meta.azureAppInsightsInfo.Cloud = cloud
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
4 changes: 2 additions & 2 deletions pkg/scalers/datadog_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func newDatadogConnection(ctx context.Context, meta *datadogMetadata, config *Sc
configuration.HTTPClient = kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, false)
apiClient := datadog.NewAPIClient(configuration)

_, _, err := apiClient.AuthenticationApi.Validate(ctx) //nolint:bodyclose
v-shenoy marked this conversation as resolved.
Show resolved Hide resolved
_, _, err := apiClient.AuthenticationApi.Validate(ctx)
if err != nil {
return nil, fmt.Errorf("error connecting to Datadog API endpoint: %v", err)
}
Expand Down Expand Up @@ -236,7 +236,7 @@ func (s *datadogScaler) getQueryResult(ctx context.Context) (float64, error) {
"site": s.metadata.datadogSite,
})

resp, r, err := s.apiClient.MetricsApi.QueryMetrics(ctx, time.Now().Unix()-int64(s.metadata.age), time.Now().Unix(), s.metadata.query) //nolint:bodyclose
v-shenoy marked this conversation as resolved.
Show resolved Hide resolved
resp, r, err := s.apiClient.MetricsApi.QueryMetrics(ctx, time.Now().Unix()-int64(s.metadata.age), time.Now().Unix(), s.metadata.query)
if err != nil {
return -1, fmt.Errorf("error when retrieving Datadog metrics: %s", err)
}
Expand Down