Skip to content

Commit c299ba3

Browse files
committed
Update worker.Platforms() in builder-next worker.
Use platform MatchComparer when checking for matching platforms. Also, add unit test to ensure the merging of defined and host-supported platforms works correctly. Signed-off-by: Cesar Talledo <[email protected]>
1 parent 0e2cc22 commit c299ba3

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

builder/builder-next/worker/worker.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,22 +158,38 @@ func (w *Worker) Labels() map[string]string {
158158
// Platforms returns one or more platforms supported by the image.
159159
func (w *Worker) Platforms(noCache bool) []ocispec.Platform {
160160
if noCache {
161-
pm := make(map[string]struct{}, len(w.Opt.Platforms))
162-
for _, p := range w.Opt.Platforms {
163-
pm[platforms.Format(p)] = struct{}{}
164-
}
165-
for _, p := range archutil.SupportedPlatforms(noCache) {
166-
if _, ok := pm[platforms.Format(p)]; !ok {
167-
w.Opt.Platforms = append(w.Opt.Platforms, p)
168-
}
169-
}
161+
w.Opt.Platforms = mergePlatforms(w.Opt.Platforms, archutil.SupportedPlatforms(noCache))
170162
}
171163
if len(w.Opt.Platforms) == 0 {
172164
return []ocispec.Platform{platforms.DefaultSpec()}
173165
}
174166
return w.Opt.Platforms
175167
}
176168

169+
// mergePlatforms merges the defined platforms with the supported platforms
170+
// and returns a new slice of platforms. It ensures no duplicates.
171+
func mergePlatforms(defined, supported []ocispec.Platform) []ocispec.Platform {
172+
result := []ocispec.Platform{}
173+
matchers := make([]platforms.MatchComparer, len(defined))
174+
for i, p := range defined {
175+
result = append(result, p)
176+
matchers[i] = platforms.Only(p)
177+
}
178+
for _, p := range supported {
179+
exists := false
180+
for _, m := range matchers {
181+
if m.Match(p) {
182+
exists = true
183+
break
184+
}
185+
}
186+
if !exists {
187+
result = append(result, p)
188+
}
189+
}
190+
return result
191+
}
192+
177193
// GCPolicy returns automatic GC Policy
178194
func (w *Worker) GCPolicy() []client.PruneInfo {
179195
return w.Opt.GCPolicy
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package worker
2+
3+
import (
4+
"testing"
5+
6+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
7+
"gotest.tools/v3/assert"
8+
is "gotest.tools/v3/assert/cmp"
9+
)
10+
11+
func TestMergePlatforms(t *testing.T) {
12+
defaultPlatform := ocispec.Platform{OS: "linux", Architecture: "amd64"}
13+
otherPlatform := ocispec.Platform{OS: "windows", Architecture: "amd64"}
14+
thirdPlatform := ocispec.Platform{OS: "darwin", Architecture: "arm64"}
15+
16+
tests := []struct {
17+
name string
18+
defined []ocispec.Platform
19+
supported []ocispec.Platform
20+
contains []ocispec.Platform
21+
wantLen int
22+
}{
23+
{
24+
name: "AllUnique",
25+
defined: []ocispec.Platform{defaultPlatform},
26+
supported: []ocispec.Platform{otherPlatform, thirdPlatform},
27+
contains: []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
28+
wantLen: 3,
29+
},
30+
{
31+
name: "SomeOverlap",
32+
defined: []ocispec.Platform{defaultPlatform, otherPlatform},
33+
supported: []ocispec.Platform{otherPlatform, thirdPlatform},
34+
contains: []ocispec.Platform{defaultPlatform, otherPlatform, thirdPlatform},
35+
wantLen: 3,
36+
},
37+
{
38+
name: "AllOverlap",
39+
defined: []ocispec.Platform{defaultPlatform, otherPlatform},
40+
supported: []ocispec.Platform{defaultPlatform, otherPlatform},
41+
contains: []ocispec.Platform{defaultPlatform, otherPlatform},
42+
wantLen: 2,
43+
},
44+
{
45+
name: "EmptySupported",
46+
defined: []ocispec.Platform{defaultPlatform},
47+
supported: []ocispec.Platform{},
48+
contains: []ocispec.Platform{defaultPlatform},
49+
wantLen: 1,
50+
},
51+
{
52+
name: "EmptyDefined",
53+
defined: []ocispec.Platform{},
54+
supported: []ocispec.Platform{defaultPlatform, otherPlatform},
55+
contains: []ocispec.Platform{defaultPlatform, otherPlatform},
56+
wantLen: 2,
57+
},
58+
{
59+
name: "BothEmpty",
60+
defined: []ocispec.Platform{},
61+
supported: []ocispec.Platform{},
62+
contains: []ocispec.Platform{},
63+
wantLen: 0,
64+
},
65+
}
66+
67+
for _, tc := range tests {
68+
t.Run(tc.name, func(t *testing.T) {
69+
got := mergePlatforms(tc.defined, tc.supported)
70+
assert.Equal(t, len(got), tc.wantLen)
71+
for _, p := range tc.contains {
72+
assert.Assert(t, is.Contains(got, p))
73+
}
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)