From 888591e46a0c60a3f5d33a04657c7958eb995b33 Mon Sep 17 00:00:00 2001 From: Sajay Antony <1821104+sajayantony@users.noreply.github.com> Date: Mon, 6 Sep 2021 00:26:18 -0700 Subject: [PATCH] Updated oras to use updated containerd with artifacts-spec v1 (#305) Signed-off-by: Sajay Antony --- cmd/oras/cache.go | 2 +- cmd/oras/discover.go | 24 ++++++++++++------------ cmd/oras/push.go | 23 ++++++++++------------- go.mod | 8 ++++---- go.sum | 9 +++++++++ internal/resolver/dummy.go | 3 ++- pkg/oras/discover.go | 3 ++- pkg/oras/pull.go | 2 +- pkg/oras/pull_opts.go | 2 +- pkg/oras/push.go | 7 +------ pkg/oras/push_opts.go | 18 +++++++----------- 11 files changed, 50 insertions(+), 51 deletions(-) diff --git a/cmd/oras/cache.go b/cmd/oras/cache.go index b23d89283..4d12209c6 100644 --- a/cmd/oras/cache.go +++ b/cmd/oras/cache.go @@ -8,9 +8,9 @@ import ( "github.com/containerd/containerd/content" "github.com/containerd/containerd/content/local" "github.com/containerd/containerd/errdefs" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "github.com/pkg/errors" ) diff --git a/cmd/oras/discover.go b/cmd/oras/discover.go index 4b6a1acaa..3820d4568 100644 --- a/cmd/oras/discover.go +++ b/cmd/oras/discover.go @@ -12,9 +12,9 @@ import ( "github.com/containerd/containerd/reference" "github.com/containerd/containerd/remotes" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/xlab/treeprint" @@ -98,8 +98,8 @@ func runDiscover(opts discoverOptions) error { return nil } -func getAllReferences(ctx context.Context, resolver remotes.Resolver, targetRef string, artifactType string, treeNode treeprint.Tree, queryGraph bool) (ocispec.Descriptor, *[]remotes.DiscoveredArtifact, error) { - var results []remotes.DiscoveredArtifact +func getAllReferences(ctx context.Context, resolver remotes.Resolver, targetRef string, artifactType string, treeNode treeprint.Tree, queryGraph bool) (ocispec.Descriptor, *[]artifactspec.Descriptor, error) { + var results []artifactspec.Descriptor spec, err := reference.Parse(targetRef) if err != nil { return ocispec.Descriptor{}, nil, err @@ -114,7 +114,7 @@ func getAllReferences(ctx context.Context, resolver remotes.Resolver, targetRef } for _, r := range refs { - branch := treeNode.AddBranch(fmt.Sprintf("[%s]%s", r.Artifact.ArtifactType, r.Digest)) + branch := treeNode.AddBranch(fmt.Sprintf("[%s]%s", r.ArtifactType, r.Digest)) if queryGraph { nestedRef := fmt.Sprintf("%s@%s", spec.Locator, r.Digest) _, refs1, err := getAllReferences(ctx, resolver, nestedRef, "", branch, queryGraph) @@ -129,11 +129,11 @@ func getAllReferences(ctx context.Context, resolver remotes.Resolver, targetRef return desc, &results, nil } -func printDiscoveredReferencesTable(refs []remotes.DiscoveredArtifact, verbose bool) { +func printDiscoveredReferencesTable(refs []artifactspec.Descriptor, verbose bool) { typeNameTitle := "Artifact Type" typeNameLength := len(typeNameTitle) for _, ref := range refs { - if length := len(ref.Artifact.ArtifactType); length > typeNameLength { + if length := len(ref.ArtifactType); length > typeNameLength { typeNameLength = length } } @@ -144,17 +144,17 @@ func printDiscoveredReferencesTable(refs []remotes.DiscoveredArtifact, verbose b print(typeNameTitle, "Digest") for _, ref := range refs { - print(ref.Artifact.ArtifactType, ref.Digest) + print(ref.ArtifactType, ref.Digest) if verbose { - printJSON(ref.Artifact) + printJSON(ref) } } } -func printDiscoveredReferencesJSON(desc ocispec.Descriptor, refs []remotes.DiscoveredArtifact) { +func printDiscoveredReferencesJSON(desc ocispec.Descriptor, refs []artifactspec.Descriptor) { type reference struct { - Digest digest.Digest `json:"digest"` - Manifest artifactspec.Artifact `json:"manifest"` + Digest digest.Digest `json:"digest"` + Artifact string `json:"artifactType"` } output := struct { Digest digest.Digest `json:"digest"` @@ -167,7 +167,7 @@ func printDiscoveredReferencesJSON(desc ocispec.Descriptor, refs []remotes.Disco for i, ref := range refs { output.References[i] = reference{ Digest: ref.Digest, - Manifest: ref.Artifact, + Artifact: ref.ArtifactType, } } diff --git a/cmd/oras/push.go b/cmd/oras/push.go index df239b8d3..1eb9ffa26 100644 --- a/cmd/oras/push.go +++ b/cmd/oras/push.go @@ -31,7 +31,7 @@ type pushOptions struct { manifestAnnotations string manifestExport string artifactType string - artifactRefs []string + artifactRefs string pathValidationDisabled bool verbose bool dryRun bool @@ -81,7 +81,7 @@ Example - Push file to the HTTP registry: cmd.Flags().StringVarP(&opts.manifestAnnotations, "manifest-annotations", "", "", "manifest annotation file") cmd.Flags().StringVarP(&opts.manifestExport, "export-manifest", "", "", "export the pushed manifest") cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type") - cmd.Flags().StringArrayVarP(&opts.artifactRefs, "artifact-reference", "", nil, "artifact reference") + cmd.Flags().StringVarP(&opts.artifactRefs, "artifact-reference", "", "", "artifact reference") cmd.Flags().BoolVarP(&opts.pathValidationDisabled, "disable-path-validation", "", false, "skip path validation") cmd.Flags().BoolVarP(&opts.verbose, "verbose", "v", false, "verbose output") cmd.Flags().BoolVarP(&opts.debug, "debug", "d", false, "debug mode") @@ -118,11 +118,11 @@ func runPush(opts pushOptions) error { if iresolver.IsDummy(resolver) { refResolver = newResolver(opts.username, opts.password, opts.insecure, opts.plainHTTP, opts.configs...) } - manifests, err := loadReferences(ctx, refResolver, opts.artifactRefs) + manifest, err := loadReference(ctx, refResolver, opts.artifactRefs) if err != nil { return err } - pushOpts = append(pushOpts, oras.AsArtifact(opts.artifactType, manifests...)) + pushOpts = append(pushOpts, oras.AsArtifact(opts.artifactType, manifest)) } // load files @@ -226,14 +226,11 @@ func loadFiles(store *content.FileStore, annotations map[string]map[string]strin return files, nil } -func loadReferences(ctx context.Context, resolver remotes.Resolver, refs []string) ([]ocispec.Descriptor, error) { - descs := make([]ocispec.Descriptor, 0, len(refs)) - for _, ref := range refs { - _, desc, err := resolver.Resolve(ctx, ref) - if err != nil { - return nil, errors.Wrapf(err, "failed to resolve ref %q", ref) - } - descs = append(descs, desc) +func loadReference(ctx context.Context, resolver remotes.Resolver, reference string) (ocispec.Descriptor, error) { + _, desc, err := resolver.Resolve(ctx, reference) + if err != nil { + return desc, errors.Wrapf(err, "failed to resolve ref %q", reference) } - return descs, nil + return desc, nil + } diff --git a/go.mod b/go.mod index e249bad81..f26112802 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,9 @@ go 1.16 replace ( // WARNING! Do NOT replace these without also replacing their lines in the `require` stanza below. // These `replace` stanzas are IGNORED when this is imported as a library - github.com/containerd/containerd => github.com/notaryproject/containerd v1.5.0-beta.4.0.20210414031158-5724862e07ff + github.com/containerd/containerd => github.com/oras-project/containerd v1.5.0-beta.4.0.20210903105219-0950109ddf9c github.com/docker/distribution => github.com/docker/distribution v0.0.0-20191216044856-a8371794149d github.com/docker/docker => github.com/moby/moby v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible - github.com/opencontainers/artifacts => github.com/notaryproject/artifacts v0.0.0-20210414030140-c7c701eff45d ) require ( @@ -19,15 +18,16 @@ require ( github.com/docker/docker-credential-helpers v0.6.3 // indirect github.com/docker/go-connections v0.4.0 // indirect github.com/morikuni/aec v1.0.0 // indirect - github.com/opencontainers/artifacts v0.0.0-20210209205009-a282023000bd + github.com/opencontainers/artifacts v0.0.0-20210209205009-a282023000bd // indirect github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.1 + github.com/oras-project/artifacts-spec v0.0.0-20210830204616-bd9edb3a9b43 github.com/phayes/freeport v0.0.0-20180830031419-95f893ade6f2 github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.1.3 github.com/stretchr/testify v1.7.0 - github.com/xlab/treeprint v1.1.0 // indirect + github.com/xlab/treeprint v1.1.0 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad golang.org/x/sync v0.0.0-20201207232520-09787c993a3a ) diff --git a/go.sum b/go.sum index 049735a80..6c67159b8 100644 --- a/go.sum +++ b/go.sum @@ -386,6 +386,7 @@ github.com/onsi/gomega v0.0.0-20151007035656-2152b45fa28a/go.mod h1:C1qb7wdrVGGV github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/opencontainers/artifacts v0.0.0-20210209205009-a282023000bd/go.mod h1:SD8DjsrkV/11lBKhbcjodFNeLmkZVCvDYPoXKZeGH5Q= github.com/opencontainers/go-digest v0.0.0-20170106003457-a6d0ee40d420/go.mod h1:cMLVZDEM3+U2I4VmLI6N8jQYUd2OVphdqWwCJHrFt2s= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= @@ -398,6 +399,14 @@ github.com/opencontainers/runtime-spec v1.0.1/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/ github.com/opencontainers/runtime-spec v1.0.2/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo= +github.com/oras-project/artifacts-spec v0.0.0-20210830204616-bd9edb3a9b43 h1:WDAdGkL9gSREKHLV3eyIWalP9Q7Nf+MTnDvXi1X6xR4= +github.com/oras-project/artifacts-spec v0.0.0-20210830204616-bd9edb3a9b43/go.mod h1:Xch2aLzSwtkhbFFN6LUzTfLtukYvMMdXJ4oZ8O7BOdc= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210414031158-5724862e07ff h1:4ix+GgOj1cHiOLZMkysU5/usgRhiSzYLkbVbfH5IMr8= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210414031158-5724862e07ff/go.mod h1:VMsSqat+Y2ZaXuQZl7COtOkGB1n9PFxu7KKTBXkNtt0= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210903081938-39fb7981ffb4 h1:L8B3UxwWyPXY/IhpLWXBPJQERQDKSvufXG8C6qGMv2U= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210903081938-39fb7981ffb4/go.mod h1:tMt2m3FYr7Hd8NBrZTdqLXQald+ciH7lcB9JV98Skaw= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210903105219-0950109ddf9c h1:o/+myPWL2IQbqoalqfrWxwlp+CT8viWkeijo3saZKtk= +github.com/oras-project/containerd v1.5.0-beta.4.0.20210903105219-0950109ddf9c/go.mod h1:tMt2m3FYr7Hd8NBrZTdqLXQald+ciH7lcB9JV98Skaw= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= diff --git a/internal/resolver/dummy.go b/internal/resolver/dummy.go index 58c3f5a5c..1389e881a 100644 --- a/internal/resolver/dummy.go +++ b/internal/resolver/dummy.go @@ -10,6 +10,7 @@ import ( "github.com/containerd/containerd/remotes" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "github.com/pkg/errors" ) @@ -58,7 +59,7 @@ func (r *dummyResolver) Pusher(ctx context.Context, ref string) (remotes.Pusher, // Discoverer returns a new discoverer for the provided reference func (r *dummyResolver) Discoverer(ctx context.Context, ref string) (remotes.Discoverer, error) { - return remotes.DiscovererFunc(func(ctx context.Context, desc ocispec.Descriptor, artifactType string) ([]remotes.DiscoveredArtifact, error) { + return remotes.DiscovererFunc(func(ctx context.Context, desc ocispec.Descriptor, artifactType string) ([]artifactspec.Descriptor, error) { return nil, errors.Wrap(errdefs.ErrNotFound, "dummy resolver") }), nil } diff --git a/pkg/oras/discover.go b/pkg/oras/discover.go index 6ff0b4385..08eb12390 100644 --- a/pkg/oras/discover.go +++ b/pkg/oras/discover.go @@ -5,10 +5,11 @@ import ( "github.com/containerd/containerd/remotes" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" ) // Discover discovers artifacts referencing the specified artifact -func Discover(ctx context.Context, resolver remotes.Resolver, ref, artifactType string) (ocispec.Descriptor, []remotes.DiscoveredArtifact, error) { +func Discover(ctx context.Context, resolver remotes.Resolver, ref, artifactType string) (ocispec.Descriptor, []artifactspec.Descriptor, error) { _, desc, err := resolver.Resolve(ctx, ref) if err != nil { return ocispec.Descriptor{}, nil, err diff --git a/pkg/oras/pull.go b/pkg/oras/pull.go index 9d31cf425..70cd6f3be 100644 --- a/pkg/oras/pull.go +++ b/pkg/oras/pull.go @@ -10,8 +10,8 @@ import ( "github.com/containerd/containerd/images" "github.com/containerd/containerd/log" "github.com/containerd/containerd/remotes" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "github.com/pkg/errors" "golang.org/x/sync/semaphore" ) diff --git a/pkg/oras/pull_opts.go b/pkg/oras/pull_opts.go index dc3d9fb50..e1c466891 100644 --- a/pkg/oras/pull_opts.go +++ b/pkg/oras/pull_opts.go @@ -9,9 +9,9 @@ import ( orascontent "github.com/deislabs/oras/pkg/content" "github.com/containerd/containerd/images" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "golang.org/x/sync/semaphore" ) diff --git a/pkg/oras/push.go b/pkg/oras/push.go index 91d84ef0c..0d28c74b5 100644 --- a/pkg/oras/push.go +++ b/pkg/oras/push.go @@ -7,10 +7,10 @@ import ( "github.com/containerd/containerd/images" "github.com/containerd/containerd/remotes" artifact "github.com/deislabs/oras/pkg/artifact" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" digest "github.com/opencontainers/go-digest" specs "github.com/opencontainers/image-spec/specs-go" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" ) // Push pushes files to the remote @@ -91,10 +91,6 @@ func pack(provider content.Provider, descriptors []ocispec.Descriptor, opts *pus var content interface{} if opts.artifact != nil { artifact := *opts.artifact - if config != nil { - artifactConfig := convertV1DescriptorToV2(*config) - artifact.Config = &artifactConfig - } artifact.Blobs = convertV1DescriptorsToV2(descriptors) artifact.Annotations = opts.manifestAnnotations mediaType = artifact.MediaType @@ -138,6 +134,5 @@ func convertV1DescriptorToV2(desc ocispec.Descriptor) artifactspec.Descriptor { Size: desc.Size, URLs: desc.URLs, Annotations: desc.Annotations, - Platform: (*artifactspec.Platform)(desc.Platform), } } diff --git a/pkg/oras/push_opts.go b/pkg/oras/push_opts.go index 7ce2d101e..b25fb4bd0 100644 --- a/pkg/oras/push_opts.go +++ b/pkg/oras/push_opts.go @@ -11,9 +11,8 @@ import ( orascontent "github.com/deislabs/oras/pkg/content" "github.com/containerd/containerd/images" - artifactspecs "github.com/opencontainers/artifacts/specs-go" - artifactspec "github.com/opencontainers/artifacts/specs-go/v2" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1" "github.com/pkg/errors" ) @@ -26,7 +25,7 @@ type pushOpts struct { manifestWriter io.Writer validateName func(desc ocispec.Descriptor) error baseHandlers []images.Handler - artifact *artifactspec.Artifact + artifact *artifactspec.Manifest } func pushOptsDefaults() *pushOpts { @@ -162,15 +161,12 @@ func pushStatusTrack(writer io.Writer) images.Handler { } // AsArtifact set oras to push contents as an artifact -func AsArtifact(artifactType string, manifests ...ocispec.Descriptor) PushOpt { +func AsArtifact(artifactType string, manifest ocispec.Descriptor) PushOpt { return func(o *pushOpts) error { - o.artifact = &artifactspec.Artifact{ - Versioned: artifactspecs.Versioned{ - SchemaVersion: 2, // historical value. does not pertain to OCI or docker version - }, - MediaType: artifactspec.MediaTypeArtifactManifest, - ArtifactType: artifactType, - Manifests: convertV1DescriptorsToV2(manifests), + o.artifact = &artifactspec.Manifest{ + MediaType: artifactspec.MediaTypeArtifactManifest, + ArtifactType: artifactType, + SubjectManifest: convertV1DescriptorToV2(manifest), } return nil }