Skip to content

Commit

Permalink
Define well known log verbosity values and log less by default (#617)
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Wall <richard.wall@venafi.com>
  • Loading branch information
wallrj authored Nov 14, 2024
1 parent 2797067 commit 1690dd9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
17 changes: 9 additions & 8 deletions pkg/agent/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/datagatherer/k8s/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/logs/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/logs/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`,
Expand Down

0 comments on commit 1690dd9

Please sign in to comment.