@@ -7,12 +7,14 @@ import (
77 "errors"
88 "fmt"
99 "io"
10+ "log/slog"
1011 "net/http"
1112 "os"
1213 "strings"
1314 "time"
1415
1516 "github.com/cenkalti/backoff"
17+ "github.com/go-logr/logr"
1618 "github.com/hashicorp/go-multierror"
1719 "github.com/prometheus/client_golang/prometheus"
1820 "github.com/prometheus/client_golang/prometheus/promhttp"
@@ -31,7 +33,6 @@ import (
3133 "github.com/jetstack/preflight/pkg/client"
3234 "github.com/jetstack/preflight/pkg/datagatherer"
3335 "github.com/jetstack/preflight/pkg/kubeconfig"
34- "github.com/jetstack/preflight/pkg/logs"
3536 "github.com/jetstack/preflight/pkg/version"
3637
3738 "net/http/pprof"
@@ -49,7 +50,7 @@ const schemaVersion string = "v2.0.0"
4950
5051// Run starts the agent process
5152func Run (cmd * cobra.Command , args []string ) error {
52- logs . Log . Printf ( "Preflight agent version: %s (%s) " , version .PreflightVersion , version .Commit )
53+ slog . Info ( "Starting agent" , " version" , version .PreflightVersion , "commit" , version .Commit )
5354 ctx , cancel := context .WithCancel (context .Background ())
5455 defer cancel ()
5556
@@ -69,7 +70,7 @@ func Run(cmd *cobra.Command, args []string) error {
6970 return fmt .Errorf ("Failed to parse config file: %s" , err )
7071 }
7172
72- config , preflightClient , err := ValidateAndCombineConfig (logs . Log , cfg , Flags )
73+ config , preflightClient , err := ValidateAndCombineConfig (logr . FromSlogHandler ( slog . Default (). Handler ()) , cfg , Flags )
7374 if err != nil {
7475 return fmt .Errorf ("While evaluating configuration: %v" , err )
7576 }
@@ -87,9 +88,9 @@ func Run(cmd *cobra.Command, args []string) error {
8788
8889 group .Go (func () error {
8990 server := http .NewServeMux ()
90-
91+ log := slog . With ( "port" , ":8081" )
9192 if Flags .Profiling {
92- logs . Log . Printf ("pprof profiling was enabled. " )
93+ log . Info ("pprof enabled" , "endpoint" , "/debug/pprof " )
9394 server .HandleFunc ("/debug/pprof/" , pprof .Index )
9495 server .HandleFunc ("/debug/pprof/cmdline" , pprof .Cmdline )
9596 server .HandleFunc ("/debug/pprof/profile" , pprof .Profile )
@@ -98,7 +99,7 @@ func Run(cmd *cobra.Command, args []string) error {
9899 }
99100
100101 if Flags .Prometheus {
101- logs . Log . Printf ( "Prometheus was enabled. \n Running prometheus on port :8081 " )
102+ log . Info ( "metrics enabled" , "endpoint" , "/metrics " )
102103 prometheus .MustRegister (metricPayloadSize )
103104 server .Handle ("/metrics" , promhttp .Handler ())
104105 }
@@ -159,7 +160,7 @@ func Run(cmd *cobra.Command, args []string) error {
159160 return fmt .Errorf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
160161 }
161162
162- logs . Log . Printf ( "starting %q datagatherer" , dgConfig .Name )
163+ slog . Info ( "Starting datagatherer" , "gatherer " , dgConfig .Name )
163164
164165 // start the data gatherers and wait for the cache sync
165166 group .Go (func () error {
@@ -194,7 +195,7 @@ func Run(cmd *cobra.Command, args []string) error {
194195 // the run.
195196 if err := dg .WaitForCacheSync (bootCtx .Done ()); err != nil {
196197 // log sync failure, this might recover in future
197- logs . Log . Printf ( "failed to complete initial sync of %q data gatherer %q: %v" , dgConfig .Kind , dgConfig .Name , err )
198+ slog . Warn ( "Failed to complete initial sync" , "kind" , dgConfig .Kind , "gatherer" , dgConfig .Name , "reason" , err )
198199 }
199200 }
200201
@@ -237,7 +238,7 @@ func newEventf(installNS string) (Eventf, error) {
237238 var eventf Eventf
238239 if os .Getenv ("POD_NAME" ) == "" {
239240 eventf = func (eventType , reason , msg string , args ... interface {}) {}
240- logs . Log . Printf ( "error messages will not show in the pod's events because the POD_NAME environment variable is empty" )
241+ slog . Warn ( "Error messages will not show in the pod's events because the POD_NAME environment variable is empty" )
241242 } else {
242243 podName := os .Getenv ("POD_NAME" )
243244
@@ -263,7 +264,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
263264 var readings []* api.DataReading
264265
265266 if config .InputPath != "" {
266- logs . Log . Printf ("Reading data from local file: %s " , config .InputPath )
267+ slog . Info ("Reading data from local file" , "path " , config .InputPath )
267268 data , err := os .ReadFile (config .InputPath )
268269 if err != nil {
269270 return fmt .Errorf ("failed to read local data file: %s" , err )
@@ -289,7 +290,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
289290 if err != nil {
290291 return fmt .Errorf ("failed to output to local file: %s" , err )
291292 }
292- logs . Log . Printf ("Data saved to local file: %s " , config .OutputPath )
293+ slog . Info ("Data saved to local file" , "path " , config .OutputPath )
293294 } else {
294295 backOff := backoff .NewExponentialBackOff ()
295296 backOff .InitialInterval = 30 * time .Second
@@ -300,7 +301,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
300301 }
301302 err := backoff .RetryNotify (post , backOff , func (err error , t time.Duration ) {
302303 eventf ("Warning" , "PushingErr" , "retrying in %v after error: %s" , t , err )
303- logs . Log . Printf ( "retrying in %v after error: %s" , t , err )
304+ slog . Warn ( "Pushing error" , "backoffInterval" , t , "reason" , err )
304305 })
305306 if err != nil {
306307 return fmt .Errorf ("Exiting due to fatal error uploading: %v" , err )
@@ -321,11 +322,8 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
321322 continue
322323 }
323324
324- if count >= 0 {
325- logs .Log .Printf ("successfully gathered %d items from %q datagatherer" , count , k )
326- } else {
327- logs .Log .Printf ("successfully gathered data from %q datagatherer" , k )
328- }
325+ slog .Info ("Successfully gathered data" , "gatherer" , k , "count" , count )
326+
329327 readings = append (readings , & api.DataReading {
330328 ClusterID : config .ClusterID ,
331329 DataGatherer : k ,
@@ -357,7 +355,7 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
357355func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
358356 baseURL := config .Server
359357
360- logs . Log . Println ("Posting data to: " , baseURL )
358+ slog . Info ("Posting data" , "to " , baseURL )
361359
362360 if config .AuthMode == VenafiCloudKeypair || config .AuthMode == VenafiCloudVenafiConnection {
363361 // orgID and clusterID are not required for Venafi Cloud auth
@@ -368,7 +366,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
368366 if err != nil {
369367 return fmt .Errorf ("post to server failed: %+v" , err )
370368 }
371- logs . Log . Println ("Data sent successfully. " )
369+ slog . Info ("Data sent successfully" )
372370
373371 return nil
374372 }
@@ -384,7 +382,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
384382 prometheus.Labels {"organization" : config .OrganizationID , "cluster" : config .ClusterID },
385383 )
386384 metric .Set (float64 (len (data )))
387- logs . Log . Printf ("Data readings upload size: %d " , len (data ))
385+ slog . Info ("Data readings" , " upload- size" , len (data ))
388386 path := config .EndpointPath
389387 if path == "" {
390388 path = "/api/v1/datareadings"
@@ -404,7 +402,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
404402
405403 return fmt .Errorf ("received response with status code %d. Body: [%s]" , code , errorContent )
406404 }
407- logs . Log . Println ("Data sent successfully. " )
405+ slog . Info ("Data sent successfully" )
408406 return err
409407 }
410408
@@ -416,7 +414,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
416414 if err != nil {
417415 return fmt .Errorf ("post to server failed: %+v" , err )
418416 }
419- logs . Log . Println ("Data sent successfully. " )
417+ slog . Info ("Data sent successfully" )
420418
421419 return nil
422420}
0 commit comments