Skip to content

Commit

Permalink
chore(cmd/driver): small improvements.
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
  • Loading branch information
FedeDP committed May 16, 2024
1 parent 6c1b051 commit 72dc74f
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions cmd/driver/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/net/context"
"gopkg.in/yaml.v3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
Expand Down Expand Up @@ -54,6 +53,13 @@ type driverConfigOptions struct {
KubeConfig string
}

type engineCfg struct {
Kind string `yaml:"kind"`
}
type falcoCfg struct {
Engine engineCfg `yaml:"engine"`
}

// NewDriverConfigCmd configures a driver and stores it in config.
func NewDriverConfigCmd(ctx context.Context, opt *options.Common, driver *options.Driver) *cobra.Command {
o := driverConfigOptions{
Expand Down Expand Up @@ -81,15 +87,13 @@ func NewDriverConfigCmd(ctx context.Context, opt *options.Common, driver *option
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(viper.AllKeys())
return o.RunDriverConfig(ctx)
},
}

cmd.Flags().BoolVar(&o.Update, "update-falco", true, "Whether to update Falco config/configmap.")
cmd.Flags().StringVar(&o.Namespace, "namespace", "", "Kubernetes namespace.")
cmd.Flags().StringVar(&o.KubeConfig, "kubeconfig", "", "Kubernetes config.")

return cmd
}

Expand All @@ -107,6 +111,7 @@ func (o *driverConfigOptions) RunDriverConfig(ctx context.Context) error {
return err
}
}
o.Printer.Logger.Info("Storing falcoctl driver config")
return config.StoreDriver(o.Driver.ToDriverConfig(), o.ConfigFile)
}

Expand All @@ -123,12 +128,6 @@ func checkFalcoRunsWithDrivers(engineKind string) error {

func (o *driverConfigOptions) replaceDriverTypeInFalcoConfig(driverType drivertype.DriverType) error {
falcoCfgFile := filepath.Clean(filepath.Join(string(os.PathSeparator), "etc", "falco", "falco.yaml"))
type engineCfg struct {
Kind string `yaml:"kind"`
}
type falcoCfg struct {
Engine engineCfg `yaml:"engine"`
}
yamlFile, err := os.ReadFile(filepath.Clean(falcoCfgFile))
if err != nil {
return err
Expand Down Expand Up @@ -176,14 +175,7 @@ func (o *driverConfigOptions) replaceDriverTypeInK8SConfigMap(ctx context.Contex
return errors.New(`no configmaps matching "app.kubernetes.io/instance: falco" label were found`)
}

type falcoEngineCfg struct {
Kind string `yaml:"kind"`
}

type falcoCfg struct {
Engine falcoEngineCfg `yaml:"engine"`
}

updated := false
for i := 0; i < len(configMapList.Items); i++ {
configMap := &configMapList.Items[i]
// Check that this is a Falco config map
Expand Down Expand Up @@ -248,6 +240,11 @@ func (o *driverConfigOptions) replaceDriverTypeInK8SConfigMap(ctx context.Contex
if err != nil {
return err
}
updated = true
}

if !updated {
return errors.New("could not update any configmap")
}
return nil
}
Expand All @@ -257,10 +254,10 @@ func (o *driverConfigOptions) replaceDriverTypeInK8SConfigMap(ctx context.Contex
func (o *driverConfigOptions) commit(ctx context.Context, driverType drivertype.DriverType) error {
if o.Namespace != "" {
// Ok we are on k8s
o.Printer.Logger.Debug("Committing driver config to k8s configmap",
o.Printer.Logger.Info("Committing driver config to k8s configmap",
o.Printer.Logger.Args("namespace", o.Namespace))
return o.replaceDriverTypeInK8SConfigMap(ctx, driverType)
}
o.Printer.Logger.Debug("Committing driver config to local Falco config")
o.Printer.Logger.Info("Committing driver config to local Falco config")
return o.replaceDriverTypeInFalcoConfig(driverType)
}

0 comments on commit 72dc74f

Please sign in to comment.