-
Notifications
You must be signed in to change notification settings - Fork 181
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: add manifest fetch-config #540
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1bf788d
feat: add manifest fetch-config
yuehaoliang 443fb5c
Merge branch 'main' of https://github.com/oras-project/oras into yueh…
yuehaoliang e5e7326
refactor
yuehaoliang baf5928
support --output -
yuehaoliang ec29d6b
add unit test
yuehaoliang 895dd31
pull from upstream and resolve conflict
yuehaoliang ebd7de8
improve the fetchConfigDesc func
yuehaoliang 3bc296b
resolve review comments
yuehaoliang 113d026
resolve conflict in pull
yuehaoliang d2e2519
move IsImageManifest to descriptor package
yuehaoliang 1e9370c
modify IsImageManifest to take a descriptor as an input
yuehaoliang 66229b4
add platform option
yuehaoliang 98e2ee2
fix a bug for used platform variable
yuehaoliang eb49aba
Merge branch 'main' of https://github.com/oras-project/oras into yueh…
yuehaoliang 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
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,169 @@ | ||||||
/* | ||||||
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 manifest | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"encoding/json" | ||||||
"errors" | ||||||
"fmt" | ||||||
"os" | ||||||
|
||||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||||||
"github.com/spf13/cobra" | ||||||
"oras.land/oras-go/v2" | ||||||
"oras.land/oras-go/v2/content" | ||||||
oerrors "oras.land/oras/cmd/oras/internal/errors" | ||||||
"oras.land/oras/cmd/oras/internal/option" | ||||||
"oras.land/oras/internal/descriptor" | ||||||
) | ||||||
|
||||||
type fetchConfigOptions struct { | ||||||
option.Cache | ||||||
option.Common | ||||||
option.Descriptor | ||||||
option.Platform | ||||||
option.Pretty | ||||||
option.Remote | ||||||
|
||||||
outputPath string | ||||||
targetRef string | ||||||
} | ||||||
|
||||||
func fetchConfigCmd() *cobra.Command { | ||||||
var opts fetchConfigOptions | ||||||
cmd := &cobra.Command{ | ||||||
Use: "fetch-config [flag] name<:tag|@digest>", | ||||||
Short: "[Preview] Fetch the config of a manifest from a remote registry", | ||||||
Long: `[Preview] Fetch the config of a manifest from a remote registry | ||||||
|
||||||
** This command is in preview and under development. ** | ||||||
|
||||||
Example - Fetch the config: | ||||||
oras manifest fetch-config localhost:5000/hello:latest | ||||||
|
||||||
Example - Fetch the config of certain platform: | ||||||
oras manifest fetch-config --platform 'linux/arm/v5' localhost:5000/hello:latest | ||||||
|
||||||
Example - Fetch and print the prettified config: | ||||||
oras manifest fetch-config --pretty localhost:5000/hello:latest | ||||||
|
||||||
Example - Fetch the config and save it to a local file: | ||||||
oras manifest fetch-config --output config.json localhost:5000/hello:latest | ||||||
|
||||||
yuehaoliang marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Example - Fetch the descriptor of the config: | ||||||
oras manifest fetch-config --descriptor localhost:5000/hello:latest | ||||||
|
||||||
Example - Fetch and print the prettified descriptor of the config: | ||||||
oras manifest fetch-config --descriptor --pretty localhost:5000/hello:latest | ||||||
`, | ||||||
Args: cobra.ExactArgs(1), | ||||||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||||||
if opts.outputPath == "-" && opts.OutputDescriptor { | ||||||
return errors.New("`--output -` cannot be used with `--descriptor` at the same time") | ||||||
} | ||||||
|
||||||
return opts.ReadPassword() | ||||||
}, | ||||||
RunE: func(cmd *cobra.Command, args []string) error { | ||||||
opts.targetRef = args[0] | ||||||
return fetchConfig(opts) | ||||||
}, | ||||||
} | ||||||
|
||||||
cmd.Flags().StringVarP(&opts.outputPath, "output", "o", "", "output file path") | ||||||
option.ApplyFlags(&opts, cmd.Flags()) | ||||||
return cmd | ||||||
} | ||||||
|
||||||
func fetchConfig(opts fetchConfigOptions) (fetchErr error) { | ||||||
ctx, _ := opts.SetLoggerLevel() | ||||||
|
||||||
repo, err := opts.NewRepository(opts.targetRef, opts.Common) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if repo.Reference.Reference == "" { | ||||||
return oerrors.NewErrInvalidReference(repo.Reference) | ||||||
} | ||||||
|
||||||
targetPlatform, err := opts.Parse() | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
src, err := opts.CachedTarget(repo) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// fetch config descriptor | ||||||
configDesc, err := fetchConfigDesc(ctx, src, opts.targetRef, targetPlatform) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if !opts.OutputDescriptor || opts.outputPath != "" { | ||||||
// fetch config content | ||||||
contentBytes, err := content.FetchAll(ctx, src, configDesc) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
if opts.outputPath == "" || opts.outputPath == "-" { | ||||||
// output config content | ||||||
return opts.Output(os.Stdout, contentBytes) | ||||||
} | ||||||
|
||||||
// save config into the local file if the output path is provided | ||||||
if err = os.WriteFile(opts.outputPath, contentBytes, 0666); err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
|
||||||
if opts.OutputDescriptor { | ||||||
// output config's descriptor | ||||||
descBytes, err := json.Marshal(configDesc) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
return opts.Output(os.Stdout, descBytes) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func fetchConfigDesc(ctx context.Context, src oras.ReadOnlyTarget, reference string, targetPlatform *ocispec.Platform) (ocispec.Descriptor, error) { | ||||||
// fetch manifest descriptor and content | ||||||
fetchOpts := oras.DefaultFetchBytesOptions | ||||||
fetchOpts.TargetPlatform = targetPlatform | ||||||
manifestDesc, manifestContent, err := oras.FetchBytes(ctx, src, reference, oras.DefaultFetchBytesOptions) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
if err != nil { | ||||||
return ocispec.Descriptor{}, err | ||||||
} | ||||||
|
||||||
if !descriptor.IsImageManifest(manifestDesc) { | ||||||
return ocispec.Descriptor{}, fmt.Errorf("%q is not an image manifest and does not have a config", manifestDesc.Digest) | ||||||
} | ||||||
|
||||||
// unmarshal manifest content to extract config descriptor | ||||||
var manifest ocispec.Manifest | ||||||
if err := json.Unmarshal(manifestContent, &manifest); err != nil { | ||||||
return ocispec.Descriptor{}, err | ||||||
} | ||||||
return manifest.Config, 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,27 @@ | ||
/* | ||
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 descriptor | ||
|
||
import ( | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
|
||
"oras.land/oras/internal/docker" | ||
) | ||
|
||
// IsImageManifest checks whether a manifest is an image manifest. | ||
func IsImageManifest(desc ocispec.Descriptor) bool { | ||
return desc.MediaType == docker.MediaTypeManifest || desc.MediaType == ocispec.MediaTypeImageManifest | ||
} |
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,48 @@ | ||
/* | ||
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 descriptor_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
"oras.land/oras/internal/descriptor" | ||
) | ||
|
||
func TestIsImageManifest(t *testing.T) { | ||
imageDesc := ocispec.Descriptor{ | ||
MediaType: "application/vnd.oci.image.manifest.v1+json", | ||
Digest: "sha256:2e0e0fe1fb3edbcdddad941c90d2b51e25a6bcd593e82545441a216de7bfa834", | ||
Size: 474, | ||
} | ||
|
||
got := descriptor.IsImageManifest(imageDesc) | ||
if !reflect.DeepEqual(got, true) { | ||
t.Fatalf("IsImageManifest() got %v, want %v", got, true) | ||
} | ||
|
||
artifactDesc := ocispec.Descriptor{ | ||
MediaType: "application/vnd.cncf.oras.artifact.manifest.v1+json", | ||
Digest: "sha256:772fbebcda7e6937de01295bae28360afd463c2d5f1f7aca59a3ef267608bc66", | ||
Size: 568, | ||
} | ||
|
||
got = descriptor.IsImageManifest(artifactDesc) | ||
if !reflect.DeepEqual(got, false) { | ||
t.Fatalf("IsImageManifest() got %v, want %v", got, false) | ||
} | ||
} |
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.
There is a scenario missing:
oras manifest fetch-config --platform 'linux/arm/v5' localhost:5000/hello:latest
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.
Supported
option.Platform
.