Skip to content

Commit

Permalink
Undo moving of some logger function reliant on shared state
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Mar 13, 2024
1 parent 540b93d commit b191e4c
Show file tree
Hide file tree
Showing 19 changed files with 24 additions and 113 deletions.
3 changes: 1 addition & 2 deletions cmd/timoni/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Check failure on line 145 in cmd/timoni/apply.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerInstance

Check failure on line 145 in cmd/timoni/apply.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerInstance

version := applyArgs.version.String()
if version == "" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func pullArtifactCmdRun(cmd *cobra.Command, args []string) error {
}
ociURL := args[0]

log := logger.LoggerFrom(cmd.Context())
log := LoggerFrom(cmd.Context())

Check failure on line 109 in cmd/timoni/artifact_pull.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: LoggerFrom

Check failure on line 109 in cmd/timoni/artifact_pull.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: LoggerFrom

if err := os.MkdirAll(pullArtifactArgs.output, os.ModePerm); err != nil {
return fmt.Errorf("invalid output path %s: %w", pullArtifactArgs.output, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 122 in cmd/timoni/artifact_push.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: LoggerFrom

Check failure on line 122 in cmd/timoni/artifact_push.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: LoggerFrom
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/artifact_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check failure on line 69 in cmd/timoni/artifact_tag.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: LoggerFrom

Check failure on line 69 in cmd/timoni/artifact_tag.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: LoggerFrom
ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout)
defer cancel()

Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 199 in cmd/timoni/bundle_apply.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundle

Check failure on line 199 in cmd/timoni/bundle_apply.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundle

if !bundleApplyArgs.overwriteOwnership {
err = bundleInstancesOwnershipConflicts(bundle.Instances)
Expand Down Expand Up @@ -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)

Check failure on line 289 in cmd/timoni/bundle_apply.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundleInstance

Check failure on line 289 in cmd/timoni/bundle_apply.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundleInstance

modDir := path.Join(rootDir, instance.Name, "module")
builder := engine.NewModuleBuilder(
Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 128 in cmd/timoni/bundle_delete.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundle

Check failure on line 128 in cmd/timoni/bundle_delete.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundle

if len(instances) == 0 {
log.Error(nil, "no instances found in bundle")
Expand All @@ -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)

Check failure on line 154 in cmd/timoni/bundle_delete.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundle

Check failure on line 154 in cmd/timoni/bundle_delete.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundle

sm, err := runtime.NewResourceManager(kubeconfigArgs)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/timoni/bundle_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 111 in cmd/timoni/bundle_status.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundle

Check failure on line 111 in cmd/timoni/bundle_status.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundle

if len(instances) == 0 {
log.Error(nil, "no instances found in bundle")
Expand All @@ -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)

Check failure on line 120 in cmd/timoni/bundle_status.go

View workflow job for this annotation

GitHub Actions / k8s-bundles

undefined: loggerBundleInstance

Check failure on line 120 in cmd/timoni/bundle_status.go

View workflow job for this annotation

GitHub Actions / k8s-modules

undefined: loggerBundleInstance

log.Info(fmt.Sprintf("last applied %s",
logger.ColorizeSubject(instance.LastTransitionTime)))
Expand Down
7 changes: 3 additions & 4 deletions cmd/timoni/bundle_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand All @@ -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")
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vendor_crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vendor_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/mod_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/runtime_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/timoni/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/dyff/dyff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))

Expand Down
88 changes: 0 additions & 88 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package logger

import (
"context"
"fmt"
"io"
"os"
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit b191e4c

Please sign in to comment.