move SwaggerSchema to DiscoveryClient

This commit is contained in:
Chao Xu
2016-01-05 13:24:44 -08:00
parent 144b5acd08
commit 6a104ee46c
4 changed files with 47 additions and 51 deletions

View File

@@ -17,16 +17,9 @@ limitations under the License.
package unversioned
import (
"encoding/json"
"fmt"
"net"
"net/url"
"strings"
"github.com/emicklei/go-restful/swagger"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/api/v1"
)
// Interface holds the methods for clients of Kubernetes,
@@ -47,7 +40,6 @@ type Interface interface {
PersistentVolumesInterface
PersistentVolumeClaimsNamespacer
ComponentStatusesInterface
SwaggerSchemaInterface
Extensions() ExtensionsInterface
Discovery() DiscoveryInterface
}
@@ -118,46 +110,6 @@ type Client struct {
*DiscoveryClient
}
// SwaggerSchemaInterface has a method to retrieve the swagger schema. Used in
// client.Interface
type SwaggerSchemaInterface interface {
SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error)
}
// SwaggerSchema retrieves and parses the swagger API schema the server supports.
func (c *Client) SwaggerSchema(version unversioned.GroupVersion) (*swagger.ApiDeclaration, error) {
if version.IsEmpty() {
return nil, fmt.Errorf("groupVersion cannot be empty")
}
groupList, err := c.Discovery().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 := c.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
}
func stringDoesntExistIn(str string, slice []string) bool {
for _, s := range slice {
if s == str {