X Tutup
Skip to content

Commit d9ff8eb

Browse files
committed
support multi-arch images for windows via ctr
Signed-off-by: James Sturtevant <jstur@microsoft.com>
1 parent 9a9bd09 commit d9ff8eb

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

pkg/cri/platforms/default_windows.go

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,59 +19,10 @@
1919
package platforms
2020

2121
import (
22-
"fmt"
23-
"strconv"
24-
"strings"
25-
2622
"github.com/containerd/containerd/platforms"
27-
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
28-
"golang.org/x/sys/windows"
2923
)
3024

31-
type matchComparer struct {
32-
defaults platforms.Matcher
33-
osVersionPrefix string
34-
}
35-
36-
// Match matches platform with the same windows major, minor
37-
// and build version.
38-
func (m matchComparer) Match(p imagespec.Platform) bool {
39-
if m.defaults.Match(p) {
40-
// TODO(windows): Figure out whether OSVersion is deprecated.
41-
return strings.HasPrefix(p.OSVersion, m.osVersionPrefix)
42-
}
43-
return false
44-
}
45-
46-
// Less sorts matched platforms in front of other platforms.
47-
// For matched platforms, it puts platforms with larger revision
48-
// number in front.
49-
func (m matchComparer) Less(p1, p2 imagespec.Platform) bool {
50-
m1, m2 := m.Match(p1), m.Match(p2)
51-
if m1 && m2 {
52-
r1, r2 := revision(p1.OSVersion), revision(p2.OSVersion)
53-
return r1 > r2
54-
}
55-
return m1 && !m2
56-
}
57-
58-
func revision(v string) int {
59-
parts := strings.Split(v, ".")
60-
if len(parts) < 4 {
61-
return 0
62-
}
63-
r, err := strconv.Atoi(parts[3])
64-
if err != nil {
65-
return 0
66-
}
67-
return r
68-
}
69-
7025
// Default returns the current platform's default platform specification.
7126
func Default() platforms.MatchComparer {
72-
major, minor, build := windows.RtlGetNtVersionNumbers()
73-
return matchComparer{
74-
defaults: platforms.Only(platforms.DefaultSpec()),
75-
osVersionPrefix: fmt.Sprintf("%d.%d.%d", major, minor, build),
76-
}
27+
return platforms.Default()
7728
}

platforms/defaults_windows.go

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,63 @@
1919
package platforms
2020

2121
import (
22+
"fmt"
2223
"runtime"
24+
"strconv"
25+
"strings"
2326

27+
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2428
specs "github.com/opencontainers/image-spec/specs-go/v1"
29+
"golang.org/x/sys/windows"
2530
)
2631

27-
// Default returns the default matcher for the platform.
32+
type matchComparer struct {
33+
defaults Matcher
34+
osVersionPrefix string
35+
}
36+
37+
// Match matches platform with the same windows major, minor
38+
// and build version.
39+
func (m matchComparer) Match(p imagespec.Platform) bool {
40+
if m.defaults.Match(p) {
41+
// TODO(windows): Figure out whether OSVersion is deprecated.
42+
return strings.HasPrefix(p.OSVersion, m.osVersionPrefix)
43+
}
44+
return false
45+
}
46+
47+
// Less sorts matched platforms in front of other platforms.
48+
// For matched platforms, it puts platforms with larger revision
49+
// number in front.
50+
func (m matchComparer) Less(p1, p2 imagespec.Platform) bool {
51+
m1, m2 := m.Match(p1), m.Match(p2)
52+
if m1 && m2 {
53+
r1, r2 := revision(p1.OSVersion), revision(p2.OSVersion)
54+
return r1 > r2
55+
}
56+
return m1 && !m2
57+
}
58+
59+
func revision(v string) int {
60+
parts := strings.Split(v, ".")
61+
if len(parts) < 4 {
62+
return 0
63+
}
64+
r, err := strconv.Atoi(parts[3])
65+
if err != nil {
66+
return 0
67+
}
68+
return r
69+
}
70+
71+
// Default returns the current platform's default platform specification.
2872
func Default() MatchComparer {
29-
return Ordered(DefaultSpec(), specs.Platform{
30-
OS: "linux",
31-
Architecture: runtime.GOARCH,
32-
})
73+
major, minor, build := windows.RtlGetNtVersionNumbers()
74+
return matchComparer{
75+
defaults: Ordered(DefaultSpec(), specs.Platform{
76+
OS: "linux",
77+
Architecture: runtime.GOARCH,
78+
}),
79+
osVersionPrefix: fmt.Sprintf("%d.%d.%d", major, minor, build),
80+
}
3381
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ import (
2222
"sort"
2323
"testing"
2424

25-
"github.com/containerd/containerd/platforms"
2625
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
2726
"github.com/stretchr/testify/assert"
2827
)
2928

3029
func TestMatchComparerMatch(t *testing.T) {
3130
m := matchComparer{
32-
defaults: platforms.Only(imagespec.Platform{
31+
defaults: Only(imagespec.Platform{
3332
Architecture: "amd64",
3433
OS: "windows",
3534
}),
@@ -85,7 +84,7 @@ func TestMatchComparerMatch(t *testing.T) {
8584

8685
func TestMatchComparerLess(t *testing.T) {
8786
m := matchComparer{
88-
defaults: platforms.Only(imagespec.Platform{
87+
defaults: Only(imagespec.Platform{
8988
Architecture: "amd64",
9089
OS: "windows",
9190
}),

0 commit comments

Comments
 (0)
X Tutup