Skip to content

Commit

Permalink
Check the validity of the platforms
Browse files Browse the repository at this point in the history
platforms can still be added but some warning message
will be emitted if the platform cannot pass the validity
check.

Signed-off-by: Dave Chen <dave.chen@arm.com>
  • Loading branch information
chendave committed Mar 21, 2019
1 parent e5b647a commit ee6f12c
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 19 deletions.
4 changes: 3 additions & 1 deletion cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,9 @@ func newWorkerController(c *cli.Context, wiOpt workerInitializerOpt) (*worker.Co
return nil, err
}
for _, w := range ws {
logrus.Infof("found worker %q, labels=%v, platforms=%v", w.ID(), w.Labels(), formatPlatforms(w.Platforms()))
p := formatPlatforms(w.Platforms())
logrus.Infof("found worker %q, labels=%v, platforms=%v", w.ID(), w.Labels(), p)
binfmt_misc.WarnIfUnsupported(p)
if err = wc.Add(w); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions util/binfmt_misc/amd64_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func amd64Supported() bool {
return check(Binaryamd64) == nil
func amd64Supported() error {
return check(Binaryamd64)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/amd64_check_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func amd64Supported() bool {
return true
func amd64Supported() error {
return nil
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm64_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func arm64Supported() bool {
return check(Binaryarm64) == nil
func arm64Supported() error {
return check(Binaryarm64)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm64_check_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func arm64Supported() bool {
return true
func arm64Supported() error {
return nil
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func armSupported() bool {
return check(Binaryarm) == nil
func armSupported() error {
return check(Binaryarm)
}
4 changes: 2 additions & 2 deletions util/binfmt_misc/arm_check_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

package binfmt_misc

func armSupported() bool {
return true
func armSupported() error {
return nil
}
11 changes: 10 additions & 1 deletion util/binfmt_misc/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/exec"
"path/filepath"
"syscall"
)

func check(bin string) error {
Expand Down Expand Up @@ -35,5 +36,13 @@ func check(bin string) error {
}
f.Close()

return exec.Command(pp).Run()
err = exec.Command(pp).Run()
if err == nil {
cmd := exec.Command("/check")
cmd.SysProcAttr = &syscall.SysProcAttr{
Chroot: tmpdir,
}
err = cmd.Run()
}
return err
}
48 changes: 43 additions & 5 deletions util/binfmt_misc/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"

"github.com/containerd/containerd/platforms"
"github.com/sirupsen/logrus"
)

var once sync.Once
Expand All @@ -14,19 +15,56 @@ func SupportedPlatforms() []string {
once.Do(func() {
def := platforms.DefaultString()
arr = append(arr, def)

if p := "linux/amd64"; def != p && amd64Supported() {
if p := "linux/amd64"; def != p && (amd64Supported() == nil) {
arr = append(arr, p)
}
if p := "linux/arm64"; def != p && arm64Supported() {
if p := "linux/arm64"; def != p && (arm64Supported() == nil) {
arr = append(arr, p)
}
if !strings.HasPrefix(def, "linux/arm/") && armSupported() {
if !strings.HasPrefix(def, "linux/arm/") && (armSupported() == nil) {
arr = append(arr, "linux/arm/v7", "linux/arm/v6")
} else if def == "linux/arm/v7" {
arr = append(arr, "linux/arm/v6")
}
})

return arr
}

//Here, the func just validate the platforms and show warning message if there is,
//the end user could fix the issue based on those warning, and thus no need to drop
//the platfrom from the candidates.
func WarnIfUnsupported(pfs []string) {
def := platforms.DefaultString()
for _, p := range pfs {
if p != def {
if p == "linux/amd64" {
err := amd64Supported()
if err != nil {
printPlatfromWarning(p, err)
}
}
if p == "linux/arm64" {
err := arm64Supported()
if err != nil {
printPlatfromWarning(p, err)
}
}
if strings.HasPrefix(p, "linux/arm/v6") || strings.HasPrefix(p, "linux/arm/v7") {
err := armSupported()
if err != nil {
printPlatfromWarning(p, err)
}
}
}
}
}

func printPlatfromWarning(p string, err error) {
if strings.Contains(err.Error(), "exec format error") {
logrus.Warnf("platform %s cannot pass the validation, kernel support for miscellaneous binary may have not enabled.", p)
} else if strings.Contains(err.Error(), "no such file or directory") {
logrus.Warnf("platforms %s cannot pass the validation, '-F' flag might have not set for 'binfmt_misc'.", p)
} else {
logrus.Warnf("platforms %s cannot pass the validation: %s", p, err.Error())
}
}

0 comments on commit ee6f12c

Please sign in to comment.