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

⚠ Add a context w/ logger to Reconciler interface #1054

Merged
merged 1 commit into from Aug 5, 2020

Conversation

vincepri
Copy link
Member

No description provided.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jul 18, 2020
@k8s-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 18, 2020
@vincepri
Copy link
Member Author

This is mostly a concept, and would like some feedback on the approach

@vincepri
Copy link
Member Author

/test pull-controller-runtime-test-master

@vincepri
Copy link
Member Author

/cc @alvaroaleman @christopherhein @devigned @alexeldeib

for feedback :)

Copy link

@luxas luxas left a 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

pkg/reconcile/reconcile.go Outdated Show resolved Hide resolved
pkg/builder/example_test.go Outdated Show resolved Hide resolved
pkg/controller/controller_integration_test.go Outdated Show resolved Hide resolved
pkg/builder/controller_test.go Outdated Show resolved Hide resolved
pkg/builder/controller_test.go Outdated Show resolved Hide resolved
@@ -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),
Copy link

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?

Copy link
Member Author

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

@alvaroaleman
Copy link
Member

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 With{Cancel,Deadline,Timeout} however if they do that, they get a context.Context and not a reconcile.Context. Maybe it would make sense to provide a context package that exposes those?

@vincepri
Copy link
Member Author

vincepri commented Jul 20, 2020

@alvaroaleman That's a good point, we can have our context utilities that would follow upstream and return our own representation instead

@luxas
Copy link

luxas commented Jul 21, 2020

/assign

@vincepri
Copy link
Member Author

/milestone v0.7.x

@k8s-ci-robot k8s-ci-robot added this to the v0.7.x milestone Jul 21, 2020
Copy link

@devigned devigned left a 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.

@vincepri
Copy link
Member Author

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 😊

@vincepri vincepri changed the title [WIP] ⚠ Add a context to Reconciler interface ⚠ Add a context to Reconciler interface Jul 31, 2020
@vincepri
Copy link
Member Author

vincepri commented Aug 3, 2020

/test pull-controller-runtime-test-master

Signed-off-by: Vince Prignano <vincepri@vmware.com>
@k8s-ci-robot
Copy link
Contributor

@vincepri: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-controller-runtime-apidiff-master e230cb4 link /test pull-controller-runtime-apidiff-master

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.

@vincepri
Copy link
Member Author

vincepri commented Aug 4, 2020

This should be good to go as well, if there are no other comments

@vincepri
Copy link
Member Author

vincepri commented Aug 5, 2020

@alvaroaleman This should also be good to go

Copy link
Member

@alvaroaleman alvaroaleman left a 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

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 5, 2020
@k8s-ci-robot k8s-ci-robot merged commit 5f112fb into kubernetes-sigs:master Aug 5, 2020
"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() {
Copy link
Member

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.

Copy link
Contributor

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")
Copy link
Member

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)
Copy link
Member

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

Suggested change
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{}
Copy link
Member

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 {
Copy link
Member

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)
Copy link
Member

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.

@vincepri
Copy link
Member Author

vincepri commented Aug 5, 2020

@detiber I'll follow-up with another PR for your comments / respond soon

Copy link
Contributor

@DirectXMan12 DirectXMan12 left a 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() {
Copy link
Contributor

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

amisevsk added a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 4, 2021
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 <amisevsk@redhat.com>
amisevsk added a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 10, 2021
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 <amisevsk@redhat.com>
sleshchenko pushed a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 11, 2021
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 <amisevsk@redhat.com>
amisevsk added a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 12, 2021
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 <amisevsk@redhat.com>
amisevsk added a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 12, 2021
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 <amisevsk@redhat.com>
amisevsk added a commit to amisevsk/devworkspace-operator that referenced this pull request Aug 12, 2021
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 <amisevsk@redhat.com>
JPinkney pushed a commit to devfile/devworkspace-operator that referenced this pull request Aug 30, 2021
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 <amisevsk@redhat.com>
sleshchenko pushed a commit to devfile/devworkspace-operator that referenced this pull request Sep 2, 2021
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 <amisevsk@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet