Repository: falcosecurity/driverkit
Pull request #124 (2022-02-12 12:55:20)

feat(pkg/kernelrelease): introduce json tags for kernelrelease
This PR introduces JSON tags for the `KernelRelease` struct, with snake case naming convention.

Signed-off-by: maxgio92 <massimiliano.giovagnoli.1992@gmail.com>

**What type of PR is this?**

/kind feature

**Any specific area of the project related to this PR?**

/area pkg
/area tests

**What this PR does / why we need it**:

If we'd need to encode `KernelRelease`s the transformed data will follow the snake case naming convention.

**Which issue(s) this PR fixes**:

None

**Special notes for your reviewer**:

This PR introduces a test for the struct encoding transformation.

**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
feat(pkg/kernelrelease): introduce json tags for kernelrelease
action required: KernelRelease JSON-encoded data fields are renamed following snake case
```


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

This pull-request has been approved by: *<a href="https://github.com/falcosecurity/driverkit/pull/124#" title="Author self-approved">maxgio92</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **fntlnz** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @fntlnz` in a comment when ready.

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

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

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

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":["fntlnz"]} -->

Patch:
diff --git a/README.md b/README.md
index cbc0635..c0a1dd7 100644
--- a/README.md
+++ b/README.md
@@ -26,13 +26,13 @@ uname -r
 ### Against a Kubernetes cluster
 
 ```bash
-driverkit kubernetes --output-module /tmp/falco.ko --kernelversion=81 --kernelrelease=4.15.0-72-generic --driverversion=dev --target=ubuntu-generic
+driverkit kubernetes --output-module /tmp/falco.ko --kernelversion=81 --kernelrelease=4.15.0-72-generic --driverversion=master --target=ubuntu-generic
 ```
 
 ### Against a Docker daemon
 
 ```bash
-driverkit docker --output-module /tmp/falco.ko --kernelversion=81 --kernelrelease=4.15.0-72-generic --driverversion=dev --target=ubuntu-generic
+driverkit docker --output-module /tmp/falco.ko --kernelversion=81 --kernelrelease=4.15.0-72-generic --driverversion=master --target=ubuntu-generic
 ```
 
 
@@ -47,7 +47,7 @@ target: ubuntu-aws
 output:
   module: /tmp/falco-ubuntu-aws.ko
   probe: /tmp/falco-ubuntu-aws.o
-driverversion: dev
+driverversion: master
 ```
 
 Now run driverkit using the configuration file:
@@ -73,7 +73,7 @@ target: ubuntu-generic
 output:
   module: /tmp/falco-ubuntu-generic.ko
   probe: /tmp/falco-ubuntu-generic.o
-driverversion: dev
+driverversion: master
 ```
 
 ### ubuntu-aws
@@ -87,7 +87,7 @@ target: ubuntu-aws
 output:
   module: /tmp/falco-ubuntu-aws.ko
   probe: /tmp/falco-ubuntu-aws.o
-driverversion: dev
+driverversion: master
 ```
 
 ### centos 6
@@ -98,7 +98,7 @@ kernelversion: 1
 target: centos
 output:
   module: /tmp/falco-centos6.ko
-driverversion: dev
+driverversion: master
 ```
 
 ### centos 7
@@ -109,7 +109,7 @@ kernelversion: 1
 target: centos
 output:
   module: /tmp/falco-centos7.ko
-driverversion: dev
+driverversion: master
 ```
 
 ### centos 8
@@ -120,7 +120,7 @@ kernelversion: 1
 target: centos
 output:
   module: /tmp/falco-centos8.ko
-driverversion: dev
+driverversion: master
 ```
 
 ### amazonlinux
@@ -155,7 +155,7 @@ output:
   module: /tmp/falco-debian.ko
   probe: /tmp/falco-debian.o
 target: debian
-driverversion: dev
+driverversion: master
 ```
 
 ### vanilla
diff --git a/cmd/root_options.go b/cmd/root_options.go
index 2076550..b2f3b8b 100644
--- a/cmd/root_options.go
+++ b/cmd/root_options.go
@@ -19,7 +19,7 @@ type OutputOptions struct {
 // RootOptions ...
 type RootOptions struct {
 	Architecture     string `default:"x86_64" validate:"required,oneof=x86_64" name:"architecture"`
-	DriverVersion    string `default:"dev" validate:"eq=dev|sha1|semver" name:"driver version"`
+	DriverVersion    string `default:"master" validate:"eq=master|sha1|semver" name:"driver version"`
 	KernelVersion    uint16 `default:"1" validate:"omitempty,number" name:"kernel version"`
 	ModuleDriverName string `default:"falco" validate:"max=60" name:"kernel module driver name"`
 	ModuleDeviceName string `default:"falco" validate:"excludes=/,max=255" name:"kernel module device name"`
diff --git a/docs/driverkit.md b/docs/driverkit.md
index 8ed5dba..788c827 100644
--- a/docs/driverkit.md
+++ b/docs/driverkit.md
@@ -14,15 +14,18 @@ driverkit
 
 ```
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
       --kernelrelease string      kernel release to build the module for, it can be found by executing 'uname -v'
       --kernelversion uint16      kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default 1)
   -l, --loglevel string           log level (default "info")
