Skip to content

Commit f870d3a

Browse files
committed
Add a context to Reconciler interface
Signed-off-by: Vince Prignano <vincepri@vmware.com>
1 parent 57ed2a3 commit f870d3a

File tree

19 files changed

+346
-100
lines changed

19 files changed

+346
-100
lines changed

alias.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type Request = reconcile.Request
4040
// Result contains the result of a Reconciler invocation.
4141
type Result = reconcile.Result
4242

43+
// Context is the context passed in a Reconciler invocation.
44+
type Context = reconcile.Context
45+
4346
// Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables.
4447
// A Manager is required to create Controllers.
4548
type Manager = manager.Manager

example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ 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 controllers.Context, req controllers.Request) (controllers.Result, error) {
120120
// Read the ReplicaSet
121121
rs := &appsv1.ReplicaSet{}
122122
err := a.Get(context.TODO(), req.NamespacedName, rs)
@@ -126,7 +126,7 @@ func (a *ReplicaSetReconciler) Reconcile(req controllers.Request) (controllers.R
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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"context"
2120
"fmt"
2221

2322
"github.com/go-logr/logr"
@@ -38,15 +37,12 @@ type reconcileReplicaSet struct {
3837
// Implement reconcile.Reconciler so the controller can reconcile objects
3938
var _ reconcile.Reconciler = &reconcileReplicaSet{}
4039

41-
func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
42-
// 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)
44-
40+
func (r *reconcileReplicaSet) Reconcile(ctx reconcile.Context, request reconcile.Request) (reconcile.Result, error) {
4541
// Fetch the ReplicaSet from the cache
4642
rs := &appsv1.ReplicaSet{}
47-
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
43+
err := r.client.Get(ctx, request.NamespacedName, rs)
4844
if errors.IsNotFound(err) {
49-
log.Error(nil, "Could not find ReplicaSet")
45+
ctx.Log.Error(nil, "Could not find ReplicaSet")
5046
return reconcile.Result{}, nil
5147
}
5248

@@ -55,7 +51,7 @@ func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Re
5551
}
5652

5753
// Print the ReplicaSet
58-
log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name)
54+
ctx.Log.Info("Reconciling ReplicaSet", "container name", rs.Spec.Template.Spec.Containers[0].Name)
5955

6056
// Set the label if it is missing
6157
if rs.Labels == nil {
@@ -67,7 +63,7 @@ func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Re
6763

6864
// Update the ReplicaSet
6965
rs.Labels["hello"] = "world"
70-
err = r.client.Update(context.TODO(), rs)
66+
err = r.client.Update(ctx, rs)
7167
if err != nil {
7268
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err)
7369
}

examples/crd/main.go

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

1919
import (
20-
"context"
2120
"math/rand"
2221
"os"
2322
"time"
@@ -30,6 +29,7 @@ import (
3029
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
3231
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3333
)
3434

3535
var (
@@ -42,22 +42,18 @@ type reconciler struct {
4242
scheme *runtime.Scheme
4343
}
4444

45-
func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
46-
log := recLog.WithValues("chaospod", req.NamespacedName)
47-
log.V(1).Info("reconciling chaos pod")
48-
ctx := context.Background()
49-
45+
func (r *reconciler) Reconcile(ctx reconcile.Context, req ctrl.Request) (ctrl.Result, error) {
5046
var chaospod api.ChaosPod
5147
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {
52-
log.Error(err, "unable to get chaosctl")
48+
ctx.Log.Error(err, "unable to get chaosctl")
5349
return ctrl.Result{}, err
5450
}
5551

5652
var pod corev1.Pod
5753
podFound := true
5854
if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {
5955
if !apierrors.IsNotFound(err) {
60-
log.Error(err, "unable to get pod")
56+
ctx.Log.Error(err, "unable to get pod")
6157
return ctrl.Result{}, err
6258
}
6359
podFound = false
@@ -70,7 +66,7 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
7066
}
7167

7268
if err := r.Delete(ctx, &pod); err != nil {
73-
log.Error(err, "unable to delete pod")
69+
ctx.Log.Error(err, "unable to delete pod")
7470
return ctrl.Result{}, err
7571
}
7672

@@ -84,19 +80,19 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
8480
pod.Spec = templ.Spec
8581

8682
if err := ctrl.SetControllerReference(&chaospod, &pod, r.scheme); err != nil {
87-
log.Error(err, "unable to set pod's owner reference")
83+
ctx.Log.Error(err, "unable to set pod's owner reference")
8884
return ctrl.Result{}, err
8985
}
9086

9187
if err := r.Create(ctx, &pod); err != nil {
92-
log.Error(err, "unable to create pod")
88+
ctx.Log.Error(err, "unable to create pod")
9389
return ctrl.Result{}, err
9490
}
9591

9692
chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second)
9793
chaospod.Status.LastRun = pod.CreationTimestamp
9894
if err := r.Update(ctx, &chaospod); err != nil {
99-
log.Error(err, "unable to update chaosctl status")
95+
ctx.Log.Error(err, "unable to update chaosctl status")
10096
return ctrl.Result{}, err
10197
}
10298
return ctrl.Result{}, nil

hack/tools/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module sigs.k8s.io/controller-runtime/hack/tools
33
go 1.13
44

55
require (
6-
github.com/golangci/golangci-lint v1.23.6
6+
github.com/golangci/golangci-lint v1.28.3
77
github.com/joelanford/go-apidiff v0.0.0-20191206194835-106bcff5f060
88
)

0 commit comments

Comments
 (0)