@@ -3,11 +3,11 @@ package agent
33import (
44 "fmt"
55 "io"
6- "log"
76 "net/url"
87 "os"
98 "time"
109
10+ "github.com/go-logr/logr"
1111 "github.com/hashicorp/go-multierror"
1212 "github.com/pkg/errors"
1313 "github.com/spf13/cobra"
@@ -20,6 +20,7 @@ import (
2020 "github.com/jetstack/preflight/pkg/datagatherer/k8s"
2121 "github.com/jetstack/preflight/pkg/datagatherer/local"
2222 "github.com/jetstack/preflight/pkg/kubeconfig"
23+ "github.com/jetstack/preflight/pkg/logs"
2324 "github.com/jetstack/preflight/pkg/version"
2425)
2526
@@ -164,9 +165,12 @@ type AgentCmdFlags struct {
164165
165166 // Prometheus (--enable-metrics) enables the Prometheus metrics server.
166167 Prometheus bool
168+
169+ LogOptions logs.LogOptions
167170}
168171
169172func InitAgentCmdFlags (c * cobra.Command , cfg * AgentCmdFlags ) {
173+ logs .SetupFlags (c .Flags (), & cfg .LogOptions )
170174 c .PersistentFlags ().StringVarP (
171175 & cfg .ConfigFilePath ,
172176 "agent-config-file" ,
@@ -346,39 +350,43 @@ type CombinedConfig struct {
346350// The error returned may be a multierror.Error. Use multierror.Prefix(err,
347351// "context:") rather than fmt.Errorf("context: %w", err) when wrapping the
348352// error.
349- func ValidateAndCombineConfig (log * log .Logger , cfg Config , flags AgentCmdFlags ) (CombinedConfig , client.Client , error ) {
353+ func ValidateAndCombineConfig (log logr .Logger , cfg Config , flags AgentCmdFlags ) (CombinedConfig , client.Client , error ) {
350354 res := CombinedConfig {}
351355 var errs error
352356
353357 {
354- var mode AuthMode
358+ var (
359+ mode AuthMode
360+ reason string
361+ )
355362 switch {
356363 case flags .VenafiCloudMode && flags .CredentialsPath != "" :
357364 mode = VenafiCloudKeypair
358- log . Printf ( "Using the %s auth mode since --venafi-cloud and --credentials-path were specified.", mode )
365+ reason = " --venafi-cloud and --credentials-path were specified."
359366 case flags .ClientID != "" && flags .PrivateKeyPath != "" :
360367 mode = VenafiCloudKeypair
361- log . Printf ( "Using the %s auth mode since --client-id and --private-key-path were specified.", mode )
368+ reason = " --client-id and --private-key-path were specified."
362369 case flags .ClientID != "" :
363370 return CombinedConfig {}, nil , fmt .Errorf ("if --client-id is specified, --private-key-path must also be specified" )
364371 case flags .PrivateKeyPath != "" :
365372 return CombinedConfig {}, nil , fmt .Errorf ("--private-key-path is specified, --client-id must also be specified" )
366373 case flags .VenConnName != "" :
367374 mode = VenafiCloudVenafiConnection
368- log . Printf ( "Using the %s auth mode since --venafi-connection was specified.", mode )
375+ reason = " --venafi-connection was specified."
369376 case flags .APIToken != "" :
370377 mode = JetstackSecureAPIToken
371- log . Printf ( "Using the %s auth mode since --api-token was specified.", mode )
378+ reason = " --api-token was specified."
372379 case ! flags .VenafiCloudMode && flags .CredentialsPath != "" :
373380 mode = JetstackSecureOAuth
374- log . Printf ( "Using the %s auth mode since --credentials-file was specified without --venafi-cloud.", mode )
381+ reason = " --credentials-file was specified without --venafi-cloud."
375382 default :
376383 return CombinedConfig {}, nil , fmt .Errorf ("no auth mode specified. You can use one of four auth modes:\n " +
377384 " - Use (--venafi-cloud with --credentials-file) or (--client-id with --private-key-path) to use the " + string (VenafiCloudKeypair ) + " mode.\n " +
378385 " - Use --venafi-connection for the " + string (VenafiCloudVenafiConnection ) + " mode.\n " +
379386 " - Use --credentials-file alone if you want to use the " + string (JetstackSecureOAuth ) + " mode.\n " +
380387 " - Use --api-token if you want to use the " + string (JetstackSecureAPIToken ) + " mode.\n " )
381388 }
389+ log .Info ("Auth mode" , "mode" , mode , "reason" , reason )
382390 res .AuthMode = mode
383391 }
384392
@@ -394,10 +402,10 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
394402 case hasServerField && hasEndpointField :
395403 // The `server` field takes precedence over the deprecated
396404 // `endpoint` field.
397- log .Printf ("The `server` and `endpoint` fields are both set in the config; using the `server` field." )
405+ log .Info ("The `server` and `endpoint` fields are both set in the config; using the `server` field." )
398406 server = cfg .Server
399407 case ! hasServerField && hasEndpointField :
400- log .Printf ("Using deprecated Endpoint configuration. User Server instead." )
408+ log .Info ("Using deprecated Endpoint configuration. User Server instead." )
401409 if cfg .Endpoint .Protocol == "" && cfg .Server == "" {
402410 cfg .Endpoint .Protocol = "http"
403411 }
@@ -415,7 +423,10 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
415423 errs = multierror .Append (errs , fmt .Errorf ("server %q is not a valid URL" , server ))
416424 }
417425 if res .AuthMode == VenafiCloudVenafiConnection && server != "" {
418- log .Printf ("ignoring the server field specified in the config file. In %s mode, this field is not needed." , VenafiCloudVenafiConnection )
426+ log .Info (
427+ "ignoring the server field specified in the config file." ,
428+ "reason" , fmt .Sprintf ("In %s mode, this field is not needed." , VenafiCloudVenafiConnection ),
429+ )
419430 server = ""
420431 }
421432 res .Server = server
@@ -445,7 +456,10 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
445456 // change this value with the new --venafi-connection flag, and this
446457 // field is simply ignored.
447458 if cfg .VenafiCloud != nil && cfg .VenafiCloud .UploadPath != "" {
448- log .Printf (`ignoring the venafi-cloud.upload_path field in the config file. In %s mode, this field is not needed.` , res .AuthMode )
459+ log .Info (
460+ "ignoring the venafi-cloud.upload_path field in the config file." ,
461+ "reason" , fmt .Sprintf (`In %s mode, this field is not needed.` , res .AuthMode ),
462+ )
449463 }
450464 uploadPath = ""
451465 }
@@ -463,7 +477,10 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
463477 // https://venafi.atlassian.net/browse/VC-35385 is done.
464478 {
465479 if cfg .VenafiCloud != nil && cfg .VenafiCloud .UploaderID != "" {
466- log .Printf (`ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in %s mode.` , res .AuthMode )
480+ log .Info (
481+ "ignoring the venafi-cloud.uploader_id field in the config file." ,
482+ "reason" , fmt .Sprintf ("This field is not needed in %s mode." , res .AuthMode ),
483+ )
467484 }
468485 }
469486
@@ -515,13 +532,13 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
515532 case flags .Period == 0 && cfg .Period == 0 :
516533 errs = multierror .Append (errs , fmt .Errorf ("period must be set using --period or -p, or using the 'period' field in the config file" ))
517534 case flags .Period == 0 && cfg .Period > 0 :
518- log .Printf ("Using period from config %s " , cfg .Period )
535+ log .Info ("Using period from config" , "period " , cfg .Period )
519536 period = cfg .Period
520537 case flags .Period > 0 && cfg .Period == 0 :
521538 period = flags .Period
522539 case flags .Period > 0 && cfg .Period > 0 :
523540 // The flag takes precedence.
524- log .Printf ("Both the 'period' field and --period are set. Using the value provided with --period." )
541+ log .Info ("Both the 'period' field and --period are set. Using the value provided with --period." )
525542 period = flags .Period
526543 }
527544 res .Period = period
@@ -582,7 +599,7 @@ func ValidateAndCombineConfig(log *log.Logger, cfg Config, flags AgentCmdFlags)
582599// The error returned may be a multierror.Error. Use multierror.Prefix(err,
583600// "context:") rather than fmt.Errorf("context: %w", err) when wrapping the
584601// error.
585- func validateCredsAndCreateClient (log * log .Logger , flagCredentialsPath , flagClientID , flagPrivateKeyPath , flagAPIToken string , cfg CombinedConfig ) (client.Client , error ) {
602+ func validateCredsAndCreateClient (log logr .Logger , flagCredentialsPath , flagClientID , flagPrivateKeyPath , flagAPIToken string , cfg CombinedConfig ) (client.Client , error ) {
586603 var errs error
587604
588605 var preflightClient client.Client
@@ -702,7 +719,7 @@ func ValidateDataGatherers(dataGatherers []DataGatherer) error {
702719
703720// The error returned may be a multierror.Error. Instead of adding context to
704721// the error with fmt.Errorf("%w", err), use multierror.Prefix(err, "context").
705- func createCredentialClient (log * log .Logger , credentials client.Credentials , cfg CombinedConfig , agentMetadata * api.AgentMetadata ) (client.Client , error ) {
722+ func createCredentialClient (log logr .Logger , credentials client.Credentials , cfg CombinedConfig , agentMetadata * api.AgentMetadata ) (client.Client , error ) {
706723 switch creds := credentials .(type ) {
707724 case * client.VenafiSvcAccountCredentials :
708725 // The uploader ID isn't actually used in the backend, let's use an
@@ -713,7 +730,7 @@ func createCredentialClient(log *log.Logger, credentials client.Credentials, cfg
713730 if cfg .AuthMode == VenafiCloudKeypair {
714731 // We don't do this for the VenafiCloudVenafiConnection mode because
715732 // the upload_path field is ignored in that mode.
716- log .Println ("Loading upload_path from \" venafi-cloud\" configuration." )
733+ log .Info ("Loading upload_path from \" venafi-cloud\" configuration." )
717734 uploadPath = cfg .UploadPath
718735 }
719736 return client .NewVenafiCloudClient (agentMetadata , creds , cfg .Server , uploaderID , uploadPath )
0 commit comments