Przeglądaj źródła

builder-next: support runtime platform detection

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
Tonis Tiigi 6 lat temu
rodzic
commit
0289aa436c

+ 21 - 0
builder/builder-next/controller.go

@@ -6,6 +6,7 @@ import (
 	"path/filepath"
 
 	"github.com/containerd/containerd/content/local"
+	"github.com/containerd/containerd/platforms"
 	"github.com/docker/docker/api/types"
 	"github.com/docker/docker/builder/builder-next/adapters/containerimage"
 	"github.com/docker/docker/builder/builder-next/adapters/localinlinecache"
@@ -28,7 +29,9 @@ import (
 	"github.com/moby/buildkit/frontend/gateway/forwarder"
 	"github.com/moby/buildkit/snapshot/blobmapping"
 	"github.com/moby/buildkit/solver/bboltcachestorage"
+	"github.com/moby/buildkit/util/binfmt_misc"
 	"github.com/moby/buildkit/worker"
+	specs "github.com/opencontainers/image-spec/specs-go/v1"
 	"github.com/pkg/errors"
 )
 
@@ -141,6 +144,11 @@ func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
 		return nil, errors.Errorf("snapshotter doesn't support differ")
 	}
 
+	p, err := parsePlatforms(binfmt_misc.SupportedPlatforms())
+	if err != nil {
+		return nil, err
+	}
+
 	wopt := mobyworker.Opt{
 		ID:                "moby",
 		MetadataStore:     md,
@@ -155,6 +163,7 @@ func newController(rt http.RoundTripper, opt Opt) (*control.Controller, error) {
 		Exporter:          exp,
 		Transport:         rt,
 		Layers:            layers,
+		Platforms:         p,
 	}
 
 	wc := &worker.Controller{}
@@ -223,3 +232,15 @@ func getGCPolicy(conf config.BuilderConfig, root string) ([]client.PruneInfo, er
 	}
 	return gcPolicy, nil
 }
+
+func parsePlatforms(platformsStr []string) ([]specs.Platform, error) {
+	out := make([]specs.Platform, 0, len(platformsStr))
+	for _, s := range platformsStr {
+		p, err := platforms.Parse(s)
+		if err != nil {
+			return nil, err
+		}
+		out = append(out, platforms.Normalize(p))
+	}
+	return out, nil
+}

+ 5 - 2
builder/builder-next/worker/worker.go

@@ -68,6 +68,7 @@ type Opt struct {
 	Transport         nethttp.RoundTripper
 	Exporter          exporter.Exporter
 	Layers            LayerAccess
+	Platforms         []ocispec.Platform
 }
 
 // Worker is a local worker instance with dedicated snapshotter, cache, and so on.
