client: prepare option-structs for multiple platforms

Some methods currently support a single platform only, but we may
be able to support multiple platforms.

This patch prepares the option-structs for multi-platform support,
but (for now) returning an error if multiple options are provided.

We need a similar check on the daemon-side, but still need to check
on the client, as older daemons will ignore multiple platforms, which
may be unexpected.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-30 14:16:31 +01:00
parent a96bc9e93c
commit 137adde33d
22 changed files with 176 additions and 91 deletions

View File

@@ -22,6 +22,7 @@ import (
"github.com/moby/moby/client/pkg/jsonmessage"
"github.com/moby/moby/v2/internal/testutil"
"github.com/moby/moby/v2/internal/testutil/fakecontext"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
@@ -662,7 +663,7 @@ func TestBuildPlatformInvalid(t *testing.T) {
_, err = testEnv.APIClient().ImageBuild(ctx, buf, client.ImageBuildOptions{
Remove: true,
ForceRemove: true,
Platform: "foobar",
Platforms: []ocispec.Platform{{OS: "foobar"}},
})
assert.Check(t, is.ErrorContains(err, "unknown operating system or architecture"))

View File

@@ -5,7 +5,6 @@ import (
"io"
"testing"
"github.com/containerd/platforms"
buildtypes "github.com/moby/moby/api/types/build"
"github.com/moby/moby/client"
build "github.com/moby/moby/v2/integration/internal/build"
@@ -19,13 +18,13 @@ import (
func TestAPIImagesHistory(t *testing.T) {
ctx := setupTest(t)
client := testEnv.APIClient()
apiClient := testEnv.APIClient()
dockerfile := "FROM busybox\nENV FOO bar"
imgID := build.Do(ctx, t, client, fakecontext.New(t, t.TempDir(), fakecontext.WithDockerfile(dockerfile)))
imgID := build.Do(ctx, t, apiClient, fakecontext.New(t, t.TempDir(), fakecontext.WithDockerfile(dockerfile)))
res, err := client.ImageHistory(ctx, imgID)
res, err := apiClient.ImageHistory(ctx, imgID)
assert.NilError(t, err)
assert.Assert(t, len(res.Items) != 0)
@@ -69,9 +68,9 @@ func TestAPIImageHistoryCrossPlatform(t *testing.T) {
// Build the image for a non-native platform
resp, err := apiClient.ImageBuild(ctx, buildCtx.AsTarReader(t), client.ImageBuildOptions{
Version: buildtypes.BuilderBuildKit,
Tags: []string{"cross-platform-test"},
Platform: platforms.FormatAll(nonNativePlatform),
Version: buildtypes.BuilderBuildKit,
Tags: []string{"cross-platform-test"},
Platforms: []ocispec.Platform{nonNativePlatform},
})
assert.NilError(t, err)
defer resp.Body.Close()
@@ -128,7 +127,9 @@ func TestAPIImageHistoryCrossPlatform(t *testing.T) {
}
func pullImageForPlatform(t *testing.T, ctx context.Context, apiClient client.APIClient, ref string, platform ocispec.Platform) {
pullResp, err := apiClient.ImagePull(ctx, ref, client.ImagePullOptions{Platform: platforms.FormatAll(platform)})
pullResp, err := apiClient.ImagePull(ctx, ref, client.ImagePullOptions{
Platforms: []ocispec.Platform{platform},
})
assert.NilError(t, err)
_, _ = io.Copy(io.Discard, pullResp)

View File

@@ -14,6 +14,7 @@ import (
"github.com/moby/moby/client"
"github.com/moby/moby/v2/internal/testutil"
"github.com/moby/moby/v2/internal/testutil/daemon"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
@@ -70,33 +71,30 @@ func TestImportWithCustomPlatform(t *testing.T) {
tests := []struct {
name string
platform string
expected platforms.Platform
platform ocispec.Platform
expected ocispec.Platform
}{
{
platform: "",
expected: platforms.Platform{
expected: ocispec.Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
},
},
{
platform: runtime.GOOS,
expected: platforms.Platform{
platform: ocispec.Platform{
OS: runtime.GOOS,
},
expected: ocispec.Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
},
},
{
platform: strings.ToUpper(runtime.GOOS),
expected: platforms.Platform{
platform: ocispec.Platform{
OS: runtime.GOOS,
Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
Architecture: "sparc64",
},
},
{
platform: runtime.GOOS + "/sparc64",
expected: platforms.Platform{
expected: ocispec.Platform{
OS: runtime.GOOS,
Architecture: "sparc64",
},
@@ -104,7 +102,7 @@ func TestImportWithCustomPlatform(t *testing.T) {
}
for i, tc := range tests {
t.Run(tc.platform, func(t *testing.T) {
t.Run(platforms.Format(tc.platform), func(t *testing.T) {
ctx := testutil.StartSpan(ctx, t)
reference := "import-with-platform:tc-" + strconv.Itoa(i)
@@ -140,36 +138,45 @@ func TestImportWithCustomPlatformReject(t *testing.T) {
tests := []struct {
name string
platform string
platform ocispec.Platform
expectedErr string
}{
{
platform: " ",
name: "whitespace-only platform",
platform: ocispec.Platform{
OS: " ",
},
expectedErr: "is an invalid OS component",
},
{
platform: "/",
expectedErr: "is an invalid OS component",
},
{
platform: "macos",
name: "valid, but unsupported os",
platform: ocispec.Platform{
OS: "macos",
},
expectedErr: "operating system is not supported",
},
{
platform: "macos/arm64",
name: "valid, but unsupported os/arch",
platform: ocispec.Platform{
OS: "macos",
Architecture: "arm64",
},
expectedErr: "operating system is not supported",
},
{
name: "valid, but unsupported os",
// TODO: platforms.Normalize() only validates os or arch if a single component is passed,
// but ignores unknown os/arch in other cases. See:
// https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
platform: "nintendo64",
platform: ocispec.Platform{
OS: "nintendo64",
},
expectedErr: "unknown operating system or architecture",
},
}
for i, tc := range tests {
t.Run(tc.platform, func(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
ctx := testutil.StartSpan(ctx, t)
reference := "import-with-platform:tc-" + strconv.Itoa(i)
_, err = apiClient.ImageImport(ctx,

View File

@@ -33,7 +33,9 @@ func TestImagePullPlatformInvalid(t *testing.T) {
apiClient := testEnv.APIClient()
_, err := apiClient.ImagePull(ctx, "docker.io/library/hello-world:latest", client.ImagePullOptions{Platform: "foobar"})
_, err := apiClient.ImagePull(ctx, "docker.io/library/hello-world:latest", client.ImagePullOptions{
Platforms: []ocispec.Platform{{OS: "foobar"}},
})
assert.Assert(t, err != nil)
assert.Check(t, is.ErrorContains(err, "unknown operating system or architecture"))
assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))

View File

@@ -219,7 +219,7 @@ func TestSaveAndLoadPlatform(t *testing.T) {
type testCase struct {
testName string
containerdStoreOnly bool
pullPlatforms []string
pullPlatforms []ocispec.Platform
savePlatforms []ocispec.Platform
loadPlatforms []ocispec.Platform
expectedSavedPlatforms []ocispec.Platform
@@ -230,9 +230,13 @@ func TestSaveAndLoadPlatform(t *testing.T) {
{
testName: "With no platforms specified",
containerdStoreOnly: true,
pullPlatforms: []string{"linux/amd64", "linux/riscv64", "linux/arm64/v8"},
savePlatforms: nil,
loadPlatforms: nil,
pullPlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "amd64"},
{OS: "linux", Architecture: "riscv64"},
{OS: "linux", Architecture: "arm64", Variant: "v8"},
},
savePlatforms: nil,
loadPlatforms: nil,
expectedSavedPlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "amd64"},
{OS: "linux", Architecture: "riscv64"},
@@ -246,16 +250,20 @@ func TestSaveAndLoadPlatform(t *testing.T) {
},
{
testName: "With single pulled platform",
pullPlatforms: []string{"linux/amd64"},
pullPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
savePlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
loadPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
expectedSavedPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
expectedLoadedPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
},
{
testName: "With single platform save and load",
containerdStoreOnly: true,
pullPlatforms: []string{"linux/amd64", "linux/riscv64", "linux/arm64/v8"},
testName: "With single platform save and load",
containerdStoreOnly: true,
pullPlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "amd64"},
{OS: "linux", Architecture: "riscv64"},
{OS: "linux", Architecture: "arm64", Variant: "v8"},
},
savePlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
loadPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
expectedSavedPlatforms: []ocispec.Platform{{OS: "linux", Architecture: "amd64"}},
@@ -264,7 +272,11 @@ func TestSaveAndLoadPlatform(t *testing.T) {
{
testName: "With multiple platforms save and load",
containerdStoreOnly: true,
pullPlatforms: []string{"linux/amd64", "linux/riscv64", "linux/arm64/v8"},
pullPlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "amd64"},
{OS: "linux", Architecture: "riscv64"},
{OS: "linux", Architecture: "arm64", Variant: "v8"},
},
savePlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "arm64", Variant: "v8"},
{OS: "linux", Architecture: "riscv64"},
@@ -285,7 +297,11 @@ func TestSaveAndLoadPlatform(t *testing.T) {
{
testName: "With mixed platform save and load",
containerdStoreOnly: true,
pullPlatforms: []string{"linux/amd64", "linux/riscv64", "linux/arm64/v8"},
pullPlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "amd64"},
{OS: "linux", Architecture: "riscv64"},
{OS: "linux", Architecture: "arm64", Variant: "v8"},
},
savePlatforms: []ocispec.Platform{
{OS: "linux", Architecture: "arm64", Variant: "v8"},
{OS: "linux", Architecture: "riscv64"},
@@ -310,7 +326,7 @@ func TestSaveAndLoadPlatform(t *testing.T) {
t.Run(tc.testName, func(t *testing.T) {
// pull the image
for _, p := range tc.pullPlatforms {
resp, err := apiClient.ImagePull(ctx, repoName, client.ImagePullOptions{Platform: p})
resp, err := apiClient.ImagePull(ctx, repoName, client.ImagePullOptions{Platforms: []ocispec.Platform{p}})
assert.NilError(t, err)
_, err = io.ReadAll(resp)
resp.Close()
@@ -348,10 +364,10 @@ func TestSaveAndLoadPlatform(t *testing.T) {
// pull the image again (start fresh)
for _, p := range tc.pullPlatforms {
resp, err := apiClient.ImagePull(ctx, repoName, client.ImagePullOptions{Platform: p})
pullRes, err := apiClient.ImagePull(ctx, repoName, client.ImagePullOptions{Platforms: []ocispec.Platform{p}})
assert.NilError(t, err)
_, err = io.ReadAll(resp)
resp.Close()
_, err = io.ReadAll(pullRes)
_ = pullRes.Close()
assert.NilError(t, err)
}