From 7d19d0a73715324380c2a3129b5cc435fceb7def Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Tue, 12 Nov 2024 16:20:47 +0000 Subject: [PATCH] Define well known log verbosity values and log less by default Signed-off-by: Richard Wall --- pkg/agent/run.go | 17 +++++++++-------- pkg/datagatherer/k8s/dynamic.go | 3 ++- pkg/logs/logs.go | 9 +++++++++ pkg/logs/logs_test.go | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/agent/run.go b/pkg/agent/run.go index 7bb629ed..0fa7730b 100644 --- a/pkg/agent/run.go +++ b/pkg/agent/run.go @@ -35,6 +35,7 @@ import ( "github.com/jetstack/preflight/pkg/client" "github.com/jetstack/preflight/pkg/datagatherer" "github.com/jetstack/preflight/pkg/kubeconfig" + "github.com/jetstack/preflight/pkg/logs" "github.com/jetstack/preflight/pkg/version" ) @@ -175,7 +176,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) { return fmt.Errorf("failed to instantiate %q data gatherer %q: %v", kind, dgConfig.Name, err) } - log.Info("Starting DataGatherer", "name", dgConfig.Name) + log.V(logs.Debug).Info("Starting DataGatherer", "name", dgConfig.Name) // start the data gatherers and wait for the cache sync group.Go(func() error { @@ -285,7 +286,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf var readings []*api.DataReading if config.InputPath != "" { - log.Info("Reading data from local file", "inputPath", config.InputPath) + log.V(logs.Debug).Info("Reading data from local file", "inputPath", config.InputPath) data, err := os.ReadFile(config.InputPath) if err != nil { return fmt.Errorf("failed to read local data file: %s", err) @@ -351,7 +352,7 @@ func gatherData(ctx context.Context, config CombinedConfig, dataGatherers map[st if count >= 0 { log = log.WithValues("count", count) } - log.Info("Successfully gathered", "name", k) + log.V(logs.Debug).Info("Successfully gathered", "name", k) } readings = append(readings, &api.DataReading{ ClusterID: config.ClusterID, @@ -385,7 +386,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client log := klog.FromContext(ctx).WithName("postData") baseURL := config.Server - log.Info("Posting data", "baseURL", baseURL) + log.V(logs.Debug).Info("Posting data", "baseURL", baseURL) if config.AuthMode == VenafiCloudKeypair || config.AuthMode == VenafiCloudVenafiConnection { // orgID and clusterID are not required for Venafi Cloud auth @@ -467,7 +468,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client func listenAndServe(ctx context.Context, server *http.Server) error { log := klog.FromContext(ctx).WithName("ListenAndServe") - log.V(1).Info("Starting") + log.V(logs.Debug).Info("Starting") listenCTX, listenCancelCause := context.WithCancelCause(context.WithoutCancel(ctx)) go func() { @@ -477,11 +478,11 @@ func listenAndServe(ctx context.Context, server *http.Server) error { select { case <-listenCTX.Done(): - log.V(1).Info("Shutdown skipped", "reason", "Server already stopped") + log.V(logs.Debug).Info("Shutdown skipped", "reason", "Server already stopped") return context.Cause(listenCTX) case <-ctx.Done(): - log.V(1).Info("Shutting down") + log.V(logs.Debug).Info("Shutting down") } shutdownCTX, shutdownCancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second*3) @@ -496,7 +497,7 @@ func listenAndServe(ctx context.Context, server *http.Server) error { closeErr = fmt.Errorf("Close: %s", closeErr) } - log.V(1).Info("Shutdown complete") + log.V(logs.Debug).Info("Shutdown complete") return errors.Join(shutdownErr, closeErr) } diff --git a/pkg/datagatherer/k8s/dynamic.go b/pkg/datagatherer/k8s/dynamic.go index e80dc75d..68753648 100644 --- a/pkg/datagatherer/k8s/dynamic.go +++ b/pkg/datagatherer/k8s/dynamic.go @@ -27,6 +27,7 @@ import ( "github.com/jetstack/preflight/api" "github.com/jetstack/preflight/pkg/datagatherer" + "github.com/jetstack/preflight/pkg/logs" ) // ConfigDynamic contains the configuration for the data-gatherer. @@ -273,7 +274,7 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error { // attach WatchErrorHandler, it needs to be set before starting an informer err := g.informer.SetWatchErrorHandler(func(r *k8scache.Reflector, err error) { if strings.Contains(fmt.Sprintf("%s", err), "the server could not find the requested resource") { - log.Info("server missing resource for datagatherer", "groupVersionResource", g.groupVersionResource) + log.V(logs.Debug).Info("Server missing resource for datagatherer", "groupVersionResource", g.groupVersionResource) } else { log.Info("datagatherer informer has failed and is backing off", "groupVersionResource", g.groupVersionResource, "reason", err) } diff --git a/pkg/logs/logs.go b/pkg/logs/logs.go index 35366ab4..cbca5b13 100644 --- a/pkg/logs/logs.go +++ b/pkg/logs/logs.go @@ -49,6 +49,14 @@ var ( features = featuregate.NewFeatureGate() ) +const ( + // Standard log verbosity levels. + // Use these instead of integers in venafi-kubernetes-agent code. + Info = 0 + Debug = 1 + Trace = 2 +) + func init() { runtime.Must(logsapi.AddFeatureGates(features)) // Turn on ALPHA options to enable the split-stream logging options. @@ -94,6 +102,7 @@ func AddFlags(fs *pflag.FlagSet) { if f.Name == "v" { f.Name = "log-level" f.Shorthand = "v" + f.Usage = fmt.Sprintf("%s. 0=Info, 1=Debug, 2=Trace. Use 3-10 for even greater detail. (default: 0)", f.Usage) } }) fs.AddFlagSet(&tfs) diff --git a/pkg/logs/logs_test.go b/pkg/logs/logs_test.go index e3869df6..f48a1b4c 100644 --- a/pkg/logs/logs_test.go +++ b/pkg/logs/logs_test.go @@ -83,7 +83,7 @@ func TestLogs(t *testing.T) { name: "help", flags: "-h", expectStdout: ` - -v, --log-level Level number for the log level verbosity + -v, --log-level Level number for the log level verbosity. 0=Info, 1=Debug, 2=Trace. Use 3-10 for even greater detail. (default: 0) --logging-format string Sets the log format. Permitted formats: "json", "text". (default "text") --vmodule pattern=N,... comma-separated list of pattern=N settings for file-filtered logging (only works for text log format) `,