diff --git a/cmd/timoni/apply.go b/cmd/timoni/apply.go index 086ece95..8f435153 100644 --- a/cmd/timoni/apply.go +++ b/cmd/timoni/apply.go @@ -32,7 +32,6 @@ import ( "github.com/stefanprodan/timoni/internal/engine/fetcher" cueerrors "github.com/stefanprodan/timoni/internal/errors" "github.com/stefanprodan/timoni/internal/flags" - "github.com/stefanprodan/timoni/internal/logger" "github.com/stefanprodan/timoni/internal/runtime" ) @@ -143,7 +142,7 @@ func runApplyCmd(cmd *cobra.Command, args []string) error { applyArgs.name = args[0] applyArgs.module = args[1] - log := logger.LoggerInstance(cmd.Context(), applyArgs.name, true) + log := loggerInstance(cmd.Context(), applyArgs.name, true) version := applyArgs.version.String() if version == "" { diff --git a/cmd/timoni/artifact_pull.go b/cmd/timoni/artifact_pull.go index f7e16923..78bf81d9 100644 --- a/cmd/timoni/artifact_pull.go +++ b/cmd/timoni/artifact_pull.go @@ -106,7 +106,7 @@ func pullArtifactCmdRun(cmd *cobra.Command, args []string) error { } ociURL := args[0] - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) if err := os.MkdirAll(pullArtifactArgs.output, os.ModePerm); err != nil { return fmt.Errorf("invalid output path %s: %w", pullArtifactArgs.output, err) diff --git a/cmd/timoni/artifact_push.go b/cmd/timoni/artifact_push.go index ca7376e4..bc67c3d8 100644 --- a/cmd/timoni/artifact_push.go +++ b/cmd/timoni/artifact_push.go @@ -119,7 +119,7 @@ func pushArtifactCmdRun(cmd *cobra.Command, args []string) error { pushArtifactArgs.ignorePaths = append(pushArtifactArgs.ignorePaths, ps...) } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) defer cancel() diff --git a/cmd/timoni/artifact_tag.go b/cmd/timoni/artifact_tag.go index 24067a79..5feb19cd 100644 --- a/cmd/timoni/artifact_tag.go +++ b/cmd/timoni/artifact_tag.go @@ -66,7 +66,7 @@ func tagArtifactCmdRun(cmd *cobra.Command, args []string) error { spin := logger.StartSpinner("tagging artifact") defer spin.Stop() - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) defer cancel() diff --git a/cmd/timoni/bundle_apply.go b/cmd/timoni/bundle_apply.go index 737d1810..95ca8933 100644 --- a/cmd/timoni/bundle_apply.go +++ b/cmd/timoni/bundle_apply.go @@ -196,7 +196,7 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error { return err } - log := logger.LoggerBundle(cmd.Context(), bundle.Name, cluster.Name, true) + log := loggerBundle(cmd.Context(), bundle.Name, cluster.Name, true) if !bundleApplyArgs.overwriteOwnership { err = bundleInstancesOwnershipConflicts(bundle.Instances) @@ -286,7 +286,7 @@ func fetchBundleInstanceModule(ctx context.Context, instance *engine.BundleInsta } func applyBundleInstance(ctx context.Context, cuectx *cue.Context, instance *engine.BundleInstance, kubeVersion string, rootDir string, diffOutput io.Writer) error { - log := logger.LoggerBundleInstance(ctx, instance.Bundle, instance.Cluster, instance.Name, true) + log := loggerBundleInstance(ctx, instance.Bundle, instance.Cluster, instance.Name, true) modDir := path.Join(rootDir, instance.Name, "module") builder := engine.NewModuleBuilder( diff --git a/cmd/timoni/bundle_delete.go b/cmd/timoni/bundle_delete.go index a499fb31..58aef17e 100644 --- a/cmd/timoni/bundle_delete.go +++ b/cmd/timoni/bundle_delete.go @@ -125,7 +125,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error { return err } - log := logger.LoggerBundle(ctx, bundleDelArgs.name, cluster.Name, true) + log := loggerBundle(ctx, bundleDelArgs.name, cluster.Name, true) if len(instances) == 0 { log.Error(nil, "no instances found in bundle") @@ -151,7 +151,7 @@ func runBundleDelCmd(cmd *cobra.Command, args []string) error { } func deleteBundleInstance(ctx context.Context, instance *engine.BundleInstance, wait bool, dryrun bool) error { - log := logger.LoggerBundle(ctx, instance.Bundle, instance.Cluster, true) + log := loggerBundle(ctx, instance.Bundle, instance.Cluster, true) sm, err := runtime.NewResourceManager(kubeconfigArgs) if err != nil { diff --git a/cmd/timoni/bundle_status.go b/cmd/timoni/bundle_status.go index cf25cf78..2332b75c 100644 --- a/cmd/timoni/bundle_status.go +++ b/cmd/timoni/bundle_status.go @@ -108,7 +108,7 @@ func runBundleStatusCmd(cmd *cobra.Command, args []string) error { return err } - log := logger.LoggerBundle(ctx, bundleStatusArgs.name, cluster.Name, true) + log := loggerBundle(ctx, bundleStatusArgs.name, cluster.Name, true) if len(instances) == 0 { log.Error(nil, "no instances found in bundle") @@ -117,7 +117,7 @@ func runBundleStatusCmd(cmd *cobra.Command, args []string) error { } for _, instance := range instances { - log := logger.LoggerBundleInstance(ctx, bundleStatusArgs.name, cluster.Name, instance.Name, true) + log := loggerBundleInstance(ctx, bundleStatusArgs.name, cluster.Name, instance.Name, true) log.Info(fmt.Sprintf("last applied %s", logger.ColorizeSubject(instance.LastTransitionTime))) diff --git a/cmd/timoni/bundle_vet.go b/cmd/timoni/bundle_vet.go index 051cb98e..e8108ddd 100644 --- a/cmd/timoni/bundle_vet.go +++ b/cmd/timoni/bundle_vet.go @@ -32,7 +32,6 @@ import ( "github.com/stefanprodan/timoni/internal/engine" cueerrors "github.com/stefanprodan/timoni/internal/errors" "github.com/stefanprodan/timoni/internal/flags" - "github.com/stefanprodan/timoni/internal/logger" "github.com/stefanprodan/timoni/internal/runtime" runtimebuild "github.com/stefanprodan/timoni/internal/runtime/build" ) @@ -81,7 +80,7 @@ func init() { } func runBundleVetCmd(cmd *cobra.Command, args []string) error { - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) files := bundleVetArgs.files if len(files) == 0 { return fmt.Errorf("no bundle provided with -f") @@ -175,7 +174,7 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error { return err } - log = logger.LoggerBundle(logr.NewContext(cmd.Context(), log), bundle.Name, apiv1.RuntimeDefaultName, true) + log = loggerBundle(logr.NewContext(cmd.Context(), log), bundle.Name, apiv1.RuntimeDefaultName, true) if len(bundle.Instances) == 0 { return fmt.Errorf("no instances found in bundle") @@ -199,7 +198,7 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error { if i.Namespace == "" { return fmt.Errorf("instance %s does not have a namespace", i.Name) } - log := logger.LoggerBundleInstance(logr.NewContext(cmd.Context(), log), bundle.Name, cluster.Name, i.Name, true) + log := loggerBundleInstance(logr.NewContext(cmd.Context(), log), bundle.Name, cluster.Name, i.Name, true) log.Info("instance is valid") } } diff --git a/cmd/timoni/delete.go b/cmd/timoni/delete.go index 5262dc7e..a5b68f6c 100644 --- a/cmd/timoni/delete.go +++ b/cmd/timoni/delete.go @@ -73,7 +73,7 @@ func runDeleteCmd(cmd *cobra.Command, args []string) error { deleteArgs.name = args[0] - log := logger.LoggerInstance(cmd.Context(), deleteArgs.name, true) + log := loggerInstance(cmd.Context(), deleteArgs.name, true) sm, err := runtime.NewResourceManager(kubeconfigArgs) if err != nil { return err diff --git a/cmd/timoni/mod_init.go b/cmd/timoni/mod_init.go index 2686ae4c..4f2b1b3c 100644 --- a/cmd/timoni/mod_init.go +++ b/cmd/timoni/mod_init.go @@ -77,7 +77,7 @@ func runInitModCmd(cmd *cobra.Command, args []string) error { initModArgs.path = "." } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) if fs, err := os.Stat(initModArgs.path); err != nil || !fs.IsDir() { return fmt.Errorf("path not found: %s", initModArgs.path) diff --git a/cmd/timoni/mod_pull.go b/cmd/timoni/mod_pull.go index bdf4185d..9da09c5e 100644 --- a/cmd/timoni/mod_pull.go +++ b/cmd/timoni/mod_pull.go @@ -123,7 +123,7 @@ func pullCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("invalid output path %s: %w", pullModArgs.output, err) } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) if pullModArgs.verify != "" { err := oci.VerifyArtifact(log, diff --git a/cmd/timoni/mod_push.go b/cmd/timoni/mod_push.go index 89ce1a7d..5ef237d2 100644 --- a/cmd/timoni/mod_push.go +++ b/cmd/timoni/mod_push.go @@ -124,7 +124,7 @@ func pushModCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("module not found at path %s", pushModArgs.module) } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) annotations, err := oci.ParseAnnotations(pushModArgs.annotations) if err != nil { diff --git a/cmd/timoni/mod_vendor_crd.go b/cmd/timoni/mod_vendor_crd.go index 5e1b788d..c60bdbf4 100644 --- a/cmd/timoni/mod_vendor_crd.go +++ b/cmd/timoni/mod_vendor_crd.go @@ -73,7 +73,7 @@ func runVendorCrdCmd(cmd *cobra.Command, args []string) error { vendorCrdArgs.modRoot = args[0] } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) cuectx := cuecontext.New() // Make sure we're importing into a CUE module. diff --git a/cmd/timoni/mod_vendor_k8s.go b/cmd/timoni/mod_vendor_k8s.go index 2b4fc4ca..ac369ad0 100644 --- a/cmd/timoni/mod_vendor_k8s.go +++ b/cmd/timoni/mod_vendor_k8s.go @@ -63,7 +63,7 @@ func runVendorK8sCmd(cmd *cobra.Command, args []string) error { vendorK8sArgs.modRoot = args[0] } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) // Make sure we're importing into a CUE module. cueModDir := path.Join(vendorK8sArgs.modRoot, "cue.mod") diff --git a/cmd/timoni/mod_vet.go b/cmd/timoni/mod_vet.go index c2ac9f79..13092b37 100644 --- a/cmd/timoni/mod_vet.go +++ b/cmd/timoni/mod_vet.go @@ -83,7 +83,7 @@ func runVetModCmd(cmd *cobra.Command, args []string) error { return fmt.Errorf("module not found at path %s", vetModArgs.path) } - log := logger.LoggerFrom(cmd.Context()) + log := LoggerFrom(cmd.Context()) cuectx := cuecontext.New() tmpDir, err := os.MkdirTemp("", apiv1.FieldManager) diff --git a/cmd/timoni/runtime_build.go b/cmd/timoni/runtime_build.go index dc659bb2..1a6a57a5 100644 --- a/cmd/timoni/runtime_build.go +++ b/cmd/timoni/runtime_build.go @@ -95,7 +95,7 @@ func runRuntimeBuildCmd(cmd *cobra.Command, args []string) error { } for _, cluster := range clusters { - log := logger.LoggerRuntime(cmd.Context(), rt.Name, cluster.Name, true) + log := loggerRuntime(cmd.Context(), rt.Name, cluster.Name, true) kubeconfigArgs.Context = &cluster.KubeContext rm, err := runtime.NewResourceManager(kubeconfigArgs) diff --git a/cmd/timoni/status.go b/cmd/timoni/status.go index 3dd83107..de61227c 100644 --- a/cmd/timoni/status.go +++ b/cmd/timoni/status.go @@ -66,7 +66,7 @@ func runStatusCmd(cmd *cobra.Command, args []string) error { statusArgs.name = args[0] - log := logger.LoggerInstance(cmd.Context(), statusArgs.name, true) + log := loggerInstance(cmd.Context(), statusArgs.name, true) rm, err := runtime.NewResourceManager(kubeconfigArgs) if err != nil { return err diff --git a/internal/dyff/dyff.go b/internal/dyff/dyff.go index d515e1f5..701b0ab7 100644 --- a/internal/dyff/dyff.go +++ b/internal/dyff/dyff.go @@ -27,6 +27,7 @@ import ( "github.com/fluxcd/pkg/ssa" ssaerr "github.com/fluxcd/pkg/ssa/errors" ssautil "github.com/fluxcd/pkg/ssa/utils" + "github.com/go-logr/logr" "github.com/gonvenience/ytbx" "github.com/homeport/dyff/pkg/dyff" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -94,7 +95,7 @@ func InstanceDryRunDiff(ctx context.Context, tmpDir string, withDiff bool, w io.Writer) error { - log := logger.LoggerFrom(ctx) + log := logr.FromContextOrDiscard(ctx) diffOpts := ssa.DefaultDiffOptions() sort.Sort(ssa.SortableUnstructureds(objects)) diff --git a/internal/logger/logger.go b/internal/logger/logger.go index afb70b1b..b41abea3 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -17,7 +17,6 @@ limitations under the License. package logger import ( - "context" "fmt" "io" "os" @@ -35,12 +34,8 @@ import ( "github.com/rs/zerolog" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" runtimeLog "sigs.k8s.io/controller-runtime/pkg/log" - - apiv1 "github.com/stefanprodan/timoni/api/v1alpha1" ) -var logger logr.Logger - // NewConsoleLogger returns a human-friendly Logger. // Pretty print adds timestamp, log level and logger.Colorized output to the logs. func NewConsoleLogger(colorize, prettify bool) logr.Logger { @@ -204,89 +199,6 @@ func ColorizeCluster(cluster string) string { return colorCallerPrefix.Sprint("c:") + colorInstance.Sprint(cluster) } -func LoggerBundle(ctx context.Context, bundle, cluster string, prettify bool) logr.Logger { - switch cluster { - case apiv1.RuntimeDefaultName: - if !prettify { - return LoggerFrom(ctx, "bundle", bundle) - } - return LoggerFrom(ctx, "caller", ColorizeBundle(bundle)) - default: - if !prettify { - return LoggerFrom(ctx, "bundle", bundle, "cluster", cluster) - } - return LoggerFrom(ctx, "caller", - fmt.Sprintf("%s %s %s", - ColorizeBundle(bundle), - color.CyanString(">"), - ColorizeCluster(cluster))) - } -} - -func LoggerInstance(ctx context.Context, instance string, prettify bool) logr.Logger { - if !prettify { - return LoggerFrom(ctx, "instance", instance) - } - return LoggerFrom(ctx, "caller", ColorizeInstance(instance)) -} - -func LoggerBundleInstance(ctx context.Context, bundle, cluster, instance string, prettify bool) logr.Logger { - switch cluster { - case apiv1.RuntimeDefaultName: - if !prettify { - return LoggerFrom(ctx, "bundle", bundle, "instance", instance) - } - return LoggerFrom(ctx, "caller", - fmt.Sprintf("%s %s %s", - ColorizeBundle(bundle), - color.CyanString(">"), - ColorizeInstance(instance))) - default: - if !prettify { - return LoggerFrom(ctx, "bundle", bundle, "cluster", cluster, "instance", instance) - } - return LoggerFrom(ctx, "caller", - fmt.Sprintf("%s %s %s %s %s", - ColorizeBundle(bundle), - color.CyanString(">"), - ColorizeCluster(cluster), - color.CyanString(">"), - ColorizeInstance(instance))) - - } -} - -func LoggerRuntime(ctx context.Context, runtime, cluster string, prettify bool) logr.Logger { - switch cluster { - case apiv1.RuntimeDefaultName: - if !prettify { - return LoggerFrom(ctx, "runtime", runtime) - } - return LoggerFrom(ctx, "caller", ColorizeRuntime(runtime)) - default: - if !prettify { - return LoggerFrom(ctx, "runtime", runtime, "cluster", cluster) - } - return LoggerFrom(ctx, "caller", - fmt.Sprintf("%s %s %s", ColorizeRuntime(runtime), - color.CyanString(">"), ColorizeCluster(cluster))) - } -} - -// LoggerFrom returns a logr.Logger with predefined values from a context.Context. -func LoggerFrom(ctx context.Context, keysAndValues ...interface{}) logr.Logger { - if logger.IsZero() { - logger = NewConsoleLogger(false, false) - } - newLogger := logger - if ctx != nil { - if l, err := logr.FromContext(ctx); err == nil { - newLogger = l - } - } - return newLogger.WithValues(keysAndValues...) -} - // StartSpinner starts a spinner with the given message. func StartSpinner(msg string) *spinner.Spinner { s := spinner.New(spinner.CharSets[11], 100*time.Millisecond, spinner.WithWriter(os.Stderr))