From 5611fdd4afe9c1f711030260b4cd5d2a4b107add Mon Sep 17 00:00:00 2001 From: Kohei Tokunaga Date: Tue, 4 Jun 2024 09:48:17 +0900 Subject: [PATCH] Transfer: Push: Enable to specify platforms Signed-off-by: Kohei Tokunaga --- cmd/ctr/commands/images/push.go | 9 ++++++++- core/transfer/image/imagestore.go | 4 ++++ core/transfer/local/push.go | 20 +++++--------------- core/transfer/transfer.go | 5 +++++ 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/cmd/ctr/commands/images/push.go b/cmd/ctr/commands/images/push.go index 1f7b05e95..aeaebbf45 100644 --- a/cmd/ctr/commands/images/push.go +++ b/cmd/ctr/commands/images/push.go @@ -112,7 +112,14 @@ var pushCommand = &cli.Command{ if err != nil { return err } - is := image.NewStore(local) + var p []ocispec.Platform + if pss := context.StringSlice("platform"); len(pss) > 0 { + p, err = platforms.ParseAll(pss) + if err != nil { + return fmt.Errorf("invalid platform %v: %w", pss, err) + } + } + is := image.NewStore(local, image.WithPlatforms(p...)) pf, done := ProgressHandler(ctx, os.Stdout) defer done() diff --git a/core/transfer/image/imagestore.go b/core/transfer/image/imagestore.go index 36f3393a0..e0a2f47fd 100644 --- a/core/transfer/image/imagestore.go +++ b/core/transfer/image/imagestore.go @@ -348,6 +348,10 @@ func (is *Store) Lookup(ctx context.Context, store images.Store) ([]images.Image return imgs, nil } +func (is *Store) Platforms() []ocispec.Platform { + return is.platforms +} + func (is *Store) UnpackPlatforms() []transfer.UnpackConfiguration { unpacks := make([]transfer.UnpackConfiguration, len(is.unpacks)) for i, uc := range is.unpacks { diff --git a/core/transfer/local/push.go b/core/transfer/local/push.go index 0af2f7f45..efa65f120 100644 --- a/core/transfer/local/push.go +++ b/core/transfer/local/push.go @@ -33,22 +33,12 @@ import ( ) func (ts *localTransferService) push(ctx context.Context, ig transfer.ImageGetter, p transfer.ImagePusher, tops *transfer.Config) error { - /* - // TODO: Platform matching - if pushCtx.PlatformMatcher == nil { - if len(pushCtx.Platforms) > 0 { - ps, err := platforms.ParseAll(pushCtx.Platforms) - if err != nil { - return err - } - pushCtx.PlatformMatcher = platforms.Any(ps...) - } else { - pushCtx.PlatformMatcher = platforms.All - } - } - */ matcher := platforms.All - // Filter push + if ipg, ok := ig.(transfer.ImagePlatformsGetter); ok { + if ps := ipg.Platforms(); len(ps) > 0 { + matcher = platforms.Any(ps...) + } + } img, err := ig.Get(ctx, ts.images) if err != nil { diff --git a/core/transfer/transfer.go b/core/transfer/transfer.go index 69e672bf8..0329bfcfb 100644 --- a/core/transfer/transfer.go +++ b/core/transfer/transfer.go @@ -100,6 +100,11 @@ type ImageUnpacker interface { UnpackPlatforms() []UnpackConfiguration } +// ImagePlatformsGetter is type which returns configured platforms. +type ImagePlatformsGetter interface { + Platforms() []ocispec.Platform +} + // UnpackConfiguration specifies the platform and snapshotter to use for resolving // the unpack Platform, if snapshotter is not specified the platform default will // be used.