hashicorp / terraform-provider-google

Terraform Provider for Google Cloud Platform
https://registry.terraform.io/providers/hashicorp/google/latest/docs
Mozilla Public License 2.0
2.36k stars 1.75k forks source link

Clarify usage of `context` in SDK provider configuration logic, especially `GetCredentials` logic, #20279

Open SarahFrench opened 1 week ago

SarahFrench commented 1 week ago

Description

The use of context in the provider is a bit confusing due to:

  1. using a mixture of ctx passed into functions, context.Background, context.TODO(), and c.Context stored on the Config struct
  2. using some SDK methods related to context that are deprecated (schema.StopContext).

1) Context usage in func (c *Config) GetCredentials

Currently in func (c *Config) GetCredentials, where provider arguments are used to get a googleoauth.Credentials struct:

c.Context is used in 2 places:

context.Background is used in 2 places:

context.TODO() is used in 3 places:

We may want to revisit this and clarify what context should be used where. We might want to use the context passed via the Config struct, or we might want to update functions like getTokenSource and GetCredentials to accept a ctx argument.

If we want to use the context passed via the Config struct we should check our usage of schema.StopContext, see below.

2) Usage of deprecated schema.StopContext

func schema.StopContext(ctx context.Context) (context.Context, bool) StopContext returns a context safe for global use that will cancel when Terraform requests a stop. This function should only be called within a ConfigureContextFunc, passing in the request scoped context received in that method.

Deprecated: The use of a global context is discouraged. Please use the new context aware CRUD methods.

We use schema.StopContext in func ProviderConfigure to make a 'stop context'. This context is passed into functions that lead up to handling credentials and configuring the client (see supporting info below).

Should schema.StopContext be removed? How would that affect configuration logic using that context?

Supporting info

Info about configuring the (SDK) provider; what leads up to using func (c *Config) GetCredentials?

How TF core tells the provider to configure itself:

What happens inside ProviderConfigure:

What happens inside func (c *Config) LoadAndValidate:

New or Affected Resource(s)

N/A

Potential Terraform Configuration

N/A

References

No response

SarahFrench commented 1 week ago

Note: One of the potential causes of context-related errors in the plugin-framework implementation of configuration logic could be because the Context stored on the framework equivalent of the Config struct isn't a stop context..

Also, GetTokenSource and GetCredentials in the plugin-framework implementation passed context through as function arguments, versus what is described above in this issue for the SDK code.

GH issues related to context and the plugin-framework code:

SarahFrench commented 1 week ago

Labelled with plugin-framework as this was potentially a factor in some auth differences between the SDK and PF implementations of provider configuration logic; would be useful to know about in future