From 55d1abe3e8418810d40f2a4c593a9c812423fe20 Mon Sep 17 00:00:00 2001 From: Shiwei Zhang Date: Fri, 26 Mar 2021 19:12:33 +0800 Subject: [PATCH] support artifact discovery for oras lib Signed-off-by: Shiwei Zhang --- go.mod | 2 +- go.sum | 4 ++-- pkg/oras/discover.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 pkg/oras/discover.go diff --git a/go.mod b/go.mod index 6432c958e..04ee78834 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ 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.20210326071533-6710065b5800 + github.com/containerd/containerd => github.com/notaryproject/containerd v1.5.0-beta.4.0.20210326100849-6094ad5d5479 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 ) diff --git a/go.sum b/go.sum index b6dce75d0..9358575a4 100644 --- a/go.sum +++ b/go.sum @@ -372,8 +372,8 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+ github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM= github.com/notaryproject/artifacts v0.0.0-20210325201627-49d06c9f1a07 h1:geJlhRFBwSJXhIv7KMgAXBz1Ie6wsuFeE5vuau3B4AQ= github.com/notaryproject/artifacts v0.0.0-20210325201627-49d06c9f1a07/go.mod h1:za0DINIIQLPCBn9AhLfPjywgT51o0zll644gbIXiQiQ= -github.com/notaryproject/containerd v1.5.0-beta.4.0.20210326071533-6710065b5800 h1:vMfubXqR1FzRCXJDKgjpuFybJWbNVJKetJTwceky9eI= -github.com/notaryproject/containerd v1.5.0-beta.4.0.20210326071533-6710065b5800/go.mod h1:uvn4m2BuoisfEj5I7GLoCJguWejxwLUlyM0L4XnVWY8= +github.com/notaryproject/containerd v1.5.0-beta.4.0.20210326100849-6094ad5d5479 h1:zan+c1XrL2pyc6RuR+xYJ43SgB2AOXnLH99uAQQrn5w= +github.com/notaryproject/containerd v1.5.0-beta.4.0.20210326100849-6094ad5d5479/go.mod h1:uvn4m2BuoisfEj5I7GLoCJguWejxwLUlyM0L4XnVWY8= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= github.com/onsi/ginkgo v0.0.0-20151202141238-7f8ab55aaf3b/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/pkg/oras/discover.go b/pkg/oras/discover.go new file mode 100644 index 000000000..496ce6ec9 --- /dev/null +++ b/pkg/oras/discover.go @@ -0,0 +1,29 @@ +package oras + +import ( + "context" + + "github.com/containerd/containerd/remotes" + artifactspec "github.com/notaryproject/artifacts/specs-go/v2" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" +) + +// Discover discovers artifacts referencing the specified artifact +func Discover(ctx context.Context, resolver remotes.Resolver, ref, artifactType string) (ocispec.Descriptor, []artifactspec.Artifact, error) { + _, desc, err := resolver.Resolve(ctx, ref) + if err != nil { + return ocispec.Descriptor{}, nil, err + } + + discoverer, err := resolver.Discoverer(ctx, ref) + if err != nil { + return ocispec.Descriptor{}, nil, err + } + + artifacts, err := discoverer.Discover(ctx, desc, artifactType) + if err != nil { + return ocispec.Descriptor{}, nil, err + } + + return desc, artifacts, err +}