Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
add a discovery client for querying server version and resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ericchiang committed Jan 11, 2017
1 parent fd7f1e0 commit 7587d80
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,16 @@ func (c *Client) urlFor(apiGroup, apiVersion, namespace, resource, name string,
return endpoint + "?" + v.Encode()
}

func (c *Client) urlForPath(path string) string {
if strings.HasPrefix(path, "/") {
path = path[1:]
}
if strings.HasSuffix(c.Endpoint, "/") {
return c.Endpoint + path
}
return c.Endpoint + "/" + path
}

func (c *Client) create(ctx context.Context, codec *codec, verb, url string, req, resp interface{}) error {
body, err := codec.marshal(req)
if err != nil {
Expand Down
63 changes: 63 additions & 0 deletions discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package k8s

import (
"context"
"path"

"github.com/ericchiang/k8s/api/unversioned"
)

type Version struct {
Major string `json:"major"`
Minor string `json:"minor"`
GitVersion string `json:"gitVersion"`
GitCommit string `json:"gitCommit"`
GitTreeState string `json:"gitTreeState"`
BuildDate string `json:"buildDate"`
GoVersion string `json:"goVersion"`
Compiler string `json:"compiler"`
Platform string `json:"platform"`
}

func (c *Client) Discovery() *Discovery {
return &Discovery{c}
}

// Discovery is a client used to determine the API version and supported
// resources of the server.
type Discovery struct {
client *Client
}

func (d *Discovery) Version(ctx context.Context) (*Version, error) {
var v Version
if err := d.client.get(ctx, jsonCodec, d.client.urlForPath("version"), &v); err != nil {
return nil, err
}
return &v, nil
}

func (d *Discovery) APIGroups(ctx context.Context) (*unversioned.APIGroupList, error) {
var groups unversioned.APIGroupList
if err := d.client.get(ctx, pbCodec, d.client.urlForPath("apis"), &groups); err != nil {
return nil, err
}
return &groups, nil
}

func (d *Discovery) APIGroup(ctx context.Context, name string) (*unversioned.APIGroup, error) {
var group unversioned.APIGroup
if err := d.client.get(ctx, pbCodec, d.client.urlForPath(path.Join("apis", name)), &group); err != nil {
return nil, err
}
return &group, nil
}

func (d *Discovery) APIResources(ctx context.Context, groupName, groupVersion string) (*unversioned.APIResourceList, error) {
var list unversioned.APIResourceList
if err := d.client.get(ctx, pbCodec, d.client.urlForPath(path.Join("apis", groupName, groupVersion)), &list); err != nil {
return nil, err
}
return &list, nil

}
28 changes: 28 additions & 0 deletions discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package k8s

import (
"context"
"testing"
)

func TestDiscovery(t *testing.T) {
client := newTestClient(t).Discovery()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if _, err := client.Version(ctx); err != nil {
t.Errorf("list version: %v", err)
}

if _, err := client.APIGroups(ctx); err != nil {
t.Errorf("list api groups: %v", err)
}

if _, err := client.APIGroup(ctx, "extensions"); err != nil {
t.Errorf("list api group: %v", err)
}

if _, err := client.APIResources(ctx, "extensions", "v1beta1"); err != nil {
t.Errorf("list api group resources: %v", err)
}
}

0 comments on commit 7587d80

Please sign in to comment.