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

How to correctly use context arguments is unclear #265

Closed
coderanger opened this issue Dec 21, 2018 · 19 comments
Closed

How to correctly use context arguments is unclear #265

coderanger opened this issue Dec 21, 2018 · 19 comments
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/documentation Categorizes issue or PR as related to documentation. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. priority/backlog Higher priority than priority/awaiting-more-evidence.
Milestone

Comments

@coderanger
Copy link
Contributor

Most of the examples use context.TODO() on each call into the client. Would be nice to have a better explanation of what this is for.

And someday it would be cool to add a .Context field on the reconcile.Request for controllers (and maybe something similar for webhooks, though that's harder), and have the controller machinery create a context.Background() at the start of a request so you can thread it down into calls to the client library and whatnot.

@DirectXMan12
Copy link
Contributor

/kind feature
/priority important-longterm

@k8s-ci-robot k8s-ci-robot added kind/feature Categorizes issue or PR as related to a new feature. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. labels Dec 21, 2018
@sr
Copy link

sr commented Dec 28, 2018

I was wondering about this too, my understanding is the controller runtime doesn't enforce any timeout on reconciliation attempts, which could cause the entire controller to lock up:

https://github.com/kubernetes-sigs/controller-runtime/blob/master/pkg/internal/controller/controller.go#L215

Doing something like this doesn't seem so bad:

func (r *MyReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
	ctx := context.WithTimeout(context.Background(), r.timeout)

	instance := &api.MyCRD{}
	_ = r.Get(ctx, request.NamespacedName, instane)
	_ = r.someAPI.Call(ctx, &RPCRequest{})

	// ...
}

Though I could also see a new field on controller.Option, with a sane default:

type Options struct {
	MaxConcurrentReconciles int
	ReconcilerTimeout time.Duration // default to a minute?
	Reconciler reconcile.Reconciler
}

Then have the controller pass in a context as part of the request:

func (c *Controller) processNextWorkItem() bool {
	if c.ReconcilerTimeout != time.Duration(0) {
		req = req.WithContext(context.WithTimeout(context.Background(), c.ReconcilerTimeout))
	}
	if result, err := c.Do.Reconcile(req); err != nil {
		// handle
	}
}

@DirectXMan12
Copy link
Contributor

we can't actually enforce timeouts, but we can totally allow you to configure a timeout context builder that gets passed to all reconcile requests that you can pass to clients. It won't help if you manually block yourself, but in the case of well-behaved clients it's probably a good idea.

@DirectXMan12
Copy link
Contributor

since you might want contexts for other things, a ContextBuilder option (with a sane timeout-based helper and good defaults) is probably something we'll want eventually.

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 28, 2019
@fejta-bot
Copy link

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 28, 2019
@DirectXMan12
Copy link
Contributor

/remove-lifecycle rotten

@k8s-ci-robot k8s-ci-robot removed the lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. label Jun 4, 2019
@seh
Copy link

seh commented Aug 1, 2019

I noticed today that Webhooks get invoked with context.Background. It would be nice if we could inject a context.Context value that would be used as the starting context for each request, in order to attach things like a shared tracing configuration handle.

@DirectXMan12
Copy link
Contributor

/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Aug 6, 2019
@seh
Copy link

seh commented Aug 6, 2019

It would be nice if we could inject a context.Context value that would be used as the starting context for each request, in order to attach things like a shared tracing configuration handle.

Putting my money where my mouth is, see #549.

@DirectXMan12
Copy link
Contributor

thanks, merged!

@vincepri
Copy link
Member

/help
/priority backlog
/kind documentation

@k8s-ci-robot
Copy link
Contributor

@vincepri:
This request has been marked as needing help from a contributor.

Please ensure the request meets the requirements listed here.

If this request no longer meets these requirements, the label can be removed
by commenting with the /remove-help command.

In response to this:

/help
/priority backlog
/kind documentation

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added priority/backlog Higher priority than priority/awaiting-more-evidence. kind/documentation Categorizes issue or PR as related to documentation. help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. labels Feb 20, 2020
@vincepri vincepri removed kind/feature Categorizes issue or PR as related to a new feature. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. labels Feb 20, 2020
@vincepri vincepri added this to the Next milestone Feb 20, 2020
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 20, 2020
@vincepri
Copy link
Member

/lifecycle frozen

@k8s-ci-robot k8s-ci-robot added lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels May 20, 2020
@vincepri
Copy link
Member

vincepri commented Mar 1, 2021

/remove-lifecycle frozen

@coderanger Seems this issue can now be closed given that most functions and reconcilers accept a context starting from v0.7.x

@k8s-ci-robot k8s-ci-robot removed the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Mar 1, 2021
@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label May 30, 2021
@coderanger
Copy link
Contributor Author

/close

Wow I had forgotten I wrote this :)

@k8s-ci-robot
Copy link
Contributor

@coderanger: Closing this issue.

In response to this:

/close

Wow I had forgotten I wrote this :)

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Denotes an issue that needs help from a contributor. Must meet "help wanted" guidelines. kind/documentation Categorizes issue or PR as related to documentation. lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. priority/backlog Higher priority than priority/awaiting-more-evidence.
Projects
None yet
Development

No branches or pull requests

7 participants