Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override container runtime for commands #181

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithEntrypointArgs(args...),
container.WithCleanup(a.cleanup),
)
Expand Down Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithCleanup(a.cleanup),
)
if err != nil {
Expand Down Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithEntrypointArgs(staticReportCmd...),
container.WithVolumes(volumes),
container.WithcFlag(true),
Expand Down Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithCleanup(a.cleanup),
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/openrewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithVolumes(volumes),
container.WithWorkDir("/tmp/source-app/input"),
container.WithCleanup(o.cleanup),
Expand Down
1 change: 1 addition & 0 deletions cmd/shimconvert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.WithContainerToolBin(Settings.PodmanBinary),
container.WithCleanup(w.cleanup),
)
if err != nil {
Expand Down
48 changes: 27 additions & 21 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -56,6 +56,12 @@ func WithEntrypointBin(b string) Option {
}
}

func WithContainerToolBin(r string) Option {
return func(c *container) {
c.containerToolBin = r
}
}

func WithEntrypointArgs(args ...string) Option {
return func(c *container) {
c.entrypointArgs = args
Expand Down Expand Up @@ -128,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,
Expand All @@ -148,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
Expand Down Expand Up @@ -192,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
Expand All @@ -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 tool", c.containerToolBin, "cmd", c.entrypointBin, "args", strings.Join(args, " "))
err = cmd.Run()
if err != nil {
c.log.Error(err, "container run error")
Expand All @@ -221,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()
}
Loading