Skip to content
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

new(cmd,pkg,docs,docgen,validate): switched from slog go library to rich-text falcoctl log library #333

Merged
merged 5 commits into from
Apr 23, 2024

Conversation

FedeDP
Copy link
Contributor

@FedeDP FedeDP commented Apr 11, 2024

What type of PR is this?

/kind feature

Any specific area of the project related to this PR?

/area cmd
/area pkg
/area docs

What this PR does / why we need it:

To better integrate falcoctl and driverkit, we needed to:

  • avoid using a global logger in driverkit (we always used default slog instance)
  • possibly use same logger in both library

Since falcoctl log library is much richer and better, i ported driverkit to use it; now each executor takes a logger parameter, that makes much easier and seamless the ingreation with falcoctl.
The result is wonderful:
driverkit.webm

While i was at it, i also:

  • updated docs (never updated since the introduction of local builder
  • ported driverkit help/usage message printing to follow same conventions as falcoctl (ie: print them less often basically :D)

TODO:

  • fix tests :)

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

new(cmd,pkg,docs,docgen,validate): switched from `slog` go library to rich-text falcoctl log library

@@ -46,12 +47,12 @@ func validateArgs() cobra.PositionalArgs {
if len(args) == 0 {
return nil
}
return cobra.ExactValidArgs(1)(c, args)
return cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs)(c, args)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids using a deprecated method.

}
}

// NewCompletionCmd ...
func NewCompletionCmd() *cobra.Command {
func NewCompletionCmd(_ *ConfigOptions, _ *RootOptions, _ *pflag.FlagSet) *cobra.Command {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same signature as other commands.

errArr := []error{}
for _, e := range errors {
var errs validator.ValidationErrors
errors.As(err, &errs)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use errors.As as suggested by ide.
This kind of cleanup is done multiple times.

}

// NewConfigOptions creates an instance of ConfigOptions.
func NewConfigOptions() *ConfigOptions {
o := &ConfigOptions{}
func NewConfigOptions() (*ConfigOptions, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When creating a configoptions, default to a INFO level logger.

@@ -212,7 +211,6 @@ func buildMirror(a amazonBuilder, r string, kv kernelrelease.KernelRelease) (str
}

mirror := fmt.Sprintf("%s/%s", baseURL, "mirror.list")
slog.With("url", mirror, "version", r).Debug("looking for repo...")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually needed these ones; passing a printer down there would be a nightmare :)
Also, i think distro builders should not log anything.

@@ -415,7 +421,6 @@ func GetResolvingURLs(urls []string) ([]string, error) {
}
if res.StatusCode == http.StatusOK {
results = append(results, u)
slog.With("url", u).Debug("kernel header url found")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, passing the printer down here would be a bit troublesome.
I preferred to just kill this debug log (and adding a log in KernelDownloadScript: https://github.com/falcosecurity/driverkit/pull/333/files#diff-16a66a3bc20a8ec693f64ed821148f71efa386a7e942444444eb591512bf129bR173)

}
base, err := url.Parse(uu.Host)
if err != nil {
slog.Error(err.Error())
os.Exit(1)
panic(err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panic instead of erroring out. Again, this was done to avoid passing the printer down here.

@@ -63,26 +63,27 @@ func (bp *DockerBuildProcessor) String() string {
return DockerBuildProcessorName
}

func mustCheckArchUseQemu(ctx context.Context, b *builder.Build, cli *client.Client) {
func (bp *DockerBuildProcessor) mustCheckArchUseQemu(ctx context.Context, b *builder.Build, cli *client.Client) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the receiver since we need to access bp.Printer. I could've just used b.Printer instead, it would be the same, but i preferred this way since this method is tied to docker build processor anyway.

slog.With("processor", c.Name()).Info("driver building, it will take a few seconds")
if !configOptions.DryRun {
b := rootOpts.ToBuild()
RunE: func(c *cobra.Command, args []string) error {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another small change: return an error from executors instead of dealing with os.Exit(1) ourselves.

cmd/docker.go Outdated
// Since we use a spinner, cache log data to a bytesbuffer;
// we will later print it once we stop the spinner.
var buf bytes.Buffer
b := rootOpts.ToBuild(configOpts.Printer.WithWriter(&buf))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small trick: since when using a Spinner we cannot print anything to screen (otherwise that would cause a spinner refresh that changes its title and looks ugly), we store to Build a printer that prints to a bytes buffer; then we defer a function that prints everything after the spinner was stopped.

@FedeDP FedeDP force-pushed the new/falcoctl_printer branch 2 times, most recently from c1d2c62 to 40efc36 Compare April 11, 2024 12:19
@FedeDP FedeDP changed the title wip: new(cmd,pkg,docs,docgen,validate): switched from slog go library to rich-text falcoctl log library new(cmd,pkg,docs,docgen,validate): switched from slog go library to rich-text falcoctl log library Apr 11, 2024
@FedeDP
Copy link
Contributor Author

FedeDP commented Apr 23, 2024

Needs a rebase! Going to rebase asap!

… use rich-text falcoctl log library.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Moreover, moved cmd output to more strictly follow `falcoctl` one,
with regards to printing usage/helper messages.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
Copy link
Contributor

@EXONER4TED EXONER4TED left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@poiana
Copy link

poiana commented Apr 23, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: EXONER4TED, FedeDP

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@poiana poiana merged commit 00c7e79 into master Apr 23, 2024
8 checks passed
@poiana poiana deleted the new/falcoctl_printer branch April 23, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants