Skip to content

Commit

Permalink
new artifact spec support
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT committed Mar 26, 2021
1 parent 5bfe0ab commit f56d806
Show file tree
Hide file tree
Showing 9 changed files with 541 additions and 94 deletions.
3 changes: 2 additions & 1 deletion cmd/oras/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
"github.com/pkg/errors"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (s *cachedStore) Writer(ctx context.Context, opts ...content.WriterOpt) (co
}
}
switch wOpts.Desc.MediaType {
case ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex:
case ocispec.MediaTypeImageManifest, ocispec.MediaTypeImageIndex, artifactspec.MediaTypeArtifact:
return s.cache.Writer(ctx, opts...)
}

Expand Down
31 changes: 29 additions & 2 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
ctxo "github.com/deislabs/oras/pkg/context"
"github.com/deislabs/oras/pkg/oras"

"github.com/containerd/containerd/remotes"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -26,6 +28,8 @@ type pushOptions struct {
fileRefs []string
manifestConfigRef string
manifestAnnotations string
artifactType string
artifactRefs []string
pathValidationDisabled bool
verbose bool

Expand Down Expand Up @@ -72,6 +76,8 @@ Example - Push file to the HTTP registry:

cmd.Flags().StringVarP(&opts.manifestConfigRef, "manifest-config", "", "", "manifest config file")
cmd.Flags().StringVarP(&opts.manifestAnnotations, "manifest-annotations", "", "", "manifest annotation file")
cmd.Flags().StringVarP(&opts.artifactType, "artifact-type", "", "", "artifact type")
cmd.Flags().StringArrayVarP(&opts.artifactRefs, "artifact-reference", "", nil, "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 All @@ -91,11 +97,21 @@ func runPush(opts pushOptions) error {
ctx = ctxo.WithLoggerDiscarded(ctx)
}

// bake artifact
var pushOpts []oras.PushOpt
resolver := newResolver(opts.username, opts.password, opts.insecure, opts.plainHTTP, opts.configs...)
if opts.artifactType != "" {
manifests, err := loadReferences(ctx, resolver, opts.artifactRefs)
if err != nil {
return err
}
pushOpts = append(pushOpts, oras.AsArtifact(opts.artifactType, manifests...))
}

// load files
var (
annotations map[string]map[string]string
store = content.NewFileStore("")
pushOpts []oras.PushOpt
)
defer store.Close()
if opts.manifestAnnotations != "" {
Expand Down Expand Up @@ -130,7 +146,6 @@ func runPush(opts pushOptions) error {
}

// ready to push
resolver := newResolver(opts.username, opts.password, opts.insecure, opts.plainHTTP, opts.configs...)
pushOpts = append(pushOpts, oras.WithPushStatusTrack(os.Stdout))
desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...)
if err != nil {
Expand Down Expand Up @@ -183,3 +198,15 @@ 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)
}
return descs, nil
}
12 changes: 4 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ 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.20210317114357-17e18de6643b
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/aviral26/artifacts v0.0.0-20210301104922-9ca8c5490fcc
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.16 // indirect
github.com/Microsoft/hcsshim v0.8.14 // indirect
github.com/containerd/containerd v1.4.4
github.com/containerd/continuity v0.0.0-20201208142359-180525291bb7 // indirect
github.com/containerd/containerd v1.5.0-beta.3
github.com/docker/cli v20.10.5+incompatible
github.com/docker/distribution v0.0.0-20191216044856-a8371794149d
github.com/docker/docker v17.12.0-ce-rc1.0.20200618181300-9dc6525e6118+incompatible
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-20200916021946-9d10a670db1b
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.1
github.com/opencontainers/runc v0.1.1 // indirect
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
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
gotest.tools/v3 v3.0.3 // indirect
)
Loading

0 comments on commit f56d806

Please sign in to comment.