Skip to content

Commit 59a625d

Browse files
authored
Merge pull request #3609 from Random-Liu/add-image-with-platform
Make default platform configurable in the client
2 parents d540b10 + 555cb31 commit 59a625d

9 files changed

Lines changed: 28 additions & 16 deletions

client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
9999
c.runtime = defaults.DefaultRuntime
100100
}
101101

102+
if copts.defaultPlatform != nil {
103+
c.platform = copts.defaultPlatform
104+
} else {
105+
c.platform = platforms.Default()
106+
}
107+
102108
if copts.services != nil {
103109
c.services = *copts.services
104110
}
@@ -193,6 +199,7 @@ type Client struct {
193199
conn *grpc.ClientConn
194200
runtime string
195201
defaultns string
202+
platform platforms.MatchComparer
196203
connector func() (*grpc.ClientConn, error)
197204
}
198205

client_opts.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import (
2626
)
2727

2828
type clientOpts struct {
29-
defaultns string
30-
defaultRuntime string
31-
services *services
32-
dialOptions []grpc.DialOption
33-
timeout time.Duration
29+
defaultns string
30+
defaultRuntime string
31+
defaultPlatform platforms.MatchComparer
32+
services *services
33+
dialOptions []grpc.DialOption
34+
timeout time.Duration
3435
}
3536

3637
// ClientOpt allows callers to set options on the containerd client
@@ -55,6 +56,14 @@ func WithDefaultRuntime(rt string) ClientOpt {
5556
}
5657
}
5758

59+
// WithDefaultPlatform sets the default platform matcher on the client
60+
func WithDefaultPlatform(platform platforms.MatchComparer) ClientOpt {
61+
return func(c *clientOpts) error {
62+
c.defaultPlatform = platform
63+
return nil
64+
}
65+
}
66+
5867
// WithDialOpts allows grpc.DialOptions to be set on the connection
5968
func WithDialOpts(opts []grpc.DialOption) ClientOpt {
6069
return func(c *clientOpts) error {

container_opts.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/containerd/containerd/containers"
2323
"github.com/containerd/containerd/errdefs"
2424
"github.com/containerd/containerd/oci"
25-
"github.com/containerd/containerd/platforms"
2625
"github.com/containerd/containerd/snapshots"
2726
"github.com/containerd/typeurl"
2827
"github.com/gogo/protobuf/types"
@@ -190,7 +189,7 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta
190189
// root filesystem in read-only mode
191190
func WithNewSnapshotView(id string, i Image, opts ...snapshots.Opt) NewContainerOpts {
192191
return func(ctx context.Context, client *Client, c *containers.Container) error {
193-
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default())
192+
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), client.platform)
194193
if err != nil {
195194
return err
196195
}

container_opts_unix.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"github.com/containerd/containerd/containers"
2929
"github.com/containerd/containerd/errdefs"
3030
"github.com/containerd/containerd/mount"
31-
"github.com/containerd/containerd/platforms"
3231
"github.com/opencontainers/image-spec/identity"
3332
)
3433

@@ -45,7 +44,7 @@ func WithRemappedSnapshotView(id string, i Image, uid, gid uint32) NewContainerO
4544

4645
func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool) NewContainerOpts {
4746
return func(ctx context.Context, client *Client, c *containers.Container) error {
48-
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default())
47+
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), client.platform)
4948
if err != nil {
5049
return err
5150
}

container_restore_opts.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/containerd/containerd/containers"
2323
"github.com/containerd/containerd/content"
2424
"github.com/containerd/containerd/images"
25-
"github.com/containerd/containerd/platforms"
2625
"github.com/gogo/protobuf/proto"
2726
ptypes "github.com/gogo/protobuf/types"
2827
"github.com/opencontainers/image-spec/identity"
@@ -58,7 +57,7 @@ func WithRestoreImage(ctx context.Context, id string, client *Client, checkpoint
5857
return err
5958
}
6059

61-
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default())
60+
diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), client.platform)
6261
if err != nil {
6362
return err
6463
}

image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func NewImage(client *Client, i images.Image) Image {
110110
return &image{
111111
client: client,
112112
i: i,
113-
platform: platforms.Default(),
113+
platform: client.platform,
114114
}
115115
}
116116

import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (c *Client) Import(ctx context.Context, reader io.Reader, opts ...ImportOpt
125125
}
126126
var platformMatcher = platforms.All
127127
if !iopts.allPlatforms {
128-
platformMatcher = platforms.Default()
128+
platformMatcher = c.platform
129129
}
130130

131131
var handler images.HandlerFunc = func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {

install.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/containerd/containerd/archive/compression"
2828
"github.com/containerd/containerd/content"
2929
"github.com/containerd/containerd/images"
30-
"github.com/containerd/containerd/platforms"
3130
"github.com/pkg/errors"
3231
)
3332

@@ -43,7 +42,7 @@ func (c *Client) Install(ctx context.Context, image Image, opts ...InstallOpts)
4342
}
4443
var (
4544
cs = image.ContentStore()
46-
platform = platforms.Default()
45+
platform = c.platform
4746
)
4847
manifest, err := images.Manifest(ctx, cs, image.Target(), platform)
4948
if err != nil {

pull.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (_ Ima
4444
if len(pullCtx.Platforms) > 1 {
4545
return nil, errors.New("cannot pull multiplatform image locally, try Fetch")
4646
} else if len(pullCtx.Platforms) == 0 {
47-
pullCtx.PlatformMatcher = platforms.Default()
47+
pullCtx.PlatformMatcher = c.platform
4848
} else {
4949
p, err := platforms.Parse(pullCtx.Platforms[0])
5050
if err != nil {

0 commit comments

Comments
 (0)