@@ -7,13 +7,15 @@ import (
77 "errors"
88 "fmt"
99 "io"
10+ "log/slog"
1011 "net"
1112 "net/http"
1213 "os"
1314 "strings"
1415 "time"
1516
1617 "github.com/cenkalti/backoff"
18+ "github.com/go-logr/logr"
1719 "github.com/hashicorp/go-multierror"
1820 "github.com/prometheus/client_golang/prometheus"
1921 "github.com/prometheus/client_golang/prometheus/promhttp"
@@ -33,7 +35,6 @@ import (
3335 "github.com/jetstack/preflight/pkg/client"
3436 "github.com/jetstack/preflight/pkg/datagatherer"
3537 "github.com/jetstack/preflight/pkg/kubeconfig"
36- "github.com/jetstack/preflight/pkg/logs"
3738 "github.com/jetstack/preflight/pkg/version"
3839
3940 "net/http/pprof"
@@ -51,7 +52,6 @@ const schemaVersion string = "v2.0.0"
5152
5253// Run starts the agent process
5354func Run (cmd * cobra.Command , args []string ) error {
54- logs .Log .Printf ("Preflight agent version: %s (%s)" , version .PreflightVersion , version .Commit )
5555 ctx , cancel := context .WithCancel (
5656 klog .NewContext (
5757 context .Background (),
@@ -60,6 +60,8 @@ func Run(cmd *cobra.Command, args []string) error {
6060 )
6161 defer cancel ()
6262
63+ slog .Info ("Starting agent" , "version" , version .PreflightVersion , "commit" , version .Commit )
64+
6365 file , err := os .Open (Flags .ConfigFilePath )
6466 if err != nil {
6567 return fmt .Errorf ("Failed to load config file for agent from: %s" , Flags .ConfigFilePath )
@@ -76,7 +78,7 @@ func Run(cmd *cobra.Command, args []string) error {
7678 return fmt .Errorf ("Failed to parse config file: %s" , err )
7779 }
7880
79- config , preflightClient , err := ValidateAndCombineConfig (logs . Log , cfg , Flags )
81+ config , preflightClient , err := ValidateAndCombineConfig (logr . FromSlogHandler ( slog . Default (). Handler ()) , cfg , Flags )
8082 if err != nil {
8183 return fmt .Errorf ("While evaluating configuration: %v" , err )
8284 }
@@ -179,7 +181,7 @@ func Run(cmd *cobra.Command, args []string) error {
179181 return fmt .Errorf ("failed to instantiate %q data gatherer %q: %v" , kind , dgConfig .Name , err )
180182 }
181183
182- logs . Log . Printf ( "starting %q datagatherer" , dgConfig .Name )
184+ slog . Info ( "Starting datagatherer" , "gatherer " , dgConfig .Name )
183185
184186 // start the data gatherers and wait for the cache sync
185187 group .Go (func () error {
@@ -214,7 +216,7 @@ func Run(cmd *cobra.Command, args []string) error {
214216 // the run.
215217 if err := dg .WaitForCacheSync (bootCtx .Done ()); err != nil {
216218 // log sync failure, this might recover in future
217- logs . Log . Printf ( "failed to complete initial sync of %q data gatherer %q: %v" , dgConfig .Kind , dgConfig .Name , err )
219+ slog . Warn ( "Failed to complete initial sync" , "kind" , dgConfig .Kind , "gatherer" , dgConfig .Name , "reason" , err )
218220 }
219221 }
220222
@@ -259,7 +261,7 @@ func newEventf(installNS string) (Eventf, error) {
259261 var eventf Eventf
260262 if os .Getenv ("POD_NAME" ) == "" {
261263 eventf = func (eventType , reason , msg string , args ... interface {}) {}
262- logs . Log . Printf ( "error messages will not show in the pod's events because the POD_NAME environment variable is empty" )
264+ slog . Warn ( "Error messages will not show in the pod's events because the POD_NAME environment variable is empty" )
263265 } else {
264266 podName := os .Getenv ("POD_NAME" )
265267
@@ -285,7 +287,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
285287 var readings []* api.DataReading
286288
287289 if config .InputPath != "" {
288- logs . Log . Printf ("Reading data from local file: %s " , config .InputPath )
290+ slog . Info ("Reading data from local file" , "path " , config .InputPath )
289291 data , err := os .ReadFile (config .InputPath )
290292 if err != nil {
291293 return fmt .Errorf ("failed to read local data file: %s" , err )
@@ -311,7 +313,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
311313 if err != nil {
312314 return fmt .Errorf ("failed to output to local file: %s" , err )
313315 }
314- logs . Log . Printf ("Data saved to local file: %s " , config .OutputPath )
316+ slog . Info ("Data saved to local file" , "path " , config .OutputPath )
315317 } else {
316318 backOff := backoff .NewExponentialBackOff ()
317319 backOff .InitialInterval = 30 * time .Second
@@ -322,7 +324,7 @@ func gatherAndOutputData(eventf Eventf, config CombinedConfig, preflightClient c
322324 }
323325 err := backoff .RetryNotify (post , backOff , func (err error , t time.Duration ) {
324326 eventf ("Warning" , "PushingErr" , "retrying in %v after error: %s" , t , err )
325- logs . Log . Printf ( "retrying in %v after error: %s" , t , err )
327+ slog . Warn ( "Pushing error" , "backoffInterval" , t , "reason" , err )
326328 })
327329 if err != nil {
328330 return fmt .Errorf ("Exiting due to fatal error uploading: %v" , err )
@@ -343,11 +345,8 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
343345 continue
344346 }
345347
346- if count >= 0 {
347- logs .Log .Printf ("successfully gathered %d items from %q datagatherer" , count , k )
348- } else {
349- logs .Log .Printf ("successfully gathered data from %q datagatherer" , k )
350- }
348+ slog .Info ("Successfully gathered data" , "gatherer" , k , "count" , count )
349+
351350 readings = append (readings , & api.DataReading {
352351 ClusterID : config .ClusterID ,
353352 DataGatherer : k ,
@@ -379,7 +378,7 @@ func gatherData(config CombinedConfig, dataGatherers map[string]datagatherer.Dat
379378func postData (config CombinedConfig , preflightClient client.Client , readings []* api.DataReading ) error {
380379 baseURL := config .Server
381380
382- logs . Log . Println ("Posting data to: " , baseURL )
381+ slog . Info ("Posting data" , "to " , baseURL )
383382
384383 if config .AuthMode == VenafiCloudKeypair || config .AuthMode == VenafiCloudVenafiConnection {
385384 // orgID and clusterID are not required for Venafi Cloud auth
@@ -390,7 +389,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
390389 if err != nil {
391390 return fmt .Errorf ("post to server failed: %+v" , err )
392391 }
393- logs . Log . Println ("Data sent successfully. " )
392+ slog . Info ("Data sent successfully" )
394393
395394 return nil
396395 }
@@ -406,7 +405,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
406405 prometheus.Labels {"organization" : config .OrganizationID , "cluster" : config .ClusterID },
407406 )
408407 metric .Set (float64 (len (data )))
409- logs . Log . Printf ("Data readings upload size: %d " , len (data ))
408+ slog . Info ("Data readings" , "uploadSize " , len (data ))
410409 path := config .EndpointPath
411410 if path == "" {
412411 path = "/api/v1/datareadings"
@@ -426,7 +425,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
426425
427426 return fmt .Errorf ("received response with status code %d. Body: [%s]" , code , errorContent )
428427 }
429- logs . Log . Println ("Data sent successfully. " )
428+ slog . Info ("Data sent successfully" )
430429 return err
431430 }
432431
@@ -438,7 +437,7 @@ func postData(config CombinedConfig, preflightClient client.Client, readings []*
438437 if err != nil {
439438 return fmt .Errorf ("post to server failed: %+v" , err )
440439 }
441- logs . Log . Println ("Data sent successfully. " )
440+ slog . Info ("Data sent successfully" )
442441
443442 return nil
444443}
0 commit comments