Skip to content

Commit 57c1a6b

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

20 files changed

+226
-76
lines changed

.golangci.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ linters:
2828
- unparam
2929
- ineffassign
3030
- nakedret
31-
- interfacer
3231
- gocyclo
3332
- lll
3433
- dupl

example_test.go

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

1919
import (
20-
"context"
2120
"fmt"
2221
"os"
2322
"time"
@@ -26,6 +25,7 @@ import (
2625
corev1 "k8s.io/api/core/v1"
2726
controllers "sigs.k8s.io/controller-runtime"
2827
"sigs.k8s.io/controller-runtime/pkg/client"
28+
"sigs.k8s.io/controller-runtime/pkg/context"
2929
)
3030

3131
// This example creates a simple application Controller that is configured for ReplicaSets and Pods.
@@ -116,24 +116,24 @@ 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
}
133133

134134
// Update the ReplicaSet
135135
rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items))
136-
err = a.Update(context.TODO(), rs)
136+
err = a.Update(ctx, rs)
137137
if err != nil {
138138
return controllers.Result{}, err
139139
}

examples/builtins/controller.go

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

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

23-
"github.com/go-logr/logr"
24-
2522
appsv1 "k8s.io/api/apps/v1"
2623
"k8s.io/apimachinery/pkg/api/errors"
2724
"sigs.k8s.io/controller-runtime/pkg/client"
25+
"sigs.k8s.io/controller-runtime/pkg/context"
2826
"sigs.k8s.io/controller-runtime/pkg/reconcile"
2927
)
3028

3129
// reconcileReplicaSet reconciles ReplicaSets
3230
type reconcileReplicaSet struct {
3331
// client can be used to retrieve objects from the APIServer.
3432
client client.Client
35-
log logr.Logger
3633
}
3734

3835
// Implement reconcile.Reconciler so the controller can reconcile objects
3936
var _ reconcile.Reconciler = &reconcileReplicaSet{}
4037

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-
38+
func (r *reconcileReplicaSet) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
4539
// Fetch the ReplicaSet from the cache
4640
rs := &appsv1.ReplicaSet{}
47-
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
41+
err := r.client.Get(ctx, request.NamespacedName, rs)
4842
if errors.IsNotFound(err) {
49-
log.Error(nil, "Could not find ReplicaSet")
43+
ctx.Log().Error(nil, "Could not find ReplicaSet")
5044
return reconcile.Result{}, nil
5145
}
5246

@@ -55,7 +49,7 @@ func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Re
5549
}
5650

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

6054
// Set the label if it is missing
6155
if rs.Labels == nil {
@@ -67,7 +61,7 @@ func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Re
6761

6862
// Update the ReplicaSet
6963
rs.Labels["hello"] = "world"
70-
err = r.client.Update(context.TODO(), rs)
64+
err = r.client.Update(ctx, rs)
7165
if err != nil {
7266
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err)
7367
}

