Skip to content

Commit

Permalink
chore: process spawning system with alias detection
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Grasso <me@leonardograsso.com>
  • Loading branch information
leogr committed Apr 9, 2020
1 parent 0f8ca21 commit 5d66f46
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 36 deletions.
5 changes: 4 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ func NewRun() *cobra.Command {
if err != nil {
return err
}
l := logger.StandardLogger()

r, err := runner.New(
runner.WithLogger(logger.StandardLogger()),
runner.WithLogger(l),
runner.WithKubeFactory(cmdutil.NewFactory(matchVersionKubeConfigFlags)),
runner.WithKubeNamespace(ns),
runner.WithExecutable("", "--loglevel", l.GetLevel().String(), "run"),
)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions events/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Helper interface {
// first called order.
Cleanup(f func(), args ...interface{})

SpawnAs(name string, action string) error

// ResourceBuilder returns a k8s' resource.Builder.
ResourceBuilder() *resource.Builder
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/runner/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package runner

import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"

logger "github.com/sirupsen/logrus"
"k8s.io/cli-runtime/pkg/resource"
)

type helper struct {
runner *Runner
log *logger.Entry
builder *resource.Builder
cleanup func()
}

func (h *helper) Log() *logger.Entry {
return h.log
}

func (h *helper) ResourceBuilder() *resource.Builder {
// todo(leogr): handle nil case
return h.builder
}

// Cleanup registers a function to be called when the action complete or later.
// Cleanup functions registered from within the same action will be called in last added,
// first called order.
func (h *helper) Cleanup(f func(), args ...interface{}) {
oldCleanup := h.cleanup
h.cleanup = func() {
if oldCleanup != nil {
defer oldCleanup()
}
args = append([]interface{}{"clenaup "}, args...)
h.Log().Info(args...)
f()
}
}

func (h *helper) SpawnAs(name string, action string) error {
h.Log().WithField("arg", action).Infof(`spawn as "%s"`, name)
tmpDir, err := ioutil.TempDir(os.TempDir(), "falco-event-generator")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

name = filepath.Join(tmpDir, name)
if err := os.Symlink(h.runner.exePath, name); err != nil {
return err
}

cmd := exec.Command(name, append(h.runner.exeArgs, action)...)

out := h.runner.log.Out
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return err
}

return nil
}
77 changes: 42 additions & 35 deletions pkg/runner/runner.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,36 @@
package runner

import (
"os"
"path/filepath"

"github.com/falcosecurity/event-generator/events"
logger "github.com/sirupsen/logrus"
"k8s.io/cli-runtime/pkg/resource"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
)

type Runner struct {
log *logger.Logger
kf cmdutil.Factory
kn string
}

type helper struct {
log *logger.Entry
builder *resource.Builder
cleanup func()
}

func (h *helper) Log() *logger.Entry {
return h.log
}

func (h *helper) ResourceBuilder() *resource.Builder {
// todo(leogr): handle nil case
return h.builder
}

// Cleanup registers a function to be called when the action complete or later.
// Cleanup functions registered from within the same action will be called in last added,
// first called order.
func (h *helper) Cleanup(f func(), args ...interface{}) {
oldCleanup := h.cleanup
h.cleanup = func() {
if oldCleanup != nil {
defer oldCleanup()
}
args = append([]interface{}{"clenaup "}, args...)
h.Log().Info(args...)
f()
}
log *logger.Logger
kf cmdutil.Factory
kn string
exePath string
exeArgs []string
alias string
}

func (r *Runner) trigger(n string, f events.Action) (cleanup func(), err error) {
fields := logger.Fields{
"action": n,
}
if r.alias != "" {
fields["as"] = r.alias
}
log := r.log.WithFields(fields)
log.Info("trigger")

h := &helper{
log: log,
runner: r,
log: log,
}
if r.kf != nil {
h.builder = r.kf.NewBuilder().RequireNamespace()
Expand Down Expand Up @@ -89,6 +68,16 @@ func (r *Runner) Run(m map[string]events.Action) error {
return nil
}

func procAlias() string {
procPath, _ := os.Executable()
procName := filepath.Base(procPath)
calledAs := filepath.Base(os.Args[0])
if procName != calledAs {
return calledAs
}
return ""
}

func New(options ...Option) (*Runner, error) {
r := &Runner{}

Expand All @@ -100,6 +89,16 @@ func New(options ...Option) (*Runner, error) {
r.log = logger.New()
}

if r.exePath == "" {
path, err := os.Executable()
if err != nil {
return nil, err
}
r.exePath = path
}

r.alias = procAlias()

return r, nil
}

Expand All @@ -123,3 +122,11 @@ func WithKubeNamespace(namespace string) Option {
return nil
}
}

func WithExecutable(path string, args ...string) Option {
return func(r *Runner) error {
r.exePath = path
r.exeArgs = args
return nil
}
}

0 comments on commit 5d66f46

Please sign in to comment.