From 9163377123eb2378b32ed469ed7e1dd1301b8154 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Wed, 20 Sep 2017 11:56:59 -0700 Subject: [PATCH] platforms: provide simpler function for common use Signed-off-by: Stephen J Day --- cmd/ctr/utils.go | 2 +- container_opts.go | 4 ++-- container_opts_unix.go | 2 +- image.go | 6 +++--- platforms/defaults.go | 9 +++++++-- platforms/defaults_test.go | 7 ++++++- spec_opts_unix.go | 4 ++-- spec_opts_windows.go | 2 +- 8 files changed, 23 insertions(+), 13 deletions(-) diff --git a/cmd/ctr/utils.go b/cmd/ctr/utils.go index da1747bcafe0..2dee686efc83 100644 --- a/cmd/ctr/utils.go +++ b/cmd/ctr/utils.go @@ -206,7 +206,7 @@ func getImageLayers(ctx gocontext.Context, image images.Image, cs content.Store) return nil, errors.Wrap(err, "failed to unmarshal manifest") } - diffIDs, err := image.RootFS(ctx, cs, platforms.Format(platforms.Default())) + diffIDs, err := image.RootFS(ctx, cs, platforms.Default()) if err != nil { return nil, errors.Wrap(err, "failed to resolve rootfs") } diff --git a/container_opts.go b/container_opts.go index 756de7ad3a1a..cbf9ecababbf 100644 --- a/container_opts.go +++ b/container_opts.go @@ -80,7 +80,7 @@ func WithSnapshot(id string) NewContainerOpts { // root filesystem in read-write mode func WithNewSnapshot(id string, i Image) NewContainerOpts { return func(ctx context.Context, client *Client, c *containers.Container) error { - diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Format(platforms.Default())) + diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default()) if err != nil { return err } @@ -109,7 +109,7 @@ func WithSnapshotCleanup(ctx context.Context, client *Client, c containers.Conta // root filesystem in read-only mode func WithNewSnapshotView(id string, i Image) NewContainerOpts { return func(ctx context.Context, client *Client, c *containers.Container) error { - diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Format(platforms.Default())) + diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default()) if err != nil { return err } diff --git a/container_opts_unix.go b/container_opts_unix.go index 8108f4b83703..961ce720d51c 100644 --- a/container_opts_unix.go +++ b/container_opts_unix.go @@ -39,7 +39,7 @@ func WithCheckpoint(desc v1.Descriptor, snapshotKey string) NewContainerOpts { fk := m rw = &fk case images.MediaTypeDockerSchema2Manifest: - config, err := images.Config(ctx, store, m, platforms.Format(platforms.Default())) + config, err := images.Config(ctx, store, m, platforms.Default()) if err != nil { return err } diff --git a/image.go b/image.go index 88f9b2e0f043..5390a6d6cff9 100644 --- a/image.go +++ b/image.go @@ -45,7 +45,7 @@ func (i *image) Target() ocispec.Descriptor { func (i *image) RootFS(ctx context.Context) ([]digest.Digest, error) { provider := i.client.ContentStore() - return i.i.RootFS(ctx, provider, platforms.Format(platforms.Default())) + return i.i.RootFS(ctx, provider, platforms.Default()) } func (i *image) Size(ctx context.Context) (int64, error) { @@ -55,11 +55,11 @@ func (i *image) Size(ctx context.Context) (int64, error) { func (i *image) Config(ctx context.Context) (ocispec.Descriptor, error) { provider := i.client.ContentStore() - return i.i.Config(ctx, provider, platforms.Format(platforms.Default())) + return i.i.Config(ctx, provider, platforms.Default()) } func (i *image) Unpack(ctx context.Context, snapshotterName string) error { - layers, err := i.getLayers(ctx, platforms.Format(platforms.Default())) + layers, err := i.getLayers(ctx, platforms.Default()) if err != nil { return err } diff --git a/platforms/defaults.go b/platforms/defaults.go index d49912052f1f..2b57b4979535 100644 --- a/platforms/defaults.go +++ b/platforms/defaults.go @@ -6,8 +6,13 @@ import ( specs "github.com/opencontainers/image-spec/specs-go/v1" ) -// Default returns the current platform's default platform specification. -func Default() specs.Platform { +// Default returns the default specifier for the platform. +func Default() string { + return Format(DefaultSpec()) +} + +// DefaultSpec returns the current platform's default platform specification. +func DefaultSpec() specs.Platform { return specs.Platform{ OS: runtime.GOOS, Architecture: runtime.GOARCH, diff --git a/platforms/defaults_test.go b/platforms/defaults_test.go index 08c40be63922..39f2e485da16 100644 --- a/platforms/defaults_test.go +++ b/platforms/defaults_test.go @@ -13,8 +13,13 @@ func TestDefault(t *testing.T) { OS: runtime.GOOS, Architecture: runtime.GOARCH, } - p := Default() + p := DefaultSpec() if !reflect.DeepEqual(p, expected) { t.Fatalf("default platform not as expected: %#v != %#v", p, expected) } + + s := Default() + if s != Format(p) { + t.Fatalf("default specifier should match formatted default spec: %v != %v", s, p) + } } diff --git a/spec_opts_unix.go b/spec_opts_unix.go index b529cfb87812..dfe875d17d6b 100644 --- a/spec_opts_unix.go +++ b/spec_opts_unix.go @@ -73,7 +73,7 @@ func WithImageConfig(i Image) SpecOpts { image = i.(*image) store = client.ContentStore() ) - ic, err := image.i.Config(ctx, store, platforms.Format(platforms.Default())) + ic, err := image.i.Config(ctx, store, platforms.Default()) if err != nil { return err } @@ -236,7 +236,7 @@ func WithRemappedSnapshotView(id string, i Image, uid, gid uint32) NewContainerO func withRemappedSnapshotBase(id string, i Image, uid, gid uint32, readonly bool) NewContainerOpts { return func(ctx context.Context, client *Client, c *containers.Container) error { - diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Format(platforms.Default())) + diffIDs, err := i.(*image).i.RootFS(ctx, client.ContentStore(), platforms.Default()) if err != nil { return err } diff --git a/spec_opts_windows.go b/spec_opts_windows.go index 33aba1a9cf55..5aa5c30297f1 100644 --- a/spec_opts_windows.go +++ b/spec_opts_windows.go @@ -21,7 +21,7 @@ func WithImageConfig(i Image) SpecOpts { image = i.(*image) store = client.ContentStore() ) - ic, err := image.i.Config(ctx, store, platforms.Format(platforms.Default())) + ic, err := image.i.Config(ctx, store, platforms.Default()) if err != nil { return err }