From 0a68de451258f0258f8a485dfbb6c91b943af9da Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 10 Jul 2017 10:47:52 -0700 Subject: [PATCH] examples: add back older versions of buildkit Signed-off-by: Tonis Tiigi --- README.md | 14 ++- examples/buildkit0/buildkit.go | 91 ++++++++++++++++ examples/buildkit1/buildkit.go | 108 +++++++++++++++++++ examples/{buildkit => buildkit2}/buildkit.go | 0 examples/llbout/example.go | 30 ------ 5 files changed, 209 insertions(+), 34 deletions(-) create mode 100644 examples/buildkit0/buildkit.go create mode 100644 examples/buildkit1/buildkit.go rename examples/{buildkit => buildkit2}/buildkit.go (100%) delete mode 100644 examples/llbout/example.go diff --git a/README.md b/README.md index d539e4af..3ee4a53c 100644 --- a/README.md +++ b/README.md @@ -36,24 +36,30 @@ go build -o buildctl ./cmd/buildctl You can also use `make binaries` that prepares all binaries into the `bin/` directory. -The first thing to test could be to try building BuildKit with BuildKit. BuildKit provides a low-level solver format that could be used by multiple build definitions. Preparation work for making the Dockerfile parser reusable as a frontend is tracked in https://github.com/moby/moby/pull/33492. As no frontends have been integrated yet we currently have to use a script to generate this low-level definition. +The first thing to test could be to try building BuildKit with BuildKit. BuildKit provides a low-level solver format that could be used by multiple build definitions. Preparation work for making the Dockerfile parser reusable as a frontend is tracked in https://github.com/moby/moby/pull/33492. As no frontends have been integrated yet we currently have to use a client library to generate this low-level definition. -`examples/buildkit/buildkit.go` is a script that defines how to build different configurations of BuildKit and its dependencies using the `client` package. Running this script generates a protobuf definition of a build graph. Note that the script itself does not execute any steps of the build. +`examples/buildkit*` directory contains scripts that define how to build different configurations of BuildKit and its dependencies using the `client` package. Running one of these script generates a protobuf definition of a build graph. Note that the script itself does not execute any steps of the build. You can use `buildctl debug dump-llb` to see what data is this definition. ```bash -go run examples/buildkit/buildkit.go | buildctl debug dump-llb | jq . +go run examples/buildkit0/buildkit.go | buildctl debug dump-llb | jq . ``` To start building use `buildctl build` command. The script accepts `--target` flag to choose between `containerd` and `standalone` configurations. In standalone mode BuildKit binaries are built together with `runc`. In containerd mode, the `containerd` binary is built as well from the upstream repo. ```bash -go run examples/buildkit/buildkit.go | buildctl build +go run examples/buildkit0/buildkit.go | buildctl build ``` `buildctl build` will show interactive progress bar by default while the build job is running. It will also show you the path to the trace file that contains all information about the timing of the individual steps and logs. +Different versions of the example scripts show different ways of describing the build definition for this project to show the capabilities of the library. New versions have been added when new features have become available. + +- `./examples/buildkit0` - uses only exec operations, defines a full stage per component. +- `./examples/buildkit1` - cloning git repositories has been separated for extra concurrency. +- `./examples/buildkit2` - uses git sources directly instead of running `git clone`, allowing better performance and much safer caching. + #### Supported runc version During development buildkit is tested with the version of runc that is being used by the containerd repository. Please refer to [runc.md](https://github.com/containerd/containerd/blob/8bd8030edec48308a8a2e9b71598a3c4c898784f/RUNC.md) for more information. diff --git a/examples/buildkit0/buildkit.go b/examples/buildkit0/buildkit.go new file mode 100644 index 00000000..21100636 --- /dev/null +++ b/examples/buildkit0/buildkit.go @@ -0,0 +1,91 @@ +package main + +import ( + "flag" + "os" + + "github.com/moby/buildkit/client/llb" + "github.com/moby/buildkit/util/system" +) + +type buildOpt struct { + target string + containerd string + runc string +} + +func main() { + var opt buildOpt + flag.StringVar(&opt.target, "target", "containerd", "target (standalone, containerd)") + flag.StringVar(&opt.containerd, "containerd", "master", "containerd version") + flag.StringVar(&opt.runc, "runc", "v1.0.0-rc3", "runc version") + flag.Parse() + + bk := buildkit(opt) + out := bk.Run(llb.Shlex("ls -l /bin")) // debug output + + dt, err := out.Marshal() + if err != nil { + panic(err) + } + llb.WriteTo(dt, os.Stdout) +} + +func goBuildBase() *llb.State { + goAlpine := llb.Image("docker.io/library/golang:1.8-alpine") + return goAlpine. + AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv). + AddEnv("GOPATH", "/go"). + Run(llb.Shlex("apk add --no-cache g++ linux-headers")). + Run(llb.Shlex("apk add --no-cache git make")).Root() +} + +func runc(version string) *llb.State { + return goBuildBase(). + Run(llb.Shlex("git clone https://github.com/opencontainers/runc.git /go/src/github.com/opencontainers/runc")). + Dir("/go/src/github.com/opencontainers/runc"). + Run(llb.Shlexf("git checkout -q %s", version)). + Run(llb.Shlex("go build -o /usr/bin/runc ./")).Root() +} + +func containerd(version string) *llb.State { + return goBuildBase(). + Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")). + Run(llb.Shlex("git clone https://github.com/containerd/containerd.git /go/src/github.com/containerd/containerd")). + Dir("/go/src/github.com/containerd/containerd"). + Run(llb.Shlexf("git checkout -q %s", version)). + Run(llb.Shlex("make bin/containerd")).Root() +} + +func buildkit(opt buildOpt) *llb.State { + src := goBuildBase(). + Run(llb.Shlex("git clone https://github.com/moby/buildkit.git /go/src/github.com/moby/buildkit")). + Dir("/go/src/github.com/moby/buildkit") + + builddStandalone := src. + Run(llb.Shlex("go build -o /bin/buildd-standalone -tags standalone ./cmd/buildd")) + + builddContainerd := src. + Run(llb.Shlex("go build -o /bin/buildd-containerd -tags containerd ./cmd/buildd")) + + buildctl := src. + Run(llb.Shlex("go build -o /bin/buildctl ./cmd/buildctl")) + + r := llb.Image("docker.io/library/alpine:latest") + r = copy(buildctl.Root(), "/bin/buildctl", r, "/bin/") + r = copy(runc(opt.runc), "/usr/bin/runc", r, "/bin/") + if opt.target == "containerd" { + r = copy(containerd(opt.containerd), "/go/src/github.com/containerd/containerd/bin/containerd", r, "/bin/") + r = copy(builddContainerd.Root(), "/bin/buildd-containerd", r, "/bin/") + } else { + r = copy(builddStandalone.Root(), "/bin/buildd-standalone", r, "/bin/") + } + return r +} + +func copy(src *llb.State, srcPath string, dest *llb.State, destPath string) *llb.State { + cpImage := llb.Image("docker.io/library/alpine:latest") + cp := cpImage.Run(llb.Shlexf("cp -a /src%s /dest%s", srcPath, destPath)) + cp.AddMount("/src", src) + return cp.AddMount("/dest", dest) +} diff --git a/examples/buildkit1/buildkit.go b/examples/buildkit1/buildkit.go new file mode 100644 index 00000000..69d15adb --- /dev/null +++ b/examples/buildkit1/buildkit.go @@ -0,0 +1,108 @@ +package main + +import ( + "flag" + "os" + + "github.com/moby/buildkit/client/llb" + "github.com/moby/buildkit/util/system" +) + +type buildOpt struct { + target string + containerd string + runc string +} + +func main() { + var opt buildOpt + flag.StringVar(&opt.target, "target", "containerd", "target (standalone, containerd)") + flag.StringVar(&opt.containerd, "containerd", "master", "containerd version") + flag.StringVar(&opt.runc, "runc", "v1.0.0-rc3", "runc version") + flag.Parse() + + bk := buildkit(opt) + out := bk.Run(llb.Shlex("ls -l /bin")) // debug output + + dt, err := out.Marshal() + if err != nil { + panic(err) + } + llb.WriteTo(dt, os.Stdout) +} + +func goBuildBase() *llb.State { + goAlpine := llb.Image("docker.io/library/golang:1.8-alpine") + return goAlpine. + AddEnv("PATH", "/usr/local/go/bin:"+system.DefaultPathEnv). + AddEnv("GOPATH", "/go"). + Run(llb.Shlex("apk add --no-cache g++ linux-headers")). + Run(llb.Shlex("apk add --no-cache git make")).Root() +} + +func runc(version string) *llb.State { + return goBuildBase(). + With(goFromGit("github.com/opencontainers/runc", version)). + Run(llb.Shlex("go build -o /usr/bin/runc ./")). + Root() +} + +func containerd(version string) *llb.State { + return goBuildBase(). + Run(llb.Shlex("apk add --no-cache btrfs-progs-dev")). + With(goFromGit("github.com/containerd/containerd", version)). + Run(llb.Shlex("make bin/containerd")).Root() +} + +func buildkit(opt buildOpt) *llb.State { + src := goBuildBase().With(goFromGit("github.com/moby/buildkit", "master")) + + builddStandalone := src. + Run(llb.Shlex("go build -o /bin/buildd-standalone -tags standalone ./cmd/buildd")).Root() + + builddContainerd := src. + Run(llb.Shlex("go build -o /bin/buildd-containerd -tags containerd ./cmd/buildd")).Root() + + buildctl := src. + Run(llb.Shlex("go build -o /bin/buildctl ./cmd/buildctl")).Root() + + r := llb.Image("docker.io/library/alpine:latest").With( + copyFrom(buildctl, "/bin/buildctl", "/bin/"), + copyFrom(runc(opt.runc), "/usr/bin/runc", "/bin/"), + ) + + if opt.target == "containerd" { + return r.With( + copyFrom(containerd(opt.containerd), "/go/src/github.com/containerd/containerd/bin/containerd", "/bin/"), + copyFrom(builddContainerd, "/bin/buildd-containerd", "/bin/")) + } + return r.With(copyFrom(builddStandalone, "/bin/buildd-standalone", "/bin/")) +} + +// goFromGit is a helper for cloning a git repo, checking out a tag and copying +// source directory into +func goFromGit(repo, tag string) llb.StateOption { + src := llb.Image("docker.io/library/alpine:latest"). + Run(llb.Shlex("apk add --no-cache git")). + Run(llb.Shlexf("git clone https://%[1]s.git /go/src/%[1]s", repo)). + Dirf("/go/src/%s", repo). + Run(llb.Shlexf("git checkout -q %s", tag)).Root() + return func(s *llb.State) *llb.State { + return s.With(copyFrom(src, "/go", "/")).Reset(s).Dir(src.GetDir()) + } +} + +// copyFrom has similar semantics as `COPY --from` +func copyFrom(src *llb.State, srcPath, destPath string) llb.StateOption { + return func(s *llb.State) *llb.State { + return copy(src, srcPath, s, destPath) + } +} + +// copy copies files between 2 states using cp until there is no copyOp +func copy(src *llb.State, srcPath string, dest *llb.State, destPath string) *llb.State { + cpImage := llb.Image("docker.io/library/alpine:latest") + cp := cpImage.Run(llb.Shlexf("cp -a /src%s /dest%s", srcPath, destPath)) + cp.AddMount("/src", src) + return cp.AddMount("/dest", dest) +} diff --git a/examples/buildkit/buildkit.go b/examples/buildkit2/buildkit.go similarity index 100% rename from examples/buildkit/buildkit.go rename to examples/buildkit2/buildkit.go diff --git a/examples/llbout/example.go b/examples/llbout/example.go deleted file mode 100644 index 0e13f809..00000000 --- a/examples/llbout/example.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -import ( - "log" - "os" - - "github.com/moby/buildkit/client/llb" -) - -func main() { - busybox := llb.Image("docker.io/library/busybox:latest") - img1 := busybox. - Run(llb.Shlex("sleep 1")). - Run(llb.Shlex("sh -c \"echo foo > /bar\"")) - - alpine := llb.Image("docker.io/library/alpine:latest") - - copy := img1.Run(llb.Shlex("cp -a /alpine/etc/passwd /baz")) - copy.AddMount("/alpine", alpine) - copy.AddMount("/subroot", busybox) - - res := copy.Run(llb.Shlex("ls -l /")) - - dt, err := res.Marshal() - if err != nil { - log.Printf("%+v\n", err) - panic(err) - } - llb.WriteTo(dt, os.Stdout) -}