Skip to content

Commit

Permalink
OpenShift should set a client UserAgent on all calls
Browse files Browse the repository at this point in the history
  • Loading branch information
smarterclayton committed Feb 4, 2015
1 parent a238540 commit b999dba
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
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)
}
}

0 comments on commit b999dba

Please sign in to comment.