examples: add back older versions of buildkit

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
docker-18.09
Tonis Tiigi 2017-07-10 10:47:52 -07:00
parent bf4e8c1610
commit 0a68de4512
5 changed files with 209 additions and 34 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)
}