Fix global schema caching (#954)

Signed-off-by: Dmitry Kisler <admin@dkisler.com>
This commit is contained in:
Dmitry Kisler 2023-12-01 15:09:40 +01:00 committed by GitHub
parent 478a6ecf81
commit 0d6a763a74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 8 deletions

View File

@ -81,9 +81,9 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
// check the global cache
if !p.Addr.IsZero() {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok {
if schemaCached, ok := providers.SchemaCache.Get(p.Addr); ok && schemaCached.ServerCapabilities.GetProviderSchemaOptional {
logger.Trace("GRPCProvider: GetProviderSchema: serving from global schema cache", "address", p.Addr)
return resp
return schemaCached
}
}
@ -143,7 +143,7 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
}
// set the global cache if we can
if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional {
if !p.Addr.IsZero() {
providers.SchemaCache.Set(p.Addr, resp)
}

View File

@ -81,9 +81,9 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
// check the global cache
if !p.Addr.IsZero() {
if resp, ok := providers.SchemaCache.Get(p.Addr); ok {
if schemaCached, ok := providers.SchemaCache.Get(p.Addr); ok && schemaCached.ServerCapabilities.GetProviderSchemaOptional {
logger.Trace("GRPCProvider: GetProviderSchema: serving from global schema cache", "address", p.Addr)
return resp
return schemaCached
}
}
@ -143,7 +143,7 @@ func (p *GRPCProvider) GetProviderSchema() (resp providers.GetProviderSchemaResp
}
// set the global cache if we can
if !p.Addr.IsZero() && resp.ServerCapabilities.GetProviderSchemaOptional {
if !p.Addr.IsZero() {
providers.SchemaCache.Set(p.Addr, resp)
}

View File

@ -123,6 +123,8 @@ type ServerCapabilities struct {
// provider does not require calling GetProviderSchema to operate
// normally, and the caller can used a cached copy of the provider's
// schema.
// In other words, the providers for which GetProviderSchemaOptional is false
// require their schema to be read after EVERY instantiation to function normally.
GetProviderSchemaOptional bool
}

View File

@ -66,8 +66,6 @@ func (cp *contextPlugins) NewProvisionerInstance(typ string) (provisioners.Inter
// to repeatedly call this method with the same address if various different
// parts of OpenTofu all need the same schema information.
func (cp *contextPlugins) ProviderSchema(addr addrs.Provider) (providers.ProviderSchema, error) {
log.Printf("[TRACE] tofu.contextPlugins: Initializing provider %q to read its schema", addr)
// Check the global schema cache first.
// This cache is only written by the provider client, and transparently
// used by GetProviderSchema, but we check it here because at this point we
@ -78,6 +76,7 @@ func (cp *contextPlugins) ProviderSchema(addr addrs.Provider) (providers.Provide
return schemas, nil
}
log.Printf("[TRACE] tofu.contextPlugins: Initializing provider %q to read its schema", addr)
provider, err := cp.NewProviderInstance(addr)
if err != nil {
return schemas, fmt.Errorf("failed to instantiate provider %q to obtain schema: %w", addr, err)