Skip to content

Commit 3f62014

Browse files
committed
Use github.com/containerd/platforms package
Signed-off-by: Derek McGowan <[email protected]>
1 parent e73942d commit 3f62014

29 files changed

+797
-1648
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
github.com/containerd/go-runc v1.1.0
1919
github.com/containerd/log v0.1.0
2020
github.com/containerd/nri v0.5.0
21+
github.com/containerd/platforms v0.1.1
2122
github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7
2223
github.com/containerd/ttrpc v1.2.2
2324
github.com/containerd/typeurl/v2 v2.1.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
6060
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
6161
github.com/containerd/nri v0.5.0 h1:bwCtKpi8i5FCA8g8WjIZNod91CEfIloYpV0+TH2prnQ=
6262
github.com/containerd/nri v0.5.0/go.mod h1:qIu2NlP3r/qK4YGnNuQf0De4VPqQWP2i2CVBfAZbGzg=
63+
github.com/containerd/platforms v0.1.1 h1:gp0xXBoY+1CjH54gJDon0kBjIbK2C4XSX1BGwP5ptG0=
64+
github.com/containerd/platforms v0.1.1/go.mod h1:XOM2BS6kN6gXafPLg80V6y/QUib+xoLyC3qVmHzibko=
6365
github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7 h1:MUbtIMHEcMzj+8mPgHd5ett0WVbY/KYHa5tMvFs5Ejs=
6466
github.com/containerd/plugin v0.0.0-20231101173250-7ec69893e1e7/go.mod h1:j6HlpMtkiZMgT4UsfVNxPBUkwdw9KQGU6nCLfRxnq+w=
6567
github.com/containerd/ttrpc v1.2.2 h1:9vqZr0pxwOF5koz6N0N3kJ0zDHokrcPxIR/ZR2YFtOs=

platforms/compare.go

Lines changed: 9 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -17,72 +17,11 @@
1717
package platforms
1818

1919
import (
20-
"strconv"
21-
"strings"
22-
20+
"github.com/containerd/platforms"
2321
specs "github.com/opencontainers/image-spec/specs-go/v1"
2422
)
2523

26-
// MatchComparer is able to match and compare platforms to
27-
// filter and sort platforms.
28-
type MatchComparer interface {
29-
Matcher
30-
31-
Less(specs.Platform, specs.Platform) bool
32-
}
33-
34-
// platformVector returns an (ordered) vector of appropriate specs.Platform
35-
// objects to try matching for the given platform object (see platforms.Only).
36-
func platformVector(platform specs.Platform) []specs.Platform {
37-
vector := []specs.Platform{platform}
38-
39-
switch platform.Architecture {
40-
case "amd64":
41-
if amd64Version, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && amd64Version > 1 {
42-
for amd64Version--; amd64Version >= 1; amd64Version-- {
43-
vector = append(vector, specs.Platform{
44-
Architecture: platform.Architecture,
45-
OS: platform.OS,
46-
OSVersion: platform.OSVersion,
47-
OSFeatures: platform.OSFeatures,
48-
Variant: "v" + strconv.Itoa(amd64Version),
49-
})
50-
}
51-
}
52-
vector = append(vector, specs.Platform{
53-
Architecture: "386",
54-
OS: platform.OS,
55-
OSVersion: platform.OSVersion,
56-
OSFeatures: platform.OSFeatures,
57-
})
58-
case "arm":
59-
if armVersion, err := strconv.Atoi(strings.TrimPrefix(platform.Variant, "v")); err == nil && armVersion > 5 {
60-
for armVersion--; armVersion >= 5; armVersion-- {
61-
vector = append(vector, specs.Platform{
62-
Architecture: platform.Architecture,
63-
OS: platform.OS,
64-
OSVersion: platform.OSVersion,
65-
OSFeatures: platform.OSFeatures,
66-
Variant: "v" + strconv.Itoa(armVersion),
67-
})
68-
}
69-
}
70-
case "arm64":
71-
variant := platform.Variant
72-
if variant == "" {
73-
variant = "v8"
74-
}
75-
vector = append(vector, platformVector(specs.Platform{
76-
Architecture: "arm",
77-
OS: platform.OS,
78-
OSVersion: platform.OSVersion,
79-
OSFeatures: platform.OSFeatures,
80-
Variant: variant,
81-
})...)
82-
}
83-
84-
return vector
85-
}
24+
type MatchComparer = platforms.MatchComparer
8625

