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

Refactor Load() and fixup BPF lifecycle #104

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions pkg/cli/internal/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
"github.com/cilium/ebpf/rlimit"
"github.com/pkg/errors"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"

"github.com/solo-io/bumblebee/pkg/cli/internal/options"
"github.com/solo-io/bumblebee/pkg/decoder"
"github.com/solo-io/bumblebee/pkg/loader"
"github.com/solo-io/bumblebee/pkg/loader/mapwatcher"
"github.com/solo-io/bumblebee/pkg/spec"
"github.com/solo-io/bumblebee/pkg/stats"
"github.com/solo-io/bumblebee/pkg/tui"
"github.com/solo-io/go-utils/contextutils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

type runOptions struct {
Expand Down Expand Up @@ -109,22 +111,18 @@ func run(cmd *cobra.Command, args []string, opts *runOptions) error {
return err
}

progLoader := loader.NewLoader(
decoder.NewDecoderFactory(),
promProvider,
)
parsedELF, err := progLoader.Parse(ctx, progReader)

parsedELF, err := loader.Parse(ctx, progReader)
if err != nil {
return fmt.Errorf("could not parse BPF program: %w", err)
}

tuiApp, err := buildTuiApp(&progLoader, progLocation, opts.filter, parsedELF)
tuiApp, err := buildTuiApp(progLocation, opts.filter, parsedELF)
if err != nil {
return err
}
loaderOpts := loader.LoadOptions{
loaderOpts := &loader.LoadOptions{
ParsedELF: parsedELF,
Watcher: tuiApp,
PinMaps: opts.pinMaps,
PinProgs: opts.pinProgs,
}
Expand All @@ -134,20 +132,38 @@ func run(cmd *cobra.Command, args []string, opts *runOptions) error {
contextutils.LoggerFrom(ctx).Info("before calling tui.Run() context is done")
return ctx.Err()
}
if opts.notty {
fmt.Println("Calling Load...")
loaderOpts.Watcher = loader.NewNoopWatcher()
err = progLoader.Load(ctx, &loaderOpts)

contextutils.LoggerFrom(ctx).Info("calling Load()")
loadedMaps, closeLifecycle, err := loader.Load(ctx, loaderOpts)
contextutils.LoggerFrom(ctx).Info("returned from Load()")
if err != nil {
return err
}

// Close our loaded program only if there's nothing set to explicitly extend our program's lifetime.
if opts.pinMaps == "" && opts.pinProgs == "" {
defer closeLifecycle()
}

if opts.notty {
fmt.Println("Running in non-interactive mode. Hit Ctrl-C to exit.")
<-ctx.Done()
} else {
mapWatcher := mapwatcher.New(parsedELF.WatchedMaps, loadedMaps, decoder.NewDecoderFactory(), promProvider)
contextutils.LoggerFrom(ctx).Info("calling tui run()")
err = tuiApp.Run(ctx, progLoader, &loaderOpts)
err = tuiApp.Run(ctx, mapWatcher)
contextutils.LoggerFrom(ctx).Info("after tui run()")
return err
}

return nil
}

func buildTuiApp(loader *loader.Loader, progLocation string, filterString []string, parsedELF *loader.ParsedELF) (*tui.App, error) {
func buildTuiApp(
progLocation string,
filterString []string,
parsedELF *loader.ParsedELF,
) (*tui.App, error) {
// TODO: add filter to UI
filter, err := tui.BuildFilter(filterString, parsedELF.WatchedMaps)
if err != nil {
Expand Down
Loading