From d24b60a88f1afe586ab07b312453da921b44cc51 Mon Sep 17 00:00:00 2001 From: jonjohnsonjr Date: Fri, 13 Dec 2019 15:08:52 -0800 Subject: [PATCH] Set UA to something ko-specific (#116) --- cmd/ko/main.go | 3 ++- pkg/commands/config.go | 4 +++- pkg/commands/resolver.go | 31 +++++++++++++++++++++++++++++-- pkg/commands/version.go | 14 +++++++++----- pkg/publish/default.go | 1 + 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/cmd/ko/main.go b/cmd/ko/main.go index e04c454818..9deddec298 100644 --- a/cmd/ko/main.go +++ b/cmd/ko/main.go @@ -15,9 +15,10 @@ package main import ( - "github.com/google/ko/pkg/commands" "log" + "github.com/google/ko/pkg/commands" + "github.com/spf13/cobra" ) diff --git a/pkg/commands/config.go b/pkg/commands/config.go index e9327e9260..11b9991895 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -42,7 +42,9 @@ func getBaseImage(s string) (v1.Image, error) { ref = defaultBaseImage } log.Printf("Using base %s for %s", ref, s) - return remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain)) + return remote.Image(ref, + remote.WithTransport(defaultTransport()), + remote.WithAuthFromKeychain(authn.DefaultKeychain)) } func getCreationTime() (*v1.Time, error) { diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index 504d1dc5ff..773a6d3b86 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -15,13 +15,14 @@ package commands import ( - "context" "bytes" + "context" "errors" "fmt" "io" "io/ioutil" "log" + "net/http" "os" "sync" @@ -36,6 +37,31 @@ 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 { @@ -104,6 +130,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option } return publish.NewDefault(repoName, + publish.WithTransport(defaultTransport()), publish.WithAuthFromKeychain(authn.DefaultKeychain), publish.WithNamer(namer), publish.WithTags(ta.Tags), @@ -121,7 +148,7 @@ func makePublisher(no *options.NameOptions, lo *options.LocalOptions, ta *option type resolvedFuture chan []byte func resolveFilesToWriter( - ctx context.Context, + ctx context.Context, builder *build.Caching, publisher publish.Interface, fo *options.FilenameOptions, diff --git a/pkg/commands/version.go b/pkg/commands/version.go index dd349694fa..8416c398ad 100644 --- a/pkg/commands/version.go +++ b/pkg/commands/version.go @@ -30,19 +30,23 @@ func addVersion(topLevel *cobra.Command) { Use: "version", Short: `Print ko version.`, Run: func(cmd *cobra.Command, args []string) { - version() + v := version() + if v == "" { + fmt.Println("could not determine build information") + } else { + fmt.Println(v) + } }, }) } -func version() { +func version() string { if Version == "" { i, ok := debug.ReadBuildInfo() if !ok { - fmt.Println("could not determine build information") - return + return "" } Version = i.Main.Version } - fmt.Println(Version) + return Version } diff --git a/pkg/publish/default.go b/pkg/publish/default.go index 3eedb22858..8d5cf33c83 100644 --- a/pkg/publish/default.go +++ b/pkg/publish/default.go @@ -89,6 +89,7 @@ func NewDefault(base string, options ...Option) (Interface, error) { return nil, err } } + return do.Open() }