-
Notifications
You must be signed in to change notification settings - Fork 4
[wanda] refactor docker_cmd to be container_cmd #329
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
Draft
andrew-anyscale
wants to merge
2
commits into
main
Choose a base branch
from
andrew/revup/main/refactor-for-podman
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| package wanda | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "os/exec" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| // ContainerCmd is the interface for building container images across | ||
| // different container runtimes and builders. | ||
| type ContainerCmd interface { | ||
| // setWorkDir sets the working directory for commands. | ||
| setWorkDir(dir string) | ||
|
|
||
| // run executes a command with the given arguments. | ||
| run(args ...string) error | ||
|
|
||
| // pull pulls an image and optionally tags it. | ||
| pull(src, asTag string) error | ||
|
|
||
| // inspectImage returns information about an image, or nil if not found. | ||
| inspectImage(tag string) (*imageInfo, error) | ||
|
|
||
| // tag tags an image. | ||
| tag(src, asTag string) error | ||
|
|
||
| // build builds an image from the given input. | ||
| build(in *buildInput, core *buildInputCore, hints *buildInputHints) error | ||
| } | ||
|
|
||
| // imageInfo contains information about a container image. | ||
| type imageInfo struct { | ||
| ID string `json:"Id"` | ||
| RepoDigests []string | ||
| RepoTags []string | ||
| } | ||
|
|
||
| // baseContainerCmd provides common functionality for container commands. | ||
| type baseContainerCmd struct { | ||
| bin string | ||
| workDir string | ||
| envs []string | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) setWorkDir(dir string) { c.workDir = dir } | ||
|
|
||
| func (c *baseContainerCmd) cmd(args ...string) *exec.Cmd { | ||
| cmd := exec.Command(c.bin, args...) | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| cmd.Env = c.envs | ||
| if c.workDir != "" { | ||
| cmd.Dir = c.workDir | ||
| } | ||
| return cmd | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) run(args ...string) error { | ||
| return c.cmd(args...).Run() | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) pull(src, asTag string) error { | ||
| if err := c.run("pull", src); err != nil { | ||
| return fmt.Errorf("pull %s: %w", src, err) | ||
| } | ||
| if src != asTag { | ||
| if err := c.tag(src, asTag); err != nil { | ||
| return fmt.Errorf("tag %s %s: %w", src, asTag, err) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) inspectImage(tag string) (*imageInfo, error) { | ||
| cmd := c.cmd("image", "inspect", tag) | ||
| buf := new(bytes.Buffer) | ||
| cmd.Stdout = buf | ||
| if err := cmd.Run(); err != nil { | ||
| if exitErr, ok := err.(*exec.ExitError); ok { | ||
| // Docker returns 1 | ||
| code := exitErr.ExitCode() | ||
| if code == 1 { | ||
| return nil, nil | ||
| } | ||
| } | ||
| return nil, err | ||
| } | ||
| var info []*imageInfo | ||
| if err := json.Unmarshal(buf.Bytes(), &info); err != nil { | ||
| return nil, fmt.Errorf("unmarshal image info: %w", err) | ||
| } | ||
| if len(info) != 1 { | ||
| return nil, fmt.Errorf("%d image(s) found, expect 1", len(info)) | ||
| } | ||
| return info[0], nil | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) tag(src, asTag string) error { | ||
| return c.run("tag", src, asTag) | ||
| } | ||
|
|
||
| func (c *baseContainerCmd) build(in *buildInput, core *buildInputCore, hints *buildInputHints) error { | ||
| return c.doBuild(in, core, hints, nil) | ||
| } | ||
|
|
||
| // doBuild is the common build implementation that accepts extra flags. | ||
| func (c *baseContainerCmd) doBuild(in *buildInput, core *buildInputCore, hints *buildInputHints, extraFlags []string) error { | ||
| if hints == nil { | ||
| hints = newBuildInputHints(nil) | ||
| } | ||
|
|
||
| // Pull down the required images, and tag them properly. | ||
| var froms []string | ||
| for from := range core.Froms { | ||
| froms = append(froms, from) | ||
| } | ||
| sort.Strings(froms) | ||
|
|
||
| for _, from := range froms { | ||
| src, ok := in.froms[from] | ||
| if !ok { | ||
| return fmt.Errorf("missing base image source for %q", from) | ||
| } | ||
| if src.local != "" { // local image, already ready. | ||
| continue | ||
| } | ||
| if err := c.pull(src.src, src.name); err != nil { | ||
| return fmt.Errorf("pull %s(%s): %w", src.name, src.src, err) | ||
| } | ||
| } | ||
|
|
||
| // Build the image. | ||
| var args []string | ||
| args = append(args, "build") | ||
| args = append(args, extraFlags...) | ||
| args = append(args, "-f", core.Dockerfile) | ||
|
|
||
| for _, t := range in.tagList() { | ||
| args = append(args, "-t", t) | ||
| } | ||
|
|
||
| buildArgs := make(map[string]string) | ||
| for k, v := range hints.BuildArgs { | ||
| buildArgs[k] = v | ||
| } | ||
| // non-hint args can overwrite hint args | ||
| for k, v := range core.BuildArgs { | ||
| buildArgs[k] = v | ||
| } | ||
|
|
||
| var buildArgKeys []string | ||
| for k := range buildArgs { | ||
| buildArgKeys = append(buildArgKeys, k) | ||
| } | ||
| sort.Strings(buildArgKeys) | ||
| for _, k := range buildArgKeys { | ||
| v := buildArgs[k] | ||
| args = append(args, "--build-arg", fmt.Sprintf("%s=%s", k, v)) | ||
| } | ||
|
|
||
| // read context from stdin | ||
| args = append(args, "-") | ||
|
|
||
| log.Printf("%s %s", c.bin, strings.Join(args, " ")) | ||
|
|
||
| buildCmd := c.cmd(args...) | ||
| if in.context != nil { | ||
| buildCmd.Stdin = newWriterToReader(in.context) | ||
| } | ||
|
|
||
| return buildCmd.Run() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error handling logic is specific to the Docker CLI, which returns exit code 1 when an image is not found. Placing this in
baseContainerCmdleaks implementation details of one container runtime into the generic base class. Other runtimes like Podman might behave differently.To keep
baseContainerCmdgeneric, this Docker-specific logic should be moved todockerCmd. One way to do this is fordockerCmdto overrideinspectImage, run the command itself, and handle the exit code. While this might introduce some duplication, it would correctly encapsulate the runtime-specific behavior.