Skip to content

Commit

Permalink
export manifest on push
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Mar 29, 2021
1 parent 8d49927 commit 0e3c1f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
12 changes: 12 additions & 0 deletions cmd/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type pushOptions struct {
fileRefs []string
manifestConfigRef string
manifestAnnotations string
manifestExport string
artifactType string
artifactRefs []string
pathValidationDisabled bool
Expand Down Expand Up @@ -76,6 +77,7 @@ 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.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().BoolVarP(&opts.pathValidationDisabled, "disable-path-validation", "", false, "skip path validation")
Expand Down Expand Up @@ -145,6 +147,16 @@ func runPush(opts pushOptions) error {
fmt.Println("Uploading empty artifact")
}

// export manifest
if opts.manifestExport != "" {
manifestFile, err := os.Create(opts.manifestExport)
if err != nil {
return err
}
defer manifestFile.Close()
pushOpts = append(pushOpts, oras.WithManifestWriter(manifestFile))
}

// ready to push
pushOpts = append(pushOpts, oras.WithPushStatusTrack(os.Stdout))
desc, err := oras.Push(ctx, resolver, opts.targetRef, store, files, pushOpts...)
Expand Down
18 changes: 13 additions & 5 deletions pkg/oras/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ func pack(provider content.Provider, descriptors []ocispec.Descriptor, opts *pus
}

// Manifest
var desc ocispec.Descriptor
var err error
var mediaType string
var content interface{}
if opts.artifact != nil {
artifact := *opts.artifact
if config != nil {
Expand All @@ -97,20 +97,28 @@ func pack(provider content.Provider, descriptors []ocispec.Descriptor, opts *pus
}
artifact.Blobs = convertV1DescriptorsToV2(descriptors)
artifact.Annotations = opts.manifestAnnotations
desc, err = store.SetObject(artifact.MediaType, artifact)
mediaType = artifact.MediaType
content = artifact
} else {
desc, err = store.SetObject(ocispec.MediaTypeImageManifest, ocispec.Manifest{
mediaType = ocispec.MediaTypeImageManifest
content = ocispec.Manifest{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
Config: *config,
Layers: descriptors,
Annotations: opts.manifestAnnotations,
})
}
}
desc, contentBytes, err := store.SetObject(mediaType, content)
if err != nil {
return ocispec.Descriptor{}, nil, err
}
if opts.manifestWriter != nil {
if _, err := opts.manifestWriter.Write(contentBytes); err != nil {
return ocispec.Descriptor{}, nil, err
}
}

return desc, store, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/oras/push_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type pushOpts struct {
configAnnotations map[string]string
manifest *ocispec.Descriptor
manifestAnnotations map[string]string
manifestWriter io.Writer
validateName func(desc ocispec.Descriptor) error
baseHandlers []images.Handler
artifact *artifactspec.Artifact
Expand Down Expand Up @@ -77,6 +78,14 @@ func WithManifestAnnotations(annotations map[string]string) PushOpt {
}
}

// WithManifestWriter exports the pushed manifest
func WithManifestWriter(writer io.Writer) PushOpt {
return func(o *pushOpts) error {
o.manifestWriter = writer
return nil
}
}

// WithNameValidation validates the image title in the descriptor.
// Pass nil to disable name validation.
func WithNameValidation(validate func(desc ocispec.Descriptor) error) PushOpt {
Expand Down
6 changes: 3 additions & 3 deletions pkg/oras/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ func (s *hybridStore) Set(desc ocispec.Descriptor, content []byte) {
s.cache.Set(desc, content)
}

func (s *hybridStore) SetObject(mediaType string, object interface{}) (ocispec.Descriptor, error) {
func (s *hybridStore) SetObject(mediaType string, object interface{}) (ocispec.Descriptor, []byte, error) {
content, err := json.Marshal(object)
if err != nil {
return ocispec.Descriptor{}, err
return ocispec.Descriptor{}, nil, err
}
desc := ocispec.Descriptor{
MediaType: mediaType,
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}
s.Set(desc, content)
return desc, nil
return desc, content, nil
}

// ReaderAt provides contents
Expand Down

0 comments on commit 0e3c1f4

Please sign in to comment.