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

Improve PreloadEmbeddedSchema performance #1369

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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