@@ -136,8 +137,10 @@ func (w *Worker) Labels() map[string]string {
 
 // Platforms returns one or more platforms supported by the image.
 func (w *Worker) Platforms() []ocispec.Platform {
-	// does not handle lcow
-	return []ocispec.Platform{platforms.DefaultSpec()}
+	if len(w.Opt.Platforms) == 0 {
+		return []ocispec.Platform{platforms.DefaultSpec()}
+	}
+	return w.Opt.Platforms
 }
 
 // GCPolicy returns automatic GC Policy

+ 8 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/amd64_binary.go

@@ -0,0 +1,8 @@
+// +build !amd64
+
+package binfmt_misc
+
+// This file is generated by running make inside the binfmt_misc package.
+// Do not edit manually.
+
+const Binaryamd64 = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xaa\x77\xf5\x71\x63\x62\x64\x64\x80\x01\x26\x06\x3b\x06\x30\x4f\xc0\x01\xcc\x77\x80\x8a\x1b\x08\xc0\x95\x30\x38\x30\x58\x30\x30\x33\x38\x30\xb0\x30\x30\x83\xd5\xb2\x30\x20\x03\x07\x14\x9a\x03\x6a\x34\x8c\x66\x80\x9a\x03\xe2\xb2\x22\xf1\x61\xf6\xc1\x68\x1e\xa8\x30\x8c\x86\xa9\x63\x81\xe2\x17\x50\xe1\x17\x50\x7b\x60\xb4\x02\x54\x1c\x46\x73\x30\x20\xf4\x09\x40\xed\x74\xf7\x0b\x05\xd9\x7f\x80\x05\xea\x8e\x51\x30\x0a\x46\xc1\x28\x18\x05\xa3\x60\x14\x8c\x82\x51\x30\x0a\x46\xc1\x28\x18\x05\xa3\x60\xb8\x03\x8f\xe3\x07\x6c\x40\x94\xe1\x7f\x7e\x56\x06\xbd\xe2\x8c\xe2\x92\xa2\x92\xc4\x24\x06\xbd\xbc\xfc\x92\x54\xbd\xf4\xbc\x52\xbd\x82\xa2\xfc\x82\xd4\xa2\x92\x4a\x06\xbd\x92\xd4\x8a\x12\x8a\xed\xe3\x66\x60\x60\x60\x07\x8f\x33\xa0\xf7\xdf\x51\xfb\xed\x0c\x68\xfd\x77\x18\x90\x83\xf6\xd9\xd9\x18\xd0\xc7\x0d\xd0\xc6\x0b\x18\x10\xe3\x0c\xe8\x7c\x66\x2c\xee\xe2\x81\xea\x57\x21\xa0\x1f\x10\x00\x00\xff\xff\x8a\x1b\xd7\x73\x30\x11\x00\x00"

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/amd64_check.go

@@ -0,0 +1,7 @@
+// +build !amd64
+
+package binfmt_misc
+
+func amd64Supported() bool {
+	return check(Binaryamd64) == nil
+}

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/amd64_check_amd64.go

@@ -0,0 +1,7 @@
+// +build amd64
+
+package binfmt_misc
+
+func amd64Supported() bool {
+	return true
+}

+ 8 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm64_binary.go

@@ -0,0 +1,8 @@
+// +build !arm64
+
+package binfmt_misc
+
+// This file is generated by running make inside the binfmt_misc package.
+// Do not edit manually.
+
+const Binaryarm64 = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\xaa\x77\xf5\x71\x63\x62\x64\x64\x80\x01\x26\x86\xed\x0c\x20\x5e\x05\x83\x03\x98\xef\x00\x15\x9f\xc1\x80\x00\x0e\x0c\x16\x0c\x8c\x0c\x0e\x0c\xcc\x0c\x4c\x60\xb5\xac\x0c\x0c\x28\xb2\xc8\x74\x0b\x94\xd7\x02\x97\x87\xd9\xd5\x70\x69\x05\x77\xc3\x25\x46\x06\x86\x2b\x0c\x7a\xc5\x19\xc5\x25\x45\x25\x89\x49\x0c\x7a\x25\xa9\x15\x25\x0c\x54\x00\xdc\x50\x9b\xd8\xa0\x7c\x98\x7f\x2a\xa0\x7c\x1e\x34\xf5\x2c\x68\x7c\x90\x5e\x66\x2c\xe6\xc2\xfc\x21\x88\x45\x3d\x32\x00\x04\x00\x00\xff\xff\xe7\x30\x54\x02\x58\x01\x00\x00"

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm64_check.go

@@ -0,0 +1,7 @@
+// +build !arm64
+
+package binfmt_misc
+
+func arm64Supported() bool {
+	return check(Binaryarm64) == nil
+}

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm64_check_arm64.go

@@ -0,0 +1,7 @@
+// +build arm64
+
+package binfmt_misc
+
+func arm64Supported() bool {
+	return true
+}

+ 8 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm_binary.go

@@ -0,0 +1,8 @@
+// +build !arm
+
+package binfmt_misc
+
+// This file is generated by running make inside the binfmt_misc package.
+// Do not edit manually.
+
+const Binaryarm = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff\x8c\x8e\x31\x0e\x82\x40\x14\x44\xdf\x17\x50\x13\x6d\xf4\x04\xda\x51\x6d\xc5\x05\x28\xb4\xd2\xc6\x70\x00\x97\x84\x44\x3a\x02\xdf\xc4\xce\x4b\x78\x00\xee\xc6\x01\xbc\x82\x01\x17\xdd\xc2\xc2\x49\x26\x93\x7d\x3b\xc9\x9f\xfb\xee\xb0\x17\x11\x46\x4d\x88\xe9\x5f\x19\x42\x02\x3c\xde\x30\x4a\xd8\x20\xc4\x84\x04\x7c\xdb\x32\xf8\x0c\x83\xa3\x0f\x6b\x3b\xa9\xda\x0e\x78\xa6\x2b\xc0\x16\x36\x2f\x91\x19\x30\x17\x4c\x73\x69\xb4\x56\x9b\x63\xb4\xb8\x29\x26\x3d\x1d\x8d\x55\xad\xcb\xfc\xaa\x45\xc3\xdf\x5a\xb8\x6b\x53\xb7\x37\x03\x96\xde\x7f\xe8\xb2\x9f\x10\x40\x35\xf2\x7e\xeb\xda\xeb\x89\x97\x81\xc7\x6b\x60\xfb\xa3\xf7\x0a\x00\x00\xff\xff\x73\x8f\xca\xf1\x34\x01\x00\x00"

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm_check.go

@@ -0,0 +1,7 @@
+// +build !arm
+
+package binfmt_misc
+
+func armSupported() bool {
+	return check(Binaryarm) == nil
+}

+ 7 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/arm_check_arm.go

@@ -0,0 +1,7 @@
+// +build arm
+
+package binfmt_misc
+
+func armSupported() bool {
+	return true
+}

+ 39 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/check.go

@@ -0,0 +1,39 @@
+package binfmt_misc
+
+import (
+	"bytes"
+	"compress/gzip"
+	"io"
+	"io/ioutil"
+	"os"
+	"os/exec"
+	"path/filepath"
+)
+
+func check(bin string) error {
+	tmpdir, err := ioutil.TempDir("", "qemu-check")
+	if err != nil {
+		return err
+	}
+	defer os.RemoveAll(tmpdir)
+	pp := filepath.Join(tmpdir, "check")
+
+	r, err := gzip.NewReader(bytes.NewReader([]byte(bin)))
+	if err != nil {
+		return err
+	}
+	defer r.Close()
+
+	f, err := os.OpenFile(pp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
+	if err != nil {
+		return err
+	}
+
+	if _, err := io.Copy(f, r); err != nil {
+		f.Close()
+		return err
+	}
+	f.Close()
+
+	return exec.Command(pp).Run()
+}

+ 32 - 0
vendor/github.com/moby/buildkit/util/binfmt_misc/detect.go

@@ -0,0 +1,32 @@
+package binfmt_misc
+
+import (
+	"strings"
+	"sync"
+
+	"github.com/containerd/containerd/platforms"
+)
+
+var once sync.Once
+var arr []string
+
+func SupportedPlatforms() []string {
+	once.Do(func() {
+		def := platforms.DefaultString()
+		arr = append(arr, def)
+
+		if p := "linux/amd64"; def != p && amd64Supported() {
+			arr = append(arr, p)
+		}
+		if p := "linux/arm64"; def != p && arm64Supported() {
+			arr = append(arr, p)
+		}
+		if !strings.HasPrefix(def, "linux/arm/") && armSupported() {
+			arr = append(arr, "linux/arm/v7", "linux/arm/v6")
+		} else if def == "linux/arm/v7" {
+			arr = append(arr, "linux/arm/v6")
+		}
+	})
+
+	return arr
+}