Skip to content

Commit

Permalink
Extract logger from context on controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Dec 20, 2020
1 parent 65ddbce commit d508031
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 64 deletions.
19 changes: 0 additions & 19 deletions cmd/workspace/workspace_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,22 +356,3 @@ func (o *NewOptions) waitForReconcile(ctx context.Context, ws *v1alpha1.Workspac
}
return nil
}

// waitForWorkspaceInitializing waits until the workspace reports it is
// initializing (or ready)
func (o *NewOptions) waitForWorkspaceInitializing(ctx context.Context, ws *v1alpha1.Workspace) error {
lw := &k8s.WorkspaceListWatcher{Client: o.EtokClient, Name: ws.Name, Namespace: ws.Namespace}
hdlr := handlers.ContainerReady(ws.PodName(), controllers.InstallerContainerName, true, false)

ctx, cancel := context.WithTimeout(ctx, o.PodTimeout)
defer cancel()

event, err := watchtools.UntilWithSync(ctx, lw, &corev1.Pod{}, nil, hdlr)
if err != nil {
if errors.Is(err, wait.ErrWaitTimeout) {
return nil, errPodTimeout
}
return nil, err
}
return event.Object.(*corev1.Pod), err
}
19 changes: 10 additions & 9 deletions pkg/controllers/run_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"

"github.com/go-logr/logr"
v1alpha1 "github.com/leg100/etok/api/etok.dev/v1alpha1"
"github.com/leg100/etok/pkg/scheme"
"github.com/leg100/etok/pkg/util/slice"
Expand All @@ -17,21 +16,20 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
)

type RunReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Image string
}

func NewRunReconciler(c client.Client, image string) *RunReconciler {
return &RunReconciler{
Client: c,
Scheme: scheme.Scheme,
Log: ctrl.Log.WithName("controllers").WithName("Run"),
Image: image,
}
}
Expand All @@ -41,8 +39,9 @@ func NewRunReconciler(c client.Client, image string) *RunReconciler {
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;create;update;patch;delete

func (r *RunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("run", req.NamespacedName)
log.V(0).Info("Reconciling")
// set up a convenient log object so we don't have to type request over and
// over again
log := log.FromContext(ctx)

// Get run obj
var run v1alpha1.Run
Expand All @@ -56,7 +55,7 @@ func (r *RunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R

// Run completed, nothing more to be done
if run.Phase == v1alpha1.RunPhaseCompleted {
return r.success(ctx, log, &run)
return r.success(ctx, &run)
}

// Fetch its Workspace object
Expand Down Expand Up @@ -106,7 +105,7 @@ func (r *RunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}
}
// Go no further
return r.success(ctx, log, &run)
return r.success(ctx, &run)
}

// Front of queue, so continue reconciliation
Expand Down Expand Up @@ -161,11 +160,13 @@ func (r *RunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}
}

return r.success(ctx, log, &run)
return r.success(ctx, &run)
}

// success marks a successful reconcile
func (r *RunReconciler) success(ctx context.Context, log logr.Logger, run *v1alpha1.Run) (ctrl.Result, error) {
func (r *RunReconciler) success(ctx context.Context, run *v1alpha1.Run) (ctrl.Result, error) {
log := log.FromContext(ctx)

if !run.Reconciled {
run.Reconciled = true
if err := r.Status().Update(ctx, run); err != nil {
Expand Down
18 changes: 10 additions & 8 deletions pkg/controllers/workspace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"regexp"
"strings"

"github.com/go-logr/logr"
"github.com/leg100/etok/api/etok.dev/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/log"

"github.com/leg100/etok/pkg/labels"
"github.com/leg100/etok/pkg/scheme"
"github.com/leg100/etok/pkg/util/slice"
Expand All @@ -29,15 +30,13 @@ var approvalAnnotationKeyRegex = regexp.MustCompile("approvals.etok.dev/[-a-z0-9
type WorkspaceReconciler struct {
client.Client
Scheme *runtime.Scheme
Log logr.Logger
Image string
}

func NewWorkspaceReconciler(cl client.Client, image string) *WorkspaceReconciler {
return &WorkspaceReconciler{
Client: cl,
Scheme: scheme.Scheme,
Log: ctrl.Log.WithName("controllers").WithName("Workspace"),
Image: image,
}
}
Expand All @@ -62,8 +61,9 @@ func NewWorkspaceReconciler(cl client.Client, image string) *WorkspaceReconciler
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := r.Log.WithValues("workspace", req.NamespacedName)
log.V(0).Info("Reconciling")
// set up a convenient log object so we don't have to type request over and
// over again
log := log.FromContext(ctx)

// Fetch the Workspace instance
var ws v1alpha1.Workspace
Expand Down Expand Up @@ -94,7 +94,7 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

// Cease reconciliation
return r.success(ctx, log, &ws)
return r.success(ctx, &ws)
}

// Manage PVC for workspace
Expand Down Expand Up @@ -190,11 +190,13 @@ func (r *WorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}
}

return r.success(ctx, log, &ws)
return r.success(ctx, &ws)
}

// success marks a successful reconcile
func (r *WorkspaceReconciler) success(ctx context.Context, log logr.Logger, ws *v1alpha1.Workspace) (ctrl.Result, error) {
func (r *WorkspaceReconciler) success(ctx context.Context, ws *v1alpha1.Workspace) (ctrl.Result, error) {
log := log.FromContext(ctx)

if !ws.Status.Reconciled {
ws.Status.Reconciled = true
if err := r.Status().Update(ctx, ws); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/controllers/workspace_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand Down Expand Up @@ -170,7 +169,6 @@ func TestReconcileWorkspaceStatus(t *testing.T) {
r := &WorkspaceReconciler{
Client: cl,
Scheme: scheme.Scheme,
Log: ctrl.Log.WithName("controllers").WithName("Workspace"),
}

req := reconcile.Request{
Expand Down
26 changes: 0 additions & 26 deletions pkg/handlers/workspace_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,6 @@ import (
watchtools "k8s.io/client-go/tools/watch"
)

// WorkspaceInitializing returns a handler that watches a workspace until it
// reports an initializing or ready phase.
func WorkspaceInitializing(runName string) watchtools.ConditionFunc {
return workspaceHandlerWrapper(func(ws *v1alpha1.Workspace) (bool, error) {
switch ws.Status.Phase {
case v1alpha1.WorkspacePhaseInitializing, v1alpha1.WorkspacePhaseReady:
return true, nil
case v1alpha1.WorkspacePhaseError
boldCyan := color.New(color.FgCyan, color.Bold).SprintFunc()
var printedQueue []string
for _, run := range ws.Status.Queue {
if run == runName {
printedQueue = append(printedQueue, boldCyan(run))
} else {
printedQueue = append(printedQueue, run)
}
}
fmt.Printf("Queued: %v\n", printedQueue)
default:
// yet to be queued
return false, nil
}
return false, nil
})
}

// Log queue position until run is at front of queue
func LogQueuePosition(runName string) watchtools.ConditionFunc {
return workspaceHandlerWrapper(func(ws *v1alpha1.Workspace) (bool, error) {
Expand Down

0 comments on commit d508031

Please sign in to comment.