mirror of
https://github.com/opentffoundation/opentf.git
synced 2025-12-19 17:59:05 -05:00
Most of our transformers are pure compute and so don't really have a strong need to generate trace spans under our current focus of only exposing user-facing concepts and external requests in our traces, but unfortunately some of them indirectly depend on provider schema, which in turn means that they can potentially be unlucky enough to be the trigger for making all of the provider requests needed to fill the schema cache and therefore would end up with provider request spans being reported beneath them. As usual with these interface updates, this initial change focuses only on changing the interface and updating its direct callers and implementers to match, without any further refactoring or attempts to plumb contexts to or from other functions that don't have them yet. That means there are a few new context.TODO() calls here that we'll tidy up in a later commit that hopefully won't involve all of the noise that is caused by changing an interface API. Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
95 lines
2.7 KiB
Go
95 lines
2.7 KiB
Go
// Copyright (c) The OpenTofu Authors
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package tofu
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/opentofu/opentofu/internal/configs"
|
|
"github.com/opentofu/opentofu/internal/dag"
|
|
)
|
|
|
|
const rootNodeName = "root"
|
|
|
|
// RootTransformer is a GraphTransformer that adds a root to the graph.
|
|
type RootTransformer struct{}
|
|
|
|
func (t *RootTransformer) Transform(_ context.Context, g *Graph) error {
|
|
addRootNodeToGraph(g)
|
|
return nil
|
|
}
|
|
|
|
// addRootNodeToGraph modifies the given graph in-place so that it has a root
|
|
// node if it didn't already have one and so that any other node which doesn't
|
|
// already depend on something will depend on that root node.
|
|
//
|
|
// After this function returns, the graph will have only one node that doesn't
|
|
// depend on any other nodes.
|
|
func addRootNodeToGraph(g *Graph) {
|
|
// We always add the root node. This is a singleton so if it's already
|
|
// in the graph this will do nothing and just retain the existing root node.
|
|
//
|
|
// Note that rootNode is intentionally added by value and not by pointer
|
|
// so that all root nodes will be equal to one another and therefore
|
|
// coalesce when two valid graphs get merged together into a single graph.
|
|
g.Add(rootNode)
|
|
|
|
// Everything that doesn't already depend on at least one other node will
|
|
// depend on the root node, except the root node itself.
|
|
for _, v := range g.Vertices() {
|
|
if v == dag.Vertex(rootNode) {
|
|
continue
|
|
}
|
|
|
|
if g.UpEdges(v).Len() == 0 {
|
|
g.Connect(dag.BasicEdge(rootNode, v))
|
|
}
|
|
}
|
|
}
|
|
|
|
type graphNodeRoot struct{}
|
|
|
|
// rootNode is the singleton value representing all root graph nodes.
|
|
//
|
|
// The root node for all graphs should be this value directly, and in particular
|
|
// _not_ a pointer to this value. Using the value directly here means that
|
|
// multiple root nodes will always coalesce together when subsuming one graph
|
|
// into another.
|
|
var rootNode graphNodeRoot
|
|
|
|
func (n graphNodeRoot) Name() string {
|
|
return rootNodeName
|
|
}
|
|
|
|
// CloseRootModuleTransformer is a GraphTransformer that adds a root to the graph.
|
|
type CloseRootModuleTransformer struct {
|
|
RootConfig *configs.Config
|
|
}
|
|
|
|
func (t *CloseRootModuleTransformer) Transform(_ context.Context, g *Graph) error {
|
|
// close the root module
|
|
closeRoot := &nodeCloseModule{
|
|
RootConfig: t.RootConfig,
|
|
}
|
|
g.Add(closeRoot)
|
|
|
|
// since this is closing the root module, make it depend on everything in
|
|
// the root module.
|
|
for _, v := range g.Vertices() {
|
|
if v == closeRoot {
|
|
continue
|
|
}
|
|
|
|
// since this is closing the root module, and must be last, we can
|
|
// connect to anything that doesn't have any up edges.
|
|
if g.UpEdges(v).Len() == 0 {
|
|
g.Connect(dag.BasicEdge(closeRoot, v))
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|