Skip to content

Commit

Permalink
chore(pkg/driver,cmd/driver): small updates.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Nov 7, 2023
1 parent 2352044 commit c4118ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
47 changes: 27 additions & 20 deletions cmd/driver/prepare/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type driverPrepareOptions struct {
*options.Common
*options.Driver
Download bool
Build bool
Compile bool
DriverVersion string
DriverKernelRelease string
DriverKernelVersion string
Expand All @@ -57,7 +57,7 @@ func NewDriverPrepareCmd(ctx context.Context, opt *options.Common) *cobra.Comman
Driver: &options.Driver{},
// Defaults to downloading or building if needed
Download: true,
Build: true,
Compile: true,
}

cmd := &cobra.Command{
Expand All @@ -71,8 +71,8 @@ func NewDriverPrepareCmd(ctx context.Context, opt *options.Common) *cobra.Comman
}

output.ExitOnErr(o.Printer, o.Driver.AddFlags(cmd))
cmd.Flags().BoolVar(&o.Download, "download", true, "Whether to enable download of drivers")
cmd.Flags().BoolVar(&o.Build, "build", true, "Whether to enable build of drivers")
cmd.Flags().BoolVar(&o.Download, "download", true, "Whether to enable download of prebuilt drivers")
cmd.Flags().BoolVar(&o.Compile, "compile", true, "Whether to enable local compilation of drivers")
cmd.Flags().StringVar(&o.DriverVersion, "driver-version", "", "Driver version to be built")
//nolint:lll // no need to check for line length.
cmd.Flags().StringVar(&o.DriverKernelRelease, "driver-kernelrelease", "", "Specify the kernel release for which to download/build the driver in the same format used by 'uname -r' (e.g. '6.1.0-10-cloud-amd64')")
Expand Down Expand Up @@ -114,18 +114,25 @@ func (o *driverPrepareOptions) RunDriverPrepare(_ context.Context, _ []string) e
"driver version", o.DriverVersion,
"driver type", driver.Type,
"driver name", o.Name,
"compile", o.Compile,
"download", o.Download,
"arch", info.Architecture.ToNonDeb(),
"kernel release", info.String(),
"kernel version", info.KernelVersion))

if !driver.Type.HasArtifacts() {
o.Printer.Logger.Info("no artifacts needed")
o.Printer.Logger.Info("No artifacts needed for the selected driver.")
return nil
}

if !o.Download && !o.Compile {
o.Printer.Logger.Info("Nothing to do: download and compile disabled.")
return nil
}

