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

Use remote.WithUserAgent where possible #294

Merged
merged 1 commit into from
Jan 18, 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
2 changes: 1 addition & 1 deletion pkg/commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func getBaseImage(platform string) build.GetBase {
}
ropt := []remote.Option{
remote.WithAuthFromKeychain(authn.DefaultKeychain),
remote.WithTransport(defaultTransport()),
remote.WithUserAgent(ua()),
remote.WithContext(ctx),
}

Expand Down
21 changes: 1 addition & 20 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
Expand All @@ -40,31 +39,13 @@ import (
"k8s.io/apimachinery/pkg/labels"
)

func defaultTransport() http.RoundTripper {
return &useragentTransport{
inner: http.DefaultTransport,
useragent: ua(),
}
}

type useragentTransport struct {
useragent string
inner http.RoundTripper
}

func ua() string {
if v := version(); v != "" {
return "ko/" + v
}
return "ko"
}

// RoundTrip implements http.RoundTripper
func (ut *useragentTransport) RoundTrip(in *http.Request) (*http.Response, error) {
in.Header.Set("User-Agent", ut.useragent)
return ut.inner.RoundTrip(in)
}

func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
creationTime, err := getCreationTime()
if err != nil {
Expand Down Expand Up @@ -178,7 +159,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
}
if po.Push {
dp, err := publish.NewDefault(repoName,
publish.WithTransport(defaultTransport()),
publish.WithUserAgent(ua()),
publish.WithAuthFromKeychain(authn.DefaultKeychain),
publish.WithNamer(namer),
publish.WithTags(po.Tags),
Expand Down
52 changes: 28 additions & 24 deletions pkg/publish/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,26 @@ import (

// defalt is intentionally misspelled to avoid keyword collision (and drive Jon nuts).
type defalt struct {
base string
t http.RoundTripper
auth authn.Authenticator
namer Namer
tags []string
insecure bool
base string
t http.RoundTripper
userAgent string
auth authn.Authenticator
namer Namer
tags []string
insecure bool
}

// Option is a functional option for NewDefault.
type Option func(*defaultOpener) error

type defaultOpener struct {
base string
t http.RoundTripper
auth authn.Authenticator
namer Namer
tags []string
insecure bool
base string
t http.RoundTripper
userAgent string
auth authn.Authenticator
namer Namer
tags []string
insecure bool
}

// Namer is a function from a supported import path to the portion of the resulting
Expand All @@ -68,24 +70,26 @@ var defaultTags = []string{"latest"}

func (do *defaultOpener) Open() (Interface, error) {
return &defalt{
base: do.base,
t: do.t,
auth: do.auth,
namer: do.namer,
tags: do.tags,
insecure: do.insecure,
base: do.base,
t: do.t,
userAgent: do.userAgent,
auth: do.auth,
namer: do.namer,
tags: do.tags,
insecure: do.insecure,
}, nil
}

// NewDefault returns a new publish.Interface that publishes references under the provided base
// repository using the default keychain to authenticate and the default naming scheme.
func NewDefault(base string, options ...Option) (Interface, error) {
do := &defaultOpener{
base: base,
t: http.DefaultTransport,
auth: authn.Anonymous,
namer: identity,
tags: defaultTags,
base: base,
t: http.DefaultTransport,
userAgent: "ko",
auth: authn.Anonymous,
namer: identity,
tags: defaultTags,
}

for _, option := range options {
Expand Down Expand Up @@ -127,7 +131,7 @@ func (d *defalt) Publish(ctx context.Context, br build.Result, s string) (name.R
// https://github.com/google/go-containerregistry/issues/212
s = strings.ToLower(s)

ro := []remote.Option{remote.WithAuth(d.auth), remote.WithTransport(d.t), remote.WithContext(ctx)}
ro := []remote.Option{remote.WithAuth(d.auth), remote.WithTransport(d.t), remote.WithContext(ctx), remote.WithUserAgent(d.userAgent)}
no := []name.Option{}
if d.insecure {
no = append(no, name.Insecure)
Expand Down
9 changes: 9 additions & 0 deletions pkg/publish/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func WithTransport(t http.RoundTripper) Option {
}
}

// WithUserAgent is a functional option for overriding the User-Agent
// on a default publisher.
func WithUserAgent(ua string) Option {
return func(i *defaultOpener) error {
i.userAgent = ua
return nil
}
}

// WithAuth is a functional option for overriding the default authenticator
// on a default publisher.
func WithAuth(auth authn.Authenticator) Option {
Expand Down