-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
⚠ Add a context w/ logger to Reconciler interface #1054
⚠ Add a context w/ logger to Reconciler interface #1054
Conversation
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: vincepri The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
This is mostly a concept, and would like some feedback on the approach |
feda045
to
f870d3a
Compare
/test pull-controller-runtime-test-master |
/cc @alvaroaleman @christopherhein @devigned @alexeldeib for feedback :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a great addition to the project!
After this we can take a stab at global timeouts.
Generally, I prefer interfaces for this kind of thing though
@@ -228,11 +229,16 @@ func (c *Controller) reconcileHandler(obj interface{}) bool { | |||
return true | |||
} | |||
|
|||
ctx := reconcile.Context{ | |||
Context: context.Background(), | |||
Log: c.Log.WithValues("name", req.Name, "namespace", req.Namespace), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also add gvk information? and/or reconciler name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reconciler/controller name is already within the c.Log logger, +1 adding gvk info
Generally I really like it. One thing I am wondering though is how we expect ppl to use this, because they will rightfully want to use the standard |
@alvaroaleman That's a good point, we can have our context utilities that would follow upstream and return our own representation instead |
f870d3a
to
57c1a6b
Compare
/assign |
/milestone v0.7.x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an exciting and promising PR! My only concern is the creation of an extended context
package.
57c1a6b
to
99cf920
Compare
Thank you all for the great feedback. Pushed some changes to use the stdlib's context.Context and have some helper methods to retrieve and set the logger. Let me know what do you all think, ready for another round 😊 |
df909ff
to
e774d6e
Compare
e774d6e
to
ce65986
Compare
/test pull-controller-runtime-test-master |
Signed-off-by: Vince Prignano <[email protected]>
ce65986
to
e230cb4
Compare
@vincepri: The following test failed, say
Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR. 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. I understand the commands that are listed here. |
This should be good to go as well, if there are no other comments |
@alvaroaleman This should also be good to go |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really, really happy about this change btw, thanks for pushing it!
/lgtm
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
var log = logf.Log.WithName("example-controller") | ||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😬 is doing this in init() actually needed? It would be great if we could avoid requiring the use of init functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I'm a bit late to the party here, but it's not clear why we switched to using init here
|
||
func main() { | ||
logf.SetLogger(zap.New()) | ||
entryLog := log.WithName("entrypoint") | ||
entryLog := log.Log.WithName("entrypoint") | ||
|
||
// Setup a Manager | ||
entryLog.Info("setting up manager") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to demonstrate plumbing the logger through to the manager here?
func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) { | ||
log := recLog.WithValues("chaospod", req.NamespacedName) | ||
func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
log := log.FromContext(ctx).WithValues("chaospod", req.NamespacedName) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of .WithValues shouldn't be needed here, since you can pass in the key/value pairs to log.FromContext
log := log.FromContext(ctx).WithValues("chaospod", req.NamespacedName) | |
log := log.FromContext(ctx, "chaospod", req.NamespacedName) |
// set up a convenient log object so we don't have to type request over and over again | ||
log := r.log.WithValues("request", request) | ||
log := log.FromContext(ctx) | ||
|
||
// Fetch the ReplicaSet from the cache | ||
rs := &appsv1.ReplicaSet{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the error cases below be updated to use the logger from the context to output error info rather than returning wrapped errors?
log.V(1).Info("reconciling chaos pod") | ||
ctx := context.Background() | ||
|
||
var chaospod api.ChaosPod | ||
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this example also show plumbing the logger through the manager in main()
below?
@@ -229,12 +233,15 @@ func (c *Controller) reconcileHandler(obj interface{}) bool { | |||
} | |||
|
|||
log := c.Log.WithValues("name", req.Name, "namespace", req.Namespace) | |||
ctx := logf.IntoContext(context.Background(), log) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would context.TODO()
work a bit better here? It seems like eventually a WithCancel
context can be plumbed into here from Start()
that could eventually be used to more cleanly stop in-flight operations rather than just forcefully killing the workers.
@detiber I'll follow-up with another PR for your comments / respond soon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
friendly reminder to please try to not merge things with empty commit messages -- it makes it harder to see an overview of what was merged and why
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
"sigs.k8s.io/controller-runtime/pkg/manager/signals" | ||
"sigs.k8s.io/controller-runtime/pkg/source" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
) | ||
|
||
var log = logf.Log.WithName("example-controller") | ||
func init() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I'm a bit late to the party here, but it's not clear why we switched to using init here
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
Prep code for supporting k8s 1.21: * Add context.Context parameter to Reconcile() functions [1] * Add RBAC get/create/update coordination.k8s.io/v1 Leases (replacing a configmap for leader election locks) [2] * Adapt event handler code to reflect simplification [3] * Adapt to apimachinery/pkg/runtime Log deprecation (replaced with pkg/client Log) [4] * client.Object is preferred in favor of runtime.Object (v0.7.0 release) [5] * Use admission/v1 instead of v1beta1 for webhook requests [6] [1] - kubernetes-sigs/controller-runtime#1054 [2] - kubernetes-sigs/controller-runtime#1144 [3] - kubernetes-sigs/controller-runtime#1119 [4] - kubernetes-sigs/controller-runtime#1105 [5] - kubernetes-sigs/controller-runtime#898 kubernetes-sigs/controller-runtime#1118 [6] - kubernetes-sigs/controller-runtime#1284 kubernetes-sigs/controller-runtime@a32b29d Signed-off-by: Angel Misevski <[email protected]>
No description provided.