Skip to content

Commit

Permalink
Add a tag-only flag to publisher.
Browse files Browse the repository at this point in the history
  • Loading branch information
chhsia0 committed Mar 30, 2021
1 parent 2e28671 commit a615999
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/commands/options/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ import (

// TagsOptions holds the list of tags to tag the built image
type TagsOptions struct {
Tags []string
Tags []string
TagOnly bool
}

func AddTagsArg(cmd *cobra.Command, ta *TagsOptions) {
cmd.Flags().StringSliceVarP(&ta.Tags, "tags", "t", []string{"latest"},
"Which tags to use for the produced image instead of the default 'latest' tag.")
cmd.Flags().BoolVar(&ta.TagOnly, "tag-only", false,
"Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated.")
}
1 change: 1 addition & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option
publish.WithAuthFromKeychain(authn.DefaultKeychain),
publish.WithNamer(namer),
publish.WithTags(ta.Tags),
publish.WithTagOnly(ta.TagOnly),
publish.Insecure(lo.InsecureRegistry))
}()
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions pkg/publish/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package publish

import (
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -33,6 +34,7 @@ type defalt struct {
auth authn.Authenticator
namer Namer
tags []string
tagOnly bool
insecure bool
}

Expand All @@ -45,6 +47,7 @@ type defaultOpener struct {
auth authn.Authenticator
namer Namer
tags []string
tagOnly bool
insecure bool
}

Expand All @@ -63,12 +66,22 @@ func identity(in string) string { return in }
var defaultTags = []string{"latest"}

func (do *defaultOpener) Open() (Interface, error) {
if do.tagOnly {
if len(do.tags) != 1 {
return nil, errors.New("must specify exactly one tag to resolve images into tag-only references")
}
if do.tags[0] == defaultTags[0] {
return nil, errors.New("latest tag cannot be used in tag-only references")
}
}

return &defalt{
base: do.base,
t: do.t,
auth: do.auth,
namer: do.namer,
tags: do.tags,
tagOnly: do.tagOnly,
insecure: do.insecure,
}, nil
}
Expand Down Expand Up @@ -123,6 +136,15 @@ func (d *defalt) Publish(img v1.Image, s string) (name.Reference, error) {
}
}

if d.tagOnly {
// We already validated that there is a single tag tag (not latest).
tag, err := name.NewTag(fmt.Sprintf("%s/%s:%s", d.base, d.namer(s), d.tags[0]))
if err != nil {
return nil, err
}
return &tag, nil
}

h, err := img.Digest()
if err != nil {
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/publish/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,25 @@ func TestDefaultWithReleaseTag(t *testing.T) {
if _, ok := createdTags["v1.2.3"]; !ok {
t.Errorf("Tag v1.2.3 was not created.")
}

for tag := range createdTags {
delete(createdTags, tag)
}

def, err = NewDefault(repoName, WithTags([]string{releaseTag}), WithTagOnly(true))
if err != nil {
t.Errorf("NewDefault() = %v", err)
}
if d, err := def.Publish(img, importpath); err != nil {
t.Errorf("Publish() = %v", err)
} else if !strings.HasPrefix(d.String(), repoName) {
t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository)
} else if !strings.HasSuffix(d.Context().String(), strings.ToLower(importpath)) {
t.Errorf("Publish() = %v, wanted suffix %v", d.Context(), md5Hash(importpath))
} else if !strings.Contains(d.String(), releaseTag) {
t.Errorf("Publish() = %v, wanted tag included: %v", d.String(), releaseTag)
} else if strings.Contains(d.String(), "@sha256:") {
t.Errorf("Publish() = %v, wanted no digest", d.String())
}

}
8 changes: 8 additions & 0 deletions pkg/publish/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ func WithTags(tags []string) Option {
}
}

// WithTagOnly is a functional option for resolving images into tag-only references
func WithTagOnly(tagOnly bool) Option {
return func(i *defaultOpener) error {
i.tagOnly = tagOnly
return nil
}
}

func Insecure(b bool) Option {
return func(i *defaultOpener) error {
i.insecure = b
Expand Down

0 comments on commit a615999

Please sign in to comment.