Skip to content

Commit

Permalink
Updated oras to use updated containerd with artifacts-spec v1 (#305)
Browse files Browse the repository at this point in the history
Signed-off-by: Sajay Antony <[email protected]>
  • Loading branch information
sajayantony authored Sep 6, 2021
1 parent 01bc2b4 commit 888591e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/oras/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
24 changes: 12 additions & 12 deletions cmd/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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"`
Expand All @@ -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,
}
}

Expand Down
23 changes: 10 additions & 13 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type pushOptions struct {
manifestAnnotations string
manifestExport string
artifactType string
artifactRefs []string
artifactRefs string
pathValidationDisabled bool
verbose bool
dryRun bool
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
3 changes: 2 additions & 1 deletion internal/resolver/dummy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/oras/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/oras/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/oras/pull_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
7 changes: 1 addition & 6 deletions pkg/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
}
18 changes: 7 additions & 11 deletions pkg/oras/push_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 888591e

Please sign in to comment.