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

refactor: move equinix-sdk-go Metal client setup into the config package #563

Merged
merged 3 commits into from
Feb 14, 2024

Conversation

ctreatma
Copy link
Contributor

@ctreatma ctreatma commented Feb 7, 2024

When we need to use a Metal API client inside a terraform resource or datasource, we need to ensure that the client is instrumented correctly. Previously this was done by repeating the same function calls in every CRUD function for every resource and datasource; as a result we were often missing instrumentation, and there was a high risk of losing instrumentation when converting from the old packngo SDK to equinix-sdk-go.

This refactors the equinix-sdk-go client setup code to better ensure that we get a correctly-configured client in all situations and help ensure that we maintain visibility into usage after doing SDK conversions.

@codecov-commenter
Copy link

codecov-commenter commented Feb 7, 2024

Codecov Report

Attention: 219 lines in your changes are missing coverage. Please review.

Comparison is base (a2797d8) 63.11% compared to head (c6efeb5) 55.69%.
Report is 52 commits behind head on main.

Files Patch % Lines
equinix/resource_fabric_network.go 40.62% 209 Missing ⚠️
equinix/data_source_fabric_network.go 84.00% 4 Missing ⚠️
...ternal/resources/metal/project_ssh_key/resource.go 73.33% 3 Missing and 1 partial ⚠️
internal/datalist/schema.go 0.00% 1 Missing ⚠️
internal/resources/metal/ssh_key/resource.go 92.30% 0 Missing and 1 partial ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #563      +/-   ##
==========================================
- Coverage   63.11%   55.69%   -7.43%     
==========================================
  Files          94       96       +2     
  Lines       18250    18613     +363     
==========================================
- Hits        11519    10367    -1152     
- Misses       6299     7919    +1620     
+ Partials      432      327     -105     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ctreatma ctreatma requested a deployment to internal February 7, 2024 19:59 — with GitHub Actions Abandoned
// NewMetalGoClient returns a new metal-go client for accessing Equinix Metal's API.
func (c *Config) NewMetalGoClient() *metalv1.APIClient {
func (c *Config) NewMetalClientForSDK(d *schema.ResourceData) *metalv1.APIClient {
client := c.newMetalClient()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note this means we're setting up a new client every time, which should avoid coordination issues for User-Agent modifications. If we're OK with creating a bunch of separate clients, we could also use this pattern for other clients and avoid the occasional concurrent map writes panics we've seen (#286, #403).

Copy link
Contributor

@cprivitere cprivitere Feb 8, 2024

Choose a reason for hiding this comment

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

I don't understand what the downside would be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not totally clear on the overhead of creating these separate clients. There's the potential for increased resource utilization, but we're still reusing the http.DefaultTransport across all clients so I think any increase should be negligible.

Copy link
Member

Choose a reason for hiding this comment

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

Shared http.DefaultTransport looks good for HTTP KeepAlive and HTTP2 Stream, TCP level concerns. The Go/memory level only introduces a few extra bytes of strings. At the application level we may run into rate limiting response handling problems if each client object is queueing up retries independently. I'm not overly concerned about this today because the API does not return 429s with RetryAfter headers.

internal/config/config.go Outdated Show resolved Hide resolved
Copy link
Contributor

@cprivitere cprivitere left a comment

Choose a reason for hiding this comment

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

With the exception of my comment above, this all seems reasonable to me. I would still like to see @displague or @ocobles sign off on it too.

@ctreatma ctreatma force-pushed the lazy-client-creation branch from 7b6c26d to 6f0e504 Compare February 8, 2024 20:27
@ctreatma ctreatma force-pushed the lazy-client-creation branch from 6f0e504 to ecad4a2 Compare February 12, 2024 18:35
@ctreatma ctreatma requested a deployment to internal February 12, 2024 20:25 — with GitHub Actions Abandoned
@@ -24,7 +24,7 @@ func dataSourceMetalPlans() *schema.Resource {
return datalist.NewResource(dataListConfig)
}

func getPlans(meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
func getPlans(_ *schema.ResourceData, meta interface{}, extra map[string]interface{}) ([]interface{}, error) {
Copy link
Member

Choose a reason for hiding this comment

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

You showed great restraint in not converting this from Packngo to the new SDK.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

usher

@ctreatma ctreatma merged commit b910a66 into main Feb 14, 2024
4 of 5 checks passed
@ctreatma ctreatma deleted the lazy-client-creation branch February 14, 2024 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants