diff --git a/cleanup_command.go b/cleanup_command.go index 0168be3c..0375c54b 100644 --- a/cleanup_command.go +++ b/cleanup_command.go @@ -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), diff --git a/count_command.go b/count_command.go index 147b8836..67bcc423 100644 --- a/count_command.go +++ b/count_command.go @@ -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), diff --git a/operator_command.go b/operator_command.go index adfb1397..4ead3790 100644 --- a/operator_command.go +++ b/operator_command.go @@ -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"), diff --git a/pkg/cleanupcmd/run.go b/pkg/cleanupcmd/run.go index 61122c0c..46bae811 100644 --- a/pkg/cleanupcmd/run.go +++ b/pkg/cleanupcmd/run.go @@ -2,7 +2,6 @@ package cleanupcmd import ( "context" - "fmt" "os" "path/filepath" @@ -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" ) @@ -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 } @@ -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 { @@ -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) @@ -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) } diff --git a/pkg/countcmd/run.go b/pkg/countcmd/run.go index acc37636..e6a01d17 100644 --- a/pkg/countcmd/run.go +++ b/pkg/countcmd/run.go @@ -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" ) @@ -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) @@ -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{ @@ -136,14 +135,14 @@ 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 { @@ -151,11 +150,11 @@ func (c *Command) updateTask(ctx *commandContext) error { 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) } diff --git a/pkg/internal/utils/utils_test.go b/pkg/internal/utils/utils_test.go new file mode 100644 index 00000000..d4b585bf --- /dev/null +++ b/pkg/internal/utils/utils_test.go @@ -0,0 +1 @@ +package utils diff --git a/pkg/operator/command.go b/pkg/operator/command.go index d64b0ad0..7ead950e 100644 --- a/pkg/operator/command.go +++ b/pkg/operator/command.go @@ -17,7 +17,6 @@ type Command struct { Log logr.Logger LeaderElectionEnabled bool - FfmpegImage string } type commandContext struct { diff --git a/pkg/scancmd/run.go b/pkg/scancmd/run.go index 01ca11e0..0d7471f7 100644 --- a/pkg/scancmd/run.go +++ b/pkg/scancmd/run.go @@ -3,7 +3,6 @@ package scancmd import ( "context" "errors" - "fmt" "os" "path/filepath" "strings" @@ -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" ) @@ -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 } @@ -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, @@ -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 { @@ -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 { @@ -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) @@ -198,7 +198,8 @@ 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 @@ -206,8 +207,8 @@ func (c *Command) abortIfNoMatchFound(ctx *commandContext, err error) error { 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, diff --git a/scan_command.go b/scan_command.go index 85ff404c..b5eb3975 100644 --- a/scan_command.go +++ b/scan_command.go @@ -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),