Skip to content
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 a tag-only flag to publisher. #332

Merged
merged 1 commit into from
May 17, 2021
Merged
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
4 changes: 4 additions & 0 deletions pkg/commands/options/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
// PublishOptions encapsulates options when publishing.
type PublishOptions struct {
Tags []string
// TagOnly resolves images into tag-only references.
TagOnly bool

// Push publishes images to a registry.
Push bool
Expand All @@ -49,6 +51,8 @@ func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"},
"Which tags to use for the produced image instead of the default 'latest' tag "+
"(may not work properly with --base-import-paths or --bare).")
cmd.Flags().BoolVar(&po.TagOnly, "tag-only", false,
"Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated.")

cmd.Flags().BoolVar(&po.Push, "push", true, "Push images to KO_DOCKER_REPO")

Expand Down
1 change: 1 addition & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
publish.WithAuthFromKeychain(authn.DefaultKeychain),
publish.WithNamer(namer),
publish.WithTags(po.Tags),
publish.WithTagOnly(po.TagOnly),
publish.Insecure(po.InsecureRegistry))
if err != nil {
return nil, err
Expand Down
22 changes: 22 additions & 0 deletions pkg/publish/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package publish

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

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

Expand All @@ -69,13 +72,23 @@ func identity(base, in string) string { return path.Join(base, 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,
userAgent: do.userAgent,
auth: do.auth,
namer: do.namer,
tags: do.tags,
tagOnly: do.tagOnly,
insecure: do.insecure,
}, nil
}
Expand Down Expand Up @@ -156,6 +169,15 @@ func (d *defalt) Publish(ctx context.Context, br build.Result, s string) (name.R
}
}

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

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

def, err = NewDefault(repoName, WithTags([]string{releaseTag}), WithTagOnly(true))
if err != nil {
t.Errorf("NewDefault() = %v", err)
}
if d, err := def.Publish(context.Background(), img, build.StrictScheme+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 @@ -94,6 +94,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