From 38c841993922df61338ee0dd518c06ce2fd8abdd Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Tue, 19 Mar 2024 15:10:57 -0400 Subject: [PATCH 1/2] override container runtime for commands Signed-off-by: Emily McMullan --- cmd/analyze.go | 4 ++++ cmd/openrewrite.go | 1 + cmd/shimconvert.go | 1 + pkg/container/container.go | 10 ++++++++-- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index f3c657c..fefb40f 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -309,6 +309,7 @@ func (a *analyzeCommand) ListLabels(ctx context.Context) error { container.WithEnv(runMode, runModeContainer), container.WithVolumes(volumes), container.WithEntrypointBin(fmt.Sprintf("/usr/local/bin/%s", Settings.RootCommandName)), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithEntrypointArgs(args...), container.WithCleanup(a.cleanup), ) @@ -753,6 +754,7 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e container.WithStderr(analysisLog), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/bin/entrypoint.sh"), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithCleanup(a.cleanup), ) if err != nil { @@ -851,6 +853,7 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error { container.WithImage(Settings.RunnerImage), container.WithLog(a.log.V(1)), container.WithEntrypointBin("/bin/sh"), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithEntrypointArgs(staticReportCmd...), container.WithVolumes(volumes), container.WithcFlag(true), @@ -1027,6 +1030,7 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) { container.WithVolumes(volumes), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/local/bin/windup-shim"), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithCleanup(a.cleanup), ) if err != nil { diff --git a/cmd/openrewrite.go b/cmd/openrewrite.go index f0445a9..6f40dae 100644 --- a/cmd/openrewrite.go +++ b/cmd/openrewrite.go @@ -158,6 +158,7 @@ func (o *openRewriteCommand) Run(ctx context.Context) error { container.WithLog(o.log.V(1)), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/bin/openrewrite_entrypoint.sh"), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithVolumes(volumes), container.WithWorkDir("/tmp/source-app/input"), container.WithCleanup(o.cleanup), diff --git a/cmd/shimconvert.go b/cmd/shimconvert.go index 9f10cd8..1ea99a6 100644 --- a/cmd/shimconvert.go +++ b/cmd/shimconvert.go @@ -172,6 +172,7 @@ func (w *windupShimCommand) Run(ctx context.Context) error { container.WithStderr(shimLog), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/local/bin/windup-shim"), + container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), container.WithCleanup(w.cleanup), ) if err != nil { diff --git a/pkg/container/container.go b/pkg/container/container.go index 195cc3a..b21e3d3 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -56,6 +56,12 @@ func WithEntrypointBin(b string) Option { } } +func WithContainerRuntimeBin(r string) Option { + return func(c *container) { + c.containerRuntimeBin = r + } +} + func WithEntrypointArgs(args ...string) Option { return func(c *container) { c.entrypointArgs = args @@ -205,8 +211,8 @@ func (c *container) Run(ctx context.Context, opts ...Option) error { cmd.Stderr = io.MultiWriter( append(c.stderr, errBytes)...) } - c.log.Info("executing podman command", - "podman", c.containerRuntimeBin, "cmd", c.entrypointBin, "args", strings.Join(args, " ")) + c.log.Info("executing command", + "container runtime", c.containerRuntimeBin, "cmd", c.entrypointBin, "args", strings.Join(args, " ")) err = cmd.Run() if err != nil { c.log.Error(err, "container run error") From aad17284110d3cbc995fecb8e4c8c907b480d272 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Wed, 20 Mar 2024 10:35:36 -0400 Subject: [PATCH 2/2] rename container tool Signed-off-by: Emily McMullan --- cmd/analyze.go | 8 +++---- cmd/openrewrite.go | 2 +- cmd/shimconvert.go | 2 +- pkg/container/container.go | 44 +++++++++++++++++++------------------- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index fefb40f..fdeff2e 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -309,7 +309,7 @@ func (a *analyzeCommand) ListLabels(ctx context.Context) error { container.WithEnv(runMode, runModeContainer), container.WithVolumes(volumes), container.WithEntrypointBin(fmt.Sprintf("/usr/local/bin/%s", Settings.RootCommandName)), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithEntrypointArgs(args...), container.WithCleanup(a.cleanup), ) @@ -754,7 +754,7 @@ func (a *analyzeCommand) RunAnalysis(ctx context.Context, xmlOutputDir string) e container.WithStderr(analysisLog), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/bin/entrypoint.sh"), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithCleanup(a.cleanup), ) if err != nil { @@ -853,7 +853,7 @@ func (a *analyzeCommand) GenerateStaticReport(ctx context.Context) error { container.WithImage(Settings.RunnerImage), container.WithLog(a.log.V(1)), container.WithEntrypointBin("/bin/sh"), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithEntrypointArgs(staticReportCmd...), container.WithVolumes(volumes), container.WithcFlag(true), @@ -1030,7 +1030,7 @@ func (a *analyzeCommand) ConvertXML(ctx context.Context) (string, error) { container.WithVolumes(volumes), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/local/bin/windup-shim"), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithCleanup(a.cleanup), ) if err != nil { diff --git a/cmd/openrewrite.go b/cmd/openrewrite.go index 6f40dae..65c8e14 100644 --- a/cmd/openrewrite.go +++ b/cmd/openrewrite.go @@ -158,7 +158,7 @@ func (o *openRewriteCommand) Run(ctx context.Context) error { container.WithLog(o.log.V(1)), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/bin/openrewrite_entrypoint.sh"), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithVolumes(volumes), container.WithWorkDir("/tmp/source-app/input"), container.WithCleanup(o.cleanup), diff --git a/cmd/shimconvert.go b/cmd/shimconvert.go index 1ea99a6..692cc6d 100644 --- a/cmd/shimconvert.go +++ b/cmd/shimconvert.go @@ -172,7 +172,7 @@ func (w *windupShimCommand) Run(ctx context.Context) error { container.WithStderr(shimLog), container.WithEntrypointArgs(args...), container.WithEntrypointBin("/usr/local/bin/windup-shim"), - container.WithContainerRuntimeBin(os.Getenv("PODMAN_BIN")), + container.WithContainerToolBin(Settings.PodmanBinary), container.WithCleanup(w.cleanup), ) if err != nil { diff --git a/pkg/container/container.go b/pkg/container/container.go index b21e3d3..127f45c 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -29,11 +29,11 @@ type container struct { // whether to delete container after run() cleanup bool // map of source -> dest paths to mount - volumes map[string]string - cFlag bool - log logr.Logger - containerRuntimeBin string - reproducerCmd *string + volumes map[string]string + cFlag bool + log logr.Logger + containerToolBin string + reproducerCmd *string } type Option func(c *container) @@ -56,9 +56,9 @@ func WithEntrypointBin(b string) Option { } } -func WithContainerRuntimeBin(r string) Option { +func WithContainerToolBin(r string) Option { return func(c *container) { - c.containerRuntimeBin = r + c.containerToolBin = r } } @@ -134,14 +134,14 @@ func randomName() string { func NewContainer() *container { return &container{ - image: "", - containerRuntimeBin: "podman", - entrypointArgs: []string{}, - volumes: make(map[string]string), - stdout: []io.Writer{os.Stdout}, - env: map[string]string{}, - stderr: []io.Writer{os.Stderr}, - name: randomName(), + image: "", + containerToolBin: "podman", + entrypointArgs: []string{}, + volumes: make(map[string]string), + stdout: []io.Writer{os.Stdout}, + env: map[string]string{}, + stderr: []io.Writer{os.Stderr}, + name: randomName(), // by default, remove the container after run() cleanup: true, cFlag: false, @@ -154,8 +154,8 @@ func (c *container) Run(ctx context.Context, opts ...Option) error { for _, opt := range opts { opt(c) } - if c.image == "" || c.containerRuntimeBin == "" { - return fmt.Errorf("image and containerRuntimeBin must be set") + if c.image == "" || c.containerToolBin == "" { + return fmt.Errorf("image and containerToolBin must be set") } args := []string{"run"} os := runtime.GOOS @@ -198,9 +198,9 @@ func (c *container) Run(ctx context.Context, opts ...Option) error { if c.reproducerCmd != nil { reproducer := strings.ReplaceAll(strings.Join(args, " "), " --rm", "") *c.reproducerCmd = fmt.Sprintf("%s %s", - c.containerRuntimeBin, reproducer) + c.containerToolBin, reproducer) } - cmd := exec.CommandContext(ctx, c.containerRuntimeBin, args...) + cmd := exec.CommandContext(ctx, c.containerToolBin, args...) errBytes := &bytes.Buffer{} cmd.Stdout = nil cmd.Stderr = errBytes @@ -212,7 +212,7 @@ func (c *container) Run(ctx context.Context, opts ...Option) error { append(c.stderr, errBytes)...) } c.log.Info("executing command", - "container runtime", c.containerRuntimeBin, "cmd", c.entrypointBin, "args", strings.Join(args, " ")) + "container tool", c.containerToolBin, "cmd", c.entrypointBin, "args", strings.Join(args, " ")) err = cmd.Run() if err != nil { c.log.Error(err, "container run error") @@ -227,9 +227,9 @@ func (c *container) Run(ctx context.Context, opts ...Option) error { func (c *container) Rm(ctx context.Context) error { cmd := exec.CommandContext( ctx, - c.containerRuntimeBin, + c.containerToolBin, "rm", c.name) c.log.Info("removing container", - "podman", c.containerRuntimeBin, "name", c.name) + "container tool", c.containerToolBin, "name", c.name) return cmd.Run() }