Repository: falcosecurity/driverkit
Pull request #214 (2022-09-22 10:30:54)

new(cmd,pkg): allow users to customize repo org and name.
**What type of PR is this?**

/kind feature

<!--
Please remove the leading whitespace before the `/kind <>` you uncommented.
-->

**Any specific area of the project related to this PR?**

/area cmd

/area pkg


**What this PR does / why we need it**:

This is useful mostly for libs fork users.

**Which issue(s) this PR fixes**:

<!--
Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
If PR is `kind/failing-tests` or `kind/flaky-test`, please post the related issues/tests in a comment and do not use `Fixes`.
-->

Fixes #

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:

<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below.
If the PR requires additional action from users switching to the new release, prepend the string "action required:".
For example, `action required: change the API interface of the rule engine`.
-->

```release-note
new: allow users to customize repo org and name.
```


poiana: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/falcosecurity/driverkit/pull/214#" title="Author self-approved">FedeDP</a>*

The full list of commands accepted by this bot can be found [here](https://prow.falco.org/command-help?repo=falcosecurity%2Fdriverkit).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/falcosecurity/driverkit/blob/master/OWNERS)~~ [FedeDP]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
FedeDP: Nope, there were a couple of TODOs left from @fntlnz i think :) 
Moreover, it can be very useful to test the build of a fork against many kernels!
poiana: LGTM label has been added.  <details>Git tree hash: 22ebd550aad7eadfadd42d38cc787cc29feb9cb3</details>

