Skip to content

Commit

Permalink
Improve PreloadEmbeddedSchema performance (#1369)
Browse files Browse the repository at this point in the history
* Only compare the address for missing schemas on preload

* Improve MissingSchemas return value

* Replace fake constraint
  • Loading branch information
dbanck authored Aug 30, 2023
1 parent 39dcd16 commit d730e72
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
34 changes: 29 additions & 5 deletions internal/state/provider_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,39 @@ func (s *ProviderSchemaStore) AllSchemasExist(pvm map[tfaddr.Provider]version.Co
return true, nil
}

func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) (map[tfaddr.Provider]version.Constraints, error) {
missingSchemas := make(map[tfaddr.Provider]version.Constraints, 0)
for pAddr, pCons := range pvm {
exists, err := s.schemaExists(pAddr, pCons)
// MissingSchemas checks which schemas are missing in order to preload them from the bundled schemas.
// Since we don't know the version of a schema on disk before loading it, we assume it's
// good to just load it by address and ignore the version constraints.
func (s *ProviderSchemaStore) MissingSchemas(pvm map[tfaddr.Provider]version.Constraints) ([]tfaddr.Provider, error) {
missingSchemas := make([]tfaddr.Provider, 0)

for pAddr := range pvm {
if pAddr.IsLegacy() && pAddr.Type == "terraform" {
// The terraform provider is built into Terraform 0.11+
// and while it's possible, users typically don't declare
// entry in required_providers block for it.
pAddr = tfaddr.NewProvider(tfaddr.BuiltInProviderHost, tfaddr.BuiltInProviderNamespace, "terraform")
} else if pAddr.IsLegacy() {
// Since we use recent version of Terraform to generate
// embedded schemas, these will never contain legacy
// addresses.
//
// A legacy namespace may come from missing
// required_providers entry & implied requirement
// from the provider block or 0.12-style entry,
// such as { grafana = "1.0" }.
//
// Implying "hashicorp" namespace here mimics behaviour
// of all recent (0.14+) Terraform versions.
pAddr.Namespace = "hashicorp"
}

exists, err := s.schemaExists(pAddr, version.Constraints{})
if err != nil {
return nil, err
}
if !exists {
missingSchemas[pAddr] = pCons
missingSchemas = append(missingSchemas, pAddr)
}
}
return missingSchemas, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func PreloadEmbeddedSchema(ctx context.Context, logger *log.Logger, fs fs.ReadDi
return nil
}

for pAddr := range missingReqs {
for _, pAddr := range missingReqs {
err := preloadSchemaForProviderAddr(ctx, pAddr, fs, schemaStore, logger)
if err != nil {
return err
Expand Down

0 comments on commit d730e72

Please sign in to comment.