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 ctx object to kubecfg and client #1516

Merged
merged 3 commits into from
Sep 30, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
"github.com/GoogleCloudPlatform/kubernetes/pkg/capabilities"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
Expand Down Expand Up @@ -126,7 +127,8 @@ func main() {
Port: *minionPort,
}

client, err := client.New(net.JoinHostPort(*address, strconv.Itoa(int(*port))), *storageVersion, nil)
ctx := api.NewContext()
client, err := client.New(ctx, net.JoinHostPort(*address, strconv.Itoa(int(*port))), *storageVersion, nil)
if err != nil {
glog.Fatalf("Invalid server address: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/controller-manager/controller-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strconv"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/controller"
Expand All @@ -53,8 +54,8 @@ func main() {
if len(*master) == 0 {
glog.Fatal("usage: controller-manager -master <master>")
}

kubeClient, err := client.New(*master, latest.OldestVersion, nil)
ctx := api.NewContext()
kubeClient, err := client.New(ctx, *master, latest.OldestVersion, nil)
if err != nil {
glog.Fatalf("Invalid -master: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func startComponents(manifestURL string) (apiServerURL string) {
}
}

cl := client.NewOrDie(apiServer.URL, "", nil)
cl := client.NewOrDie(api.NewContext(), apiServer.URL, "", nil)
cl.PollPeriod = time.Second * 1
cl.Sync = true

Expand Down Expand Up @@ -311,7 +311,7 @@ func main() {
// Wait for the synchronization threads to come up.
time.Sleep(time.Second * 10)

kubeClient := client.NewOrDie(apiServerURL, "", nil)
kubeClient := client.NewOrDie(api.NewContext(), apiServerURL, "", nil)

// Run tests in parallel
testFuncs := []testFunc{
Expand Down
8 changes: 6 additions & 2 deletions cmd/kubecfg/kubecfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ func main() {
} else {
masterServer = "http://localhost:8080"
}
kubeClient, err := client.New(masterServer, *apiVersion, nil)

// TODO: get the namespace context when kubecfg ns is completed
ctx := api.NewContext()

kubeClient, err := client.New(ctx, masterServer, *apiVersion, nil)
if err != nil {
glog.Fatalf("Can't configure client: %v", err)
}
Expand All @@ -194,7 +198,7 @@ func main() {
if *keyFile != "" {
auth.KeyFile = *keyFile
}
kubeClient, err = client.New(masterServer, *apiVersion, auth)
kubeClient, err = client.New(ctx, masterServer, *apiVersion, auth)
if err != nil {
glog.Fatalf("Can't configure client: %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"flag"
"time"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/proxy"
Expand Down Expand Up @@ -54,8 +55,9 @@ func main() {
// define api config source
if *master != "" {
glog.Infof("Using api calls to get config %v", *master)
ctx := api.NewContext()
//TODO: add auth info
client, err := client.New(*master, latest.OldestVersion, nil)
client, err := client.New(ctx, *master, latest.OldestVersion, nil)
if err != nil {
glog.Fatalf("Invalid -master: %v", err)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type Client struct {
// to a URL will prepend the server path. The API version to use may be specified or left
// empty to use the client preferred version. Returns an error if host cannot be converted to
// a valid URL.
func New(host, version string, auth *AuthInfo) (*Client, error) {
func New(ctx api.Context, host, version string, auth *AuthInfo) (*Client, error) {
if version == "" {
// Clients default to the preferred code API version
// TODO: implement version negotation (highest version supported by server)
Expand All @@ -113,16 +113,16 @@ func New(host, version string, auth *AuthInfo) (*Client, error) {
return nil, fmt.Errorf("API version '%s' is not recognized (valid values: %s)", version, strings.Join(latest.Versions, ", "))
}
prefix := fmt.Sprintf("/api/%s/", version)
restClient, err := NewRESTClient(host, auth, prefix, versionInterfaces.Codec)
restClient, err := NewRESTClient(ctx, host, auth, prefix, versionInterfaces.Codec)
if err != nil {
return nil, fmt.Errorf("API URL '%s' is not valid: %v", host, err)
}
return &Client{restClient}, nil
}

// NewOrDie creates a Kubernetes client and panics if the provided host is invalid.
func NewOrDie(host, version string, auth *AuthInfo) *Client {
client, err := New(host, version, auth)
func NewOrDie(ctx api.Context, host, version string, auth *AuthInfo) *Client {
client, err := New(ctx, host, version, auth)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -152,6 +152,7 @@ type AuthInfo struct {
// Kubernetes API pattern.
// Host is the http://... base for the URL
type RESTClient struct {
ctx api.Context
host string
prefix string
secure bool
Expand All @@ -165,7 +166,7 @@ type RESTClient struct {

// NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths.
func NewRESTClient(host string, auth *AuthInfo, path string, c runtime.Codec) (*RESTClient, error) {
func NewRESTClient(ctx api.Context, host string, auth *AuthInfo, path string, c runtime.Codec) (*RESTClient, error) {
prefix, err := normalizePrefix(host, path)
if err != nil {
return nil, err
Expand Down Expand Up @@ -202,6 +203,7 @@ func NewRESTClient(host string, auth *AuthInfo, path string, c runtime.Codec) (*
}

return &RESTClient{
ctx: ctx,
host: base.String(),
prefix: prefix.Path,
secure: prefix.Scheme == "https",
Expand Down
19 changes: 12 additions & 7 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func TestChecksCodec(t *testing.T) {
"v1beta2": {false, "/api/v1beta2/", v1beta2.Codec},
"v1beta3": {true, "", nil},
}
ctx := api.NewContext()
for version, expected := range testCases {
client, err := New("127.0.0.1", version, nil)
client, err := New(ctx, "127.0.0.1", version, nil)
switch {
case err == nil && expected.Err:
t.Errorf("expected error but was nil")
Expand Down Expand Up @@ -82,8 +83,9 @@ func TestValidatesHostParameter(t *testing.T) {
"http://host/server": {"http://host", "/server/api/v1beta1/", false},
"host/server": {"", "", true},
}
ctx := api.NewContext()
for k, expected := range testCases {
c, err := NewRESTClient(k, nil, "/api/v1beta1/", v1beta1.Codec)
c, err := NewRESTClient(ctx, k, nil, "/api/v1beta1/", v1beta1.Codec)
switch {
case err == nil && expected.Err:
t.Errorf("expected error but was nil")
Expand Down Expand Up @@ -381,6 +383,7 @@ type testClient struct {
}

func (c *testClient) Setup() *testClient {
ctx := api.NewContext()
c.handler = &util.FakeHandler{
StatusCode: c.Response.StatusCode,
}
Expand All @@ -389,7 +392,7 @@ func (c *testClient) Setup() *testClient {
}
c.server = httptest.NewServer(c.handler)
if c.Client == nil {
c.Client = NewOrDie("localhost", "v1beta1", nil)
c.Client = NewOrDie(ctx, "localhost", "v1beta1", nil)
}
c.Client.host = c.server.URL
c.Client.prefix = "/api/v1beta1/"
Expand Down Expand Up @@ -573,7 +576,7 @@ func TestDoRequest(t *testing.T) {
testClients := []testClient{
{Request: testRequest{Method: "GET", Path: "good"}, Response: Response{StatusCode: 200}},
{Request: testRequest{Method: "GET", Path: "bad%ZZ"}, Error: true},
{Client: NewOrDie("localhost", "v1beta1", &AuthInfo{"foo", "bar", "", "", ""}), Request: testRequest{Method: "GET", Path: "auth", Header: "Authorization"}, Response: Response{StatusCode: 200}},
{Client: NewOrDie(api.NewContext(), "localhost", "v1beta1", &AuthInfo{"foo", "bar", "", "", ""}), Request: testRequest{Method: "GET", Path: "auth", Header: "Authorization"}, Response: Response{StatusCode: 200}},
{Client: &Client{&RESTClient{httpClient: http.DefaultClient}}, Request: testRequest{Method: "GET", Path: "nocertificate"}, Error: true},
{Request: testRequest{Method: "GET", Path: "error"}, Response: Response{StatusCode: 500}, Error: true},
{Request: testRequest{Method: "POST", Path: "faildecode"}, Response: Response{StatusCode: 200, RawBody: &invalid}},
Expand Down Expand Up @@ -604,7 +607,8 @@ func TestDoRequestAccepted(t *testing.T) {
testServer := httptest.NewServer(&fakeHandler)
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
auth := AuthInfo{User: "user", Password: "pass"}
c, err := New(testServer.URL, "", &auth)
ctx := api.NewContext()
c, err := New(ctx, testServer.URL, "", &auth)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -641,7 +645,8 @@ func TestDoRequestAcceptedSuccess(t *testing.T) {
testServer := httptest.NewServer(&fakeHandler)
request, _ := http.NewRequest("GET", testServer.URL+"/foo/bar", nil)
auth := AuthInfo{User: "user", Password: "pass"}
c, err := New(testServer.URL, "", &auth)
ctx := api.NewContext()
c, err := New(ctx, testServer.URL, "", &auth)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down Expand Up @@ -678,7 +683,7 @@ func TestGetServerVersion(t *testing.T) {
w.WriteHeader(http.StatusOK)
w.Write(output)
}))
client := NewOrDie(server.URL, "", nil)
client := NewOrDie(api.NewContext(), server.URL, "", nil)

got, err := client.ServerVersion()
if err != nil {
Expand Down
37 changes: 24 additions & 13 deletions pkg/client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func TestDoRequestNewWay(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, "v1beta2", &auth)
ctx := api.NewContext()
c := NewOrDie(ctx, testServer.URL, "v1beta2", &auth)
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Expand Down Expand Up @@ -84,7 +85,8 @@ func TestDoRequestNewWayReader(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, "v1beta1", &auth)
ctx := api.NewContext()
c := NewOrDie(ctx, testServer.URL, "v1beta1", &auth)
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Expand Down Expand Up @@ -121,7 +123,8 @@ func TestDoRequestNewWayObj(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, "v1beta2", &auth)
ctx := api.NewContext()
c := NewOrDie(ctx, testServer.URL, "v1beta2", &auth)
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Expand Down Expand Up @@ -171,7 +174,8 @@ func TestDoRequestNewWayFile(t *testing.T) {
}
testServer := httptest.NewServer(&fakeHandler)
auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, "v1beta1", &auth)
ctx := api.NewContext()
c := NewOrDie(ctx, testServer.URL, "v1beta1", &auth)
obj, err := c.Verb("POST").
Path("foo/bar").
Path("baz").
Expand All @@ -196,7 +200,8 @@ func TestDoRequestNewWayFile(t *testing.T) {
}

func TestVerbs(t *testing.T) {
c := NewOrDie("localhost", "", nil)
ctx := api.NewContext()
c := NewOrDie(ctx, "localhost", "", nil)
if r := c.Post(); r.verb != "POST" {
t.Errorf("Post verb is wrong")
}
Expand All @@ -212,16 +217,18 @@ func TestVerbs(t *testing.T) {
}

func TestAbsPath(t *testing.T) {
ctx := api.NewContext()
expectedPath := "/bar/foo"
c := NewOrDie("localhost", "", nil)
c := NewOrDie(ctx, "localhost", "", nil)
r := c.Post().Path("/foo").AbsPath(expectedPath)
if r.path != expectedPath {
t.Errorf("unexpected path: %s, expected %s", r.path, expectedPath)
}
}

func TestSync(t *testing.T) {
c := NewOrDie("localhost", "", nil)
ctx := api.NewContext()
c := NewOrDie(ctx, "localhost", "", nil)
r := c.Get()
if r.sync {
t.Errorf("sync has wrong default")
Expand All @@ -247,8 +254,9 @@ func TestUintParam(t *testing.T) {
{"baz", 0, "http://localhost?baz=0"},
}

ctx := api.NewContext()
for _, item := range table {
c := NewOrDie("localhost", "", nil)
c := NewOrDie(ctx, "localhost", "", nil)
r := c.Get().AbsPath("").UintParam(item.name, item.testVal)
if e, a := item.expectStr, r.finalURL(); e != a {
t.Errorf("expected %v, got %v", e, a)
Expand All @@ -265,9 +273,9 @@ func TestUnacceptableParamNames(t *testing.T) {
{"sync", "foo", false},
{"timeout", "42", false},
}

ctx := api.NewContext()
for _, item := range table {
c := NewOrDie("localhost", "", nil)
c := NewOrDie(ctx, "localhost", "", nil)
r := c.Get().setParam(item.name, item.testVal)
if e, a := item.expectSuccess, r.err == nil; e != a {
t.Errorf("expected %v, got %v (%v)", e, a, r.err)
Expand All @@ -276,7 +284,8 @@ func TestUnacceptableParamNames(t *testing.T) {
}

func TestSetPollPeriod(t *testing.T) {
c := NewOrDie("localhost", "", nil)
ctx := api.NewContext()
c := NewOrDie(ctx, "localhost", "", nil)
r := c.Get()
if r.pollPeriod == 0 {
t.Errorf("polling should be on by default")
Expand Down Expand Up @@ -307,7 +316,8 @@ func TestPolling(t *testing.T) {
}))

auth := AuthInfo{User: "user", Password: "pass"}
c := NewOrDie(testServer.URL, "v1beta1", &auth)
ctx := api.NewContext()
c := NewOrDie(ctx, testServer.URL, "v1beta1", &auth)

trials := []func(){
func() {
Expand Down Expand Up @@ -411,7 +421,8 @@ func TestWatch(t *testing.T) {
}
}))

s, err := New(testServer.URL, "v1beta1", &auth)
ctx := api.NewContext()
s, err := New(ctx, testServer.URL, "v1beta1", &auth)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/replication_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestSyncReplicationControllerDoesNothing(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
client := client.NewOrDie(api.NewContext(), testServer.URL, "v1beta1", nil)

fakePodControl := FakePodControl{}

Expand All @@ -137,7 +137,7 @@ func TestSyncReplicationControllerDeletes(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
client := client.NewOrDie(api.NewContext(), testServer.URL, "v1beta1", nil)

fakePodControl := FakePodControl{}

Expand All @@ -157,7 +157,7 @@ func TestSyncReplicationControllerCreates(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
client := client.NewOrDie(api.NewContext(), testServer.URL, "v1beta1", nil)

fakePodControl := FakePodControl{}

Expand All @@ -177,7 +177,7 @@ func TestCreateReplica(t *testing.T) {
ResponseBody: string(body),
}
testServer := httptest.NewTLSServer(&fakeHandler)
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
client := client.NewOrDie(api.NewContext(), testServer.URL, "v1beta1", nil)

podControl := RealPodControl{
kubeClient: client,
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestSynchonize(t *testing.T) {
t.Errorf("Unexpected request for %v", req.RequestURI)
})
testServer := httptest.NewServer(mux)
client := client.NewOrDie(testServer.URL, "v1beta1", nil)
client := client.NewOrDie(api.NewContext(), testServer.URL, "v1beta1", nil)
manager := NewReplicationManager(client)
fakePodControl := FakePodControl{}
manager.podControl = &fakePodControl
Expand Down