archutil: refactor to return structs

Working with strings is error-prone because a platform
can be in multiple string forms and less flexible.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
master
Tonis Tiigi 2022-01-29 20:26:22 -08:00
parent 2633c96bac
commit e38f34c7d4
4 changed files with 77 additions and 94 deletions

View File

@ -407,10 +407,10 @@ func setDefaultConfig(cfg *config.Config) {
} }
if cfg.Workers.OCI.Platforms == nil { if cfg.Workers.OCI.Platforms == nil {
cfg.Workers.OCI.Platforms = archutil.SupportedPlatforms(false) cfg.Workers.OCI.Platforms = formatPlatforms(archutil.SupportedPlatforms(false))
} }
if cfg.Workers.Containerd.Platforms == nil { if cfg.Workers.Containerd.Platforms == nil {
cfg.Workers.Containerd.Platforms = archutil.SupportedPlatforms(false) cfg.Workers.Containerd.Platforms = formatPlatforms(archutil.SupportedPlatforms(false))
} }
cfg.Workers.OCI.NetworkConfig = setDefaultNetworkConfig(cfg.Workers.OCI.NetworkConfig) cfg.Workers.OCI.NetworkConfig = setDefaultNetworkConfig(cfg.Workers.OCI.NetworkConfig)
@ -700,8 +700,8 @@ func newWorkerController(c *cli.Context, wiOpt workerInitializerOpt) (*worker.Co
return nil, err return nil, err
} }
for _, w := range ws { for _, w := range ws {
p := formatPlatforms(w.Platforms(false)) p := w.Platforms(false)
logrus.Infof("found worker %q, labels=%v, platforms=%v", w.ID(), w.Labels(), p) logrus.Infof("found worker %q, labels=%v, platforms=%v", w.ID(), w.Labels(), formatPlatforms(p))
archutil.WarnIfUnsupported(p) archutil.WarnIfUnsupported(p)
if err = wc.Add(w); err != nil { if err = wc.Add(w); err != nil {
return nil, err return nil, err

View File

@ -91,13 +91,11 @@ func getEmulator(p *pb.Platform, idmap *idtools.IdentityMapping) (*emulator, err
Variant: p.Variant, Variant: p.Variant,
}) })
for _, ps := range all { for _, p := range all {
if p, err := platforms.Parse(ps); err == nil {
if platforms.Only(p).Match(pp) { if platforms.Only(p).Match(pp) {
return nil, nil return nil, nil
} }
} }
}
a, ok := qemuArchMap[pp.Architecture] a, ok := qemuArchMap[pp.Architecture]
if !ok { if !ok {

View File

@ -10,129 +10,105 @@ import (
) )
var mu sync.Mutex var mu sync.Mutex
var arr []string var arr []ocispecs.Platform
func SupportedPlatforms(noCache bool) []string { func SupportedPlatforms(noCache bool) []ocispecs.Platform {
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
if !noCache && arr != nil { if !noCache && arr != nil {
return arr return arr
} }
def := defaultPlatform() def := nativePlatform()
arr = append([]string{}, def) arr = append([]ocispecs.Platform{}, def)
if p := "linux/amd64"; def != p && amd64Supported() == nil {
arr = append(arr, p) if def.OS != "linux" {
}
if p := "linux/arm64"; def != p && arm64Supported() == nil {
arr = append(arr, p)
}
if p := "linux/riscv64"; def != p && riscv64Supported() == nil {
arr = append(arr, p)
}
if p := "linux/ppc64le"; def != p && ppc64leSupported() == nil {
arr = append(arr, p)
}
if p := "linux/s390x"; def != p && s390xSupported() == nil {
arr = append(arr, p)
}
if p := "linux/386"; def != p && i386Supported() == nil {
arr = append(arr, p)
}
if p := "linux/mips64le"; def != p && mips64leSupported() == nil {
arr = append(arr, p)
}
if p := "linux/mips64"; def != p && mips64Supported() == nil {
arr = append(arr, p)
}
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 return arr
} }
func Check(pp ocispecs.Platform) bool { if p := "amd64"; def.Architecture != p && amd64Supported() == nil {
p := platforms.Format(pp) arr = append(arr, linux(p))
if p == "linux/amd64" && amd64Supported() == nil {
return true
} }
if p == "linux/arm64" && arm64Supported() == nil { if p := "arm64"; def.Architecture != p && arm64Supported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/riscv64" && riscv64Supported() == nil { if p := "riscv64"; def.Architecture != p && riscv64Supported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/ppc64le" && ppc64leSupported() == nil { if p := "ppc64le"; def.Architecture != p && ppc64leSupported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/s390x" && s390xSupported() == nil { if p := "s390x"; def.Architecture != p && s390xSupported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/386" && i386Supported() == nil { if p := "386"; def.Architecture != p && i386Supported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/mips64le" && mips64leSupported() == nil { if p := "mips64le"; def.Architecture != p && mips64leSupported() == nil {
return true arr = append(arr, linux(p))
} }
if p == "linux/mips64" && mips64Supported() == nil { if p := "mips64"; def.Architecture != p && mips64Supported() == nil {
return true arr = append(arr, linux(p))
} }
if !strings.HasPrefix(p, "linux/arm/") && armSupported() == nil { if p := "arm"; def.Architecture != p && armSupported() == nil {
return true p := linux("arm")
p.Variant = "v6"
arr = append(arr, linux("arm"), p)
} else if def.Architecture == "arm" && def.Variant == "" {
p := linux("arm")
p.Variant = "v6"
arr = append(arr, p)
} }
return arr
return false
} }
//WarnIfUnsupported validates the platforms and show warning message if there is, //WarnIfUnsupported validates 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 end user could fix the issue based on those warning, and thus no need to drop
//the platform from the candidates. //the platform from the candidates.
func WarnIfUnsupported(pfs []string) { func WarnIfUnsupported(pfs []ocispecs.Platform) {
def := defaultPlatform() def := nativePlatform()
for _, p := range pfs { for _, p := range pfs {
if p != def { if p.Architecture != def.Architecture {
if p == "linux/amd64" { if p.Architecture == "amd64" {
if err := amd64Supported(); err != nil { if err := amd64Supported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/arm64" { if p.Architecture == "arm64" {
if err := arm64Supported(); err != nil { if err := arm64Supported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/riscv64" { if p.Architecture == "riscv64" {
if err := riscv64Supported(); err != nil { if err := riscv64Supported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/ppc64le" { if p.Architecture == "ppc64le" {
if err := ppc64leSupported(); err != nil { if err := ppc64leSupported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/s390x" { if p.Architecture == "s390x" {
if err := s390xSupported(); err != nil { if err := s390xSupported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/386" { if p.Architecture == "386" {
if err := i386Supported(); err != nil { if err := i386Supported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/mips64le" { if p.Architecture == "mips64le" {
if err := mips64leSupported(); err != nil { if err := mips64leSupported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if p == "linux/mips64" { if p.Architecture == "mips64" {
if err := mips64Supported(); err != nil { if err := mips64Supported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
} }
if strings.HasPrefix(p, "linux/arm/v6") || strings.HasPrefix(p, "linux/arm/v7") { if p.Architecture == "arm" {
if err := armSupported(); err != nil { if err := armSupported(); err != nil {
printPlatformWarning(p, err) printPlatformWarning(p, err)
} }
@ -141,16 +117,23 @@ func WarnIfUnsupported(pfs []string) {
} }
} }
func defaultPlatform() string { func nativePlatform() ocispecs.Platform {
return platforms.Format(platforms.Normalize(platforms.DefaultSpec())) return platforms.Normalize(platforms.DefaultSpec())
} }
func printPlatformWarning(p string, err error) { func linux(arch string) ocispecs.Platform {
return ocispecs.Platform{
OS: "linux",
Architecture: arch,
}
}
func printPlatformWarning(p ocispecs.Platform, err error) {
if strings.Contains(err.Error(), "exec format 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) logrus.Warnf("platform %s cannot pass the validation, kernel support for miscellaneous binary may have not enabled.", platforms.Format(p))
} else if strings.Contains(err.Error(), "no such file or directory") { } 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 'archutil'.", p) logrus.Warnf("platforms %s cannot pass the validation, '-F' flag might have not set for 'archutil'.", platforms.Format(p))
} else { } else {
logrus.Warnf("platforms %s cannot pass the validation: %s", p, err.Error()) logrus.Warnf("platforms %s cannot pass the validation: %s", platforms.Format(p), err.Error())
} }
} }

View File

@ -198,14 +198,16 @@ func (w *Worker) Labels() map[string]string {
func (w *Worker) Platforms(noCache bool) []ocispecs.Platform { func (w *Worker) Platforms(noCache bool) []ocispecs.Platform {
if noCache { if noCache {
pm := make(map[string]struct{}, len(w.WorkerOpt.Platforms))
for _, p := range w.WorkerOpt.Platforms {
pm[platforms.Format(p)] = struct{}{}
}
for _, p := range archutil.SupportedPlatforms(noCache) { for _, p := range archutil.SupportedPlatforms(noCache) {
if _, ok := pm[p]; !ok { exists := false
pp, _ := platforms.Parse(p) for _, pp := range w.WorkerOpt.Platforms {
w.WorkerOpt.Platforms = append(w.WorkerOpt.Platforms, pp) if platforms.Only(pp).Match(p) {
exists = true
break
}
}
if !exists {
w.WorkerOpt.Platforms = append(w.WorkerOpt.Platforms, p)
} }
} }
} }