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

Use context correctly #360

Merged
merged 3 commits into from
Nov 18, 2021
Merged

Use context correctly #360

merged 3 commits into from
Nov 18, 2021

Conversation

squeedee
Copy link
Member


name: Pull request
about: Submit changes to the code


Changes proposed by this PR

  • Use reconciler context in subsequent async calls, all the way to the client.
  • Remove redundant WithValues from context based logging

Release Note

PR Checklist

Note: Please do not remove items. Mark items as done [x] or use strikethrough if you believe they are not relevant

  • [ ] Linked to a relevant issue. Eg: Fixes #123 or Updates #123
  • Removed non-atomic or wip commits
  • [ ] Filled in the Release Note section above
  • [ ] Modified the docs to match changes

@netlify
Copy link

netlify bot commented Nov 18, 2021

✔️ Deploy Preview for elated-stonebraker-105904 canceled.

🔨 Explore the source changes: 67d8796

🔍 Inspect the deploy log: https://app.netlify.com/sites/elated-stonebraker-105904/deploys/6196976325770900073b2ec4

Rasheed Abdul-Aziz and others added 3 commits November 18, 2021 12:56
- at some point controller-runtime started doing this for us

Co-authored-by: Emily Johnson <emjohnson@vmware.com>
@squeedee squeedee force-pushed the use-context-correctly branch from f00efb2 to 67d8796 Compare November 18, 2021 18:11
@cirocosta
Copy link
Contributor

cirocosta commented Nov 18, 2021

yayy nice! that's a lot of changes 😅 thanks for putting the work! overall, lgtm!


trying to follow the chain of ctx being propagated through the code, one thing that I noticed that seems a little off is how we pass from cmd/main.go down to pkg/root:

  • here we're passing it via a struct field

  • here we're consuming from the struct

if err := mgr.Start(cmd.Context); err != nil {
return fmt.Errorf("manager start: %w", err)

that's not very usual, and discouraged - the idiomatic way of doing doing would be to pass it via the function instead, being the first argument of the list.

it's not something introduced in this PR, but, sounds somewhat appropriate to patch given the context (no pun intended) 😁 wdyt?

thx!

@cirocosta
Copy link
Contributor

oh also, here

if err := registrar.IndexResources(mgr, cmd.Context); err != nil {
return fmt.Errorf("index resources: %w", err)
}

we should make ctx the first argument instead.

@cirocosta
Copy link
Contributor

sorry, one more 😅 we're doing the right thing creating a context with an associated cancel function

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

but that will not by default lead to a proper smooth termination when a signal is sent (either us sending a SIGINT / SIGTERM to make run, or kubelet trying to stop the pod with SIGINT)

By default, a synchronous signal is converted into a run-time panic. A SIGHUP, SIGINT, or SIGTERM signal causes the program to exit.

(from https://pkg.go.dev/os/signal#hdr-Default_behavior_of_signals_in_Go_programs)

i.e., despite passing the context to the right places, we're not being graceful at all.

controller-runtime provides a nice little utility for covering this though: https://github.com/kubernetes-sigs/controller-runtime/blob/4d10a0615b11507451ecb58bfd59f0f6ef313a29/pkg/manager/signals/signal.go#L26-L30

// SetupSignalHandler registers for SIGTERM and SIGINT. A context is returned
// which is canceled on one of these signals. If a second signal is caught, the program
// is terminated with exit code 1.
func SetupSignalHandler() context.Context {

so, doing something like

-       if err = cmd.Execute(); err != nil {
+       if err = cmd.Execute(ctrl.SetupSignalHandler()); err != nil {
                panic(err)
        }
 }

we should see that after sending a signal, it actually exits gracefully:

^C{"level":"info","ts":1637262059.6318526,"logger":"controller.workload","msg":"Shutdown signal received, waiting for all workers to finish"}
{"level":"info","ts":1637262059.63193,"logger":"controller.supply-chain","msg":"Shutdown signal received, waiting for all workers to finish"}
{"level":"info","ts":1637262059.6319268,"logger":"controller.deliverable","msg":"Shutdown signal received, waiting for all workers to finish"}
{"level":"info","ts":1637262059.6318586,"logger":"controller.delivery","msg":"Shutdown signal received, waiting for all workers to finish"}
{"level":"info","ts":1637262059.6319535,"logger":"controller.runnable-service","msg":"Shutdown signal received, waiting for all workers to finish"}
{"level":"info","ts":1637262059.6319625,"logger":"controller.delivery","msg":"All workers finished"}
{"level":"info","ts":1637262059.6319673,"logger":"controller.runnable-service","msg":"All workers finished"}
{"level":"info","ts":1637262059.6319716,"logger":"controller.deliverable","msg":"All workers finished"}
{"level":"info","ts":1637262059.631974,"logger":"controller.supply-chain","msg":"All workers finished"}
{"level":"info","ts":1637262059.6319742,"logger":"controller.workload","msg":"All workers finished"}

(without that context w/ proper signal handling, we don't have the messages above ^)

Copy link
Contributor

@cirocosta cirocosta left a comment

Choose a reason for hiding this comment

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

left a couple comments about some complementary changes that could go in together, but that's it! lgtm 😁

@squeedee
Copy link
Member Author

yayy nice! that's a lot of changes 😅 thanks for putting the work! overall, lgtm!

trying to follow the chain of ctx being propagated through the code, one thing that I noticed that seems a little off is how we pass from cmd/main.go down to pkg/root:

  • here we're passing it via a struct field

  • here we're consuming from the struct

if err := mgr.Start(cmd.Context); err != nil {
return fmt.Errorf("manager start: %w", err)

that's not very usual, and discouraged - the idiomatic way of doing doing would be to pass it via the function instead, being the first argument of the list.

it's not something introduced in this PR, but, sounds somewhat appropriate to patch given the context (no pun intended) 😁 wdyt?

thx!

I agree but I don't think I want to wait to rebase against main again just to get this work in.

@squeedee squeedee mentioned this pull request Nov 18, 2021
@squeedee
Copy link
Member Author

Issue created: #361

@squeedee squeedee merged commit a3bad4b into main Nov 18, 2021
@squeedee squeedee deleted the use-context-correctly branch November 18, 2021 20:26
cirocosta pushed a commit that referenced this pull request Nov 19, 2021
as suggested in [#360], we should be closer to idiomatic go where rather
than passing context via structs, we make use of 1st-arg for ctx.

see https://pkg.go.dev/context for more details

[#360]: #360 (comment)

Signed-off-by: Ciro S. Costa <ciroscosta@vmware.com>
@cirocosta cirocosta mentioned this pull request Nov 19, 2021
4 tasks
cirocosta pushed a commit that referenced this pull request Nov 19, 2021
as suggested in [#360], we should be closer to idiomatic go where rather
than passing context via structs, we make use of 1st-arg for ctx.

see https://pkg.go.dev/context for more details

[#360]: #360 (comment)

Signed-off-by: Ciro S. Costa <ciroscosta@vmware.com>
cirocosta pushed a commit that referenced this pull request Nov 22, 2021
as suggested in [#360], we should be closer to idiomatic go where rather
than passing context via structs, we make use of 1st-arg for ctx.

see https://pkg.go.dev/context for more details

[#360]: #360 (comment)

Signed-off-by: Ciro S. Costa <ciroscosta@vmware.com>
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