Skip to content

Commit

Permalink
Alternative way to define the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ccremer committed Aug 21, 2022
1 parent 59b447c commit 7112295
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 40 deletions.
6 changes: 3 additions & 3 deletions cleanup_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func newCleanupCommand() *cli.Command {
Name: "cleanup",
Usage: "Remove intermediary files and finish the task",
Before: LogMetadata,
Action: func(context *cli.Context) error {
ctx := SetLogger(context)
return command.Execute(ctx)
Action: func(ctx *cli.Context) error {
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
return command.Execute(ctx.Context)
},
Flags: []cli.Flag{
newTaskNameFlag(&command.TaskName),
Expand Down
6 changes: 3 additions & 3 deletions count_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func newCountCommand() *cli.Command {
Name: "count",
Usage: "Counts the number of generated intermediary media files",
Before: LogMetadata,
Action: func(context *cli.Context) error {
ctx := SetLogger(context)
return command.Execute(ctx)
Action: func(ctx *cli.Context) error {
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
return command.Execute(ctx.Context)
},
Flags: []cli.Flag{
newTaskNameFlag(&command.TaskName),
Expand Down
7 changes: 4 additions & 3 deletions operator_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func newOperatorCommand() *cli.Command {
Name: "operator",
Usage: "Start provider in operator mode",
Before: LogMetadata,
Action: func(c *cli.Context) error {
blueprintcontroller.ScanRoleKind = c.String(newScanRoleKindFlag().Name)
return command.Execute(c.Context)
Action: func(ctx *cli.Context) error {
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
blueprintcontroller.ScanRoleKind = ctx.String(newScanRoleKindFlag().Name)
return command.Execute(ctx.Context)
},
Flags: []cli.Flag{
&cli.BoolFlag{Name: "leader-election-enabled", Value: false, EnvVars: envVars("LEADER_ELECTION_ENABLED"),
Expand Down
15 changes: 7 additions & 8 deletions pkg/cleanupcmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cleanupcmd

import (
"context"
"fmt"
"os"
"path/filepath"

Expand All @@ -12,7 +11,6 @@ import (
pipeline "github.com/ccremer/go-command-pipeline"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand Down Expand Up @@ -62,14 +60,14 @@ func (c *Command) createClient(ctx *commandContext) error {

func (c *Command) fetchTask(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient)
log := c.getLogger()

log := ctx.getLogger()
task := &v1alpha1.Task{}
if err := ctx.kube.Get(ctx, types.NamespacedName{Namespace: c.Namespace, Name: c.TaskName}, task); err != nil {
return err
}
ctx.task = task
log.Info("fetched task", "task", fmt.Sprintf("%s/%s", task.Namespace, task.Name))
log.Info("fetched task")
return nil
}

Expand All @@ -83,7 +81,8 @@ func (c *Command) listIntermediaryFiles(ctx *commandContext) error {

func (c *Command) deleteFiles(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.listIntermediaryFiles)
log := ctx.getLogger()
log := c.getLogger()

for _, file := range ctx.intermediaryFiles {
log.Info("deleting file", "file", file)
if err := os.Remove(file); err != nil {
Expand All @@ -95,7 +94,7 @@ func (c *Command) deleteFiles(ctx *commandContext) error {

func (c *Command) deleteSourceFile(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.fetchTask)
log := ctx.getLogger()
log := c.getLogger()

sourceFile := filepath.Join(c.SourceRootDir, internaltypes.SourceSubMountPath, ctx.task.Spec.SourceUrl.GetPath())
log.Info("deleting file", "file", sourceFile)
Expand All @@ -107,6 +106,6 @@ func (c *Command) deleteTask(ctx *commandContext) error {
return ctx.kube.Delete(ctx.Context, ctx.task)
}

func (c *commandContext) getLogger() logr.Logger {
return ctrl.LoggerFrom(c.Context).WithName("cleanup")
func (c *Command) getLogger() logr.Logger {
return c.Log.WithValues("task_name", c.TaskName, "namespace", c.Namespace)
}
19 changes: 9 additions & 10 deletions pkg/countcmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -67,20 +66,20 @@ func (c *Command) createClient(ctx *commandContext) error {

func (c *Command) fetchTask(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient)
log := c.getLogger()

log := ctx.getLogger()
task := &v1alpha1.Task{}
if err := ctx.kube.Get(ctx, types.NamespacedName{Namespace: c.Namespace, Name: c.TaskName}, task); err != nil {
return err
}
ctx.task = task
log.Info("fetched task", "task", fmt.Sprintf("%s/%s", task.Namespace, task.Name))
log.Info("fetched task")
return nil
}

func (c *Command) scanSegmentFiles(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.fetchTask)
log := ctx.getLogger()
log := c.getLogger()

prefix := ctx.task.Spec.TaskId.String() + "_"
files := make([]string, 0)
Expand Down Expand Up @@ -115,7 +114,7 @@ func matchesTaskSegment(path string, prefix string) bool {

func (c *Command) ensureConfigMap(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient, c.fetchTask, c.scanSegmentFiles)
log := ctx.getLogger()
log := c.getLogger()

task := ctx.task
cm := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{
Expand All @@ -136,26 +135,26 @@ func (c *Command) ensureConfigMap(ctx *commandContext) error {
return controllerutil.SetOwnerReference(task, cm, ctx.kube.Scheme())
})
if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
log.Info("Updated config map", "name", cm.Name)
log.Info("Updated config map", "configmap", cm.Name)
}
return err
}

func (c *Command) updateTask(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient)
log := ctx.getLogger()
log := c.getLogger()

task := ctx.task
op, err := controllerutil.CreateOrPatch(ctx, ctx.kube, task, func() error {
task.Spec.SlicesPlannedCount = len(ctx.segmentFiles)
return nil
})
if op == controllerutil.OperationResultCreated || op == controllerutil.OperationResultUpdated {
log.Info("Updated task", "name", task.Name)
log.Info("Updated task")
}
return err
}

func (c *commandContext) getLogger() logr.Logger {
return ctrl.LoggerFrom(c.Context).WithName("count")
func (c *Command) getLogger() logr.Logger {
return c.Log.WithValues("task_name", c.TaskName, "namespace", c.Namespace)
}
1 change: 1 addition & 0 deletions pkg/internal/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package utils
1 change: 0 additions & 1 deletion pkg/operator/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Command struct {
Log logr.Logger

LeaderElectionEnabled bool
FfmpegImage string
}

type commandContext struct {
Expand Down
19 changes: 10 additions & 9 deletions pkg/scancmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scancmd
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -17,7 +16,6 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/uuid"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
Expand Down Expand Up @@ -73,14 +71,14 @@ func (c *Command) createClient(ctx *commandContext) error {

func (c *Command) fetchBlueprint(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient)
log := c.getLogger()

log := ctx.getLogger()
blueprint := &v1alpha1.Blueprint{}
if err := ctx.kube.Get(ctx, types.NamespacedName{Namespace: c.Namespace, Name: c.BlueprintName}, blueprint); err != nil {
return err
}
ctx.blueprint = blueprint
log.Info("fetched blueprint", "blueprint", fmt.Sprintf("%s/%s", blueprint.Namespace, blueprint.Name))
log.Info("fetched blueprint")
return nil
}

Expand All @@ -90,6 +88,7 @@ func (c *Command) hasFreeTaskSlots(ctx *commandContext) bool {

func (c *Command) fetchCurrentTasks(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.createClient, c.fetchBlueprint)
log := c.getLogger()

taskList := v1alpha1.TaskList{}
err := ctx.kube.List(ctx, &taskList,
Expand All @@ -111,12 +110,14 @@ func (c *Command) fetchCurrentTasks(ctx *commandContext) error {
}
}
}
log.Info("fetched current tasks", "count", len(filteredTasks))
ctx.currentTasks = filteredTasks
return nil
}

func (c *Command) selectNewFile(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.fetchBlueprint, c.fetchCurrentTasks)
log := c.getLogger()

alreadyQueuedFiles := make([]string, len(ctx.currentTasks))
for i, task := range ctx.currentTasks {
Expand All @@ -125,7 +126,6 @@ func (c *Command) selectNewFile(ctx *commandContext) error {

var foundFileErr = errors.New("found")

log := ctx.getLogger()
root := filepath.Join(c.SourceRootDir, internaltypes.SourceSubMountPath)
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *Command) getAbsolutePath(uri v1alpha1.ClusterCodeUrl) string {

func (c *Command) createTask(ctx *commandContext) error {
ctx.dependencyResolver.MustRequireDependencyByFuncName(c.selectNewFile)
log := ctx.getLogger()
log := c.getLogger()

bp := ctx.blueprint
selectedFile, err := filepath.Rel(filepath.Join(c.SourceRootDir, internaltypes.SourceSubMountPath), ctx.selectedRelPath)
Expand Down Expand Up @@ -198,16 +198,17 @@ func (c *Command) createTask(ctx *commandContext) error {
}

func (c *Command) abortIfNoMatchFound(ctx *commandContext, err error) error {
log := ctx.getLogger()
log := c.getLogger()

if errors.Is(err, noMatchFoundErr) {
log.Info("no media files found")
return nil
}
return err
}

func (c *commandContext) getLogger() logr.Logger {
return ctrl.LoggerFrom(c.Context).WithName("scan")
func (c *Command) getLogger() logr.Logger {
return c.Log.WithValues("blueprint", c.BlueprintName, "namespace", c.Namespace)
}

// containsExtension returns true if the given extension is in the given acceptableFileExtensions. For each entry in the list,
Expand Down
6 changes: 3 additions & 3 deletions scan_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ func newScanCommand() *cli.Command {
Name: "scan",
Usage: "Scan source storage for new files and queue task",
Before: LogMetadata,
Action: func(c *cli.Context) error {
ctx := SetLogger(c)
return command.Execute(ctx)
Action: func(ctx *cli.Context) error {
command.Log = AppLogger(ctx).WithName(ctx.Command.Name)
return command.Execute(ctx.Context)
},
Flags: []cli.Flag{
newBlueprintNameFlag(&command.BlueprintName),
Expand Down

0 comments on commit 7112295

Please sign in to comment.