feat: add graceful shutdown option for apply&destroy#5
Conversation
5d66158 to
53d2b1d
Compare
| module github.com/hashicorp/terraform-exec | ||
|
|
||
| go 1.18 | ||
| go 1.20 |
There was a problem hiding this comment.
This is the least version supporting Cancel and WaitDelay: https://tip.golang.org/doc/go1.20#minor_library_changes
| return tf.runTerraformCmdInner(ctx, cmd, false /* enableGracefulShutdown */) | ||
| } | ||
|
|
||
| func (tf *Terraform) runTerraformCmdInner(ctx context.Context, cmd *exec.Cmd, enableGracefulShutdown bool) error { |
There was a problem hiding this comment.
Merged the runTerraformCmd implementation in cmd_default.go an cmd_linux.go into one.
All changes are commented below.
| func (tf *Terraform) runTerraformCmdInner(ctx context.Context, cmd *exec.Cmd, enableGracefulShutdown bool) error { | ||
| var errBuf strings.Builder | ||
|
|
||
| cmd.SysProcAttr = defaultSysProcAttr |
There was a problem hiding this comment.
Use different value for cmd.SysProcAttr due to the difference values for different platforms.
| if enableGracefulShutdown { | ||
| errStdoutCtx = context.WithoutCancel(ctx) | ||
| } |
There was a problem hiding this comment.
When graceful shutdown is enabled, do not cancel pipe copy. Otherwise graceful shutdown will fail due to broken pipe.
| } | ||
| } | ||
|
|
||
| func TestRunTerraformCmd(t *testing.T) { |
There was a problem hiding this comment.
This is also a merged from cmd_default_test.go and cmd_linux_test.go.
| if c.gracefulShutdownConfig.Enable { | ||
| return tf.runTerraformCmdWithGracefulShutdown(ctx, cmd) | ||
| } |
There was a problem hiding this comment.
Use the config value to decide whether to cancel pipe copy on context cancellation. Same for Destory.
The default behavior of CommandContext is to immediately kill the command process when receiving cancellation signal from the context, which may lead to corrupted terraform state.
This PR adds the option to allow sending interruption signal to the Apply/Destroy command and wait for a graceful period before forcefully killing the process. Note that the option doesn't work on Windows due to the lack of support of sending interrupt signal.
Test: all ut and e2e passed, verified the behavior with RWC CLI