Patch:
diff --git a/cmd/root.go b/cmd/root.go
index f83327b..11fddda 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -148,6 +148,9 @@ func NewRootCmd() *RootCmd {
 
 	flags.StringSliceVar(&rootOpts.KernelUrls, "kernelurls", nil, "list of kernel header urls (e.g. --kernelurls <URL1> --kernelurls <URL2> --kernelurls \"<URL3>,<URL4>\")")
 
+	flags.StringVar(&rootOpts.Repo.Org, "repo-org", rootOpts.Repo.Org, "repository github organization")
+	flags.StringVar(&rootOpts.Repo.Name, "repo-name", rootOpts.Repo.Name, "repository github name")
+
 	viper.BindPFlags(flags)
 
 	// Flag annotations and custom completions
diff --git a/cmd/root_options.go b/cmd/root_options.go
index dc8dcad..3ace5d7 100644
--- a/cmd/root_options.go
+++ b/cmd/root_options.go
@@ -15,6 +15,11 @@ type OutputOptions struct {
 	Probe  string `validate:"required_without=Module,filepath,omitempty,endswith=.o" name:"output probe path"`
 }
 
+type RepoOptions struct {
+	Org  string `default:"falcosecurity" name:"organization name"`
+	Name string `default:"libs" name:"repo name"`
+}
+
 // RootOptions ...
 type RootOptions struct {
 	Architecture     string   `validate:"required,architecture" name:"architecture"`
@@ -28,6 +33,7 @@ type RootOptions struct {
 	BuilderImage     string   `validate:"imagename" name:"builder image"`
 	GCCVersion       float64  `name:"gcc version"`
 	KernelUrls       []string `name:"kernel header urls"`
+	Repo             RepoOptions
 	Output           OutputOptions
 }
 
@@ -86,6 +92,8 @@ func (ro *RootOptions) Log() {
 	if len(ro.KernelUrls) > 0 {
 		fields["kernelurls"] = ro.KernelUrls
 	}
+	fields["repo-org"] = ro.Repo.Org
+	fields["repo-name"] = ro.Repo.Name
 
 	logger.WithFields(fields).Debug("running with options")
 }
@@ -110,6 +118,8 @@ func (ro *RootOptions) toBuild() *builder.Build {
 		GCCVersion:         ro.GCCVersion,
 		CustomBuilderImage: ro.BuilderImage,
 		KernelUrls:         ro.KernelUrls,
+		RepoOrg:            ro.Repo.Org,
+		RepoName:           ro.Repo.Name,
 	}
 }
 
diff --git a/pkg/driverbuilder/builder/build.go b/pkg/driverbuilder/builder/build.go
index 73bf35a..80ab5d4 100644
--- a/pkg/driverbuilder/builder/build.go
+++ b/pkg/driverbuilder/builder/build.go
@@ -1,6 +1,9 @@
 package builder
 
-import "github.com/falcosecurity/driverkit/pkg/kernelrelease"
+import (
+	"fmt"
+	"github.com/falcosecurity/driverkit/pkg/kernelrelease"
+)
 
 // Build contains the info about the on-going build.
 type Build struct {
@@ -17,6 +20,8 @@ type Build struct {
 	CustomBuilderImage string
 	KernelUrls         []string
 	GCCVersion         float64
+	RepoOrg            string
+	RepoName           string
 }
 
 func (b *Build) KernelReleaseFromBuildConfig() kernelrelease.KernelRelease {
@@ -24,3 +29,16 @@ func (b *Build) KernelReleaseFromBuildConfig() kernelrelease.KernelRelease {
 	kv.Architecture = kernelrelease.Architecture(b.Architecture)
 	return kv
 }
+
+func (b *Build) toGithubRepoArchive() string {
+	return fmt.Sprintf("https://github.com/%s/%s/archive", b.RepoOrg, b.RepoName)
+}
+
+func (b *Build) ToConfig() Config {
+	return Config{
+		DriverName:      b.ModuleDriverName,
+		DeviceName:      b.ModuleDeviceName,
+		DownloadBaseURL: b.toGithubRepoArchive(),
+		Build:           b,
+	}
+}
diff --git a/pkg/driverbuilder/docker.go b/pkg/driverbuilder/docker.go
index 2c82d23..36238a3 100644
--- a/pkg/driverbuilder/docker.go
+++ b/pkg/driverbuilder/docker.go
@@ -118,12 +118,7 @@ func (bp *DockerBuildProcessor) Start(b *builder.Build) error {
 	if err != nil {
 		return err
 	}
-	c := builder.Config{
-		DriverName:      b.ModuleDriverName,
-		DeviceName:      b.ModuleDeviceName,
-		DownloadBaseURL: "https://github.com/falcosecurity/libs/archive", // TODO: make this configurable
-		Build:           b,
-	}
+	c := b.ToConfig()
 
 	// Generate the build script from the builder
 	driverkitScript, err := builder.Script(v, c, kr)
diff --git a/pkg/driverbuilder/kubernetes.go b/pkg/driverbuilder/kubernetes.go
index e02403f..bb925f9 100644
--- a/pkg/driverbuilder/kubernetes.go
+++ b/pkg/driverbuilder/kubernetes.go
@@ -63,7 +63,7 @@ func (bp *KubernetesBuildProcessor) Start(b *builder.Build) error {
 	return bp.buildModule(b)
 }
 
-func (bp *KubernetesBuildProcessor) buildModule(build *builder.Build) error {
+func (bp *KubernetesBuildProcessor) buildModule(b *builder.Build) error {
 	deadline := int64(bp.timeout)
 	namespace := bp.namespace
 	uid := uuid.NewUUID()
@@ -72,20 +72,15 @@ func (bp *KubernetesBuildProcessor) buildModule(build *builder.Build) error {
 	podClient := bp.coreV1Client.Pods(namespace)
 	configClient := bp.coreV1Client.ConfigMaps(namespace)
 
-	kr := build.KernelReleaseFromBuildConfig()
+	kr := b.KernelReleaseFromBuildConfig()
 
 	// create a builder based on the chosen build type
-	v, err := builder.Factory(build.TargetType)
+	v, err := builder.Factory(b.TargetType)
 	if err != nil {
 		return err
 	}
 
-	c := builder.Config{
-		DriverName:      build.ModuleDriverName,
-		DeviceName:      build.ModuleDeviceName,
-		DownloadBaseURL: "https://github.com/falcosecurity/libs/archive", // TODO: make this configurable
-		Build:           build,
-	}
+	c := b.ToConfig()
 
 	// generate the build script from the builder
 	res, err := builder.Script(v, c, kr)
@@ -137,7 +132,7 @@ func (bp *KubernetesBuildProcessor) buildModule(build *builder.Build) error {
 		return err
 	}
 
-	configDecoded, err := base64.StdEncoding.DecodeString(build.KernelConfigData)
+	configDecoded, err := base64.StdEncoding.DecodeString(b.KernelConfigData)
 	if err != nil {
 		return err
 	}
@@ -169,7 +164,7 @@ func (bp *KubernetesBuildProcessor) buildModule(build *builder.Build) error {
 		)
 	}
 
-	builderImage := build.GetBuilderImage()
+	builderImage := b.GetBuilderImage()
 
 	secuContext := corev1.PodSecurityContext{
 		RunAsUser: &bp.runAsUser,
@@ -235,7 +230,7 @@ func (bp *KubernetesBuildProcessor) buildModule(build *builder.Build) error {
 		return err
 	}
 	defer podClient.Delete(ctx, pod.Name, metav1.DeleteOptions{})
-	return bp.copyModuleAndProbeFromPodWithUID(ctx, build, namespace, string(uid))
+	return bp.copyModuleAndProbeFromPodWithUID(ctx, b, namespace, string(uid))
 }
 
 func (bp *KubernetesBuildProcessor) copyModuleAndProbeFromPodWithUID(ctx context.Context, build *builder.Build, namespace string, falcoBuilderUID string) error {
diff --git a/pkg/driverbuilder/templates.go b/pkg/driverbuilder/templates.go
index 09ac10a..d320573 100644
--- a/pkg/driverbuilder/templates.go
+++ b/pkg/driverbuilder/templates.go
@@ -70,8 +70,7 @@ func renderMakefile(w io.Writer, md makefileData) error {
 }
 
 func LoadMakefileObjList(c builder.Config) (string, error) {
-	// TODO: make this configurable
-	makefileUrl := fmt.Sprintf("https://raw.githubusercontent.com/falcosecurity/libs/%s/driver/Makefile.in", c.DriverVersion)
+	makefileUrl := fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/driver/Makefile.in", c.RepoOrg, c.RepoName, c.DriverVersion)
 	resp, err := http.Get(makefileUrl)
 	if err != nil {
 		return "", err


Test patch:
diff --git a/cmd/testdata/docker-from-config-debug.txt b/cmd/testdata/docker-from-config-debug.txt
index 27717c1..4f4055a 100644
--- a/cmd/testdata/docker-from-config-debug.txt
+++ b/cmd/testdata/docker-from-config-debug.txt
@@ -1,3 +1,3 @@
 INFO using config file                             file=testdata/configs/1.yaml
-DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu
+DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko repo-name=libs repo-org=falcosecurity target=ubuntu
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/docker-override-from-config-debug.txt b/cmd/testdata/docker-override-from-config-debug.txt
index 29c0dcb..9a12db6 100644
--- a/cmd/testdata/docker-override-from-config-debug.txt
+++ b/cmd/testdata/docker-override-from-config-debug.txt
@@ -1,3 +1,3 @@
 INFO using config file                             file=testdata/configs/1.yaml
-DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 output-module=/tmp/override.ko target=ubuntu
+DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 output-module=/tmp/override.ko repo-name=libs repo-org=falcosecurity target=ubuntu
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/docker-override-urls-from-config-debug.txt b/cmd/testdata/docker-override-urls-from-config-debug.txt
index 0069c9c..346fe5a 100644
--- a/cmd/testdata/docker-override-urls-from-config-debug.txt
+++ b/cmd/testdata/docker-override-urls-from-config-debug.txt
@@ -1,3 +1,3 @@
 INFO using config file                             file=testdata/configs/2.yaml
-DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu
+DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko repo-name=libs repo-org=falcosecurity target=ubuntu
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/docker-related-target-debug.txt b/cmd/testdata/docker-related-target-debug.txt
index a28a6d6..eb9d1c5 100644
--- a/cmd/testdata/docker-related-target-debug.txt
+++ b/cmd/testdata/docker-related-target-debug.txt
@@ -1,3 +1,3 @@
 DEBU running without a configuration file         
-DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-azure kernelurls="[http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-azure-headers-4.15.0-1057_4.15.0-1057.62_all.deb http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-headers-4.15.0-1057-azure_4.15.0-1057.62_amd64.deb]" kernelversion=62 output-module=/tmp/falco-ubuntu-azure.ko target=ubuntu
+DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-azure kernelurls="[http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-azure-headers-4.15.0-1057_4.15.0-1057.62_all.deb http://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-azure/linux-headers-4.15.0-1057-azure_4.15.0-1057.62_amd64.deb]" kernelversion=62 output-module=/tmp/falco-ubuntu-azure.ko repo-name=libs repo-org=falcosecurity target=ubuntu
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/docker-with-flags-debug.txt b/cmd/testdata/docker-with-flags-debug.txt
index eeb4e33..6c28c3b 100644
--- a/cmd/testdata/docker-with-flags-debug.txt
+++ b/cmd/testdata/docker-with-flags-debug.txt
@@ -1,3 +1,3 @@
 DEBU running without a configuration file         
-DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu
+DEBU running with options                          arch={{ .CurrentArch }} driverversion=master kernelrelease=4.15.0-1057-aws kernelurls="[https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-aws-headers-4.15.0-1057_4.15.0-1057.59_all.deb https://mirrors.edge.kernel.org/ubuntu/pool/main/l/linux-aws/linux-headers-4.15.0-1057-aws_4.15.0-1057.59_amd64.deb]" kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko repo-name=libs repo-org=falcosecurity target=ubuntu
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/templates/flags.txt b/cmd/testdata/templates/flags.txt
index 124867d..fac5883 100644
--- a/cmd/testdata/templates/flags.txt
+++ b/cmd/testdata/templates/flags.txt
@@ -16,5 +16,7 @@ Flags:
       --output-module string      filepath where to save the resulting kernel module
       --output-probe string       filepath where to save the resulting eBPF probe
       --proxy string              the proxy to use to download data
+      --repo-name string          repository github name (default "libs")
+      --repo-org string           repository github organization (default "falcosecurity")
   -t, --target string             the system to target the build for, one of {{ .Targets }}
       --timeout int               timeout in seconds (default 120)
\ No newline at end of file