8726
// Only returns a match comparer for a single platform
8827
// using default resolution logic for the platform.
@@ -92,7 +31,7 @@ func platformVector(platform specs.Platform) []specs.Platform {
9231
// For arm/v6, will also match arm/v5
9332
// For amd64, will also match 386
9433
func Only(platform specs.Platform) MatchComparer {
95-
return Ordered(platformVector(Normalize(platform))...)
34+
return platforms.Only(platform)
9635
}
9736

9837
// OnlyStrict returns a match comparer for a single platform.
@@ -104,100 +43,21 @@ func Only(platform specs.Platform) MatchComparer {
10443
// OnlyStrict matches non-canonical forms.
10544
// So, "arm64" matches "arm/64/v8".
10645
func OnlyStrict(platform specs.Platform) MatchComparer {
107-
return Ordered(Normalize(platform))
46+
return platforms.OnlyStrict(platform)
10847
}
10948

11049
// Ordered returns a platform MatchComparer which matches any of the platforms
11150
// but orders them in order they are provided.
112-
func Ordered(platforms ...specs.Platform) MatchComparer {
113-
matchers := make([]Matcher, len(platforms))
114-
for i := range platforms {
115-
matchers[i] = NewMatcher(platforms[i])
116-
}
117-
return orderedPlatformComparer{
118-
matchers: matchers,
119-
}
51+
func Ordered(ps ...specs.Platform) MatchComparer {
52+
return platforms.Ordered(ps...)
12053
}
12154

12255
// Any returns a platform MatchComparer which matches any of the platforms
12356
// with no preference for ordering.
124-
func Any(platforms ...specs.Platform) MatchComparer {
125-
matchers := make([]Matcher, len(platforms))
126-
for i := range platforms {
127-
matchers[i] = NewMatcher(platforms[i])
128-
}
129-
return anyPlatformComparer{
130-
matchers: matchers,
131-
}
57+
func Any(ps ...specs.Platform) MatchComparer {
58+
return platforms.Any(ps...)
13259
}
13360

13461
// All is a platform MatchComparer which matches all platforms
13562
// with preference for ordering.
136-
var All MatchComparer = allPlatformComparer{}
137-
138-
type orderedPlatformComparer struct {
139-
matchers []Matcher
140-
}
141-
142-
func (c orderedPlatformComparer) Match(platform specs.Platform) bool {
143-
for _, m := range c.matchers {
144-
if m.Match(platform) {
145-
return true
146-
}
147-
}
148-
return false
149-
}
150-
151-
func (c orderedPlatformComparer) Less(p1 specs.Platform, p2 specs.Platform) bool {
152-
for _, m := range c.matchers {
153-
p1m := m.Match(p1)
154-
p2m := m.Match(p2)
155-
if p1m && !p2m {
156-
return true
157-
}
158-
if p1m || p2m {
159-
return false
160-
}
161-
}
162-
return false
163-
}
164-
165-
type anyPlatformComparer struct {
166-
matchers []Matcher
167-
}
168-
169-
func (c anyPlatformComparer) Match(platform specs.Platform) bool {
170-
for _, m := range c.matchers {
171-
if m.Match(platform) {
172-
return true
173-
}
174-
}
175-
return false
176-
}
177-
178-
func (c anyPlatformComparer) Less(p1, p2 specs.Platform) bool {
179-
var p1m, p2m bool
180-
for _, m := range c.matchers {
181-
if !p1m && m.Match(p1) {
182-
p1m = true
183-
}
184-
if !p2m && m.Match(p2) {
185-
p2m = true
186-
}
187-
if p1m && p2m {
188-
return false
189-
}
190-
}
191-
// If one matches, and the other does, sort match first
192-
return p1m && !p2m
193-
}
194-
195-
type allPlatformComparer struct{}
196-
197-
func (allPlatformComparer) Match(specs.Platform) bool {
198-
return true
199-
}
200-
201-
func (allPlatformComparer) Less(specs.Platform, specs.Platform) bool {
202-
return false
203-
}
63+
var All = platforms.All

0 commit comments

Comments
 (0)