d, err := driverdistro.DiscoverDistro(info, o.HostRoot)
if err != nil {
if errors.Is(err, driverdistro.ErrUnsupported) && o.Build {
if errors.Is(err, driverdistro.ErrUnsupported) && o.Compile {
o.Download = false
o.Printer.Logger.Info("Detected an unsupported target system, please get in touch with the Falco community. Trying to compile anyway.")
} else {
Expand All @@ -142,33 +149,33 @@ func (o *driverPrepareOptions) RunDriverPrepare(_ context.Context, _ []string) e
setDefaultHTTPClientOpts(o.driverDownloadOptions)

var dest string
defer func() {
// We don't care about errors at this stage
// Fallback: try to load any available driver if leaving with an error.
// It is only useful for kmod, as it will try to
// modprobe a pre-existent version of the driver,
// hoping it will be compatible.
_ = driver.Type.Load(o.Printer, dest, err != nil)
}()

if o.Download {
dest, err = driverdistro.Download(d, o.Printer, info, o.Name, driver.Type, o.DriverVersion, o.DriverRepos)
if err == nil {
// We don't care about errors at this stage
_ = driver.Type.Load(o.Printer, dest, false)
return nil
}
// Print the error but go on
// attempting a build if requested
o.Printer.Logger.Error(err.Error())
}

if o.Build {
if o.Compile {
dest, err = driverdistro.Build(d, o.Printer)
if err == nil {
// We don't care about errors
_ = driver.Type.Load(o.Printer, dest, false)
return nil
}
}

if err != nil {
o.Printer.Logger.Error(err.Error())
// Fallback: try to load any available driver.
// It is only useful for kmod, as it will try to
// modprobe a pre-existent version of the driver,
// hoping it will be compatible.
_ = driver.Type.Load(o.Printer, o.Name, true)
}
return fmt.Errorf("last error: %w", err)

dest = o.Name // used by deferred function above
return fmt.Errorf("failed: %w", err)
}
16 changes: 8 additions & 8 deletions pkg/driver/distro/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ func getOSReleaseDistro(kr *kernelrelease.KernelRelease, hostRoot string) (Distr
return distro, nil
}

func toURL(repo, driverVer, fileName string, kr *kernelrelease.KernelRelease) string {
return fmt.Sprintf("%s/%s/%s/%s", repo, driverVer, kr.Architecture.ToNonDeb(), fileName)
func toURL(repo, driverVer, fileName, arch string) string {
return fmt.Sprintf("%s/%s/%s/%s", repo, driverVer, arch, fileName)
}

func toLocalPath(driverVer, fileName string, kr *kernelrelease.KernelRelease) string {
return fmt.Sprintf("%s/.falco/%s/%s/%s", homedir.Get(), driverVer, kr.Architecture.ToNonDeb(), fileName)
func toLocalPath(driverVer, fileName, arch string) string {
return fmt.Sprintf("%s/.falco/%s/%s/%s", homedir.Get(), driverVer, arch, fileName)
}

func driverToFilename(d Distro, kr *kernelrelease.KernelRelease, driverName string, driverType drivertype.DriverType) string {
func toFilename(d Distro, kr *kernelrelease.KernelRelease, driverName string, driverType drivertype.DriverType) string {
// Fixup kernel information before attempting to download
fixedKR := d.fixupKernel(*kr)
return fmt.Sprintf("%s_%s_%s_%s%s", driverName, d, fixedKR.String(), fixedKR.KernelVersion, driverType.Extension())
Expand All @@ -143,9 +143,9 @@ func Download(d Distro,
driverType drivertype.DriverType,
driverVer string, repos []string,
) (string, error) {
driverFileName := driverToFilename(d, &kr, driverName, driverType)
driverFileName := toFilename(d, &kr, driverName, driverType)
// Skip if existent
destination := toLocalPath(driverVer, driverFileName, &kr)
destination := toLocalPath(driverVer, driverFileName, kr.Architecture.ToNonDeb())
f, err := os.Open(destination) //nolint:gosec // false positive
if err == nil {
_ = f.Close()
Expand All @@ -156,7 +156,7 @@ func Download(d Distro,
// Try to download from any specified repository,
// stopping at first successful http GET.
for _, repo := range repos {
url := toURL(repo, driverVer, driverFileName, &kr)
url := toURL(repo, driverVer, driverFileName, kr.Architecture.ToNonDeb())
printer.Logger.Info("Trying to download a driver", printer.Logger.Args("url", url))

resp, err := http.Get(url) //nolint:gosec,noctx // false positive
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/kernel/kernel.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@ func FetchInfo(enforcedKR, enforcedKV string) (kernelrelease.KernelRelease, erro
return kernelrelease.KernelRelease{}, err
}

// Take eg: "#1 SMP PREEMPT_DYNAMIC Tue, 10 Oct 2023 21:10:21 +0000" and return "1".
kv = string(bytes.Trim(u.Version[:], "\x00"))
kr = string(bytes.Trim(u.Release[:], "\x00"))
kv = string(bytes.Trim(u.Version[:], "\x00"))
} else {
kr = enforcedKR
kv = enforcedKV
Expand All @@ -52,6 +51,7 @@ func FetchInfo(enforcedKR, enforcedKV string) (kernelrelease.KernelRelease, erro

// formatVersion takes a kernelversion string (as taken from `uname -v`
// and extracts the first part of the string.
// Eg: '#1 SMP PREEMPT_DYNAMIC Tue, 10 Oct 2023 21:10:21 +0000' -> '1'.
// Eg: '#26~22.04.1-Ubuntu SMP Mon Apr 24 01:58:15 UTC 2023' -> '26~22.04.1-Ubuntu'.
func formatVersion(kv string) string {
// Take eg: "#1 SMP PREEMPT_DYNAMIC Tue, 10 Oct 2023 21:10:21 +0000" and return "1".
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/type/kmod.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (k *kmod) Load(printer *output.Printer, driverName string, fallback bool) e
if err == nil {
printer.Logger.Info("Success: module found and loaded with modprobe.")
} else {
printer.Logger.Error("Consider compiling your own driver and loading it or getting in touch with the Falco community.")
printer.Logger.Warn("Consider compiling your own driver and loading it or getting in touch with the Falco community.")
}
return err
}
Expand Down

0 comments on commit c4118ae

Please sign in to comment.