Set a default user agent on all client.Client calls

Form:
  kube-controller-manager/v0.10.0 (linux/amd64) kubernetes/550b98e

  <basename(os.Argv)>/<gitVersion> (<GOOS>/<GOARCH>) kubernetes/<shortGitCommit>

Can be set by other clients
This commit is contained in:
Clayton Coleman
2015-02-04 16:35:53 -05:00
parent 550b98ebf4
commit 0307d7ba98
7 changed files with 132 additions and 185 deletions

View File

@@ -21,8 +21,10 @@ import (
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"reflect"
gruntime "runtime"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
@@ -68,6 +70,9 @@ type Config struct {
// certificate. For testing only.
Insecure bool
// UserAgent is an optional field that specifies the caller of this request.
UserAgent string
// Transport may be used for custom HTTP behavior. This attribute may not
// be specified with the TLS client certificate options.
Transport http.RoundTripper
@@ -151,6 +156,9 @@ func SetKubernetesDefaults(config *Config) error {
if config.Prefix == "" {
config.Prefix = "/api"
}
if len(config.UserAgent) == 0 {
config.UserAgent = DefaultKubernetesUserAgent()
}
if len(config.Version) == 0 {
config.Version = defaultVersionFor(config)
}
@@ -252,6 +260,9 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip
case hasBasicAuth:
rt = NewBasicAuthRoundTripper(config.Username, config.Password, rt)
}
if len(config.UserAgent) > 0 {
rt = NewUserAgentRoundTripper(config.UserAgent, rt)
}
return rt, nil
}
@@ -353,3 +364,18 @@ func defaultVersionFor(config *Config) string {
}
return version
}
// DefaultKubernetesUserAgent returns the default user agent that clients can use.
func DefaultKubernetesUserAgent() string {
commit := version.Get().GitCommit
if len(commit) > 7 {
commit = commit[:7]
}
if len(commit) == 0 {
commit = "unknown"
}
version := version.Get().GitVersion
seg := strings.SplitN(version, "-", 2)
version = seg[0]
return fmt.Sprintf("%s/%s (%s/%s) kubernetes/%s", path.Base(os.Args[0]), version, gruntime.GOOS, gruntime.GOARCH, commit)
}