Skip to content

Commit 83f7690

Browse files
committed
Add support for multiple platforms in docker image load.
Signed-off-by: Cesar Talledo <[email protected]>
1 parent 8b8f558 commit 83f7690

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed

cli/command/image/load.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
"github.com/docker/cli/internal/jsonstream"
1212
"github.com/docker/docker/client"
1313
"github.com/moby/sys/sequential"
14+
v1 "github.com/opencontainers/image-spec/specs-go/v1"
1415
"github.com/pkg/errors"
1516
"github.com/spf13/cobra"
1617
)
1718

1819
type loadOptions struct {
1920
input string
2021
quiet bool
21-
platform string
22+
platform []string
2223
}
2324

2425
// NewLoadCommand creates a new `docker load` command
@@ -42,7 +43,7 @@ func NewLoadCommand(dockerCli command.Cli) *cobra.Command {
4243

4344
flags.StringVarP(&opts.input, "input", "i", "", "Read from tar archive file, instead of STDIN")
4445
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Suppress the load output")
45-
flags.StringVar(&opts.platform, "platform", "", `Load only the given platform variant. Formatted as "os[/arch[/variant]]" (e.g., "linux/amd64")`)
46+
flags.StringSliceVar(&opts.platform, "platform", []string{}, `Load only the given platform(s). Formatted as a comma-separated list of "os[/arch[/variant]]" (e.g., "linux/amd64,linux/arm64/v8").`)
4647
_ = flags.SetAnnotation("platform", "version", []string{"1.48"})
4748

4849
_ = cmd.RegisterFlagCompletionFunc("platform", completion.Platforms)
@@ -76,13 +77,16 @@ func runLoad(ctx context.Context, dockerCli command.Cli, opts loadOptions) error
7677
options = append(options, client.ImageLoadWithQuiet(true))
7778
}
7879

79-
if opts.platform != "" {
80-
p, err := platforms.Parse(opts.platform)
80+
platformList := []v1.Platform{}
81+
for _, p := range opts.platform {
82+
pp, err := platforms.Parse(p)
8183
if err != nil {
8284
return errors.Wrap(err, "invalid platform")
8385
}
84-
// TODO(thaJeztah): change flag-type to support multiple platforms.
85-
options = append(options, client.ImageLoadWithPlatforms(p))
86+
platformList = append(platformList, pp)
87+
}
88+
if len(platformList) > 0 {
89+
options = append(options, client.ImageLoadWithPlatforms(platformList...))
8690
}
8791

8892
response, err := dockerCli.Client().ImageLoad(ctx, input, options...)

cli/command/image/load_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestNewLoadCommandSuccess(t *testing.T) {
104104
},
105105
},
106106
{
107-
name: "with platform",
107+
name: "with-single-platform",
108108
args: []string{"--platform", "linux/amd64"},
109109
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (image.LoadResponse, error) {
110110
// FIXME(thaJeztah): need to find appropriate way to test the result of "ImageHistoryWithPlatform" being applied
@@ -113,6 +113,22 @@ func TestNewLoadCommandSuccess(t *testing.T) {
113113
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
114114
},
115115
},
116+
{
117+
name: "with-comma-separated-platforms",
118+
args: []string{"--platform", "linux/amd64,linux/arm64/v8,linux/riscv64"},
119+
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (image.LoadResponse, error) {
120+
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
121+
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
122+
},
123+
},
124+
{
125+
name: "with-multiple-platform-options",
126+
args: []string{"--platform", "linux/amd64", "--platform", "linux/arm64/v8", "--platform", "linux/riscv64"},
127+
imageLoadFunc: func(input io.Reader, options ...client.ImageLoadOption) (image.LoadResponse, error) {
128+
assert.Check(t, len(options) > 0) // can be 1 or two depending on whether a terminal is attached :/
129+
return image.LoadResponse{Body: io.NopCloser(strings.NewReader("Success"))}, nil
130+
},
131+
},
116132
}
117133
for _, tc := range testCases {
118134
t.Run(tc.name, func(t *testing.T) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Success
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Success

0 commit comments

Comments
 (0)