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(pkg,cmd): refactored builder script logic. #324

Merged
merged 5 commits into from
Mar 25, 2024
Merged
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
47 changes: 14 additions & 33 deletions cmd/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,33 @@ import (
"github.com/spf13/viper"
"log/slog"
"os"
"os/user"
"runtime"
)

type localCmdOptions struct {
useDKMS bool
srcDir string
envMap map[string]string
useDKMS bool
downloadHeaders bool
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Local cmd now supports automatically downloading kernel headers for the build.
Note that it is not a fatal error if kernel headers are not found; it will just continue trying with locally installed ones, if any.

srcDir string
envMap map[string]string
}

// NewLocalCmd creates the `driverkit local` command.
func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.FlagSet) *cobra.Command {
func NewLocalCmd(rootOpts *RootOptions, rootFlags *pflag.FlagSet) *cobra.Command {
opts := localCmdOptions{}
localCmd := &cobra.Command{
Use: "local",
Short: "Build Falco kernel modules and eBPF probes in local env with local kernel sources and gcc/clang.",
PersistentPreRunE: persistentPreRunFunc(rootCommand, rootOpts),
Use: "local",
Short: "Build Falco kernel modules and eBPF probes in local env with local kernel sources and gcc/clang.",
Run: func(c *cobra.Command, args []string) {
slog.With("processor", c.Name()).Info("driver building, it will take a few seconds")
if !configOptions.DryRun {
b := rootOpts.ToBuild()
if !b.HasOutputs() {
return
}
if opts.useDKMS {
currentUser, err := user.Current()
if err != nil {
slog.With("err", err.Error()).Error("Failed to retrieve user. Exiting.")
os.Exit(1)
}
if currentUser.Username != "root" {
slog.Error("Must be run as root for DKMS build.")
os.Exit(1)
}
}
if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"), opts.useDKMS, opts.srcDir, opts.envMap).Start(b); err != nil {
if err := driverbuilder.NewLocalBuildProcessor(viper.GetInt("timeout"),
opts.useDKMS,
opts.downloadHeaders,
opts.srcDir,
opts.envMap).Start(b); err != nil {
slog.With("err", err.Error()).Error("exiting")
os.Exit(1)
}
Expand All @@ -52,7 +43,6 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F
// Add root flags, but not the ones unneeded
unusedFlagsSet := map[string]struct{}{
"architecture": {},
"target": {},
"kernelurls": {},
"builderrepo": {},
"builderimage": {},
Expand All @@ -71,18 +61,9 @@ func NewLocalCmd(rootCommand *RootCmd, rootOpts *RootOptions, rootFlags *pflag.F
}
})
flagSet.BoolVar(&opts.useDKMS, "dkms", false, "Enforce usage of DKMS to build the kernel module.")
flagSet.BoolVar(&opts.downloadHeaders, "download-headers", false, "Try to automatically download kernel headers.")
flagSet.StringVar(&opts.srcDir, "src-dir", "", "Enforce usage of local source dir to build drivers.")
flagSet.StringToStringVar(&opts.envMap, "env", nil, "Env variables to be enforced during the driver build.")
flagSet.StringToStringVar(&opts.envMap, "env", make(map[string]string), "Env variables to be enforced during the driver build.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default to an empty map instead of nil. Not a big deal anyway.

localCmd.PersistentFlags().AddFlagSet(flagSet)
return localCmd
}

// Partially overrides rootCmd.persistentPreRunFunc setting some defaults before config init/validation stage.
func persistentPreRunFunc(rootCommand *RootCmd, rootOpts *RootOptions) func(c *cobra.Command, args []string) error {
return func(c *cobra.Command, args []string) error {
// Default values
rootOpts.Target = "local"
rootOpts.Architecture = runtime.GOARCH
return rootCommand.c.PersistentPreRunE(c, args)
}
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func NewRootCmd() *RootCmd {
rootCmd.AddCommand(NewKubernetesCmd(rootOpts, flags))
rootCmd.AddCommand(NewKubernetesInClusterCmd(rootOpts, flags))
rootCmd.AddCommand(NewDockerCmd(rootOpts, flags))
rootCmd.AddCommand(NewLocalCmd(ret, rootOpts, flags))
rootCmd.AddCommand(NewLocalCmd(rootOpts, flags))
rootCmd.AddCommand(NewImagesCmd(rootOpts, flags))
rootCmd.AddCommand(NewCompletionCmd())

Expand Down
13 changes: 9 additions & 4 deletions pkg/driverbuilder/builder/aliyunlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/alinux_kernel.sh
var alinuxKernelTemplate string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Each builder does now expose a kernel download/extract template too.


//go:embed templates/alinux.sh
var alinuxTemplate string

Expand All @@ -32,7 +35,6 @@ func init() {
}

type alinuxTemplateData struct {
commonTemplateData
KernelDownloadURL string
}

Expand All @@ -43,6 +45,10 @@ func (c *alinux) Name() string {
return TargetTypeAlinux.String()
}

func (c *alinux) TemplateKernelUrlsScript() string {
return alinuxKernelTemplate
}

func (c *alinux) TemplateScript() string {
return alinuxTemplate
}
Expand All @@ -51,10 +57,9 @@ func (c *alinux) URLs(kr kernelrelease.KernelRelease) ([]string, error) {
return fetchAlinuxKernelURLS(kr), nil
}

func (c *alinux) TemplateData(cfg Config, kr kernelrelease.KernelRelease, urls []string) interface{} {
func (c *alinux) KernelTemplateData(_ kernelrelease.KernelRelease, urls []string) interface{} {
return alinuxTemplateData{
commonTemplateData: cfg.toTemplateData(c, kr),
KernelDownloadURL: urls[0],
KernelDownloadURL: urls[0],
}
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/driverbuilder/builder/almalinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/almalinux_kernel.sh
var almaKernelTemplate string

//go:embed templates/almalinux.sh
var almaTemplate string

Expand All @@ -32,7 +35,6 @@ func init() {
}

type almaTemplateData struct {
commonTemplateData
KernelDownloadURL string
}

Expand All @@ -44,6 +46,10 @@ func (c *alma) Name() string {
return TargetTypeAlma.String()
}

func (c *alma) TemplateKernelUrlsScript() string {
return almaKernelTemplate
}

func (c *alma) TemplateScript() string {
return almaTemplate
}
Expand All @@ -52,10 +58,9 @@ func (c *alma) URLs(kr kernelrelease.KernelRelease) ([]string, error) {
return fetchAlmaKernelURLS(kr), nil
}

func (c *alma) TemplateData(cfg Config, kr kernelrelease.KernelRelease, urls []string) interface{} {
func (c *alma) KernelTemplateData(_ kernelrelease.KernelRelease, urls []string) interface{} {
return almaTemplateData{
commonTemplateData: cfg.toTemplateData(c, kr),
KernelDownloadURL: urls[0],
KernelDownloadURL: urls[0],
}
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/driverbuilder/builder/amazonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/amazonlinux_kernel.sh
var amazonlinuxKernelTemplate string

//go:embed templates/amazonlinux.sh
var amazonlinuxTemplate string

Expand Down Expand Up @@ -80,14 +83,15 @@ func init() {
}

type amazonlinuxTemplateData struct {
commonTemplateData
KernelDownloadURLs []string
}

func (a *amazonlinux) Name() string {
return TargetTypeAmazonLinux.String()
}

func (a *amazonlinux) TemplateKernelUrlsScript() string { return amazonlinuxKernelTemplate }

func (a *amazonlinux) TemplateScript() string {
return amazonlinuxTemplate
}
Expand All @@ -96,9 +100,8 @@ func (a *amazonlinux) URLs(kr kernelrelease.KernelRelease) ([]string, error) {
return fetchAmazonLinuxPackagesURLs(a, kr)
}

func (a *amazonlinux) TemplateData(c Config, kr kernelrelease.KernelRelease, urls []string) interface{} {
func (a *amazonlinux) KernelTemplateData(_ kernelrelease.KernelRelease, urls []string) interface{} {
return amazonlinuxTemplateData{
commonTemplateData: c.toTemplateData(a, kr),
KernelDownloadURLs: urls,
}
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/driverbuilder/builder/archlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"github.com/falcosecurity/driverkit/pkg/kernelrelease"
)

//go:embed templates/archlinux_kernel.sh
var archlinuxKernelTemplate string

//go:embed templates/archlinux.sh
var archlinuxTemplate string

Expand All @@ -37,19 +40,26 @@ type archlinux struct {
}

type archlinuxTemplateData struct {
commonTemplateData
KernelDownloadURL string
}

func (c *archlinux) Name() string {
return TargetTypeArchlinux.String()
}

func (c *archlinux) TemplateKernelUrlsScript() string { return archlinuxKernelTemplate }

func (c *archlinux) TemplateScript() string {
return archlinuxTemplate
}

func (c *archlinux) URLs(kr kernelrelease.KernelRelease) ([]string, error) {
// uname -r returns "6.8.1-arch1-1" but headers URL is "6.8.1.arch1-1"
// Also, for 0-patch releases, like: "6.8.0-arch1-1", headers url is "6.8.arch1-1"
kr.FullExtraversion = strings.Replace(kr.FullExtraversion, "-arch", ".arch", 1)
if kr.Patch == 0 {
kr.Fullversion = strings.TrimSuffix(kr.Fullversion, ".0")
}

urls := []string{}
possibleCompressionSuffixes := []string{
Expand Down Expand Up @@ -140,9 +150,8 @@ func (c *archlinux) URLs(kr kernelrelease.KernelRelease) ([]string, error) {
return urls, nil
}

func (c *archlinux) TemplateData(cfg Config, kr kernelrelease.KernelRelease, urls []string) interface{} {
func (c *archlinux) KernelTemplateData(_ kernelrelease.KernelRelease, urls []string) interface{} {
return archlinuxTemplateData{
commonTemplateData: cfg.toTemplateData(c, kr),
KernelDownloadURL: urls[0],
KernelDownloadURL: urls[0],
}
}
3 changes: 1 addition & 2 deletions pkg/driverbuilder/builder/bottlerocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ func (b *bottlerocket) Name() string {
return TargetTypeBottlerocket.String()
}

func (b *bottlerocket) TemplateData(c Config, kr kernelrelease.KernelRelease, urls []string) interface{} {
func (b *bottlerocket) KernelTemplateData(kr kernelrelease.KernelRelease, urls []string) interface{} {
return vanillaTemplateData{
commonTemplateData: c.toTemplateData(b, kr),
KernelDownloadURL: urls[0],
KernelLocalVersion: kr.FullExtraversion,
}
Expand Down
Loading
Loading