-
Notifications
You must be signed in to change notification settings - Fork 395
Adds the possibility to choose the image by the variant field in a manifest #786
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
Closed
JirkaChadima
wants to merge
7
commits into
containers:master
from
JirkaChadima:feat-override-variant
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ff9be61
Adds the possibility to choose the image by the variant field in a ma…
JirkaChadima 89ddfce
Introduces ARM Variant detection from /proc/cpuinfo
JirkaChadima 78151d3
Allows matching of compatible OS Variants when selecting the appropri…
JirkaChadima 6a3e032
Makes platform_matcher internal
JirkaChadima 345c9c5
Improves generality of platform matcher, adds a few tests
JirkaChadima 6389bbc
Makes copy command ready to check Variant eventually
JirkaChadima fc304bb
Improves error messages when matching image to a platform
JirkaChadima File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| package platform | ||
|
|
||
| // Largely based on | ||
| // https://github.com/moby/moby/blob/bc846d2e8fe5538220e0c31e9d0e8446f6fbc022/distribution/cpuinfo_unix.go | ||
| // https://github.com/containerd/containerd/blob/726dcaea50883e51b2ec6db13caff0e7936b711d/platforms/cpuinfo.go | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
|
|
||
| "github.com/containers/image/v5/types" | ||
| imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| // For Linux, the kernel has already detected the ABI, ISA and Features. | ||
| // So we don't need to access the ARM registers to detect platform information | ||
| // by ourselves. We can just parse these information from /proc/cpuinfo | ||
| func getCPUInfo(pattern string) (info string, err error) { | ||
| if runtime.GOOS != "linux" { | ||
| return "", fmt.Errorf("getCPUInfo for OS %s not implemented", runtime.GOOS) | ||
| } | ||
|
|
||
| cpuinfo, err := os.Open("/proc/cpuinfo") | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer cpuinfo.Close() | ||
|
|
||
| // Start to Parse the Cpuinfo line by line. For SMP SoC, we parse | ||
| // the first core is enough. | ||
| scanner := bufio.NewScanner(cpuinfo) | ||
| for scanner.Scan() { | ||
| newline := scanner.Text() | ||
| list := strings.Split(newline, ":") | ||
|
|
||
| if len(list) > 1 && strings.EqualFold(strings.TrimSpace(list[0]), pattern) { | ||
| return strings.TrimSpace(list[1]), nil | ||
| } | ||
| } | ||
|
|
||
| // Check whether the scanner encountered errors | ||
| err = scanner.Err() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return "", fmt.Errorf("getCPUInfo for pattern: %s not found", pattern) | ||
| } | ||
|
|
||
| func getCPUVariantWindows() string { | ||
| // Windows only supports v7 for ARM32 and v8 for ARM64 and so we can use | ||
| // runtime.GOARCH to determine the variants | ||
| var variant string | ||
| switch runtime.GOARCH { | ||
| case "arm64": | ||
| variant = "v8" | ||
| case "arm": | ||
| variant = "v7" | ||
| default: | ||
| variant = "" | ||
| } | ||
|
|
||
| return variant | ||
| } | ||
|
|
||
| func getCPUVariantArm() string { | ||
| variant, err := getCPUInfo("Cpu architecture") | ||
| if err != nil { | ||
| return "" | ||
| } | ||
| // TODO handle RPi Zero mismatch (https://github.com/moby/moby/pull/36121#issuecomment-398328286) | ||
|
|
||
| switch strings.ToLower(variant) { | ||
| case "8", "aarch64": | ||
| variant = "v8" | ||
| case "7", "7m", "?(12)", "?(13)", "?(14)", "?(15)", "?(16)", "?(17)": | ||
| variant = "v7" | ||
| case "6", "6tej": | ||
| variant = "v6" | ||
| case "5", "5t", "5te", "5tej": | ||
| variant = "v5" | ||
| case "4", "4t": | ||
| variant = "v4" | ||
| case "3": | ||
| variant = "v3" | ||
| default: | ||
| variant = "" | ||
| } | ||
|
|
||
| return variant | ||
| } | ||
|
|
||
| func getCPUVariant(os string, arch string) string { | ||
| if os == "windows" { | ||
| return getCPUVariantWindows() | ||
| } | ||
| if arch == "arm" || arch == "arm64" { | ||
| return getCPUVariantArm() | ||
| } | ||
| return "" | ||
| } | ||
|
|
||
| var compatibility = map[string][]string{ | ||
| "arm": {"v7", "v6", "v5"}, | ||
| "arm64": {"v8"}, | ||
| } | ||
|
|
||
| // Returns all compatible platforms with the platform specifics possibly overriden by user, | ||
| // the most compatible platform is first. | ||
| // If some option (arch, os, variant) is not present, a value from current platform is detected. | ||
| func WantedPlatforms(ctx *types.SystemContext) ([]imgspecv1.Platform, error) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking: This can currently never fail, and it’s an internal API. The |
||
| wantedArch := runtime.GOARCH | ||
| if ctx != nil && ctx.ArchitectureChoice != "" { | ||
| wantedArch = ctx.ArchitectureChoice | ||
| } | ||
| wantedOS := runtime.GOOS | ||
| if ctx != nil && ctx.OSChoice != "" { | ||
| wantedOS = ctx.OSChoice | ||
| } | ||
|
|
||
| wantedVariant := getCPUVariant(runtime.GOOS, runtime.GOARCH) | ||
| if ctx != nil && ctx.VariantChoice != "" { | ||
| wantedVariant = ctx.VariantChoice | ||
| } | ||
|
|
||
| var wantedPlatforms []imgspecv1.Platform | ||
| if wantedVariant != "" && compatibility[wantedArch] != nil { | ||
| wantedPlatforms = make([]imgspecv1.Platform, 0, len(compatibility[wantedArch])) | ||
| wantedIndex := -1 | ||
| for i, v := range compatibility[wantedArch] { | ||
| if wantedVariant == v { | ||
| wantedIndex = i | ||
| break | ||
| } | ||
| } | ||
| // user wants a variant which we know nothing about - not even compatibility | ||
| if wantedIndex == -1 { | ||
| wantedPlatforms = []imgspecv1.Platform{ | ||
| { | ||
| OS: wantedOS, | ||
| Architecture: wantedArch, | ||
| Variant: wantedVariant, | ||
| }, | ||
| } | ||
| } else { | ||
| for i := wantedIndex; i < len(compatibility[wantedArch]); i++ { | ||
| v := compatibility[wantedArch][i] | ||
| wantedPlatforms = append(wantedPlatforms, imgspecv1.Platform{ | ||
| OS: wantedOS, | ||
| Architecture: wantedArch, | ||
| Variant: v, | ||
| }) | ||
| } | ||
| } | ||
| } else { | ||
| wantedPlatforms = []imgspecv1.Platform{ | ||
| { | ||
| OS: wantedOS, | ||
| Architecture: wantedArch, | ||
| Variant: wantedVariant, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return wantedPlatforms, nil | ||
| } | ||
|
|
||
| func MatchesPlatform(image imgspecv1.Platform, wanted imgspecv1.Platform) bool { | ||
| if image.Architecture != wanted.Architecture { | ||
| return false | ||
| } | ||
| if image.OS != wanted.OS { | ||
| return false | ||
| } | ||
|
|
||
| if wanted.Variant == "" || image.Variant == wanted.Variant { | ||
| return true | ||
| } | ||
|
|
||
| return false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package platform | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/containers/image/v5/types" | ||
| imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestWantedPlatformsCompatibility(t *testing.T) { | ||
| ctx := &types.SystemContext{ | ||
| ArchitectureChoice: "arm", | ||
| OSChoice: "linux", | ||
| VariantChoice: "v6", | ||
| } | ||
| platforms, err := WantedPlatforms(ctx) | ||
| assert.Nil(t, err) | ||
| assert.Equal(t, len(platforms), 2) | ||
| assert.Equal(t, platforms[0], imgspecv1.Platform{ | ||
| OS: ctx.OSChoice, | ||
| Architecture: ctx.ArchitectureChoice, | ||
| Variant: "v6", | ||
| }) | ||
| assert.Equal(t, platforms[1], imgspecv1.Platform{ | ||
| OS: ctx.OSChoice, | ||
| Architecture: ctx.ArchitectureChoice, | ||
| Variant: "v5", | ||
| }) | ||
| } | ||
|
|
||
| func TestWantedPlatformsCustom(t *testing.T) { | ||
| ctx := &types.SystemContext{ | ||
| ArchitectureChoice: "armel", | ||
| OSChoice: "freeBSD", | ||
| VariantChoice: "custom", | ||
| } | ||
| platforms, err := WantedPlatforms(ctx) | ||
| assert.Nil(t, err) | ||
| assert.Equal(t, len(platforms), 1) | ||
| assert.Equal(t, platforms[0], imgspecv1.Platform{ | ||
| OS: ctx.OSChoice, | ||
| Architecture: ctx.ArchitectureChoice, | ||
| Variant: ctx.VariantChoice, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This now needs to just use
logrus.Infofinstead of failing.