-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support push a blob to a remote registry #489
Merged
+440
−3
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
654ac4f
feat: support push a blob to a remote registry
lizMSFT 3391f71
[PRFix move func PrepareBlob to internal package, add exists check on…
lizMSFT c383469
[PRFix add test]
lizMSFT e13e539
[PRFix modify command msg]
lizMSFT 366ba0f
[PRFix modify test and use display.PrintStatus]
lizMSFT 7e1326c
[PRFix modify print message]
lizMSFT 2063c0d
[PRFix add new flags]
lizMSFT 5c63692
[PRFix rebase]
lizMSFT 4f5d0a8
[PRFix add ut]
lizMSFT af09a19
[PRFix rebase]
lizMSFT 9bf3990
[PRFix fix comments]
lizMSFT 479aa6f
[PRFix add an empty line]
lizMSFT d1606bd
[PRFix add --size and --mediatype flags, support push with digest]
lizMSFT 1b47d78
Merge branch 'main' into liz/476
lizMSFT b146c68
[PRFix fix comments]
lizMSFT 734dc78
[PRFix fix comments]
lizMSFT 5de8d5a
[PRFix add digest check and ut]
lizMSFT e37222e
[PRFix validate input size if provided]
lizMSFT dc00d70
[PRFix add size check in prerun]
lizMSFT c8b5c29
[PRFix fix test]
lizMSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ func Cmd() *cobra.Command { | |
|
||
cmd.AddCommand( | ||
fetchCmd(), | ||
pushCmd(), | ||
) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package blob | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"github.com/spf13/cobra" | ||
"oras.land/oras/cmd/oras/internal/display" | ||
"oras.land/oras/cmd/oras/internal/option" | ||
"oras.land/oras/internal/file" | ||
) | ||
|
||
type pushBlobOptions struct { | ||
option.Common | ||
option.Descriptor | ||
option.Pretty | ||
option.Remote | ||
|
||
fileRef string | ||
mediaType string | ||
size int64 | ||
targetRef string | ||
} | ||
|
||
func pushCmd() *cobra.Command { | ||
var opts pushBlobOptions | ||
cmd := &cobra.Command{ | ||
Use: "push [flags] name[@digest] file", | ||
Short: "[Preview] Push a blob to a remote registry", | ||
Long: `[Preview] Push a blob to a remote registry | ||
|
||
** This command is in preview and under development. ** | ||
|
||
Example - Push blob "hi.txt": | ||
oras blob push localhost:5000/hello hi.txt | ||
|
||
Example - Push blob "hi.txt" with the specific digest: | ||
oras blob push localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 hi.txt | ||
|
||
Example - Push blob from stdin with blob size and digest: | ||
oras blob push --size 12 localhost:5000/hello@sha256:9a201d228ebd966211f7d1131be19f152be428bd373a92071c71d8deaf83b3e5 - | ||
|
||
Example - Push blob "hi.txt" and output the descriptor: | ||
oras blob push --descriptor localhost:5000/hello hi.txt | ||
|
||
Example - Push blob "hi.txt" with the specific returned media type in the descriptor: | ||
oras blob push --media-type application/vnd.oci.image.config.v1+json --descriptor localhost:5000/hello hi.txt | ||
|
||
Example - Push blob "hi.txt" and output the prettified descriptor: | ||
oras blob push --descriptor --pretty localhost:5000/hello hi.txt | ||
|
||
Example - Push blob without TLS: | ||
oras blob push --insecure localhost:5000/hello hi.txt | ||
`, | ||
Args: cobra.ExactArgs(2), | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
opts.targetRef = args[0] | ||
opts.fileRef = args[1] | ||
if opts.fileRef == "-" { | ||
if opts.PasswordFromStdin { | ||
return errors.New("`-` read file from input and `--password-stdin` read password from input cannot be both used") | ||
} | ||
if opts.size < 0 { | ||
return errors.New("`--size` must be provided if the blob is read from stdin") | ||
} | ||
} | ||
return opts.ReadPassword() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return pushBlob(opts) | ||
}, | ||
} | ||
|
||
cmd.Flags().Int64VarP(&opts.size, "size", "", -1, "provide the blob size") | ||
cmd.Flags().StringVarP(&opts.mediaType, "media-type", "", ocispec.MediaTypeImageLayer, "specify the returned media type in the descriptor if `--descriptor` is used") | ||
option.ApplyFlags(&opts, cmd.Flags()) | ||
return cmd | ||
} | ||
|
||
func pushBlob(opts pushBlobOptions) (err error) { | ||
ctx, _ := opts.SetLoggerLevel() | ||
|
||
repo, err := opts.NewRepository(opts.targetRef, opts.Common) | ||
lizMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
// prepare blob content | ||
desc, rc, err := file.PrepareContent(opts.fileRef, opts.mediaType, repo.Reference.Reference, opts.size) | ||
if err != nil { | ||
return err | ||
} | ||
defer rc.Close() | ||
|
||
exists, err := repo.Exists(ctx, desc) | ||
if err != nil { | ||
return err | ||
} | ||
verbose := opts.Verbose && !opts.OutputDescriptor | ||
if exists { | ||
if err := display.PrintStatus(desc, "Exists", verbose); err != nil { | ||
return err | ||
} | ||
} else { | ||
if err := display.PrintStatus(desc, "Uploading", verbose); err != nil { | ||
return err | ||
} | ||
if err = repo.Push(ctx, desc, rc); err != nil { | ||
return err | ||
} | ||
lizMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err := display.PrintStatus(desc, "Uploaded ", verbose); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// outputs blob's descriptor | ||
if opts.OutputDescriptor { | ||
descJSON, err := opts.Marshal(desc) | ||
if err != nil { | ||
return err | ||
} | ||
return opts.Output(os.Stdout, descJSON) | ||
} | ||
|
||
fmt.Println("Pushed", opts.targetRef) | ||
fmt.Println("Digest:", desc.Digest) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
Copyright The ORAS Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package file | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
digest "github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
// PrepareContent prepares the content descriptor from the file path or stdin. | ||
// Use the input digest and size if they are provided. Will return error if the | ||
// content is from stdin but the content digest and size are missing. | ||
func PrepareContent(path string, mediaType string, dgstStr string, size int64) (desc ocispec.Descriptor, rc io.ReadCloser, prepareErr error) { | ||
if path == "" { | ||
return ocispec.Descriptor{}, nil, errors.New("missing file name") | ||
} | ||
|
||
// validate digest | ||
var dgst digest.Digest | ||
if dgstStr != "" { | ||
var err error | ||
dgst, err = digest.Parse(dgstStr) | ||
if err != nil { | ||
return ocispec.Descriptor{}, nil, fmt.Errorf("invalid digest %s: %w", dgstStr, err) | ||
} | ||
} | ||
|
||
// prepares the content descriptor from stdin | ||
if path == "-" { | ||
// throw err if size or digest is not provided. | ||
if size < 0 { | ||
return ocispec.Descriptor{}, nil, errors.New("content size must be provided if it is read from stdin") | ||
} | ||
if dgst == "" { | ||
return ocispec.Descriptor{}, nil, errors.New("content digest must be provided if it is read from stdin") | ||
} | ||
return ocispec.Descriptor{ | ||
MediaType: mediaType, | ||
Digest: dgst, | ||
Size: size, | ||
}, os.Stdin, nil | ||
} | ||
|
||
file, err := os.Open(path) | ||
if err != nil { | ||
return ocispec.Descriptor{}, nil, fmt.Errorf("failed to open %s: %w", path, err) | ||
} | ||
defer func() { | ||
if prepareErr != nil { | ||
file.Close() | ||
} | ||
}() | ||
|
||
fi, err := file.Stat() | ||
if err != nil { | ||
return ocispec.Descriptor{}, nil, fmt.Errorf("failed to stat %s: %w", path, err) | ||
} | ||
actualSize := fi.Size() | ||
if size >= 0 && size != actualSize { | ||
return ocispec.Descriptor{}, nil, fmt.Errorf("input size %d does not match the actual content size %d", size, actualSize) | ||
} | ||
|
||
if dgst == "" { | ||
dgst, err = digest.FromReader(file) | ||
if err != nil { | ||
lizMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ocispec.Descriptor{}, nil, err | ||
} | ||
if _, err = file.Seek(0, io.SeekStart); err != nil { | ||
lizMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ocispec.Descriptor{}, nil, err | ||
} | ||
} | ||
|
||
return ocispec.Descriptor{ | ||
MediaType: mediaType, | ||
Digest: dgst, | ||
Size: actualSize, | ||
}, file, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@FeynmanZhou @yizha1 What's the default media type for blobs? Currently, it is
ocispec.MediaTypeImageLayer
, which is consistent withoras push
. We need to review it later.