Skip to content

Commit 99cf920

Browse files
committed
⚠ Add a context to Reconciler interface
Signed-off-by: Vince Prignano <[email protected]>
1 parent 67f09e6 commit 99cf920

File tree

19 files changed

+94
-53
lines changed

19 files changed

+94
-53
lines changed

alias.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ var (
125125
// get any actual logging.
126126
Log = log.Log
127127

128+
// LoggerFromContext returns a logger with predefined values from a context.Context.
129+
//
130+
// This is meant to be used with the context supplied in a struct that satisfies the Reconciler interface.
131+
LoggerFromContext = log.FromContext
132+
128133
// SetLogger sets a concrete logging implementation for all deferred Loggers.
129134
SetLogger = log.SetLogger
130135
)

example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,17 @@ type ReplicaSetReconciler struct {
116116
// * Read the ReplicaSet
117117
// * Read the Pods
118118
// * Set a Label on the ReplicaSet with the Pod count
119-
func (a *ReplicaSetReconciler) Reconcile(req controllers.Request) (controllers.Result, error) {
119+
func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req controllers.Request) (controllers.Result, error) {
120120
// Read the ReplicaSet
121121
rs := &appsv1.ReplicaSet{}
122-
err := a.Get(context.TODO(), req.NamespacedName, rs)
122+
err := a.Get(ctx, req.NamespacedName, rs)
123123
if err != nil {
124124
return controllers.Result{}, err
125125
}
126126

127127
// List the Pods matching the PodTemplate Labels
128128
pods := &corev1.PodList{}
129-
err = a.List(context.TODO(), pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
129+
err = a.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
130130
if err != nil {
131131
return controllers.Result{}, err
132132
}

examples/builtins/controller.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,25 @@ import (
2020
"context"
2121
"fmt"
2222

23-
"github.com/go-logr/logr"
24-
2523
appsv1 "k8s.io/api/apps/v1"
2624
"k8s.io/apimachinery/pkg/api/errors"
2725
"sigs.k8s.io/controller-runtime/pkg/client"
26+
"sigs.k8s.io/controller-runtime/pkg/log"
2827
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2928
)
3029

3130
// reconcileReplicaSet reconciles ReplicaSets
3231
type reconcileReplicaSet struct {
3332
// client can be used to retrieve objects from the APIServer.
3433
client client.Client
35-
log logr.Logger
3634
}
3735

3836
// Implement reconcile.Reconciler so the controller can reconcile objects
3937
var _ reconcile.Reconciler = &reconcileReplicaSet{}
4038

41-
func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
39+
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
4240
// set up a convenient log object so we don't have to type request over and over again
43-
log := r.log.WithValues("request", request)
41+
log := log.FromContext(ctx)
4442

4543
// Fetch the ReplicaSet from the cache
4644
rs := &appsv1.ReplicaSet{}

examples/builtins/main.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@ import (
2525
"sigs.k8s.io/controller-runtime/pkg/client/config"
2626
"sigs.k8s.io/controller-runtime/pkg/controller"
2727
"sigs.k8s.io/controller-runtime/pkg/handler"
28-
logf "sigs.k8s.io/controller-runtime/pkg/log"
28+
"sigs.k8s.io/controller-runtime/pkg/log"
2929
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3030
"sigs.k8s.io/controller-runtime/pkg/manager"
3131
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
3232
"sigs.k8s.io/controller-runtime/pkg/source"
3333
"sigs.k8s.io/controller-runtime/pkg/webhook"
3434
)
3535

36-
var log = logf.Log.WithName("example-controller")
36+
func init() {
37+
log.SetLogger(zap.Logger(false))
38+
}
3739

3840
func main() {
39-
logf.SetLogger(zap.Logger(false))
40-
entryLog := log.WithName("entrypoint")
41+
entryLog := log.Log.WithName("entrypoint")
4142

4243
// Setup a Manager
4344
entryLog.Info("setting up manager")
@@ -50,7 +51,7 @@ func main() {
5051
// Setup a new controller to reconcile ReplicaSets
5152
entryLog.Info("Setting up controller")
5253
c, err := controller.New("foo-controller", mgr, controller.Options{
53-
Reconciler: &reconcileReplicaSet{client: mgr.GetClient(), log: log.WithName("reconciler")},
54+
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()},
5455
})
5556
if err != nil {
5657
entryLog.Error(err, "unable to set up individual controller")

examples/crd/main.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ import (
2929
ctrl "sigs.k8s.io/controller-runtime"
3030
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
3131
"sigs.k8s.io/controller-runtime/pkg/client"
32+
"sigs.k8s.io/controller-runtime/pkg/log"
3233
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3334
)
3435

3536
var (
3637
setupLog = ctrl.Log.WithName("setup")
37-
recLog = ctrl.Log.WithName("reconciler")
3838
)
3939

4040
type reconciler struct {
4141
client.Client
4242
scheme *runtime.Scheme
4343
}
4444

45-
func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
46-
log := recLog.WithValues("chaospod", req.NamespacedName)
45+
func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46+
log := log.FromContext(ctx).WithValues("chaospod", req.NamespacedName)
4747
log.V(1).Info("reconciling chaos pod")
48-
ctx := context.Background()
4948

5049
var chaospod api.ChaosPod
5150
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {

pkg/builder/controller_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444

4545
type typedNoop struct{}
4646

47-
func (typedNoop) Reconcile(reconcile.Request) (reconcile.Result, error) {
47+
func (typedNoop) Reconcile(context.Context, reconcile.Request) (reconcile.Result, error) {
4848
return reconcile.Result{}, nil
4949
}
5050

@@ -60,7 +60,9 @@ var _ = Describe("application", func() {
6060
close(stop)
6161
})
6262

63-
noop := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) { return reconcile.Result{}, nil })
63+
noop := reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
64+
return reconcile.Result{}, nil
65+
})
6466

6567
Describe("New", func() {
6668
It("should return success if given valid objects", func() {
@@ -302,7 +304,7 @@ func doReconcileTest(nameSuffix string, stop chan struct{}, blder *Builder, mgr
302304

303305
By("Creating the application")
304306
ch := make(chan reconcile.Request)
305-
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
307+
fn := reconcile.Func(func(_ context.Context, req reconcile.Request) (reconcile.Result, error) {
306308
defer GinkgoRecover()
307309
if !strings.HasSuffix(req.Name, nameSuffix) {
308310
// From different test, ignore this request. Etcd is shared across tests.

pkg/builder/example_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,24 @@ type ReplicaSetReconciler struct {
7979
// * Read the ReplicaSet
8080
// * Read the Pods
8181
// * Set a Label on the ReplicaSet with the Pod count
82-
func (a *ReplicaSetReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
82+
func (a *ReplicaSetReconciler) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
8383
// Read the ReplicaSet
8484
rs := &appsv1.ReplicaSet{}
85-
err := a.Get(context.TODO(), req.NamespacedName, rs)
85+
err := a.Get(ctx, req.NamespacedName, rs)
8686
if err != nil {
8787
return reconcile.Result{}, err
8888
}
8989

9090
// List the Pods matching the PodTemplate Labels
9191
pods := &corev1.PodList{}
92-
err = a.List(context.TODO(), pods, client.InNamespace(req.Namespace),
93-
client.MatchingLabels(rs.Spec.Template.Labels))
92+
err = a.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
9493
if err != nil {
9594
return reconcile.Result{}, err
9695
}
9796

9897
// Update the ReplicaSet
9998
rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items))
100-
err = a.Update(context.TODO(), rs)
99+
err = a.Update(ctx, rs)
101100
if err != nil {
102101
return reconcile.Result{}, err
103102
}

pkg/controller/controller_integration_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var _ = Describe("controller", func() {
6262
By("Creating the Controller")
6363
instance, err := controller.New("foo-controller", cm, controller.Options{
6464
Reconciler: reconcile.Func(
65-
func(request reconcile.Request) (reconcile.Result, error) {
65+
func(_ context.Context, request reconcile.Request) (reconcile.Result, error) {
6666
reconciled <- request
6767
return reconcile.Result{}, nil
6868
}),

pkg/controller/controller_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controller_test
1818

1919
import (
20+
"context"
2021
"fmt"
2122
rt "runtime"
2223

@@ -33,7 +34,7 @@ import (
3334
var _ = Describe("controller.Controller", func() {
3435
var stop chan struct{}
3536

36-
rec := reconcile.Func(func(reconcile.Request) (reconcile.Result, error) {
37+
rec := reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
3738
return reconcile.Result{}, nil
3839
})
3940
BeforeEach(func() {
@@ -121,7 +122,7 @@ var _ inject.Client = &failRec{}
121122

122123
type failRec struct{}
123124

124-
func (*failRec) Reconcile(reconcile.Request) (reconcile.Result, error) {
125+
func (*failRec) Reconcile(context.Context, reconcile.Request) (reconcile.Result, error) {
125126
return reconcile.Result{}, nil
126127
}
127128

pkg/controller/example_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controller_test
1818

1919
import (
20+
"context"
2021
"os"
2122

2223
corev1 "k8s.io/api/core/v1"
@@ -41,7 +42,7 @@ var (
4142
// manager.Manager will be used to Start the Controller, and will provide it a shared Cache and Client.
4243
func ExampleNew() {
4344
_, err := controller.New("pod-controller", mgr, controller.Options{
44-
Reconciler: reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
45+
Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
4546
// Your business logic to implement the API by creating, updating, deleting objects goes here.
4647
return reconcile.Result{}, nil
4748
}),
@@ -59,7 +60,7 @@ func ExampleController() {
5960
// Create a new Controller that will call the provided Reconciler function in response
6061
// to events.
6162
c, err := controller.New("pod-controller", mgr, controller.Options{
62-
Reconciler: reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
63+
Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
6364
// Your business logic to implement the API by creating, updating, deleting objects goes here.
6465
return reconcile.Result{}, nil
6566
}),
@@ -90,7 +91,7 @@ func ExampleController_unstructured() {
9091
// Create a new Controller that will call the provided Reconciler function in response
9192
// to events.
9293
c, err := controller.New("pod-controller", mgr, controller.Options{
93-
Reconciler: reconcile.Func(func(o reconcile.Request) (reconcile.Result, error) {
94+
Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
9495
// Your business logic to implement the API by creating, updating, deleting objects goes here.
9596
return reconcile.Result{}, nil
9697
}),
@@ -129,7 +130,7 @@ func ExampleNewUnmanaged() {
129130
// Configure creates a new controller but does not add it to the supplied
130131
// manager.
131132
c, err := controller.NewUnmanaged("pod-controller", mgr, controller.Options{
132-
Reconciler: reconcile.Func(func(_ reconcile.Request) (reconcile.Result, error) {
133+
Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
133134
return reconcile.Result{}, nil
134135
}),
135136
})

0 commit comments

Comments
 (0)