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 user agent to the client calls #891

Merged
merged 2 commits into from
Feb 5, 2015
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 44 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package client

import (
"fmt"
"os"
"path"
"runtime"
"strings"

kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"

"github.com/openshift/origin/pkg/api/latest"
"github.com/openshift/origin/pkg/version"
)

// Interface exposes methods on OpenShift resources.
Expand Down Expand Up @@ -114,21 +121,38 @@ type Client struct {
// objects. An error is returned if the provided configuration is not valid.
func New(c *kclient.Config) (*Client, error) {
config := *c
if err := SetOpenShiftDefaults(&config); err != nil {
return nil, err
}
client, err := kclient.RESTClientFor(&config)
if err != nil {
return nil, err
}
return &Client{client}, nil
}

func SetOpenShiftDefaults(config *kclient.Config) error {
if config.Prefix == "" {
config.Prefix = "/osapi"
}
if len(config.UserAgent) == 0 {
config.UserAgent = DefaultOpenShiftUserAgent()
}
if config.Version == "" {
// Clients default to the preferred code API version
// TODO: implement version negotiation (highest version supported by server)
config.Version = latest.Version
}
config.Codec = latest.Codec
config.LegacyBehavior = (config.Version == "v1beta1")
client, err := kclient.RESTClientFor(&config)
version := config.Version
versionInterfaces, err := latest.InterfacesFor(version)
if err != nil {
return nil, err
return fmt.Errorf("API version '%s' is not recognized (valid values: %s)", version, strings.Join(latest.Versions, ", "))
}
return &Client{client}, nil
if config.Codec == nil {
config.Codec = versionInterfaces.Codec
}
config.LegacyBehavior = (config.Version == "v1beta1")
return nil
}

// NewOrDie creates an OpenShift client and panics if the provided API version is not recognized.
Expand All @@ -139,3 +163,18 @@ func NewOrDie(c *kclient.Config) *Client {
}
return client
}

// DefaultOpenShiftUserAgent returns the default user agent that clients can use.
func DefaultOpenShiftUserAgent() 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) openshift/%s", path.Base(os.Args[0]), version, runtime.GOOS, runtime.GOARCH, commit)
}
29 changes: 29 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import (
"net/http"
"net/http/httptest"
"strings"
"testing"

kclient "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)

func TestUserAgent(t *testing.T) {
ch := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
header := req.Header.Get("User-Agent")
ch <- header
}))
defer server.Close()

c, _ := New(&kclient.Config{
Host: server.URL,
})
c.DeploymentConfigs("test").Get("other")

header := <-ch
if !strings.Contains(header, "openshift/") || !strings.Contains(header, "client.test/") {
t.Fatalf("no user agent header: %s", header)
}
}