-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[VC-35738] Use klog and logr logger instead of log in the agent package #609
Conversation
066593e
to
4197b28
Compare
4197b28
to
6b0e405
Compare
3d4876b
to
a8f16c7
Compare
3b4e136
to
5787c06
Compare
@@ -33,10 +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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the main goal of this PR; to remove the use of logs from this package.
@@ -3,11 +3,11 @@ package agent | |||
import ( | |||
"fmt" | |||
"io" | |||
"log" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And to remove the use of the log
module in this package.
pkg/agent/run.go
Outdated
@@ -230,7 +225,7 @@ func Run(cmd *cobra.Command, args []string) (returnErr error) { | |||
// TODO(wallrj): Pass a context to gatherAndOutputData, so that we don't | |||
// have to wait for it to finish before exiting the process. | |||
for { | |||
if err := gatherAndOutputData(eventf, config, preflightClient, dataGatherers); err != nil { | |||
if err := gatherAndOutputData(ctx, eventf, config, preflightClient, dataGatherers); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now, the context is only used to the logger into the function.
In a future PR, the context can be passed through to the http clients and used to cancel ongoing connections.
The TODO comment above therefore still stands.
@@ -262,7 +257,7 @@ func newEventf(installNS string) (Eventf, error) { | |||
var eventf Eventf | |||
if os.Getenv("POD_NAME") == "" { | |||
eventf = func(eventType, reason, msg string, args ...interface{}) {} | |||
logs.Log.Printf("error messages will not show in the pod's events because the POD_NAME environment variable is empty") | |||
log.Error(nil, "Error messages will not show in the pod's events because the POD_NAME environment variable is empty") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have made this an error so that administrators are more likely to see it and act upon it.
@@ -45,8 +46,9 @@ func init() { | |||
// will be logged and the process will exit with status 1. | |||
func Execute() { | |||
logs.AddFlags(rootCmd.PersistentFlags()) | |||
ctx := klog.NewContext(context.Background(), klog.Background()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was unsure whether calling klog.Background()
here, before logs.Initialize()
has been called in the PersistentPreRunE
function (above); but it seems to work fine.
For example here is an example of the --logging-format=json
flag having the desired effect:
$ ./preflight agent --one-shot --output-path=/dev/null --api-token=foo --install-namespace=default --logging-format=json 2>&1 | jl
[2024-11-07 15:54:38] Starting [caller=agent/run.go:58 commit= logger=Run version=development]
[2024-11-07 15:54:38] Using the Jetstack Secure API Token auth mode since --api-token was specified. [caller=agent/config.go:395 logger=Run]
[2024-11-07 15:54:38] ignoring the venafi-cloud.uploader_id field in the config file. This field is not needed in Jetstack Secure API Token mode. [caller=agent/config.go:479 logger=Run]
[2024-11-07 15:54:38] Healthz endpoints enabled [addr=:8081 caller=agent/run.go:116 logger=Run.APIServer path=/healthz]
[2024-11-07 15:54:38] Readyz endpoints enabled [addr=:8081 caller=agent/run.go:120 logger=Run.APIServer path=/readyz]
[2024-11-07 15:54:38] Error messages will not show in the pod's events because the POD_NAME environment variable is empty [caller=agent/run.go:262 logger=Run]
[2024-11-07 15:54:38] Starting DataGatherer [caller=agent/run.go:179 logger=Run]
[2024-11-07 15:54:38] Starting DataGatherer [caller=agent/run.go:179 logger=Run]
[2024-11-07 15:54:38] Data saved to local file [caller=agent/run.go:315 outputPath=/dev/null]
var ( | ||
mode AuthMode | ||
reason string | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided not to make these log messages structured, instead I just log the original message at Info level,
to avoid too many changes in the associated tests.
I moved the message to a reason
var to make the diff a bit easier to read and makes it easier to turn these into structured logs in the future, e.g.
log.Info("Auth mode selected", "mode", mode, "reason", reason")
5787c06
to
2243d7f
Compare
a8f16c7
to
c3b5306
Compare
pkg/agent/run.go
Outdated
} else { | ||
logs.Log.Printf("successfully gathered data from %q datagatherer", k) | ||
} | ||
log.Info("Successfully gathered", "count", count, "name", k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fetch count was introduced in #537. I decided to always include the count in the structured logs, to simplify the code a bit and so that it's clear to the user when 0 items are found.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait...I just looked closer. I see that the count may be -1
.
This needs fixing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
INFO Using the Jetstack Secure OAuth auth mode since --credentials-file was specified without --venafi-cloud. | ||
INFO Both the 'period' field and --period are set. Using the value provided with --period. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this output not matching the klog text format and uses a custom format? I'd prefer reviewing the logs as seen by the users
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because these tests are using the ktesting test logger which has a buffer containing simplified output format for easier testing and matching.
The log format of the preflight
command is tested elsewhere.
I'll leave this as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed the last commit of this stacked PR, and it looks good. I left two nits that aren't that important.
This PR seems to be low risk: I'm confident that nothing will break, and thus I haven't felt the need to perform manual tests (running the CLI locally or running the manual script ./hack/e2e/test.sh
).
Feel free to fix the -1
issue above and merge this.
Signed-off-by: Richard Wall <richard.wall@venafi.com>
2243d7f
to
1f19391
Compare
Thanks @maelvls I added those suggested TODO comments and answered your other points. |
The aim of this PR is to remove the stdlib
log
package and the use of the legacygh.neting.cc/jetstack/jetstack-secure/logs.Log
variable, from theagent
package.I'll create followup PRs to remove
logs.Log
elsewhere in the code, and ultimately remove that global variable.