Skip to content

Commit

Permalink
prune unused resources from apply
Browse files Browse the repository at this point in the history
If a resource is only destroying instances, there is no reason to
prepare the state and we can remove the Resource (prepare state) nodes.
They normally have pose no issue, but if the instances are being
destroyed along with their dependencies, the resource node may fail to
evaluate due to the missing dependencies (since destroy happens in the
reverse order).

These failures were previously blocked by there being a cycle when the
destroy nodes were directly attached to the resource nodes.
  • Loading branch information
jbardin committed Oct 24, 2019
1 parent 7108560 commit f766bb8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions terraform/transform_destroy_edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,47 @@ func (t *DestroyEdgeTransformer) Transform(g *Graph) error {
}
}

return t.pruneResources(g)
}

// If there are only destroy instances for a particular resource, there's no
// reason for the resource node to prepare the state. Remove Resource nodes so
// that they don't fail by trying to evaluate a resource that is only being
// destroyed along with its dependencies.
func (t *DestroyEdgeTransformer) pruneResources(g *Graph) error {
for _, v := range g.Vertices() {
n, ok := v.(*NodeApplyableResource)
if !ok {
continue
}

// if there are only destroy dependencies, we don't need this node
des, err := g.Descendents(n)
if err != nil {
return err
}

descendents := des.List()
nonDestroyInstanceFound := false
for _, v := range descendents {
if _, ok := v.(*NodeApplyableResourceInstance); ok {
nonDestroyInstanceFound = true
break
}
}

if nonDestroyInstanceFound {
continue
}

// connect all the through-edges, then delete the node
for _, d := range g.DownEdges(n).List() {
for _, u := range g.UpEdges(n).List() {
g.Connect(dag.BasicEdge(u, d))
}
}
log.Printf("DestroyEdgeTransformer: pruning unused resource node %s", dag.VertexName(n))
g.Remove(n)
}
return nil
}

0 comments on commit f766bb8

Please sign in to comment.