2016-09-21 19:55:07 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2016-12-02 15:46:42 +01:00
|
|
|
"fmt"
|
2016-09-21 19:55:07 +02:00
|
|
|
"log"
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
"github.com/hashicorp/terraform/configs"
|
2016-09-22 01:37:41 +02:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
"github.com/hashicorp/terraform/states"
|
2016-09-21 19:55:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GraphNodeDestroyerCBD must be implemented by nodes that might be
|
2018-09-22 02:08:52 +02:00
|
|
|
// create-before-destroy destroyers, or might plan a create-before-destroy
|
|
|
|
// action.
|
2016-09-21 19:55:07 +02:00
|
|
|
type GraphNodeDestroyerCBD interface {
|
|
|
|
// CreateBeforeDestroy returns true if this node represents a node
|
|
|
|
// that is doing a CBD.
|
|
|
|
CreateBeforeDestroy() bool
|
2016-12-02 15:46:42 +01:00
|
|
|
|
|
|
|
// ModifyCreateBeforeDestroy is called when the CBD state of a node
|
|
|
|
// is changed dynamically. This can return an error if this isn't
|
|
|
|
// allowed.
|
|
|
|
ModifyCreateBeforeDestroy(bool) error
|
2016-09-21 19:55:07 +02:00
|
|
|
}
|
|
|
|
|
2018-06-01 15:13:14 +02:00
|
|
|
// GraphNodeAttachDestroyer is implemented by applyable nodes that have a
|
|
|
|
// companion destroy node. This allows the creation node to look up the status
|
|
|
|
// of the destroy node and determine if it needs to depose the existing state,
|
|
|
|
// or replace it.
|
|
|
|
// If a node is not marked as create-before-destroy in the configuration, but a
|
|
|
|
// dependency forces that status, only the destroy node will be aware of that
|
|
|
|
// status.
|
|
|
|
type GraphNodeAttachDestroyer interface {
|
|
|
|
// AttachDestroyNode takes a destroy node and saves a reference to that
|
|
|
|
// node in the receiver, so it can later check the status of
|
|
|
|
// CreateBeforeDestroy().
|
|
|
|
AttachDestroyNode(n GraphNodeDestroyerCBD)
|
|
|
|
}
|
|
|
|
|
2018-09-22 02:08:52 +02:00
|
|
|
// ForcedCBDTransformer detects when a particular CBD-able graph node has
|
|
|
|
// dependencies with another that has create_before_destroy set that require
|
|
|
|
// it to be forced on, and forces it on.
|
|
|
|
//
|
|
|
|
// This must be used in the plan graph builder to ensure that
|
|
|
|
// create_before_destroy settings are properly propagated before constructing
|
|
|
|
// the planned changes. This requires that the plannable resource nodes
|
|
|
|
// implement GraphNodeDestroyerCBD.
|
|
|
|
type ForcedCBDTransformer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ForcedCBDTransformer) Transform(g *Graph) error {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
dn, ok := v.(GraphNodeDestroyerCBD)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !dn.CreateBeforeDestroy() {
|
|
|
|
// If there are no CBD decendent (dependent nodes), then we
|
|
|
|
// do nothing here.
|
|
|
|
if !t.hasCBDDescendent(g, v) {
|
2018-09-26 01:22:19 +02:00
|
|
|
log.Printf("[TRACE] ForcedCBDTransformer: %q (%T) has no CBD descendent, so skipping", dag.VertexName(v), v)
|
2018-09-22 02:08:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:22:19 +02:00
|
|
|
// If this isn't naturally a CBD node, this means that an descendent is
|
2018-09-22 02:08:52 +02:00
|
|
|
// and we need to auto-upgrade this node to CBD. We do this because
|
|
|
|
// a CBD node depending on non-CBD will result in cycles. To avoid this,
|
|
|
|
// we always attempt to upgrade it.
|
|
|
|
log.Printf("[TRACE] ForcedCBDTransformer: forcing create_before_destroy on for %q (%T)", dag.VertexName(v), v)
|
|
|
|
if err := dn.ModifyCreateBeforeDestroy(true); err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s: must have create before destroy enabled because "+
|
|
|
|
"a dependent resource has CBD enabled. However, when "+
|
|
|
|
"attempting to automatically do this, an error occurred: %s",
|
|
|
|
dag.VertexName(v), err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Printf("[TRACE] ForcedCBDTransformer: %q (%T) already has create_before_destroy set", dag.VertexName(v), v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:22:19 +02:00
|
|
|
// hasCBDDescendent returns true if any descendent (node that depends on this)
|
2018-09-22 02:08:52 +02:00
|
|
|
// has CBD set.
|
|
|
|
func (t *ForcedCBDTransformer) hasCBDDescendent(g *Graph, v dag.Vertex) bool {
|
|
|
|
s, _ := g.Descendents(v)
|
|
|
|
if s == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-07 23:49:34 +01:00
|
|
|
for _, ov := range s {
|
2018-09-22 02:08:52 +02:00
|
|
|
dn, ok := ov.(GraphNodeDestroyerCBD)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if dn.CreateBeforeDestroy() {
|
|
|
|
// some descendent is CreateBeforeDestroy, so we need to follow suit
|
|
|
|
log.Printf("[TRACE] ForcedCBDTransformer: %q has CBD descendent %q", dag.VertexName(v), dag.VertexName(ov))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-21 19:55:07 +02:00
|
|
|
// CBDEdgeTransformer modifies the edges of CBD nodes that went through
|
|
|
|
// the DestroyEdgeTransformer to have the right dependencies. There are
|
|
|
|
// two real tasks here:
|
|
|
|
//
|
|
|
|
// 1. With CBD, the destroy edge is inverted: the destroy depends on
|
|
|
|
// the creation.
|
|
|
|
//
|
|
|
|
// 2. A_d must depend on resources that depend on A. This is to enable
|
|
|
|
// the destroy to only happen once nodes that depend on A successfully
|
|
|
|
// update to A. Example: adding a web server updates the load balancer
|
|
|
|
// before deleting the old web server.
|
|
|
|
//
|
2018-09-22 02:08:52 +02:00
|
|
|
// This transformer requires that a previous transformer has already forced
|
|
|
|
// create_before_destroy on for nodes that are depended on by explicit CBD
|
|
|
|
// nodes. This is the logic in ForcedCBDTransformer, though in practice we
|
|
|
|
// will get here by recording the CBD-ness of each change in the plan during
|
|
|
|
// the plan walk and then forcing the nodes into the appropriate setting during
|
|
|
|
// DiffTransformer when building the apply graph.
|
2016-09-21 19:55:07 +02:00
|
|
|
type CBDEdgeTransformer struct {
|
|
|
|
// Module and State are only needed to look up dependencies in
|
|
|
|
// any way possible. Either can be nil if not availabile.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
Config *configs.Config
|
terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.
The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.
The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.
Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-08-14 23:24:45 +02:00
|
|
|
State *states.State
|
2018-05-10 02:05:18 +02:00
|
|
|
|
2018-05-31 21:39:45 +02:00
|
|
|
// If configuration is present then Schemas is required in order to
|
|
|
|
// obtain schema information from providers and provisioners so we can
|
|
|
|
// properly resolve implicit dependencies.
|
|
|
|
Schemas *Schemas
|
2016-09-21 19:55:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *CBDEdgeTransformer) Transform(g *Graph) error {
|
|
|
|
// Go through and reverse any destroy edges
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
dn, ok := v.(GraphNodeDestroyerCBD)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-19 23:31:38 +01:00
|
|
|
if _, ok = v.(GraphNodeDestroyer); !ok {
|
2018-09-22 02:08:52 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-09-21 19:55:07 +02:00
|
|
|
|
|
|
|
if !dn.CreateBeforeDestroy() {
|
2018-09-22 02:08:52 +02:00
|
|
|
continue
|
2016-09-21 19:55:07 +02:00
|
|
|
}
|
|
|
|
|
2019-11-17 15:56:44 +01:00
|
|
|
// Find the resource edges
|
2016-09-21 20:33:47 +02:00
|
|
|
for _, e := range g.EdgesTo(v) {
|
2019-11-19 23:31:38 +01:00
|
|
|
src := e.Source()
|
|
|
|
|
|
|
|
// If source is a create node, invert the edge.
|
|
|
|
// This covers both the node's own creator, as well as reversing
|
|
|
|
// any dependants' edges.
|
|
|
|
if _, ok := src.(GraphNodeCreator); ok {
|
|
|
|
log.Printf("[TRACE] CBDEdgeTransformer: reversing edge %s -> %s", dag.VertexName(src), dag.VertexName(v))
|
|
|
|
g.RemoveEdge(e)
|
|
|
|
g.Connect(dag.BasicEdge(v, src))
|
2016-09-21 19:55:07 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-22 01:37:41 +02:00
|
|
|
}
|
2016-09-21 19:55:07 +02:00
|
|
|
return nil
|
|
|
|
}
|