From d8bfa99829c98458d12385f9215efb52eb0f688a Mon Sep 17 00:00:00 2001 From: knqyf263 Date: Wed, 2 Oct 2024 10:18:57 +0400 Subject: [PATCH] chore: change prefixes from checks to misconfig Signed-off-by: knqyf263 --- pkg/commands/artifact/run.go | 8 ++++---- pkg/commands/operation/operation.go | 1 - pkg/k8s/commands/cluster.go | 14 +++++--------- pkg/log/logger.go | 1 - pkg/version/version.go | 2 +- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/pkg/commands/artifact/run.go b/pkg/commands/artifact/run.go index f19c5aa5ac3e..eeca48436605 100644 --- a/pkg/commands/artifact/run.go +++ b/pkg/commands/artifact/run.go @@ -618,8 +618,8 @@ func (r *runner) scan(ctx context.Context, opts flag.Options, initializeScanner } func initMisconfScannerOption(ctx context.Context, opts flag.Options) (misconf.ScannerOption, error) { - logger := log.WithPrefix(log.PrefixMisconfiguration) - logger.Info("Misconfiguration scanning is enabled") + ctx = log.WithContextPrefix(ctx, log.PrefixMisconfiguration) + log.InfoContext(ctx, "Misconfiguration scanning is enabled") var downloadedPolicyPaths []string var disableEmbedded bool @@ -627,10 +627,10 @@ func initMisconfScannerOption(ctx context.Context, opts flag.Options) (misconf.S downloadedPolicyPaths, err := operation.InitBuiltinChecks(ctx, opts.CacheDir, opts.Quiet, opts.SkipCheckUpdate, opts.MisconfOptions.ChecksBundleRepository, opts.RegistryOpts()) if err != nil { if !opts.SkipCheckUpdate { - logger.Error("Falling back to embedded checks", log.Err(err)) + log.ErrorContext(ctx, "Falling back to embedded checks", log.Err(err)) } } else { - logger.Debug("Checks successfully loaded from disk") + log.DebugContext(ctx, "Checks successfully loaded from disk") disableEmbedded = true } diff --git a/pkg/commands/operation/operation.go b/pkg/commands/operation/operation.go index 8e4a10472489..ac52eee7fb1e 100644 --- a/pkg/commands/operation/operation.go +++ b/pkg/commands/operation/operation.go @@ -82,7 +82,6 @@ func InitBuiltinChecks(ctx context.Context, cacheDir string, quiet, skipUpdate b mu.Lock() defer mu.Unlock() - ctx = log.WithContextPrefix(ctx, log.PrefixChecksBundle) client, err := policy.NewClient(cacheDir, quiet, checkBundleRepository) if err != nil { return nil, xerrors.Errorf("check client error: %w", err) diff --git a/pkg/k8s/commands/cluster.go b/pkg/k8s/commands/cluster.go index 00d381a63cd5..11dd5e0d3a00 100644 --- a/pkg/k8s/commands/cluster.go +++ b/pkg/k8s/commands/cluster.go @@ -37,7 +37,7 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err trivyk8s.WithExcludeOwned(opts.ExcludeOwned), } if opts.Scanners.AnyEnabled(types.MisconfigScanner) && !opts.DisableNodeCollector { - artifacts, err = trivyk8s.New(cluster, k8sOpts...).ListArtifactAndNodeInfo(ctx, nodeCollectorOptions(opts)...) + artifacts, err = trivyk8s.New(cluster, k8sOpts...).ListArtifactAndNodeInfo(ctx, nodeCollectorOptions(ctx, opts)...) if err != nil { return xerrors.Errorf("get k8s artifacts with node info error: %w", err) } @@ -59,7 +59,7 @@ func clusterRun(ctx context.Context, opts flag.Options, cluster k8s.Cluster) err return runner.run(ctx, artifacts) } -func nodeCollectorOptions(opts flag.Options) []trivyk8s.NodeCollectorOption { +func nodeCollectorOptions(ctx context.Context, opts flag.Options) []trivyk8s.NodeCollectorOption { nodeCollectorOptions := []trivyk8s.NodeCollectorOption{ trivyk8s.WithScanJobNamespace(opts.NodeCollectorNamespace), trivyk8s.WithIgnoreLabels(opts.ExcludeNodes), @@ -67,13 +67,9 @@ func nodeCollectorOptions(opts flag.Options) []trivyk8s.NodeCollectorOption { trivyk8s.WithTolerations(opts.Tolerations), } - contentPath, err := operation.InitBuiltinChecks(context.Background(), - opts.CacheDir, - opts.Quiet, - opts.SkipCheckUpdate, - opts.MisconfOptions.ChecksBundleRepository, - opts.RegistryOpts()) - + ctx = log.WithContextPrefix(ctx, log.PrefixMisconfiguration) + contentPath, err := operation.InitBuiltinChecks(ctx, opts.CacheDir, opts.Quiet, opts.SkipCheckUpdate, + opts.MisconfOptions.ChecksBundleRepository, opts.RegistryOpts()) if err != nil { log.Error("Falling back to embedded checks", log.Err(err)) nodeCollectorOptions = append(nodeCollectorOptions, diff --git a/pkg/log/logger.go b/pkg/log/logger.go index 302a98daf991..2948886b9559 100644 --- a/pkg/log/logger.go +++ b/pkg/log/logger.go @@ -25,7 +25,6 @@ const ( PrefixLicense = "license" PrefixVulnerabilityDB = "vulndb" PrefixJavaDB = "javadb" - PrefixChecksBundle = "checks" ) // Logger is an alias of slog.Logger diff --git a/pkg/version/version.go b/pkg/version/version.go index 4a8870dd4f79..17da77a2a1bd 100644 --- a/pkg/version/version.go +++ b/pkg/version/version.go @@ -81,7 +81,7 @@ func NewVersionInfo(cacheDir string) VersionInfo { log.Debug("Failed to instantiate policy client", log.Err(err)) } if pc != nil && err == nil { - ctx := log.WithContextPrefix(context.TODO(), log.PrefixChecksBundle) + ctx := log.WithContextPrefix(context.TODO(), log.PrefixMisconfiguration) pbMetaRaw, err := pc.GetMetadata(ctx) if err != nil {