Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions cmd/crane/cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
// NewCmdTag creates a new cobra.Command for the tag subcommand.
func NewCmdTag(options *[]crane.Option) *cobra.Command {
return &cobra.Command{
Use: "tag IMG TAG",
Use: "tag IMG TAG [TAG...]",
Short: "Efficiently tag a remote image",
Long: `Tag remote image without downloading it.

Expand All @@ -34,13 +34,22 @@ crane cp registry.example.com/library/ubuntu:v0 registry.example.com/library/ubu
crane tag registry.example.com/library/ubuntu:v0 v1
` + "```" + `

2. We can skip layer existence checks because we know the manifest already exists. This makes "tag" slightly faster than "copy".`,
2. We can skip layer existence checks because we know the manifest already exists. This makes "tag" slightly faster than "copy".

You can also specify multiple tags to apply to the same image:
` + "```" + `
crane tag registry.example.com/library/ubuntu:v0 v1 v2 latest
` + "```" + ``,
Example: `# Add a v1 tag to ubuntu
crane tag ubuntu v1`,
Args: cobra.ExactArgs(2),
crane tag ubuntu v1

# Add multiple tags to ubuntu
crane tag ubuntu v1 v2 latest`,
Args: cobra.MinimumNArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
img, tag := args[0], args[1]
return crane.Tag(img, tag, *options...)
img := args[0]
tags := args[1:]
return crane.TagMultiple(img, tags, *options...)
},
}
}
10 changes: 9 additions & 1 deletion cmd/crane/doc/crane_tag.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions pkg/crane/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (

// Tag adds tag to the remote img.
func Tag(img, tag string, opt ...Option) error {
return TagMultiple(img, []string{tag}, opt...)
}

// TagMultiple adds one or more tags to the remote img.
func TagMultiple(img string, tags []string, opt ...Option) error {
o := makeOptions(opt...)
ref, err := name.ParseReference(img, o.Name...)
if err != nil {
Expand All @@ -33,7 +38,16 @@ func Tag(img, tag string, opt ...Option) error {
return fmt.Errorf("fetching %q: %w", img, err)
}

dst := ref.Context().Tag(tag)
// Apply each tag
for i, tag := range tags {
dst := ref.Context().Tag(tag)
if err := remote.Tag(dst, desc, o.Remote...); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nice thing about tagging being done on only one tag is that we know exactly whether to continue or fail when tagging fails. When there are multiple tags requested and one fails, should we:

  1. fail right away
  2. try to undo the successful tags?
  3. keep trying to apply more tags?

I don't have a super strong opinion, and failing right away like this is probably fine, but with single-tagging it's up to the caller what to do. I guess it's still up to them, and they could just keep using single-tagging.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that's what I was thinking about here

if i > 0 {
return fmt.Errorf("tagging %q with %q failed (successfully tagged with %v): %w", img, tag, tags[:i], err)
}
return fmt.Errorf("tagging %q with %q: %w", img, tag, err)
}
}

return remote.Tag(dst, desc, o.Remote...)
return nil
}
Loading