-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
New command: build
to build images using minikube
#11164
Changes from 21 commits
af5828d
a274482
9ef5d25
6f26e4b
6cfbf2c
844c696
c94a6d7
f670368
22150fb
a8a8788
02b4267
40cbe65
9f9958f
db203cd
c961190
947e31a
e230f02
9516122
c715324
f7e0777
943561b
a5f835b
93c40d1
c9a0a7a
5d8d3d3
d508459
96e199c
4d0c0c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ import ( | |
"encoding/base64" | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"strings" | ||
|
@@ -277,6 +279,109 @@ func (r *Containerd) RemoveImage(name string) error { | |
return removeCRIImage(r.Runner, name) | ||
} | ||
|
||
func gitClone(cr CommandRunner, src string) (string, error) { | ||
// clone to a temporary directory | ||
rr, err := cr.RunCmd(exec.Command("mktemp", "-d")) | ||
if err != nil { | ||
return "", err | ||
} | ||
tmp := strings.TrimSpace(rr.Stdout.String()) | ||
cmd := exec.Command("git", "clone", src, tmp) | ||
if _, err := cr.RunCmd(cmd); err != nil { | ||
return "", err | ||
} | ||
return tmp, nil | ||
} | ||
|
||
func downloadRemote(cr CommandRunner, src string) (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add unit test for this ? or maybe break down the logic of detecting remote and add unit tests, so we know exactly what user would input There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, but then we need to start a web server or something to host the files to build (the ones in testdata/build) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the detection part of the (the scheme detection) could be put in a separate func with unit test ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We only implement it for buildkit, both docker and podman have their own (internal) implementation... |
||
u, err := url.Parse(src) | ||
if err != nil { | ||
return "", err | ||
} | ||
if u.Scheme == "" && u.Host == "" { // regular file, return | ||
return src, nil | ||
} | ||
if u.Scheme == "git" { | ||
return gitClone(cr, src) | ||
} | ||
|
||
// download to a temporary file | ||
rr, err := cr.RunCmd(exec.Command("mktemp")) | ||
if err != nil { | ||
return "", err | ||
} | ||
dst := strings.TrimSpace(rr.Stdout.String()) | ||
cmd := exec.Command("curl", "-L", "-o", dst, src) | ||
if _, err := cr.RunCmd(cmd); err != nil { | ||
return "", err | ||
} | ||
|
||
// extract to a temporary directory | ||
rr, err = cr.RunCmd(exec.Command("mktemp", "-d")) | ||
if err != nil { | ||
return "", err | ||
} | ||
tmp := strings.TrimSpace(rr.Stdout.String()) | ||
cmd = exec.Command("tar", "-C", tmp, "-xf", dst) | ||
if _, err := cr.RunCmd(cmd); err != nil { | ||
return "", err | ||
} | ||
|
||
return tmp, nil | ||
} | ||
|
||
// BuildImage builds an image into this runtime | ||
func (r *Containerd) BuildImage(src string, file string, tag string, push bool, env []string, opts []string) error { | ||
// download url if not already present | ||
dir, err := downloadRemote(r.Runner, src) | ||
if err != nil { | ||
return err | ||
} | ||
if file != "" { | ||
if dir != src { | ||
file = path.Join(dir, file) | ||
} | ||
// copy to standard path for Dockerfile | ||
df := path.Join(dir, "Dockerfile") | ||
if file != df { | ||
cmd := exec.Command("sudo", "cp", "-f", file, df) | ||
if _, err := r.Runner.RunCmd(cmd); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
klog.Infof("Building image: %s", dir) | ||
extra := "" | ||
if tag != "" { | ||
// add default tag if missing | ||
if !strings.Contains(tag, ":") { | ||
tag += ":latest" | ||
} | ||
extra = fmt.Sprintf(",name=%s", tag) | ||
if push { | ||
extra += ",push=true" | ||
} | ||
} | ||
args := []string{"buildctl", "build", | ||
"--frontend", "dockerfile.v0", | ||
"--local", fmt.Sprintf("context=%s", dir), | ||
"--local", fmt.Sprintf("dockerfile=%s", dir), | ||
"--output", fmt.Sprintf("type=image%s", extra)} | ||
for _, opt := range opts { | ||
args = append(args, "--"+opt) | ||
} | ||
c := exec.Command("sudo", args...) | ||
e := os.Environ() | ||
e = append(e, env...) | ||
c.Env = e | ||
c.Stdout = os.Stdout | ||
c.Stderr = os.Stderr | ||
if _, err := r.Runner.RunCmd(c); err != nil { | ||
return errors.Wrap(err, "buildctl build.") | ||
} | ||
return nil | ||
} | ||
|
||
// CGroupDriver returns cgroup driver ("cgroupfs" or "systemd") | ||
func (r *Containerd) CGroupDriver() (string, error) { | ||
info, err := getCRIInfo(r.Runner) | ||
|
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.
is there a PR upstream that we could comment here to remove the replace after the PR gets merged ?
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.
I had to backport a build fix, since the older Docker 19.03 doesn't build with the newer Windows versions.
See moby/moby@19.03...afbjorklund:v19.03.15-windows
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.
Alternatively one could upgrade everything to the Docker 20.10 API, but that is much more intrusive and big
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.
The patch was: moby/moby@c3a0a37