examples/builtins/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func main() {
5050
// Setup a new controller to reconcile ReplicaSets
5151
entryLog.Info("Setting up controller")
5252
c, err := controller.New("foo-controller", mgr, controller.Options{
53-
Reconciler: &reconcileReplicaSet{client: mgr.GetClient(), log: log.WithName("reconciler")},
53+
Reconciler: &reconcileReplicaSet{client: mgr.GetClient()},
5454
})
5555
if err != nil {
5656
entryLog.Error(err, "unable to set up individual controller")

examples/crd/main.go

Lines changed: 9 additions & 11 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"
@@ -29,6 +28,7 @@ import (
2928
ctrl "sigs.k8s.io/controller-runtime"
3029
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
3130
"sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/controller-runtime/pkg/context"
3232
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3333
)
3434

@@ -42,22 +42,20 @@ 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()
45+
func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
46+
ctx.Log().V(1).Info("reconciling chaos pod")
4947

5048
var chaospod api.ChaosPod
5149
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {
52-
log.Error(err, "unable to get chaosctl")
50+
ctx.Log().Error(err, "unable to get chaosctl")
5351
return ctrl.Result{}, err
5452
}
5553

5654
var pod corev1.Pod
5755
podFound := true
5856
if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {
5957
if !apierrors.IsNotFound(err) {
60-
log.Error(err, "unable to get pod")
58+
ctx.Log().Error(err, "unable to get pod")
6159
return ctrl.Result{}, err
6260
}
6361
podFound = false
@@ -70,7 +68,7 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
7068
}
7169

7270
if err := r.Delete(ctx, &pod); err != nil {
73-
log.Error(err, "unable to delete pod")
71+
ctx.Log().Error(err, "unable to delete pod")
7472
return ctrl.Result{}, err
7573
}
7674

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

8684
if err := ctrl.SetControllerReference(&chaospod, &pod, r.scheme); err != nil {
87-
log.Error(err, "unable to set pod's owner reference")
85+
ctx.Log().Error(err, "unable to set pod's owner reference")
8886
return ctrl.Result{}, err
8987
}
9088

9189
if err := r.Create(ctx, &pod); err != nil {
92-
log.Error(err, "unable to create pod")
90+
ctx.Log().Error(err, "unable to create pod")
9391
return ctrl.Result{}, err
9492
}
9593

9694
chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second)
9795
chaospod.Status.LastRun = pod.CreationTimestamp
9896
if err := r.Update(ctx, &chaospod); err != nil {
99-
log.Error(err, "unable to update chaosctl status")
97+
ctx.Log().Error(err, "unable to update chaosctl status")
10098
return ctrl.Result{}, err
10199
}
102100
return ctrl.Result{}, nil

pkg/builder/controller_test.go

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

1919
import (
20-
"context"
2120
"fmt"
2221
"strings"
2322
"sync/atomic"
@@ -32,6 +31,7 @@ import (
3231
"k8s.io/apimachinery/pkg/runtime/schema"
3332
"k8s.io/apimachinery/pkg/types"
3433
"k8s.io/client-go/util/workqueue"
34+
"sigs.k8s.io/controller-runtime/pkg/context"
3535
"sigs.k8s.io/controller-runtime/pkg/controller"
3636
"sigs.k8s.io/controller-runtime/pkg/event"
3737
"sigs.k8s.io/controller-runtime/pkg/handler"
@@ -54,7 +54,9 @@ var _ = Describe("application", func() {
5454
close(stop)
5555
})
5656

57-
noop := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) { return reconcile.Result{}, nil })
57+
noop := reconcile.Func(func(_ context.Context, req reconcile.Request) (reconcile.Result, error) {
58+
return reconcile.Result{}, nil
59+
})
5860

5961
Describe("New", func() {
6062
It("should return success if given valid objects", func() {
@@ -275,7 +277,7 @@ func doReconcileTest(nameSuffix string, stop chan struct{}, blder *Builder, mgr
275277

276278
By("Creating the application")
277279
ch := make(chan reconcile.Request)
278-
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
280+
fn := reconcile.Func(func(_ context.Context, req reconcile.Request) (reconcile.Result, error) {
279281
defer GinkgoRecover()
280282
if !strings.HasSuffix(req.Name, nameSuffix) {
281283
// From different test, ignore this request. Etcd is shared across tests.
@@ -327,7 +329,7 @@ func doReconcileTest(nameSuffix string, stop chan struct{}, blder *Builder, mgr
327329
},
328330
},
329331
}
330-
err := mgr.GetClient().Create(context.TODO(), dep)
332+
err := mgr.GetClient().Create(context.Background(), dep)
331333
Expect(err).NotTo(HaveOccurred())
332334

333335
By("Waiting for the Deployment Reconcile")
@@ -357,7 +359,7 @@ func doReconcileTest(nameSuffix string, stop chan struct{}, blder *Builder, mgr
357359
Template: dep.Spec.Template,
358360
},
359361
}
360-
err = mgr.GetClient().Create(context.TODO(), rs)
362+
err = mgr.GetClient().Create(context.Background(), rs)
361363
Expect(err).NotTo(HaveOccurred())
362364

363365
By("Waiting for the ReplicaSet Reconcile")

pkg/builder/example_test.go

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

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

@@ -28,6 +27,7 @@ import (
2827
"sigs.k8s.io/controller-runtime/pkg/builder"
2928
"sigs.k8s.io/controller-runtime/pkg/client"
3029
"sigs.k8s.io/controller-runtime/pkg/client/config"
30+
"sigs.k8s.io/controller-runtime/pkg/context"
3131
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3232
"sigs.k8s.io/controller-runtime/pkg/manager"
3333
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
@@ -79,25 +79,25 @@ 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),
92+
err = a.List(ctx, pods, client.InNamespace(req.Namespace),
9393
client.MatchingLabels(rs.Spec.Template.Labels))
9494
if err != nil {
9595
return reconcile.Result{}, err
9696
}
9797

9898
// Update the ReplicaSet
9999
rs.Labels["pod-count"] = fmt.Sprintf("%v", len(pods.Items))
100-
err = a.Update(context.TODO(), rs)
100+
err = a.Update(ctx, rs)
101101
if err != nil {
102102
return reconcile.Result{}, err
103103
}

pkg/context/alias.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package context
18+
19+
import "context"
20+
21+
// A CancelFunc tells an operation to abandon its work.
22+
// A CancelFunc does not wait for the work to stop.
23+
// A CancelFunc may be called by multiple goroutines simultaneously.
24+
// After the first call, subsequent calls to a CancelFunc do nothing.
25+
type CancelFunc context.CancelFunc
26+
27+
// DeadlineExceeded is the error returned by Context.Err when the context's
28+
// deadline passes.
29+
var DeadlineExceeded error = context.DeadlineExceeded
30+
31+
// Canceled is the error returned by Context.Err when the context is canceled.
32+
var Canceled error = context.Canceled

0 commit comments

Comments
 (0)