-
Notifications
You must be signed in to change notification settings - Fork 3.8k
platforms: define selectors for platforms #1403
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package platforms | ||
|
|
||
| import ( | ||
| "runtime" | ||
| "strings" | ||
| ) | ||
|
|
||
| // These function are generated from from https://golang.org/src/go/build/syslist.go. | ||
| // | ||
| // We use switch statements because they are slightly faster than map lookups | ||
| // and use a little less memory. | ||
|
|
||
| // isKnownOS returns true if we know about the operating system. | ||
| // | ||
| // The OS value should be normalized before calling this function. | ||
| func isKnownOS(os string) bool { | ||
| switch os { | ||
| case "android", "darwin", "dragonfly", "freebsd", "linux", "nacl", "netbsd", "openbsd", "plan9", "solaris", "windows", "zos": | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // isKnownArch returns true if we know about the architecture. | ||
| // | ||
| // The arch value should be normalized before being passed to this function. | ||
| func isKnownArch(arch string) bool { | ||
| switch arch { | ||
| case "386", "amd64", "amd64p32", "arm", "armbe", "arm64", "arm64be", "ppc64", "ppc64le", "mips", "mipsle", "mips64", "mips64le", "mips64p32", "mips64p32le", "ppc", "s390", "s390x", "sparc", "sparc64": | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func normalizeOS(os string) string { | ||
| if os == "" { | ||
| return runtime.GOOS | ||
| } | ||
| os = strings.ToLower(os) | ||
|
|
||
| switch os { | ||
| case "macos": | ||
| os = "darwin" | ||
| } | ||
| return os | ||
| } | ||
|
|
||
| // normalizeArch normalizes the architecture. | ||
| func normalizeArch(arch, variant string) (string, string) { | ||
| arch, variant = strings.ToLower(arch), strings.ToLower(variant) | ||
| switch arch { | ||
| case "i386": | ||
| arch = "386" | ||
| variant = "" | ||
| case "x86_64", "x86-64": | ||
| arch = "amd64" | ||
| variant = "" | ||
| case "aarch64": | ||
| arch = "arm64" | ||
| variant = "" // v8 is implied | ||
|
||
| case "armhf": | ||
| arch = "arm" | ||
| variant = "" | ||
|
||
| case "armel": | ||
| arch = "arm" | ||
| variant = "v6" | ||
| case "arm": | ||
| switch variant { | ||
| case "v7", "7": | ||
| variant = "v7" | ||
|
||
| case "5", "6", "8": | ||
| variant = "v" + variant | ||
| } | ||
| } | ||
|
|
||
| return arch, variant | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package platforms | ||
|
|
||
| import ( | ||
| "runtime" | ||
|
|
||
| specs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| // Default returns the current platform's default platform specification. | ||
| func Default() specs.Platform { | ||
| return specs.Platform{ | ||
| OS: runtime.GOOS, | ||
| Architecture: runtime.GOARCH, | ||
| // TODO(stevvooe): Need to resolve GOARM for arm hosts. | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package platforms | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "runtime" | ||
| "testing" | ||
|
|
||
| specs "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| func TestDefault(t *testing.T) { | ||
| expected := specs.Platform{ | ||
| OS: runtime.GOOS, | ||
| Architecture: runtime.GOARCH, | ||
| } | ||
| p := Default() | ||
| if !reflect.DeepEqual(p, expected) { | ||
| t.Fatalf("default platform not as expected: %#v != %#v", p, expected) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure one wants to do this matching above of commonly used but not part of the OCI spec names for architectures.
I would suggest to simply just use the names from the spec and that's it. Otherwise you'd have to do the same of OSes too.
darwinis now officially calledmacOS, for example. If you translatex86-64toamd64why not translatemacOStodarwin?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can add a translation for
macOS.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer a strict approach, but I think we can make everyone happy here because the sets (os/arch) are disjoint. To keep this nice property across projects, the normalization needs to be centralized.
Do you see an issue with this in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to say that at time I find the mismatch between
uname -mand Go architectures (and therefor OCI architectures) quite annoying, so I can see an argument for tools to accept both (and then internally translate to the canonical GOARCH/GOOS format). But I can also see an argument for keeping it simple and just use OCI names and leave it to the user to translate.For now I don't see any issues in the future (I left my crystal ball at home) so if the general desire is to allow commonly used alternative arch/OS names to be used, centralising it in one place like this PR tries certainly a good idea.