+      --moduledevicename string   kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
+      --moduledrivername string   kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
       --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
   -t, --target string             the system to target the build for
       --timeout int               timeout in seconds (default 60)
 ```
diff --git a/docs/driverkit_docker.md b/docs/driverkit_docker.md
index c893e63..8d707f6 100644
--- a/docs/driverkit_docker.md
+++ b/docs/driverkit_docker.md
@@ -14,15 +14,18 @@ driverkit docker [flags]
 
 ```
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for docker
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
       --kernelrelease string      kernel release to build the module for, it can be found by executing 'uname -v'
       --kernelversion uint16      kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default 1)
   -l, --loglevel string           log level (default "info")
+      --moduledevicename string   kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
+      --moduledrivername string   kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
       --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
   -t, --target string             the system to target the build for
       --timeout int               timeout in seconds (default 60)
 ```
diff --git a/docs/driverkit_kubernetes.md b/docs/driverkit_kubernetes.md
index d4bb061..da5ac1f 100644
--- a/docs/driverkit_kubernetes.md
+++ b/docs/driverkit_kubernetes.md
@@ -22,7 +22,7 @@ driverkit kubernetes [flags]
       --cluster string                 the name of the kubeconfig cluster to use
   -c, --config string                  config file path (default $HOME/.driverkit.yaml if exists)
       --context string                 the name of the kubeconfig context to use
-      --driverversion string           driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string           driver version as a git commit hash or as a git tag (default "master")
       --dryrun                         do not actually perform the action
   -h, --help                           help for kubernetes
       --insecure-skip-tls-verify       if true, the server's certificate will not be checked for validity, this will make your HTTPS connections insecure
@@ -31,9 +31,12 @@ driverkit kubernetes [flags]
       --kernelversion uint16           kernel version to build the module for, it's the numeric value after the hash when you execute 'uname -v' (default 1)
       --kubeconfig string              path to the kubeconfig file to use for CLI requests
   -l, --loglevel string                log level (default "info")
+      --moduledevicename string        kernel module device name (the default is falco, so the device will be under /dev/falco*) (default "falco")
+      --moduledrivername string        kernel module driver name, i.e. the name you see when you check installed modules via lsmod (default "falco")
   -n, --namespace string               if present, the namespace scope for this CLI request
       --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
       --request-timeout string         the length of time to wait before giving up on a single server request, non-zero values should contain a corresponding time unit (e.g, 1s, 2m, 3h), a value of zero means don't timeout requests (default "0")
   -s, --server string                  the address and port of the Kubernetes API server
   -t, --target string                  the system to target the build for
diff --git a/pkg/kernelrelease/kernelrelease.go b/pkg/kernelrelease/kernelrelease.go
index 6ff1fa8..a203bd4 100644
--- a/pkg/kernelrelease/kernelrelease.go
+++ b/pkg/kernelrelease/kernelrelease.go
@@ -11,12 +11,12 @@ var (
 
 // KernelRelease contains all the version parts.
 type KernelRelease struct {
-	Fullversion      string
-	Version          string
-	PatchLevel       string
-	Sublevel         string
-	Extraversion     string
-	FullExtraversion string
+	Fullversion      string `json:"full_version"`
+	Version          string `json:"version"`
+	PatchLevel       string `json:"patch_level"`
+	Sublevel         string `json:"sublevel"`
+	Extraversion     string `json:"extra_version"`
+	FullExtraversion string `json:"full_extra_version"`
 }
 
 // IsGKE tells whether the current kernel release is for GKE by looking at its name.
diff --git a/validate/validate.go b/validate/validate.go
index 2d2661e..1a6c71e 100644
--- a/validate/validate.go
+++ b/validate/validate.go
@@ -113,7 +113,7 @@ func init() {
 		"eq=dev|sha1|semver",
 		T,
 		func(ut ut.Translator) error {
-			return ut.Add("eq=dev|sha1|semver", `{0} must be a valid SHA1, semver-ish, or the "dev" string`, true)
+			return ut.Add("eq=dev|sha1|semver", `{0} must be a valid SHA1, semver-ish, or the "master" string`, true)
 		},
 		func(ut ut.Translator, fe validator.FieldError) string {
 			t, _ := ut.T(fe.Tag(), fe.Field())


Test patch:
diff --git a/cmd/testdata/autohelp.txt b/cmd/testdata/autohelp.txt
index 8302ecd..19fc21b 100644
--- a/cmd/testdata/autohelp.txt
+++ b/cmd/testdata/autohelp.txt
@@ -13,7 +13,7 @@ Available Commands:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/cmd/testdata/configs/1.yaml b/cmd/testdata/configs/1.yaml
index 7448f4e..98cd8d7 100644
--- a/cmd/testdata/configs/1.yaml
+++ b/cmd/testdata/configs/1.yaml
@@ -3,4 +3,4 @@ kernelversion: 59
 target: ubuntu-aws
 output:
     module: /tmp/falco-ubuntu-aws.ko
-driverversion: dev
\ No newline at end of file
+driverversion: master
diff --git a/cmd/testdata/docker-from-config-debug.txt b/cmd/testdata/docker-from-config-debug.txt
index 99ea3fb..647e68e 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                          driverversion=dev kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu-aws
+DEBU running with options                          driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu-aws
 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 abcb7a5..ed0a61e 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                          driverversion=dev kernelrelease=4.15.0-1057-aws kernelversion=229 output-module=/tmp/override.ko target=ubuntu-aws
+DEBU running with options                          driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 output-module=/tmp/override.ko target=ubuntu-aws
 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 1383c36..ceb8b50 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                          driverversion=dev kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu-aws
+DEBU running with options                          driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 output-module=/tmp/falco-ubuntu-aws.ko target=ubuntu-aws
 INFO driver building, it will take a few seconds   processor=docker
diff --git a/cmd/testdata/dockernoopts.txt b/cmd/testdata/dockernoopts.txt
index b6e0cba..1c71c04 100644
--- a/cmd/testdata/dockernoopts.txt
+++ b/cmd/testdata/dockernoopts.txt
@@ -8,7 +8,7 @@ Usage:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for docker
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/cmd/testdata/help-flag.txt b/cmd/testdata/help-flag.txt
index 45e08c0..ef6fd0b 100644
--- a/cmd/testdata/help-flag.txt
+++ b/cmd/testdata/help-flag.txt
@@ -12,7 +12,7 @@ Available Commands:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/cmd/testdata/help.txt b/cmd/testdata/help.txt
index c587a6c..44aac46 100644
--- a/cmd/testdata/help.txt
+++ b/cmd/testdata/help.txt
@@ -12,7 +12,7 @@ Available Commands:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/cmd/testdata/invalid-proxyconfig.txt b/cmd/testdata/invalid-proxyconfig.txt
index da3934a..409b214 100644
--- a/cmd/testdata/invalid-proxyconfig.txt
+++ b/cmd/testdata/invalid-proxyconfig.txt
@@ -12,7 +12,7 @@ Available Commands:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/cmd/testdata/non-existent-processor.txt b/cmd/testdata/non-existent-processor.txt
index 367e203..dad2861 100644
--- a/cmd/testdata/non-existent-processor.txt
+++ b/cmd/testdata/non-existent-processor.txt
@@ -11,7 +11,7 @@ Available Commands:
 
 Flags:
   -c, --config string             config file path (default $HOME/.driverkit.yaml if exists)
-      --driverversion string      driver version as a git commit hash or as a git tag (default "dev")
+      --driverversion string      driver version as a git commit hash or as a git tag (default "master")
       --dryrun                    do not actually perform the action
   -h, --help                      help for driverkit
       --kernelconfigdata string   base64 encoded kernel config data: in some systems it can be found under the /boot directory, in other it is gzip compressed under /proc
diff --git a/pkg/kernelrelease/kernelrelease_test.go b/pkg/kernelrelease/kernelrelease_test.go
index 1ffa892..a1415a4 100644
--- a/pkg/kernelrelease/kernelrelease_test.go
+++ b/pkg/kernelrelease/kernelrelease_test.go
@@ -2,10 +2,32 @@ package kernelrelease
 
 import (
 	"testing"
+	"encoding/json"
 
 	"gotest.tools/assert"
 )
 
+func TestFromKrToJson(t *testing.T) {
+	test := struct {
+		kernelRelease	KernelRelease
+		want			string
+	}{
+		kernelRelease: KernelRelease{
+			Fullversion:      "5.16.5",
+			Version:          "5",
+			PatchLevel:       "16",
+			Sublevel:         "5",
+			Extraversion:     "arch1-1",
+			FullExtraversion: "-arch1-1",
+		},
+		want: `{"full_version":"5.16.5","version":"5","patch_level":"16","sublevel":"5","extra_version":"arch1-1","full_extra_version":"-arch1-1"}`,
+	}
+	t.Run("version with local version", func(t *testing.T) {
+		got, _ := json.Marshal(test.kernelRelease)
+		assert.Equal(t, test.want, string(got))
+	})
+}
+
 func TestFromString(t *testing.T) {
 	tests := map[string]struct {
 		kernelVersionStr string