Skip to content

Commit

Permalink
cleanup(cmd,pkg): properly manage ubuntu flavors at builders level.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP authored and poiana committed Oct 23, 2023
1 parent c40e6ce commit 56573f4
Show file tree
Hide file tree
Showing 29 changed files with 48 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ type flagsTemplateData struct {
}

func initFlagsTemplateData(args []string) flagsTemplateData {
targets := builder.BuilderByTarget.Targets()
targets := builder.Targets()
sort.Strings(targets)

cmd := "driverkit"
Expand Down
7 changes: 1 addition & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ func persistentValidateFunc(rootCommand *RootCmd, rootOpts *RootOptions) func(c
// Avoid sensitive info into default values help line
rootCommand.StripSensitive()

// We just use ubuntu internally
if strings.HasPrefix(rootOpts.Target, "ubuntu") {
rootOpts.Target = "ubuntu"
}

// Do not block root or help command to exec disregarding the root flags validity
if c.Root() != c && c.Name() != "help" && c.Name() != "__complete" && c.Name() != "__completeNoDesc" && c.Name() != "completion" {
if errs := rootOpts.Validate(); errs != nil {
Expand Down Expand Up @@ -141,7 +136,7 @@ func NewRootCmd() *RootCmd {

flags := rootCmd.Flags()

targets := builder.BuilderByTarget.Targets()
targets := builder.Targets()
sort.Strings(targets)

flags.StringVarP(&configOptions.ConfigFile, "config", "c", configOptions.ConfigFile, "config file path (default $HOME/.driverkit.yaml if exists)")
Expand Down
2 changes: 1 addition & 1 deletion cmd/testdata/docker-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
level=INFO msg="using config file" file=testdata/configs/1.yaml
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
2 changes: 1 addition & 1 deletion cmd/testdata/docker-override-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
level=INFO msg="using config file" file=testdata/configs/1.yaml
level=DEBUG msg="running with options" output-module=/tmp/override.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 target=ubuntu arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=DEBUG msg="running with options" output-module=/tmp/override.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=229 target=ubuntu-aws arch={{ .CurrentArch }} kernelurls=[] repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
2 changes: 1 addition & 1 deletion cmd/testdata/docker-override-urls-from-config-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
level=INFO msg="using config file" file=testdata/configs/2.yaml
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
2 changes: 1 addition & 1 deletion cmd/testdata/docker-related-target-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
level=DEBUG msg="running without a configuration file"
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-azure.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-azure kernelversion=62 target=ubuntu arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-azure.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-azure kernelversion=62 target=ubuntu-azure arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
2 changes: 1 addition & 1 deletion cmd/testdata/docker-with-flags-debug.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
level=DEBUG msg="running without a configuration file"
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=DEBUG msg="running with options" output-module=/tmp/falco-ubuntu-aws.ko output-probe="" driverversion=master kernelrelease=4.15.0-1057-aws kernelversion=59 target=ubuntu-aws arch={{ .CurrentArch }} 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]" repo-org=falcosecurity repo-name=libs
level=INFO msg="driver building, it will take a few seconds" processor=docker
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/aliyunlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var alinuxTemplate string
const TargetTypeAlinux Type = "alinux"

func init() {
BuilderByTarget[TargetTypeAlinux] = &alinux{}
byTarget[TargetTypeAlinux] = &alinux{}
}

type alinuxTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/almalinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var almaTemplate string
const TargetTypeAlma Type = "almalinux"

func init() {
BuilderByTarget[TargetTypeAlma] = &alma{}
byTarget[TargetTypeAlma] = &alma{}
}

type almaTemplateData struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/driverbuilder/builder/amazonlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ const TargetTypeAmazonLinux2 Type = "amazonlinux2"
const TargetTypeAmazonLinux Type = "amazonlinux"

func init() {
BuilderByTarget[TargetTypeAmazonLinux2023] = &amazonlinux2023{}
BuilderByTarget[TargetTypeAmazonLinux2022] = &amazonlinux2022{}
BuilderByTarget[TargetTypeAmazonLinux2] = &amazonlinux2{}
BuilderByTarget[TargetTypeAmazonLinux] = &amazonlinux{}
byTarget[TargetTypeAmazonLinux2023] = &amazonlinux2023{}
byTarget[TargetTypeAmazonLinux2022] = &amazonlinux2022{}
byTarget[TargetTypeAmazonLinux2] = &amazonlinux2{}
byTarget[TargetTypeAmazonLinux] = &amazonlinux{}
}

type amazonlinuxTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/archlinux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var archlinuxTemplate string
const TargetTypeArchlinux Type = "arch"

func init() {
BuilderByTarget[TargetTypeArchlinux] = &archlinux{}
byTarget[TargetTypeArchlinux] = &archlinux{}
}

// archlinux is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/bottlerocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
const TargetTypeBottlerocket Type = "bottlerocket"

func init() {
BuilderByTarget[TargetTypeBottlerocket] = &bottlerocket{
byTarget[TargetTypeBottlerocket] = &bottlerocket{
vanilla{},
}
}
Expand Down
16 changes: 15 additions & 1 deletion pkg/driverbuilder/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"net/url"
"os"
"path"
"strings"
"text/template"

"github.com/blang/semver"
Expand Down Expand Up @@ -249,13 +250,26 @@ func (b *Build) GetBuilderImage() string {

// Factory returns a builder for the given target.
func Factory(target Type) (Builder, error) {
b, ok := BuilderByTarget[target]
// Driverkit builder is named "ubuntu"; there is no ubuntu-foo
if strings.HasPrefix(target.String(), "ubuntu") {
target = Type("ubuntu")
}
b, ok := byTarget[target]
if !ok {
return nil, fmt.Errorf("no builder found for target: %s", target)
}
return b, nil
}

// Targets returns the list of all the supported targets.
func Targets() []string {
res := []string{}
for k := range byTarget {
res = append(res, k.String())
}
return res
}

func (c Config) toTemplateData(b Builder, kr kernelrelease.KernelRelease) commonTemplateData {
c.setGCCVersion(b, kr)
return commonTemplateData{
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/centos.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var centosTemplate string
const TargetTypeCentos Type = "centos"

func init() {
BuilderByTarget[TargetTypeCentos] = &centos{}
byTarget[TargetTypeCentos] = &centos{}
}

// centos is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const TargetTypeDebian Type = "debian"
const debianRequiredURLs = 3

func init() {
BuilderByTarget[TargetTypeDebian] = &debian{}
byTarget[TargetTypeDebian] = &debian{}
}

type debianTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var fedoraTemplate string
const TargetTypeFedora Type = "fedora"

func init() {
BuilderByTarget[TargetTypeFedora] = &fedora{}
byTarget[TargetTypeFedora] = &fedora{}
}

// fedora is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/flatcar.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var flatcarTemplate string
const TargetTypeFlatcar Type = "flatcar"

func init() {
BuilderByTarget[TargetTypeFlatcar] = &flatcar{}
byTarget[TargetTypeFlatcar] = &flatcar{}
}

type flatcarTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/minikube.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
const TargetTypeMinikube Type = "minikube"

func init() {
BuilderByTarget[TargetTypeMinikube] = &minikube{
byTarget[TargetTypeMinikube] = &minikube{
vanilla{},
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/opensuse.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var releases []string = []string{
}

func init() {
BuilderByTarget[TargetTypeOpenSUSE] = &opensuse{}
byTarget[TargetTypeOpenSUSE] = &opensuse{}
}

// opensuse is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var oracleTemplate string
const TargetTypeoracle Type = "ol"

func init() {
BuilderByTarget[TargetTypeoracle] = &oracle{}
byTarget[TargetTypeoracle] = &oracle{}
}

// oracle is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/photon.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const TargetTypePhoton Type = "photon"
var photonTemplate string

func init() {
BuilderByTarget[TargetTypePhoton] = &photon{}
byTarget[TargetTypePhoton] = &photon{}
}

// photon is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type redhat struct {
}

func init() {
BuilderByTarget[TargetTypeRedhat] = &redhat{}
byTarget[TargetTypeRedhat] = &redhat{}
}

type redhatTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/rocky.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var rockyTemplate string
const TargetTypeRocky Type = "rocky"

func init() {
BuilderByTarget[TargetTypeRocky] = &rocky{}
byTarget[TargetTypeRocky] = &rocky{}
}

type rockyTemplateData struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/talos.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
const TargetTypeTalos Type = "talos"

func init() {
BuilderByTarget[TargetTypeTalos] = &talos{
byTarget[TargetTypeTalos] = &talos{
vanilla{},
}
}
Expand Down
16 changes: 2 additions & 14 deletions pkg/driverbuilder/builder/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,12 @@ limitations under the License.

package builder

// BuilderByTarget maps targets to their builder.
var BuilderByTarget = Targets{}
// byTarget maps targets to their builder.
var byTarget = map[Type]Builder{}

// Type is a type representing targets.
type Type string

func (t Type) String() string {
return string(t)
}

// Targets is a type representing the list of the supported targets.
type Targets map[Type]Builder

// Targets returns the list of all the supported targets.
func (t Targets) Targets() []string {
res := []string{}
for k := range t {
res = append(res, k.String())
}
return res
}
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type ubuntuTemplateData struct {
}

func init() {
BuilderByTarget[TargetTypeUbuntu] = &ubuntu{}
byTarget[TargetTypeUbuntu] = &ubuntu{}
}

// ubuntu is a driverkit target.
Expand Down
2 changes: 1 addition & 1 deletion pkg/driverbuilder/builder/vanilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type vanilla struct {
const TargetTypeVanilla Type = "vanilla"

func init() {
BuilderByTarget[TargetTypeVanilla] = &vanilla{}
byTarget[TargetTypeVanilla] = &vanilla{}
}

type vanillaTemplateData struct {
Expand Down
4 changes: 2 additions & 2 deletions validate/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func isTargetSupported(fl validator.FieldLevel) bool {

switch field.Kind() {
case reflect.String:
_, ok := builder.BuilderByTarget[builder.Type(field.String())]
return ok
_, err := builder.Factory(builder.Type(field.String()))
return err == nil
}

panic(fmt.Sprintf("Bad field type %T", field.Interface()))
Expand Down
2 changes: 1 addition & 1 deletion validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func init() {
"target",
T,
func(ut ut.Translator) error {
return ut.Add("target", fmt.Sprintf("{0} must be a valid target (%s)", builder.BuilderByTarget.Targets()), true)
return ut.Add("target", fmt.Sprintf("{0} must be a valid target (%s)", builder.Targets()), true)
},
func(ut ut.Translator, fe validator.FieldError) string {
t, _ := ut.T(fe.Tag(), fe.Field())
Expand Down

0 comments on commit 56573f4

Please sign in to comment.