-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Add manifest command #138
Merged
Merged
Add manifest command #138
Changes from all commits
Commits
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,93 @@ | ||
package manifest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/cli/cli" | ||
"github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/cli/manifest/store" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type annotateOptions struct { | ||
target string // the target manifest list name (also transaction ID) | ||
image string // the manifest to annotate within the list | ||
variant string // an architecture variant | ||
os string | ||
arch string | ||
osFeatures []string | ||
} | ||
|
||
// NewAnnotateCommand creates a new `docker manifest annotate` command | ||
func newAnnotateCommand(dockerCli command.Cli) *cobra.Command { | ||
var opts annotateOptions | ||
|
||
cmd := &cobra.Command{ | ||
Use: "annotate [OPTIONS] MANIFEST_LIST MANIFEST", | ||
Short: "Add additional information to a local image manifest", | ||
Args: cli.ExactArgs(2), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
opts.target = args[0] | ||
opts.image = args[1] | ||
return runManifestAnnotate(dockerCli, opts) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
|
||
flags.StringVar(&opts.os, "os", "", "Set operating system") | ||
flags.StringVar(&opts.arch, "arch", "", "Set architecture") | ||
flags.StringSliceVar(&opts.osFeatures, "os-features", []string{}, "Set operating system feature") | ||
flags.StringVar(&opts.variant, "variant", "", "Set architecture variant") | ||
|
||
return cmd | ||
} | ||
|
||
func runManifestAnnotate(dockerCli command.Cli, opts annotateOptions) error { | ||
targetRef, err := normalizeReference(opts.target) | ||
if err != nil { | ||
return errors.Wrapf(err, "annotate: error parsing name for manifest list %s", opts.target) | ||
} | ||
imgRef, err := normalizeReference(opts.image) | ||
if err != nil { | ||
return errors.Wrapf(err, "annotate: error parsing name for manifest %s", opts.image) | ||
} | ||
|
||
manifestStore := dockerCli.ManifestStore() | ||
imageManifest, err := manifestStore.Get(targetRef, imgRef) | ||
switch { | ||
case store.IsNotFound(err): | ||
return fmt.Errorf("manifest for image %s does not exist in %s", opts.image, opts.target) | ||
case err != nil: | ||
return err | ||
} | ||
|
||
// Update the mf | ||
if opts.os != "" { | ||
imageManifest.Platform.OS = opts.os | ||
} | ||
if opts.arch != "" { | ||
imageManifest.Platform.Architecture = opts.arch | ||
} | ||
for _, osFeature := range opts.osFeatures { | ||
imageManifest.Platform.OSFeatures = appendIfUnique(imageManifest.Platform.OSFeatures, osFeature) | ||
} | ||
if opts.variant != "" { | ||
imageManifest.Platform.Variant = opts.variant | ||
} | ||
|
||
if !isValidOSArch(imageManifest.Platform.OS, imageManifest.Platform.Architecture) { | ||
return errors.Errorf("manifest entry for image has unsupported os/arch combination: %s/%s", opts.os, opts.arch) | ||
} | ||
return manifestStore.Save(targetRef, imgRef, imageManifest) | ||
} | ||
|
||
func appendIfUnique(list []string, str string) []string { | ||
for _, s := range list { | ||
if s == str { | ||
return list | ||
} | ||
} | ||
return append(list, str) | ||
} |
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,78 @@ | ||
package manifest | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/docker/cli/internal/test" | ||
"github.com/docker/cli/internal/test/testutil" | ||
"github.com/gotestyourself/gotestyourself/golden" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestManifestAnnotateError(t *testing.T) { | ||
testCases := []struct { | ||
args []string | ||
expectedError string | ||
}{ | ||
{ | ||
args: []string{"too-few-arguments"}, | ||
expectedError: "requires exactly 2 arguments", | ||
}, | ||
{ | ||
args: []string{"th!si'sa/fa!ke/li$t/name", "example.com/alpine:3.0"}, | ||
expectedError: "error parsing name for manifest list", | ||
}, | ||
{ | ||
args: []string{"example.com/list:v1", "th!si'sa/fa!ke/im@ge/nam32"}, | ||
expectedError: "error parsing name for manifest", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
cli := test.NewFakeCli(nil) | ||
cmd := newAnnotateCommand(cli) | ||
cmd.SetArgs(tc.args) | ||
cmd.SetOutput(ioutil.Discard) | ||
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError) | ||
} | ||
} | ||
|
||
func TestManifestAnnotate(t *testing.T) { | ||
store, cleanup := newTempManifestStore(t) | ||
defer cleanup() | ||
|
||
cli := test.NewFakeCli(nil) | ||
cli.SetManifestStore(store) | ||
namedRef := ref(t, "alpine:3.0") | ||
imageManifest := fullImageManifest(t, namedRef) | ||
err := store.Save(ref(t, "list:v1"), namedRef, imageManifest) | ||
require.NoError(t, err) | ||
|
||
cmd := newAnnotateCommand(cli) | ||
cmd.SetArgs([]string{"example.com/list:v1", "example.com/fake:0.0"}) | ||
cmd.SetOutput(ioutil.Discard) | ||
expectedError := "manifest for image example.com/fake:0.0 does not exist" | ||
testutil.ErrorContains(t, cmd.Execute(), expectedError) | ||
|
||
cmd.SetArgs([]string{"example.com/list:v1", "example.com/alpine:3.0"}) | ||
cmd.Flags().Set("os", "freebsd") | ||
cmd.Flags().Set("arch", "fake") | ||
cmd.Flags().Set("os-features", "feature1") | ||
cmd.Flags().Set("variant", "v7") | ||
expectedError = "manifest entry for image has unsupported os/arch combination" | ||
testutil.ErrorContains(t, cmd.Execute(), expectedError) | ||
|
||
cmd.Flags().Set("arch", "arm") | ||
require.NoError(t, cmd.Execute()) | ||
|
||
cmd = newInspectCommand(cli) | ||
err = cmd.Flags().Set("verbose", "true") | ||
require.NoError(t, err) | ||
cmd.SetArgs([]string{"example.com/list:v1", "example.com/alpine:3.0"}) | ||
require.NoError(t, cmd.Execute()) | ||
actual := cli.OutBuffer() | ||
expected := golden.Get(t, "inspect-annotate.golden") | ||
assert.Equal(t, string(expected), actual.String()) | ||
} |
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,46 @@ | ||
package manifest | ||
|
||
import ( | ||
manifesttypes "github.com/docker/cli/cli/manifest/types" | ||
"github.com/docker/cli/cli/registry/client" | ||
"github.com/docker/distribution" | ||
"github.com/docker/distribution/reference" | ||
"github.com/opencontainers/go-digest" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
type fakeRegistryClient struct { | ||
client.RegistryClient | ||
getManifestFunc func(ctx context.Context, ref reference.Named) (manifesttypes.ImageManifest, error) | ||
getManifestListFunc func(ctx context.Context, ref reference.Named) ([]manifesttypes.ImageManifest, error) | ||
mountBlobFunc func(ctx context.Context, source reference.Canonical, target reference.Named) error | ||
putManifestFunc func(ctx context.Context, source reference.Named, mf distribution.Manifest) (digest.Digest, error) | ||
} | ||
|
||
func (c *fakeRegistryClient) GetManifest(ctx context.Context, ref reference.Named) (manifesttypes.ImageManifest, error) { | ||
if c.getManifestFunc != nil { | ||
return c.getManifestFunc(ctx, ref) | ||
} | ||
return manifesttypes.ImageManifest{}, nil | ||
} | ||
|
||
func (c *fakeRegistryClient) GetManifestList(ctx context.Context, ref reference.Named) ([]manifesttypes.ImageManifest, error) { | ||
if c.getManifestListFunc != nil { | ||
return c.getManifestListFunc(ctx, ref) | ||
} | ||
return nil, nil | ||
} | ||
|
||
func (c *fakeRegistryClient) MountBlob(ctx context.Context, source reference.Canonical, target reference.Named) error { | ||
if c.mountBlobFunc != nil { | ||
return c.mountBlobFunc(ctx, source, target) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *fakeRegistryClient) PutManifest(ctx context.Context, ref reference.Named, mf distribution.Manifest) (digest.Digest, error) { | ||
if c.putManifestFunc != nil { | ||
return c.putManifestFunc(ctx, ref, mf) | ||
} | ||
return digest.Digest(""), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package manifest | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/cli/cli" | ||
"github.com/docker/cli/cli/command" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// NewManifestCommand returns a cobra command for `manifest` subcommands | ||
func NewManifestCommand(dockerCli command.Cli) *cobra.Command { | ||
// use dockerCli as command.Cli | ||
cmd := &cobra.Command{ | ||
Use: "manifest COMMAND", | ||
Short: "Manage Docker image manifests and manifest lists", | ||
Long: manifestDescription, | ||
Args: cli.NoArgs, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Fprintf(dockerCli.Err(), "\n"+cmd.UsageString()) | ||
}, | ||
} | ||
cmd.AddCommand( | ||
newCreateListCommand(dockerCli), | ||
newInspectCommand(dockerCli), | ||
newAnnotateCommand(dockerCli), | ||
newPushListCommand(dockerCli), | ||
) | ||
return cmd | ||
} | ||
|
||
var manifestDescription = ` | ||
The **docker manifest** command has subcommands for managing image manifests and | ||
manifest lists. A manifest list allows you to use one name to refer to the same image | ||
built for multiple architectures. | ||
|
||
To see help for a subcommand, use: | ||
|
||
docker manifest CMD --help | ||
|
||
For full details on using docker manifest lists, see the registry v2 specification. | ||
|
||
` |
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.
It seems like maybe we are missing the simple
docker manifest list
command? I looke for it to check that it supported filters and formats, and can't find it at all.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.
are you looking for
docker manifest list ...
?p.s. sorry for the delay on responding to your comments! i've been saving up the doc stuff