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 a discovery-client #15659

Merged
merged 1 commit into from Oct 17, 2015
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
2 changes: 1 addition & 1 deletion cmd/kube-controller-manager/app/controllermanager.go
Expand Up @@ -292,7 +292,7 @@ func (s *CMServer) Run(_ []string) error {
}
versions := &unversioned.APIVersions{Versions: versionStrings}

resourceMap, err := client.SupportedResources(kubeClient, kubeconfig)
resourceMap, err := kubeClient.Discovery().ServerResources()
if err != nil {
glog.Fatalf("Failed to get supported resources from server: %v", err)
}
Expand Down
49 changes: 7 additions & 42 deletions pkg/client/unversioned/client.go
Expand Up @@ -52,7 +52,7 @@ type Interface interface {
ComponentStatusesInterface
SwaggerSchemaInterface
Extensions() ExtensionsInterface
ResourcesInterface
Discovery() DiscoveryInterface
}

func (c *Client) ReplicationControllers(namespace string) ReplicationControllerInterface {
Expand Down Expand Up @@ -120,11 +120,6 @@ type VersionInterface interface {
ServerAPIVersions() (*unversioned.APIVersions, error)
}

// ResourcesInterface has methods for obtaining supported resources on the API server
type ResourcesInterface interface {
SupportedResourcesForGroupVersion(groupVersion string) (*unversioned.APIResourceList, error)
}

// APIStatus is exposed by errors that can be converted to an api.Status object
// for finer grained details.
type APIStatus interface {
Expand All @@ -135,6 +130,8 @@ type APIStatus interface {
type Client struct {
*RESTClient
*ExtensionsClient
// TODO: remove this when we re-structure pkg/client.
*DiscoveryClient
}

// ServerVersion retrieves and parses the server's version.
Expand All @@ -151,42 +148,6 @@ func (c *Client) ServerVersion() (*version.Info, error) {
return &info, nil
}

// SupportedResourcesForGroupVersion retrieves the list of resources supported by the API server for a group version.
func (c *Client) SupportedResourcesForGroupVersion(groupVersion string) (*unversioned.APIResourceList, error) {
var prefix string
if groupVersion == "v1" {
prefix = "/api"
} else {
prefix = "/apis"
}
body, err := c.Get().AbsPath(prefix, groupVersion).Do().Raw()
if err != nil {
return nil, err
}
resources := unversioned.APIResourceList{}
if err := json.Unmarshal(body, &resources); err != nil {
return nil, err
}
return &resources, nil
}

// SupportedResources gets all supported resources for all group versions. The key in the map is an API groupVersion.
func SupportedResources(c Interface, cfg *Config) (map[string]*unversioned.APIResourceList, error) {
apis, err := ServerAPIVersions(cfg)
if err != nil {
return nil, err
}
result := map[string]*unversioned.APIResourceList{}
for _, groupVersion := range apis {
resources, err := c.SupportedResourcesForGroupVersion(groupVersion)
if err != nil {
return nil, err
}
result[groupVersion] = resources
}
return result, nil
}

// ServerAPIVersions retrieves and parses the list of API versions the server supports.
func (c *Client) ServerAPIVersions() (*unversioned.APIVersions, error) {
body, err := c.Get().UnversionedPath("").Do().Raw()
Expand Down Expand Up @@ -288,3 +249,7 @@ func IsTimeout(err error) bool {
func (c *Client) Extensions() ExtensionsInterface {
return c.ExtensionsClient
}

func (c *Client) Discovery() DiscoveryInterface {
return c.DiscoveryClient
}
4 changes: 2 additions & 2 deletions pkg/client/unversioned/client_test.go
Expand Up @@ -352,7 +352,7 @@ func TestGetServerResources(t *testing.T) {
}))
client := NewOrDie(&Config{Host: server.URL})
for _, test := range tests {
got, err := client.SupportedResourcesForGroupVersion(test.request)
got, err := client.Discovery().ServerResourcesForGroupVersion(test.request)
if test.expectErr {
if err == nil {
t.Error("unexpected non-error")
Expand All @@ -368,7 +368,7 @@ func TestGetServerResources(t *testing.T) {
}
}

resourceMap, err := SupportedResources(client, &Config{Host: server.URL})
resourceMap, err := client.Discovery().ServerResources()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
Expand Down
175 changes: 175 additions & 0 deletions pkg/client/unversioned/discovery_client.go
@@ -0,0 +1,175 @@
/*
Copyright 2015 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package unversioned

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"k8s.io/kubernetes/pkg/api/unversioned"
)

// DiscoveryInterface holds the methods that discover server-supported API groups,
// versions and resources.
type DiscoveryInterface interface {
ServerGroupsInterface
ServerResourcesInterface
}

// GroupsInterface has methods for obtaining supported groups on the API server
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ServerGroupsInterface

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. BTW, do you have any preference over these names? Are they too verbose?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are they too verbose?

Yes. But, that matches with the style of the rest of the package. In my ideal world, these interfaces wouldn't exist. It's a long rant of mine nicely summarized by this article, so I won't repeat it here. If I were to define an interface, I wouldn't put Interface in it's name.

But more importantly this matches the current style of this package, so I probably wouldn't change it too much.

type ServerGroupsInterface interface {
// ServerGroups returns the supported groups, with information like supported versions and the
// preferred version.
ServerGroups() (*unversioned.APIGroupList, error)
}

// ServerResourcesInterface has methods for obtaining supported resources on the API server
type ServerResourcesInterface interface {
// ServerResourcesForGroupVersion returns the supported resources for a group and version.
ServerResourcesForGroupVersion(groupVersion string) (*unversioned.APIResourceList, error)
// ServerResources returns the supported resources for all groups and versions.
ServerResources() (map[string]*unversioned.APIResourceList, error)
}

// DiscoveryClient implements the functions that dicovery server-supported API groups,
// versions and resources.
type DiscoveryClient struct {
httpClient HTTPClient
baseURL url.URL
}

// Convert unversioned.APIVersions to unversioned.APIGroup. APIVersions is used by legacy v1, so
// group would be "".
func apiVersionsToAPIGroup(apiVersions *unversioned.APIVersions) (apiGroup unversioned.APIGroup) {
groupVersions := []unversioned.GroupVersion{}
for _, version := range apiVersions.Versions {
groupVersion := unversioned.GroupVersion{
GroupVersion: version,
Version: version,
}
groupVersions = append(groupVersions, groupVersion)
}
apiGroup.Versions = groupVersions
// There should be only one groupVersion returned at /api
apiGroup.PreferredVersion = groupVersions[0]
return
}

func (d *DiscoveryClient) get(url string) (resp *http.Response, err error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return d.httpClient.Do(req)
}

// APIGroups returns the supported groups, with information like supported versions and the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is APIGroups the function name? Many of the comments in this file contains incorrect function name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I submitted a commit to fix these, I guess the commit is lost during the squash. I'll submit a patch.

// preferred version.
func (d *DiscoveryClient) ServerGroups() (apiGroupList *unversioned.APIGroupList, err error) {
// Get the groupVersions exposed at /api
url := d.baseURL
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krousey I made a copy of d.baseURL to avoid race condition

url.Path = "/api"
resp, err := d.get(url.String())
if err != nil {
return nil, err
}
var v unversioned.APIVersions
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&v)
if err != nil {
return nil, fmt.Errorf("unexpected error: %v", err)
}
apiGroup := apiVersionsToAPIGroup(&v)

// Get the groupVersions exposed at /apis
url.Path = "/apis"
resp2, err := d.get(url.String())
if err != nil {
return nil, err
}
defer resp2.Body.Close()
apiGroupList = &unversioned.APIGroupList{}
if err = json.NewDecoder(resp2.Body).Decode(&apiGroupList); err != nil {
return nil, fmt.Errorf("unexpected error: %v", err)
}

// append the group retrieved from /api to the list
apiGroupList.Groups = append(apiGroupList.Groups, apiGroup)
return apiGroupList, nil
}

// APIResourcesForGroup returns the supported resources for a group and version.
func (d *DiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (resources *unversioned.APIResourceList, err error) {
url := d.baseURL
if groupVersion == "v1" {
url.Path = "/api/" + groupVersion
} else {
url.Path = "/apis/" + groupVersion
}
resp, err := d.get(url.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
resources = &unversioned.APIResourceList{}
if err = json.NewDecoder(resp.Body).Decode(resources); err != nil {
return nil, fmt.Errorf("unexpected error: %v", err)
}
return resources, nil
}

// APIResources returns the supported resources for all groups and versions.
func (d *DiscoveryClient) ServerResources() (map[string]*unversioned.APIResourceList, error) {
apiGroups, err := d.ServerGroups()
if err != nil {
return nil, err
}
groupVersions := extractGroupVersions(apiGroups)
result := map[string]*unversioned.APIResourceList{}
for _, groupVersion := range groupVersions {
resources, err := d.ServerResourcesForGroupVersion(groupVersion)
if err != nil {
return nil, err
}
result[groupVersion] = resources
}
return result, nil
}

func setDiscoveryDefaults(config *Config) error {
config.Prefix = ""
config.Version = ""
return nil
}

// NewDiscoveryClient creates a new DiscoveryClient for the given config. This client
// can be used to discover supported resources in the API server.
func NewDiscoveryClient(c *Config) (*DiscoveryClient, error) {
config := *c
if err := setDiscoveryDefaults(&config); err != nil {
return nil, err
}
transport, err := TransportFor(c)
if err != nil {
return nil, err
}
client := &http.Client{Transport: transport}
baseURL, err := defaultServerUrlFor(c)
return &DiscoveryClient{client, *baseURL}, nil
}
11 changes: 9 additions & 2 deletions pkg/client/unversioned/helper.go
Expand Up @@ -146,15 +146,22 @@ func New(c *Config) (*Client, error) {
return nil, err
}

discoveryConfig := *c
discoveryClient, err := NewDiscoveryClient(&discoveryConfig)
if err != nil {
return nil, err
}

if _, err := latest.Group("extensions"); err != nil {
return &Client{RESTClient: client, ExtensionsClient: nil}, nil
return &Client{RESTClient: client, ExtensionsClient: nil, DiscoveryClient: discoveryClient}, nil
}
experimentalConfig := *c
experimentalClient, err := NewExtensions(&experimentalConfig)
if err != nil {
return nil, err
}
return &Client{RESTClient: client, ExtensionsClient: experimentalClient}, nil

return &Client{RESTClient: client, ExtensionsClient: experimentalClient, DiscoveryClient: discoveryClient}, nil
}

// MatchesServerVersion queries the server to compares the build version
Expand Down
42 changes: 29 additions & 13 deletions pkg/client/unversioned/testclient/testclient.go
Expand Up @@ -61,7 +61,7 @@ type Fake struct {
// ProxyReactionChain is the list of proxy reactors that will be attempted for every request in the order they are tried
ProxyReactionChain []ProxyReactor

Resources []unversioned.APIResourceList
Resources map[string]*unversioned.APIResourceList
}

// Reactor is an interface to allow the composition of reaction functions.
Expand Down Expand Up @@ -274,18 +274,8 @@ func (c *Fake) Extensions() client.ExtensionsInterface {
return &FakeExperimental{c}
}

func (c *Fake) SupportedResourcesForGroupVersion(version string) (*unversioned.APIResourceList, error) {
action := ActionImpl{
Verb: "get",
Resource: "resource",
}
c.Invokes(action, nil)
for _, resource := range c.Resources {
if resource.GroupVersion == version {
return &resource, nil
}
}
return nil, nil
func (c *Fake) Discovery() client.DiscoveryInterface {
return &FakeDiscovery{c}
}

func (c *Fake) ServerVersion() (*version.Info, error) {
Expand Down Expand Up @@ -348,3 +338,29 @@ func (c *FakeExperimental) Jobs(namespace string) client.JobInterface {
func (c *FakeExperimental) Ingress(namespace string) client.IngressInterface {
return &FakeIngress{Fake: c, Namespace: namespace}
}

type FakeDiscovery struct {
*Fake
}

func (c *FakeDiscovery) ServerResourcesForGroupVersion(groupVersion string) (*unversioned.APIResourceList, error) {
action := ActionImpl{
Verb: "get",
Resource: "resource",
}
c.Invokes(action, nil)
return c.Resources[groupVersion], nil
}

func (c *FakeDiscovery) ServerResources() (map[string]*unversioned.APIResourceList, error) {
action := ActionImpl{
Verb: "get",
Resource: "resource",
}
c.Invokes(action, nil)
return c.Resources, nil
}

func (c *FakeDiscovery) ServerGroups() (*unversioned.APIGroupList, error) {
return nil, nil
}
2 changes: 1 addition & 1 deletion pkg/controller/namespace/namespace_controller.go
Expand Up @@ -194,7 +194,7 @@ func deleteAllContent(kubeClient client.Interface, versions *unversioned.APIVers
}
// If experimental mode, delete all experimental resources for the namespace.
if containsVersion(versions, "extensions/v1beta1") {
resources, err := kubeClient.SupportedResourcesForGroupVersion("extensions/v1beta1")
resources, err := kubeClient.Discovery().ServerResourcesForGroupVersion("extensions/v1beta1")
if err != nil {
return estimate, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/namespace/namespace_controller_test.go
Expand Up @@ -95,8 +95,8 @@ func testSyncNamespaceThatIsTerminating(t *testing.T, versions *unversioned.APIV
for _, resource := range []string{"daemonsets", "deployments", "jobs", "horizontalpodautoscalers", "ingress"} {
resources = append(resources, unversioned.APIResource{Name: resource})
}
mockClient.Resources = []unversioned.APIResourceList{
{
mockClient.Resources = map[string]*unversioned.APIResourceList{
"extensions/v1beta1": {
GroupVersion: "extensions/v1beta1",
APIResources: resources,
},
Expand Down