Skip to content

Commit

Permalink
Add a context to Reconciler interface
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Jul 18, 2020
1 parent 57ed2a3 commit f870d3a
Show file tree
Hide file tree
Showing 19 changed files with 346 additions and 100 deletions.
3 changes: 3 additions & 0 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type Request = reconcile.Request
// Result contains the result of a Reconciler invocation.
type Result = reconcile.Result

// Context is the context passed in a Reconciler invocation.
type Context = reconcile.Context

// Manager initializes shared dependencies such as Caches and Clients, and provides them to Runnables.
// A Manager is required to create Controllers.
type Manager = manager.Manager
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ type ReplicaSetReconciler struct {
// * Read the ReplicaSet
// * Read the Pods
// * Set a Label on the ReplicaSet with the Pod count
func (a *ReplicaSetReconciler) Reconcile(req controllers.Request) (controllers.Result, error) {
func (a *ReplicaSetReconciler) Reconcile(ctx controllers.Context, req controllers.Request) (controllers.Result, error) {
// Read the ReplicaSet
rs := &appsv1.ReplicaSet{}
err := a.Get(context.TODO(), req.NamespacedName, rs)
Expand All @@ -126,7 +126,7 @@ func (a *ReplicaSetReconciler) Reconcile(req controllers.Request) (controllers.R

// List the Pods matching the PodTemplate Labels
pods := &corev1.PodList{}
err = a.List(context.TODO(), pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
err = a.List(ctx, pods, client.InNamespace(req.Namespace), client.MatchingLabels(rs.Spec.Template.Labels))
if err != nil {
return controllers.Result{}, err
}
Expand Down
14 changes: 5 additions & 9 deletions examples/builtins/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"context"
"fmt"

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

func (r *reconcileReplicaSet) Reconcile(request reconcile.Request) (reconcile.Result, error) {
// set up a convenient log object so we don't have to type request over and over again
log := r.log.WithValues("request", request)

func (r *reconcileReplicaSet) Reconcile(ctx reconcile.Context, request reconcile.Request) (reconcile.Result, error) {
// Fetch the ReplicaSet from the cache
rs := &appsv1.ReplicaSet{}
err := r.client.Get(context.TODO(), request.NamespacedName, rs)
err := r.client.Get(ctx, request.NamespacedName, rs)
if errors.IsNotFound(err) {
log.Error(nil, "Could not find ReplicaSet")
ctx.Log.Error(nil, "Could not find ReplicaSet")
return reconcile.Result{}, nil
}

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

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

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

// Update the ReplicaSet
rs.Labels["hello"] = "world"
err = r.client.Update(context.TODO(), rs)
err = r.client.Update(ctx, rs)
if err != nil {
return reconcile.Result{}, fmt.Errorf("could not write ReplicaSet: %+v", err)
}
Expand Down
20 changes: 8 additions & 12 deletions examples/crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"context"
"math/rand"
"os"
"time"
Expand All @@ -30,6 +29,7 @@ import (
api "sigs.k8s.io/controller-runtime/examples/crd/pkg"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var (
Expand All @@ -42,22 +42,18 @@ type reconciler struct {
scheme *runtime.Scheme
}

func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
log := recLog.WithValues("chaospod", req.NamespacedName)
log.V(1).Info("reconciling chaos pod")
ctx := context.Background()

func (r *reconciler) Reconcile(ctx reconcile.Context, req ctrl.Request) (ctrl.Result, error) {
var chaospod api.ChaosPod
if err := r.Get(ctx, req.NamespacedName, &chaospod); err != nil {
log.Error(err, "unable to get chaosctl")
ctx.Log.Error(err, "unable to get chaosctl")
return ctrl.Result{}, err
}

var pod corev1.Pod
podFound := true
if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {
if !apierrors.IsNotFound(err) {
log.Error(err, "unable to get pod")
ctx.Log.Error(err, "unable to get pod")
return ctrl.Result{}, err
}
podFound = false
Expand All @@ -70,7 +66,7 @@ func (r *reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}

if err := r.Delete(ctx, &pod); err != nil {
log.Error(err, "unable to delete pod")
ctx.Log.Error(err, "unable to delete pod")
return ctrl.Result{}, err
}

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

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

if err := r.Create(ctx, &pod); err != nil {
log.Error(err, "unable to create pod")
ctx.Log.Error(err, "unable to create pod")
return ctrl.Result{}, err
}

chaospod.Spec.NextStop.Time = time.Now().Add(time.Duration(10*(rand.Int63n(2)+1)) * time.Second)
chaospod.Status.LastRun = pod.CreationTimestamp
if err := r.Update(ctx, &chaospod); err != nil {
log.Error(err, "unable to update chaosctl status")
ctx.Log.Error(err, "unable to update chaosctl status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
Expand Down
2 changes: 1 addition & 1 deletion hack/tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module sigs.k8s.io/controller-runtime/hack/tools
go 1.13

require (
github.com/golangci/golangci-lint v1.23.6
github.com/golangci/golangci-lint v1.28.3
github.com/joelanford/go-apidiff v0.0.0-20191206194835-106bcff5f060
)
Loading

0 comments on commit f870d3a

Please sign in to comment.