-
Notifications
You must be signed in to change notification settings - Fork 60
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
cleanup(cmd,internal,pkg): move driver config
options to be common to all driver
commands
#365
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,19 +20,28 @@ package driver | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
drivercleanup "github.com/falcosecurity/falcoctl/cmd/driver/cleanup" | ||
driverconfig "github.com/falcosecurity/falcoctl/cmd/driver/config" | ||
driverinstall "github.com/falcosecurity/falcoctl/cmd/driver/install" | ||
driverprintenv "github.com/falcosecurity/falcoctl/cmd/driver/printenv" | ||
"github.com/falcosecurity/falcoctl/internal/config" | ||
commonoptions "github.com/falcosecurity/falcoctl/pkg/options" | ||
driverdistro "github.com/falcosecurity/falcoctl/pkg/driver/distro" | ||
driverkernel "github.com/falcosecurity/falcoctl/pkg/driver/kernel" | ||
drivertype "github.com/falcosecurity/falcoctl/pkg/driver/type" | ||
"github.com/falcosecurity/falcoctl/pkg/options" | ||
) | ||
|
||
// NewDriverCmd returns the driver command. | ||
func NewDriverCmd(ctx context.Context, opt *commonoptions.Common) *cobra.Command { | ||
func NewDriverCmd(ctx context.Context, opt *options.Common) *cobra.Command { | ||
driver := &options.Driver{} | ||
driverTypes := options.NewDriverTypes() | ||
|
||
cmd := &cobra.Command{ | ||
Use: "driver", | ||
DisableFlagsInUseLine: true, | ||
|
@@ -41,13 +50,118 @@ func NewDriverCmd(ctx context.Context, opt *commonoptions.Common) *cobra.Command | |
** This command is in preview and under development. **`, | ||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { | ||
opt.Initialize() | ||
return config.Load(opt.ConfigFile) | ||
if err := config.Load(opt.ConfigFile); err != nil { | ||
return err | ||
} | ||
|
||
// Override "version" flag with viper config if not set by user. | ||
f := cmd.Flags().Lookup("version") | ||
if f == nil { | ||
// should never happen | ||
return fmt.Errorf("unable to retrieve flag version") | ||
} else if !f.Changed && viper.IsSet(config.DriverVersionKey) { | ||
val := viper.Get(config.DriverVersionKey) | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
return fmt.Errorf("unable to overwrite \"version\" flag: %w", err) | ||
} | ||
} | ||
|
||
// Override "repo" flag with viper config if not set by user. | ||
f = cmd.Flags().Lookup("repo") | ||
if f == nil { | ||
// should never happen | ||
return fmt.Errorf("unable to retrieve flag repo") | ||
} else if !f.Changed && viper.IsSet(config.DriverReposKey) { | ||
val, err := config.DriverRepos() | ||
if err != nil { | ||
return err | ||
} | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
return fmt.Errorf("unable to overwrite \"repo\" flag: %w", err) | ||
} | ||
} | ||
|
||
// Override "name" flag with viper config if not set by user. | ||
f = cmd.Flags().Lookup("name") | ||
if f == nil { | ||
// should never happen | ||
return fmt.Errorf("unable to retrieve flag name") | ||
} else if !f.Changed && viper.IsSet(config.DriverNameKey) { | ||
val := viper.Get(config.DriverNameKey) | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
return fmt.Errorf("unable to overwrite \"name\" flag: %w", err) | ||
} | ||
} | ||
|
||
// Override "host-root" flag with viper config if not set by user. | ||
f = cmd.Flags().Lookup("host-root") | ||
if f == nil { | ||
// should never happen | ||
return fmt.Errorf("unable to retrieve flag host-root") | ||
} else if !f.Changed && viper.IsSet(config.DriverHostRootKey) { | ||
val := viper.Get(config.DriverHostRootKey) | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
return fmt.Errorf("unable to overwrite \"host-root\" flag: %w", err) | ||
} | ||
} | ||
|
||
// Override "type" flag with viper config if not set by user. | ||
f = cmd.Flags().Lookup("type") | ||
if f == nil { | ||
// should never happen | ||
return fmt.Errorf("unable to retrieve flag type") | ||
} else if !f.Changed && viper.IsSet(config.DriverTypeKey) { | ||
val := viper.Get(config.DriverTypeKey) | ||
if err := cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val)); err != nil { | ||
return fmt.Errorf("unable to overwrite \"type\" flag: %w", err) | ||
} | ||
} | ||
|
||
if driverTypes.String() != "auto" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FedeDP just nit |
||
var err error | ||
// Ok driver type was enforced by the user | ||
driver.Type, err = drivertype.Parse(driverTypes.String()) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
// automatic logic | ||
info, err := driverkernel.FetchInfo("", "") | ||
if err != nil { | ||
return err | ||
} | ||
opt.Printer.Logger.Debug("Fetched kernel info", opt.Printer.Logger.Args( | ||
"arch", info.Architecture.ToNonDeb(), | ||
"kernel release", info.String(), | ||
"kernel version", info.KernelVersion)) | ||
|
||
d, err := driverdistro.Discover(info, driver.HostRoot) | ||
if err != nil { | ||
if !errors.Is(err, driverdistro.ErrUnsupported) { | ||
return err | ||
} | ||
opt.Printer.Logger.Info("Detected an unsupported target system; falling back at generic logic.") | ||
} | ||
opt.Printer.Logger.Debug("Discovered distro", opt.Printer.Logger.Args("target", d)) | ||
|
||
driver.Type = d.PreferredDriver(info) | ||
if driver.Type == nil { | ||
return fmt.Errorf("automatic driver selection failed") | ||
} | ||
} | ||
return driver.Validate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the end, we validate driver options. |
||
}, | ||
} | ||
|
||
cmd.AddCommand(driverinstall.NewDriverInstallCmd(ctx, opt)) | ||
cmd.AddCommand(driverconfig.NewDriverConfigCmd(ctx, opt)) | ||
cmd.AddCommand(drivercleanup.NewDriverCleanupCmd(ctx, opt)) | ||
cmd.AddCommand(driverprintenv.NewDriverPrintenvCmd(ctx, opt)) | ||
cmd.PersistentFlags().Var(driverTypes, "type", "Driver type to be used "+driverTypes.Allowed()) | ||
cmd.PersistentFlags().StringVar(&driver.Version, "version", config.DefaultDriver.Version, "Driver version to be used.") | ||
cmd.PersistentFlags().StringSliceVar(&driver.Repos, "repo", config.DefaultDriver.Repos, "Driver repo to be used.") | ||
cmd.PersistentFlags().StringVar(&driver.Name, "name", config.DefaultDriver.Name, "Driver name to be used.") | ||
cmd.PersistentFlags().StringVar(&driver.HostRoot, "host-root", config.DefaultDriver.HostRoot, "Driver host root to be used.") | ||
|
||
cmd.AddCommand(driverinstall.NewDriverInstallCmd(ctx, opt, driver)) | ||
cmd.AddCommand(driverconfig.NewDriverConfigCmd(ctx, opt, driver)) | ||
cmd.AddCommand(drivercleanup.NewDriverCleanupCmd(ctx, opt, driver)) | ||
cmd.AddCommand(driverprintenv.NewDriverPrintenvCmd(ctx, opt, driver)) | ||
return cmd | ||
} |
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.
Here we load
driver.Type
from givendriverTypes
enum parameter.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.
We now support
auto
logic for all driver commands.This can be useful to eg: test with
driver printenv
which driver is being discovered forauto
driver type.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.
Or, to store driver config with correct driver built from automatic discovered one (when using
driver config --type auto
).