Skip to content

Commit 1690dd9

Browse files
authored
Define well known log verbosity values and log less by default (#617)
Signed-off-by: Richard Wall <richard.wall@venafi.com>
1 parent 2797067 commit 1690dd9

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

pkg/agent/run.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/jetstack/preflight/pkg/client"
3636
"github.com/jetstack/preflight/pkg/datagatherer"
3737
"github.com/jetstack/preflight/pkg/kubeconfig"
38+
"github.com/jetstack/preflight/pkg/logs"
3839
"github.com/jetstack/preflight/pkg/version"
3940
)
4041

@@ -175,7 +176,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) {
175176
return fmt.Errorf("failed to instantiate %q data gatherer %q: %v", kind, dgConfig.Name, err)
176177
}
177178

178-
log.Info("Starting DataGatherer", "name", dgConfig.Name)
179+
log.V(logs.Debug).Info("Starting DataGatherer", "name", dgConfig.Name)
179180

180181
// start the data gatherers and wait for the cache sync
181182
group.Go(func() error {
@@ -285,7 +286,7 @@ func gatherAndOutputData(ctx context.Context, eventf Eventf, config CombinedConf
285286
var readings []*api.DataReading
286287

287288
if config.InputPath != "" {
288-
log.Info("Reading data from local file", "inputPath", config.InputPath)
289+
log.V(logs.Debug).Info("Reading data from local file", "inputPath", config.InputPath)
289290
data, err := os.ReadFile(config.InputPath)
290291
if err != nil {
291292
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
351352
if count >= 0 {
352353
log = log.WithValues("count", count)
353354
}
354-
log.Info("Successfully gathered", "name", k)
355+
log.V(logs.Debug).Info("Successfully gathered", "name", k)
355356
}
356357
readings = append(readings, &api.DataReading{
357358
ClusterID: config.ClusterID,
@@ -385,7 +386,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
385386
log := klog.FromContext(ctx).WithName("postData")
386387
baseURL := config.Server
387388

388-
log.Info("Posting data", "baseURL", baseURL)
389+
log.V(logs.Debug).Info("Posting data", "baseURL", baseURL)
389390

390391
if config.AuthMode == VenafiCloudKeypair || config.AuthMode == VenafiCloudVenafiConnection {
391392
// orgID and clusterID are not required for Venafi Cloud auth
@@ -467,7 +468,7 @@ func postData(ctx context.Context, config CombinedConfig, preflightClient client
467468
func listenAndServe(ctx context.Context, server *http.Server) error {
468469
log := klog.FromContext(ctx).WithName("ListenAndServe")
469470

470-
log.V(1).Info("Starting")
471+
log.V(logs.Debug).Info("Starting")
471472

472473
listenCTX, listenCancelCause := context.WithCancelCause(context.WithoutCancel(ctx))
473474
go func() {
@@ -477,11 +478,11 @@ func listenAndServe(ctx context.Context, server *http.Server) error {
477478

478479
select {
479480
case <-listenCTX.Done():
480-
log.V(1).Info("Shutdown skipped", "reason", "Server already stopped")
481+
log.V(logs.Debug).Info("Shutdown skipped", "reason", "Server already stopped")
481482
return context.Cause(listenCTX)
482483

483484
case <-ctx.Done():
484-
log.V(1).Info("Shutting down")
485+
log.V(logs.Debug).Info("Shutting down")
485486
}
486487

487488
shutdownCTX, shutdownCancel := context.WithTimeout(context.WithoutCancel(ctx), time.Second*3)
@@ -496,7 +497,7 @@ func listenAndServe(ctx context.Context, server *http.Server) error {
496497
closeErr = fmt.Errorf("Close: %s", closeErr)
497498
}
498499

499-
log.V(1).Info("Shutdown complete")
500+
log.V(logs.Debug).Info("Shutdown complete")
500501

501502
return errors.Join(shutdownErr, closeErr)
502503
}

pkg/datagatherer/k8s/dynamic.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/jetstack/preflight/api"
2929
"github.com/jetstack/preflight/pkg/datagatherer"
30+
"github.com/jetstack/preflight/pkg/logs"
3031
)
3132

3233
// ConfigDynamic contains the configuration for the data-gatherer.
@@ -273,7 +274,7 @@ func (g *DataGathererDynamic) Run(stopCh <-chan struct{}) error {
273274
// attach WatchErrorHandler, it needs to be set before starting an informer
274275
err := g.informer.SetWatchErrorHandler(func(r *k8scache.Reflector, err error) {
275276
if strings.Contains(fmt.Sprintf("%s", err), "the server could not find the requested resource") {
276-
log.Info("server missing resource for datagatherer", "groupVersionResource", g.groupVersionResource)
277+
log.V(logs.Debug).Info("Server missing resource for datagatherer", "groupVersionResource", g.groupVersionResource)
277278
} else {
278279
log.Info("datagatherer informer has failed and is backing off", "groupVersionResource", g.groupVersionResource, "reason", err)
279280
}

pkg/logs/logs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ var (
4949
features = featuregate.NewFeatureGate()
5050
)
5151

52+
const (
53+
// Standard log verbosity levels.
54+
// Use these instead of integers in venafi-kubernetes-agent code.
55+
Info = 0
56+
Debug = 1
57+
Trace = 2
58+
)
59+
5260
func init() {
5361
runtime.Must(logsapi.AddFeatureGates(features))
5462
// Turn on ALPHA options to enable the split-stream logging options.
@@ -94,6 +102,7 @@ func AddFlags(fs *pflag.FlagSet) {
94102
if f.Name == "v" {
95103
f.Name = "log-level"
96104
f.Shorthand = "v"
105+
f.Usage = fmt.Sprintf("%s. 0=Info, 1=Debug, 2=Trace. Use 3-10 for even greater detail. (default: 0)", f.Usage)
97106
}
98107
})
99108
fs.AddFlagSet(&tfs)

pkg/logs/logs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func TestLogs(t *testing.T) {
8383
name: "help",
8484
flags: "-h",
8585
expectStdout: `
86-
-v, --log-level Level number for the log level verbosity
86+
-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)
8787
--logging-format string Sets the log format. Permitted formats: "json", "text". (default "text")
8888
--vmodule pattern=N,... comma-separated list of pattern=N settings for file-filtered logging (only works for text log format)
8989
`,

0 commit comments

Comments
 (0)