2015-10-15 23:34:30 +00:00
|
|
|
/*
|
|
|
|
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 (
|
2016-01-05 19:55:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-10-15 23:34:30 +00:00
|
|
|
"net/url"
|
|
|
|
|
2016-01-05 21:24:44 +00:00
|
|
|
"github.com/emicklei/go-restful/swagger"
|
|
|
|
|
2015-10-20 21:33:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api"
|
|
|
|
"k8s.io/kubernetes/pkg/api/errors"
|
2015-10-15 23:34:30 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/unversioned"
|
2016-01-05 21:24:44 +00:00
|
|
|
"k8s.io/kubernetes/pkg/api/v1"
|
2016-02-12 18:58:43 +00:00
|
|
|
"k8s.io/kubernetes/pkg/client/restclient"
|
2015-12-21 05:32:52 +00:00
|
|
|
"k8s.io/kubernetes/pkg/runtime"
|
2016-01-05 19:55:34 +00:00
|
|
|
"k8s.io/kubernetes/pkg/version"
|
2015-10-15 23:34:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DiscoveryInterface holds the methods that discover server-supported API groups,
|
|
|
|
// versions and resources.
|
|
|
|
type DiscoveryInterface interface {
|
|
|
|
ServerGroupsInterface
|
|
|
|
ServerResourcesInterface
|
2016-01-05 19:55:34 +00:00
|
|
|
ServerVersionInterface
|
2016-01-05 21:24:44 +00:00
|
|
|
SwaggerSchemaInterface
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 23:10:02 +00:00
|
|
|
// ServerGroupsInterface has methods for obtaining supported groups on the API server
|
2015-10-15 23:34:30 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:55:34 +00:00
|
|
|
// ServerVersionInterface has a method for retrieving the server's version.
|
|
|
|
type ServerVersionInterface interface {
|
|
|
|
// ServerVersion retrieves and parses the server's version (git version).
|
|
|
|
ServerVersion() (*version.Info, error)
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:24:44 +00:00
|
|
|
// SwaggerSchemaInterface has a method to retrieve the swagger schema.
|
|
|
|
type SwaggerSchemaInterface interface {
|
|
|
|
// SwaggerSchema retrieves and parses the swagger API schema the server supports.
|
|
|
|
SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error)
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:33:32 +00:00
|
|
|
// DiscoveryClient implements the functions that discover server-supported API groups,
|
2015-10-15 23:34:30 +00:00
|
|
|
// versions and resources.
|
|
|
|
type DiscoveryClient struct {
|
2016-02-12 18:58:43 +00:00
|
|
|
*restclient.RESTClient
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert unversioned.APIVersions to unversioned.APIGroup. APIVersions is used by legacy v1, so
|
|
|
|
// group would be "".
|
|
|
|
func apiVersionsToAPIGroup(apiVersions *unversioned.APIVersions) (apiGroup unversioned.APIGroup) {
|
2015-11-05 22:52:58 +00:00
|
|
|
groupVersions := []unversioned.GroupVersionForDiscovery{}
|
2015-10-15 23:34:30 +00:00
|
|
|
for _, version := range apiVersions.Versions {
|
2015-11-05 22:52:58 +00:00
|
|
|
groupVersion := unversioned.GroupVersionForDiscovery{
|
2015-10-15 23:34:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-19 23:10:02 +00:00
|
|
|
// ServerGroups returns the supported groups, with information like supported versions and the
|
2015-10-15 23:34:30 +00:00
|
|
|
// preferred version.
|
|
|
|
func (d *DiscoveryClient) ServerGroups() (apiGroupList *unversioned.APIGroupList, err error) {
|
|
|
|
// Get the groupVersions exposed at /api
|
2015-10-20 21:33:43 +00:00
|
|
|
v := &unversioned.APIVersions{}
|
|
|
|
err = d.Get().AbsPath("/api").Do().Into(v)
|
|
|
|
apiGroup := unversioned.APIGroup{}
|
|
|
|
if err == nil {
|
|
|
|
apiGroup = apiVersionsToAPIGroup(v)
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
2015-10-20 21:33:43 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) && !errors.IsForbidden(err) {
|
|
|
|
return nil, err
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the groupVersions exposed at /apis
|
2015-10-20 21:33:43 +00:00
|
|
|
apiGroupList = &unversioned.APIGroupList{}
|
|
|
|
err = d.Get().AbsPath("/apis").Do().Into(apiGroupList)
|
|
|
|
if err != nil && !errors.IsNotFound(err) && !errors.IsForbidden(err) {
|
2015-10-15 23:34:30 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-20 21:33:43 +00:00
|
|
|
// to be compatible with a v1.0 server, if it's a 403 or 404, ignore and return whatever we got from /api
|
|
|
|
if err != nil && (errors.IsNotFound(err) || errors.IsForbidden(err)) {
|
|
|
|
apiGroupList = &unversioned.APIGroupList{}
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// append the group retrieved from /api to the list
|
|
|
|
apiGroupList.Groups = append(apiGroupList.Groups, apiGroup)
|
|
|
|
return apiGroupList, nil
|
|
|
|
}
|
|
|
|
|
2015-10-19 23:10:02 +00:00
|
|
|
// ServerResourcesForGroupVersion returns the supported resources for a group and version.
|
2015-10-15 23:34:30 +00:00
|
|
|
func (d *DiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (resources *unversioned.APIResourceList, err error) {
|
2015-10-20 21:33:43 +00:00
|
|
|
url := url.URL{}
|
2015-10-15 23:34:30 +00:00
|
|
|
if groupVersion == "v1" {
|
|
|
|
url.Path = "/api/" + groupVersion
|
|
|
|
} else {
|
|
|
|
url.Path = "/apis/" + groupVersion
|
|
|
|
}
|
|
|
|
resources = &unversioned.APIResourceList{}
|
2015-10-20 21:33:43 +00:00
|
|
|
err = d.Get().AbsPath(url.String()).Do().Into(resources)
|
|
|
|
if err != nil {
|
|
|
|
// ignore 403 or 404 error to be compatible with an v1.0 server.
|
|
|
|
if groupVersion == "v1" && (errors.IsNotFound(err) || errors.IsForbidden(err)) {
|
|
|
|
return resources, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2015-10-19 23:10:02 +00:00
|
|
|
// ServerResources returns the supported resources for all groups and versions.
|
2015-10-15 23:34:30 +00:00
|
|
|
func (d *DiscoveryClient) ServerResources() (map[string]*unversioned.APIResourceList, error) {
|
|
|
|
apiGroups, err := d.ServerGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-16 21:36:49 +00:00
|
|
|
groupVersions := ExtractGroupVersions(apiGroups)
|
2015-10-15 23:34:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:55:34 +00:00
|
|
|
// ServerVersion retrieves and parses the server's version (git version).
|
|
|
|
func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
|
|
|
|
body, err := d.Get().AbsPath("/version").Do().Raw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var info version.Info
|
|
|
|
err = json.Unmarshal(body, &info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("got '%s': %v", string(body), err)
|
|
|
|
}
|
|
|
|
return &info, nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:24:44 +00:00
|
|
|
// SwaggerSchema retrieves and parses the swagger API schema the server supports.
|
|
|
|
func (d *DiscoveryClient) SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error) {
|
|
|
|
if version.IsEmpty() {
|
|
|
|
return nil, fmt.Errorf("groupVersion cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
groupList, err := d.ServerGroups()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
groupVersions := ExtractGroupVersions(groupList)
|
|
|
|
// This check also takes care the case that kubectl is newer than the running endpoint
|
|
|
|
if stringDoesntExistIn(version.String(), groupVersions) {
|
|
|
|
return nil, fmt.Errorf("API version: %v is not supported by the server. Use one of: %v", version, groupVersions)
|
|
|
|
}
|
|
|
|
var path string
|
|
|
|
if version == v1.SchemeGroupVersion {
|
|
|
|
path = "/swaggerapi/api/" + version.Version
|
|
|
|
} else {
|
|
|
|
path = "/swaggerapi/apis/" + version.Group + "/" + version.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := d.Get().AbsPath(path).Do().Raw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var schema swagger.ApiDeclaration
|
|
|
|
err = json.Unmarshal(body, &schema)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("got '%s': %v", string(body), err)
|
|
|
|
}
|
|
|
|
return &schema, nil
|
|
|
|
}
|
|
|
|
|
2016-02-12 18:58:43 +00:00
|
|
|
func setDiscoveryDefaults(config *restclient.Config) error {
|
2016-01-06 23:59:54 +00:00
|
|
|
config.APIPath = ""
|
2015-11-13 21:20:54 +00:00
|
|
|
config.GroupVersion = nil
|
2015-12-21 05:32:52 +00:00
|
|
|
config.Codec = runtime.NoopEncoder{api.Codecs.UniversalDecoder()}
|
2015-10-15 23:34:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-29 07:43:47 +00:00
|
|
|
// NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. This client
|
2015-10-15 23:34:30 +00:00
|
|
|
// can be used to discover supported resources in the API server.
|
2016-02-12 18:58:43 +00:00
|
|
|
func NewDiscoveryClientForConfig(c *restclient.Config) (*DiscoveryClient, error) {
|
2015-10-15 23:34:30 +00:00
|
|
|
config := *c
|
|
|
|
if err := setDiscoveryDefaults(&config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-02-12 18:58:43 +00:00
|
|
|
client, err := restclient.UnversionedRESTClientFor(&config)
|
2015-10-20 21:33:43 +00:00
|
|
|
return &DiscoveryClient{client}, err
|
2015-10-15 23:34:30 +00:00
|
|
|
}
|
2016-01-29 07:43:47 +00:00
|
|
|
|
|
|
|
// NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. If
|
|
|
|
// there is an error, it panics.
|
2016-02-12 18:58:43 +00:00
|
|
|
func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
|
2016-01-29 07:43:47 +00:00
|
|
|
client, err := NewDiscoveryClientForConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return client
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new DiscoveryClient for the given RESTClient.
|
2016-02-12 18:58:43 +00:00
|
|
|
func NewDiscoveryClient(c *restclient.RESTClient) *DiscoveryClient {
|
2016-01-29 07:43:47 +00:00
|
|
|
return &DiscoveryClient{c}
|
|
|
|
}
|