This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a discovery client for querying server version and resources
- Loading branch information
1 parent
fd7f1e0
commit 7587d80
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |