Add a platform.ParseAll helper
Signed-off-by: Jin Dong <djdongjin95@gmail.com>
This commit is contained in:
parent
b6abda73b8
commit
0a92661e69
20
client.go
20
client.go
@ -403,13 +403,9 @@ func (c *Client) Fetch(ctx context.Context, ref string, opts ...RemoteOpt) (imag
|
|||||||
if len(fetchCtx.Platforms) == 0 {
|
if len(fetchCtx.Platforms) == 0 {
|
||||||
fetchCtx.PlatformMatcher = platforms.All
|
fetchCtx.PlatformMatcher = platforms.All
|
||||||
} else {
|
} else {
|
||||||
var ps []ocispec.Platform
|
ps, err := platforms.ParseAll(fetchCtx.Platforms)
|
||||||
for _, s := range fetchCtx.Platforms {
|
if err != nil {
|
||||||
p, err := platforms.Parse(s)
|
return images.Image{}, err
|
||||||
if err != nil {
|
|
||||||
return images.Image{}, fmt.Errorf("invalid platform %s: %w", s, err)
|
|
||||||
}
|
|
||||||
ps = append(ps, p)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchCtx.PlatformMatcher = platforms.Any(ps...)
|
fetchCtx.PlatformMatcher = platforms.Any(ps...)
|
||||||
@ -439,13 +435,9 @@ func (c *Client) Push(ctx context.Context, ref string, desc ocispec.Descriptor,
|
|||||||
}
|
}
|
||||||
if pushCtx.PlatformMatcher == nil {
|
if pushCtx.PlatformMatcher == nil {
|
||||||
if len(pushCtx.Platforms) > 0 {
|
if len(pushCtx.Platforms) > 0 {
|
||||||
var ps []ocispec.Platform
|
ps, err := platforms.ParseAll(pushCtx.Platforms)
|
||||||
for _, platform := range pushCtx.Platforms {
|
if err != nil {
|
||||||
p, err := platforms.Parse(platform)
|
return err
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid platform %s: %w", platform, err)
|
|
||||||
}
|
|
||||||
ps = append(ps, p)
|
|
||||||
}
|
}
|
||||||
pushCtx.PlatformMatcher = platforms.Any(ps...)
|
pushCtx.PlatformMatcher = platforms.Any(ps...)
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/containerd/containerd/images/converter"
|
"github.com/containerd/containerd/images/converter"
|
||||||
"github.com/containerd/containerd/images/converter/uncompress"
|
"github.com/containerd/containerd/images/converter/uncompress"
|
||||||
"github.com/containerd/containerd/platforms"
|
"github.com/containerd/containerd/platforms"
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -70,13 +69,9 @@ When '--all-platforms' is given all images in a manifest list must be available.
|
|||||||
|
|
||||||
if !context.Bool("all-platforms") {
|
if !context.Bool("all-platforms") {
|
||||||
if pss := context.StringSlice("platform"); len(pss) > 0 {
|
if pss := context.StringSlice("platform"); len(pss) > 0 {
|
||||||
var all []ocispec.Platform
|
all, err := platforms.ParseAll(pss)
|
||||||
for _, ps := range pss {
|
if err != nil {
|
||||||
p, err := platforms.Parse(ps)
|
return err
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid platform %q: %w", ps, err)
|
|
||||||
}
|
|
||||||
all = append(all, p)
|
|
||||||
}
|
}
|
||||||
convertOpts = append(convertOpts, converter.WithPlatform(platforms.Ordered(all...)))
|
convertOpts = append(convertOpts, converter.WithPlatform(platforms.Ordered(all...)))
|
||||||
} else {
|
} else {
|
||||||
|
@ -22,7 +22,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
|
||||||
"github.com/containerd/containerd/cmd/ctr/commands"
|
"github.com/containerd/containerd/cmd/ctr/commands"
|
||||||
@ -133,13 +132,9 @@ When '--all-platforms' is given all images in a manifest list must be available.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if pss := context.StringSlice("platform"); len(pss) > 0 {
|
if pss := context.StringSlice("platform"); len(pss) > 0 {
|
||||||
var all []ocispec.Platform
|
all, err := platforms.ParseAll(pss)
|
||||||
for _, ps := range pss {
|
if err != nil {
|
||||||
p, err := platforms.Parse(ps)
|
return err
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid platform %q: %w", ps, err)
|
|
||||||
}
|
|
||||||
all = append(all, p)
|
|
||||||
}
|
}
|
||||||
exportOpts = append(exportOpts, archive.WithPlatform(platforms.Ordered(all...)))
|
exportOpts = append(exportOpts, archive.WithPlatform(platforms.Ordered(all...)))
|
||||||
} else {
|
} else {
|
||||||
|
@ -101,13 +101,9 @@ command. As part of this process, we do the following:
|
|||||||
|
|
||||||
var sopts []image.StoreOpt
|
var sopts []image.StoreOpt
|
||||||
if !context.Bool("all-platforms") {
|
if !context.Bool("all-platforms") {
|
||||||
var p []ocispec.Platform
|
p, err := platforms.ParseAll(context.StringSlice("platform"))
|
||||||
for _, s := range context.StringSlice("platform") {
|
if err != nil {
|
||||||
ps, err := platforms.Parse(s)
|
return err
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to parse platform %s: %w", s, err)
|
|
||||||
}
|
|
||||||
p = append(p, ps)
|
|
||||||
}
|
}
|
||||||
if len(p) == 0 {
|
if len(p) == 0 {
|
||||||
p = append(p, platforms.DefaultSpec())
|
p = append(p, platforms.DefaultSpec())
|
||||||
@ -168,12 +164,9 @@ command. As part of this process, we do the following:
|
|||||||
return fmt.Errorf("unable to resolve image platforms: %w", err)
|
return fmt.Errorf("unable to resolve image platforms: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for _, s := range context.StringSlice("platform") {
|
p, err = platforms.ParseAll(context.StringSlice("platform"))
|
||||||
ps, err := platforms.Parse(s)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return fmt.Errorf("unable to parse platform %s: %w", s, err)
|
|
||||||
}
|
|
||||||
p = append(p, ps)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(p) == 0 {
|
if len(p) == 0 {
|
||||||
|
@ -37,13 +37,9 @@ func (ts *localTransferService) push(ctx context.Context, ig transfer.ImageGette
|
|||||||
// TODO: Platform matching
|
// TODO: Platform matching
|
||||||
if pushCtx.PlatformMatcher == nil {
|
if pushCtx.PlatformMatcher == nil {
|
||||||
if len(pushCtx.Platforms) > 0 {
|
if len(pushCtx.Platforms) > 0 {
|
||||||
var ps []ocispec.Platform
|
ps, err := platforms.ParseAll(pushCtx.Platforms)
|
||||||
for _, platform := range pushCtx.Platforms {
|
if err != nil {
|
||||||
p, err := platforms.Parse(platform)
|
return err
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("invalid platform %s: %w", platform, err)
|
|
||||||
}
|
|
||||||
ps = append(ps, p)
|
|
||||||
}
|
}
|
||||||
pushCtx.PlatformMatcher = platforms.Any(ps...)
|
pushCtx.PlatformMatcher = platforms.Any(ps...)
|
||||||
} else {
|
} else {
|
||||||
|
@ -158,6 +158,19 @@ func (m *matcher) String() string {
|
|||||||
return Format(m.Platform)
|
return Format(m.Platform)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseAll parses a list of platform specifiers into a list of platform.
|
||||||
|
func ParseAll(specifiers []string) ([]specs.Platform, error) {
|
||||||
|
platforms := make([]specs.Platform, len(specifiers))
|
||||||
|
for i, s := range specifiers {
|
||||||
|
p, err := Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("invalid platform %s: %w", s, err)
|
||||||
|
}
|
||||||
|
platforms[i] = p
|
||||||
|
}
|
||||||
|
return platforms, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Parse parses the platform specifier syntax into a platform declaration.
|
// Parse parses the platform specifier syntax into a platform declaration.
|
||||||
//
|
//
|
||||||
// Platform specifiers are in the format `<os>|<arch>|<os>/<arch>[/<variant>]`.
|
// Platform specifiers are in the format `<os>|<arch>|<os>/<arch>[/<variant>]`.
|
||||||
|
@ -39,7 +39,6 @@ import (
|
|||||||
"github.com/containerd/containerd/runtime"
|
"github.com/containerd/containerd/runtime"
|
||||||
shimbinary "github.com/containerd/containerd/runtime/v2/shim"
|
shimbinary "github.com/containerd/containerd/runtime/v2/shim"
|
||||||
"github.com/containerd/containerd/sandbox"
|
"github.com/containerd/containerd/sandbox"
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config for the v2 runtime
|
// Config for the v2 runtime
|
||||||
@ -63,7 +62,7 @@ func init() {
|
|||||||
},
|
},
|
||||||
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
|
InitFn: func(ic *plugin.InitContext) (interface{}, error) {
|
||||||
config := ic.Config.(*Config)
|
config := ic.Config.(*Config)
|
||||||
supportedPlatforms, err := parsePlatforms(config.Platforms)
|
supportedPlatforms, err := platforms.ParseAll(config.Platforms)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -387,18 +386,6 @@ func (m *ShimManager) Delete(ctx context.Context, id string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func parsePlatforms(platformStr []string) ([]ocispec.Platform, error) {
|
|
||||||
p := make([]ocispec.Platform, len(platformStr))
|
|
||||||
for i, v := range platformStr {
|
|
||||||
parsed, err := platforms.Parse(v)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
p[i] = parsed
|
|
||||||
}
|
|
||||||
return p, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TaskManager wraps task service client on top of shim manager.
|
// TaskManager wraps task service client on top of shim manager.
|
||||||
type TaskManager struct {
|
type TaskManager struct {
|
||||||
manager *ShimManager
|
manager *ShimManager
|
||||||
|
Loading…
Reference in New Issue
Block a user