Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wire ctx into plugin hooks #5039

Merged
merged 1 commit into from Apr 29, 2024
Merged

Conversation

Benehiko
Copy link
Member

In the spirit of wiring up context.Context for graceful exit and early termination of tasks, I have done some more wiring up of ctx inside the plugin hooks. This allows early returns for when the ctx is cancelled so that hooks aren't printed as well as cancelling the execution of the plugin through exec.CommandContext.

- What I did
Allow early cancellation of plugin hooks through context.Context and plugin execution cancellation through exec.CommandContext.

- How I did it
Wired up ctx inside the RunCLICommandHooks and RunPluginHooks.

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 0% with 16 lines in your changes are missing coverage. Please review.

Project coverage is 61.07%. Comparing base (7f15dfa) to head (1d666b4).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5039      +/-   ##
==========================================
- Coverage   61.08%   61.07%   -0.02%     
==========================================
  Files         295      295              
  Lines       20660    20664       +4     
==========================================
  Hits        12621    12621              
- Misses       7142     7146       +4     
  Partials      897      897              

commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
flags := getCommandFlags(subCommand)

runHooks(dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
runHooks(ctx, dockerCli, rootCmd, subCommand, commandName, flags, cmdErrorMessage)
Copy link
Contributor

Choose a reason for hiding this comment

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

nothing to change now, but these signatures are getting a bit rough 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah I agree, but I didn't want to refactor it in this PR 😓

Copy link
Member

@laurazard laurazard left a comment

Choose a reason for hiding this comment

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

LGTM

@laurazard
Copy link
Member

Looks great! (and reminds me I'd really like for us to add some e2e tests for hooks when we get a chance 🥲)

@Benehiko
Copy link
Member Author

Looks great! (and reminds me I'd really like for us to add some e2e tests for hooks when we get a chance 🥲)

oh yeah, saw this while adding these. I can do that in another PR once I have some time. I have a plan to utilize a pattern I used inside DD which re-uses the go test library to simulate external executables.

@Benehiko Benehiko merged commit 67aa271 into docker:master Apr 29, 2024
91 checks passed
@laurazard
Copy link
Member

We already have a bunch of "test" plugins in this repo that get compiled for end to end tests, so give those a look too! I don't know how the simulating external
binaries stuff works, but there's a lot of complexity with the plugins whether it's discovering them on the file system, pgid/signal changes, etc. that I'd be worried about capturing in e2e tests.

I guess what I mean is for e2e tests I'd prefer actual plugin binaries, but also we could/should look into other approaches for unit/integration tests.

@Benehiko Benehiko deleted the hooks-ctx-wiring branch April 29, 2024 12:57
@thaJeztah thaJeztah added this to the 27.0.0 milestone Apr 30, 2024
Comment on lines +31 to 33
func RunCLICommandHooks(ctx context.Context, dockerCli command.Cli, rootCmd, subCommand *cobra.Command, cmdErrorMessage string) {
commandName := strings.TrimPrefix(subCommand.CommandPath(), rootCmd.Name()+" ")
flags := getCommandFlags(subCommand)
Copy link
Member

Choose a reason for hiding this comment

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

@Benehiko @laurazard Sorry, late to the party, and not 100% sure what's best (things get somewhat confusing 😅)

Which context should be used in these cases? I see we already have the rootCmd and subCommand passed; both of which carry a context; should that context be the one to use? (rootCmd.Context() or subCommand.Context())

(If that's the one to use, then possibly the signature wouldn't have to be changed 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

that's true, although adding the ctx parameter is way more explicit and the recommended way according to the Go documentation. The context stored inside the rootCommand and subCommand is the same as the one passed down. While I was looking at this I actually wanted to refactor the signature into an interface or using only the docker.Cli interface since passing down rootCmd and subCommand seems superfluous. But this is a topic for another time I think

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

Successfully merging this pull request may close these issues.

None yet

5 participants