Skip to content

feat: add graceful shutdown option for apply&destroy#5

Merged
wjf3121 merged 1 commit intomainfrom
junfeng/gracefulshutdown
May 13, 2024
Merged

feat: add graceful shutdown option for apply&destroy#5
wjf3121 merged 1 commit intomainfrom
junfeng/gracefulshutdown

Conversation

@wjf3121
Copy link
Copy Markdown

@wjf3121 wjf3121 commented May 13, 2024

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

@wjf3121 wjf3121 force-pushed the junfeng/gracefulshutdown branch from 5d66158 to 53d2b1d Compare May 13, 2024 08:35
module github.com/hashicorp/terraform-exec

go 1.18
go 1.20
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return tf.runTerraformCmdInner(ctx, cmd, false /* enableGracefulShutdown */)
}

func (tf *Terraform) runTerraformCmdInner(ctx context.Context, cmd *exec.Cmd, enableGracefulShutdown bool) error {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use different value for cmd.SysProcAttr due to the difference values for different platforms.

Comment on lines +363 to +365
if enableGracefulShutdown {
errStdoutCtx = context.WithoutCancel(ctx)
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When graceful shutdown is enabled, do not cancel pipe copy. Otherwise graceful shutdown will fail due to broken pipe.

}
}

func TestRunTerraformCmd(t *testing.T) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a merged from cmd_default_test.go and cmd_linux_test.go.

Comment on lines +158 to +160
if c.gracefulShutdownConfig.Enable {
return tf.runTerraformCmdWithGracefulShutdown(ctx, cmd)
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the config value to decide whether to cancel pipe copy on context cancellation. Same for Destory.

Copy link
Copy Markdown

@Gogomoe Gogomoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants