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

cache resource providers to reduce expensive api call #20050

Conversation

ms-henglu
Copy link
Contributor

When running azurerm provider initialize, there're two GET https://management.azure.com/subscriptions/{subscription_id}/providers?api-version=2016-02-01 calls, which are very time consuming, each can take about 7 seconds.

The two API calls are used to ensure resource providers are registered and enhance resource provider's name validation.

This PR cached the results of the first request, so even with enabling the above 2 features, there'll only be one API request.

After:
Run terraform plan command with service principal authentication

Executed in    9.19 secs 

There's only one /providers API call
image

Before:
Run terraform plan command with service principal authentication

Executed in    16.01 secs 

There're two /providers API call
image

Copy link
Contributor

@tombuildsstuff tombuildsstuff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @ms-henglu

Thanks for this PR - I've taken a look through and left some comments inline to make this consistent with how caching for the Locations are done, if we can make those changes so this becomes consistent then we should be able to get this merged.

Thanks!

internal/resourceproviders/cache.go Show resolved Hide resolved
internal/clients/builder.go Outdated Show resolved Hide resolved
internal/resourceproviders/validation_test.go Show resolved Hide resolved
internal/clients/builder.go Outdated Show resolved Hide resolved
@@ -416,14 +416,13 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc {
if !skipProviderRegistration {
// List all the available providers and their registration state to avoid unnecessary
// requests. This also lets us check if the provider credentials are correct.
providerList, err := client.Resource.ProvidersClient.List(ctx, nil, "")
availableResourceProviders, err := resourceproviders.CacheSupportedProviders(ctx, client.Resource.ProvidersClient)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use the same approach we do for Cached Locations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, but the location cache is part of the go-azure-helper, I can make another PR for it, but I think it will be a breaking change of go-azure-helper.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds like the place for it to be, How come it would be a breaking change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @katbyte , not sure I followed, do you mean that we should move the resource provider cache logic to go-azure-helper?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes? as tom suggested we should follow the same approach we used for location

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The move-cache-to-go-azure-helper has been made here: hashicorp/go-azure-helpers#158. Please help review that and after that appreciate ms-henglu could continue here. @tombuildsstuff @katbyte

@ms-henglu ms-henglu changed the title cache resource providers to reduce expensive api call [wip] cache resource providers to reduce expensive api call Jan 29, 2023
@github-actions github-actions bot added size/M and removed size/S labels Jan 30, 2023
@ms-henglu
Copy link
Contributor Author

Hi @tombuildsstuff ,

Thanks for the code review!

I changed the CacheSupportedProviders to CachedSupportedProviders, so it could not only be caching the resource providers, but returning them if they're already cached and caching them if not cached yet. The cached resource providers only contain the name and registration state, I've removed the raw http response.

Would you please take another look? thanks!

@ms-henglu ms-henglu changed the title [wip] cache resource providers to reduce expensive api call cache resource providers to reduce expensive api call Jan 30, 2023
@tombuildsstuff tombuildsstuff self-assigned this May 4, 2023
tombuildsstuff added a commit that referenced this pull request May 8, 2023
…API Version `2022-09-01`

This PR supersedes both #20050 and
hashicorp/go-azure-helpers#158 by refactoring this to use a cache
and then migrating this across to `hashicorp/go-azure-sdk`, since this needs to be migrated away
from using `Azure/azure-sdk-for-go` anyway.
@tombuildsstuff
Copy link
Contributor

hey @ms-henglu

Apologies for the delayed reply to this - it's taken a little time to review the PR's on hashicorp/go-azure-helpers (and so to get to this PR).

I spent some time digging into this one last week, and whilst this PR looks good - in retrospect it appears that we're actually going to need to move this logic from hashicorp/go-azure-helpers fully into the provider.

Whilst having this located within hashicorp/go-azure-helpers makes sense at the moment (since this is shared between AzureRM and Azure Stack) - leaving this within that repository means that we'll be unable to migrate this across to using hashicorp/go-azure-sdk (without a circular reference), so unfortunately this is actually going to need to live elsewhere.

Because this logic needs to be migrated across to using hashicorp/go-azure-sdk too, I hope you don't mind but I've opened #21695 which both moves Resource Provider Registration into this repository, adds this cache, migrates this across to using hashicorp/go-azure-sdk at API Version 2022-09-01 (from 2016-02-01) and fixes a bug spotted during provider registration. There's an open question as to whether that logic belongs in this Provider or in hashicorp/go-azure-sdk (so that the Azure Stack provider can reuse this logic), but for now moving this into the Provider seems reasonable.

As such whilst I'd like to thank you for this contribution, I'm going to close this PR in favour of #21695 which implements this functionality using hashicorp/go-azure-sdk.

Thanks!

Copy link

github-actions bot commented Jun 1, 2024

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants