From 7587d8004169429659b865857adeb18ba9e2767a Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Tue, 10 Jan 2017 22:05:37 -0800 Subject: [PATCH] add a discovery client for querying server version and resources --- client.go | 10 ++++++++ discovery.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++ discovery_test.go | 28 +++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 discovery.go create mode 100644 discovery_test.go diff --git a/client.go b/client.go index aae77f8..75718f3 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/discovery.go b/discovery.go new file mode 100644 index 0000000..b2713cb --- /dev/null +++ b/discovery.go @@ -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 + +} diff --git a/discovery_test.go b/discovery_test.go new file mode 100644 index 0000000..562a98f --- /dev/null +++ b/discovery_test.go @@ -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) + } +}