summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/client-go/discovery/discovery_client.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/client-go/discovery/discovery_client.go')
-rw-r--r--vendor/k8s.io/client-go/discovery/discovery_client.go86
1 files changed, 18 insertions, 68 deletions
diff --git a/vendor/k8s.io/client-go/discovery/discovery_client.go b/vendor/k8s.io/client-go/discovery/discovery_client.go
index 011dd9ecf..24c11f33b 100644
--- a/vendor/k8s.io/client-go/discovery/discovery_client.go
+++ b/vendor/k8s.io/client-go/discovery/discovery_client.go
@@ -23,10 +23,9 @@ import (
"sort"
"strings"
- "github.com/emicklei/go-restful-swagger12"
+ "github.com/golang/protobuf/proto"
+ "github.com/googleapis/gnostic/OpenAPIv2"
- "github.com/go-openapi/loads"
- "github.com/go-openapi/spec"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
@@ -34,7 +33,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/kubernetes/scheme"
- "k8s.io/client-go/pkg/api/v1"
restclient "k8s.io/client-go/rest"
)
@@ -48,14 +46,17 @@ type DiscoveryInterface interface {
ServerGroupsInterface
ServerResourcesInterface
ServerVersionInterface
- SwaggerSchemaInterface
OpenAPISchemaInterface
}
// CachedDiscoveryInterface is a DiscoveryInterface with cache invalidation and freshness.
type CachedDiscoveryInterface interface {
DiscoveryInterface
- // Fresh returns true if no cached data was used that had been retrieved before the instantiation.
+ // Fresh is supposed to tell the caller whether or not to retry if the cache
+ // fails to find something (false = retry, true = no need to retry).
+ //
+ // TODO: this needs to be revisited, this interface can't be locked properly
+ // and doesn't make a lot of sense.
Fresh() bool
// Invalidate enforces that no cached data is used in the future that is older than the current time.
Invalidate()
@@ -88,16 +89,10 @@ type ServerVersionInterface interface {
ServerVersion() (*version.Info, error)
}
-// 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 schema.GroupVersion) (*swagger.ApiDeclaration, error)
-}
-
// OpenAPISchemaInterface has a method to retrieve the open API schema.
type OpenAPISchemaInterface interface {
// OpenAPISchema retrieves and parses the swagger API schema the server supports.
- OpenAPISchema() (*spec.Swagger, error)
+ OpenAPISchema() (*openapi_v2.Document, error)
}
// DiscoveryClient implements the functions that discover server-supported API groups,
@@ -150,9 +145,9 @@ func (d *DiscoveryClient) ServerGroups() (apiGroupList *metav1.APIGroupList, err
apiGroupList = &metav1.APIGroupList{}
}
- // append the group retrieved from /api to the list if not empty
+ // prepend the group retrieved from /api to the list if not empty
if len(v.Versions) != 0 {
- apiGroupList.Groups = append(apiGroupList.Groups, apiGroup)
+ apiGroupList.Groups = append([]metav1.APIGroup{apiGroup}, apiGroupList.Groups...)
}
return apiGroupList, nil
}
@@ -332,54 +327,18 @@ func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
return &info, nil
}
-// SwaggerSchema retrieves and parses the swagger API schema the server supports.
-// TODO: Replace usages with Open API. Tracked in https://github.com/kubernetes/kubernetes/issues/44589
-func (d *DiscoveryClient) SwaggerSchema(version schema.GroupVersion) (*swagger.ApiDeclaration, error) {
- if version.Empty() {
- return nil, fmt.Errorf("groupVersion cannot be empty")
- }
-
- groupList, err := d.ServerGroups()
+// OpenAPISchema fetches the open api schema using a rest client and parses the proto.
+func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
+ data, err := d.restClient.Get().AbsPath("/swagger-2.0.0.pb-v1").Do().Raw()
if err != nil {
return nil, err
}
- groupVersions := metav1.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 len(d.LegacyPrefix) > 0 && version == v1.SchemeGroupVersion {
- path = "/swaggerapi" + d.LegacyPrefix + "/" + version.Version
- } else {
- path = "/swaggerapi/apis/" + version.Group + "/" + version.Version
- }
-
- body, err := d.restClient.Get().AbsPath(path).Do().Raw()
+ document := &openapi_v2.Document{}
+ err = proto.Unmarshal(data, document)
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
-}
-
-// OpenAPISchema fetches the open api schema using a rest client and parses the json.
-// Warning: this is very expensive (~1.2s)
-func (d *DiscoveryClient) OpenAPISchema() (*spec.Swagger, error) {
- data, err := d.restClient.Get().AbsPath("/swagger.json").Do().Raw()
- if err != nil {
- return nil, err
- }
- msg := json.RawMessage(data)
- doc, err := loads.Analyzed(msg, "")
- if err != nil {
- return nil, err
- }
- return doc.Spec(), err
+ return document, nil
}
// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
@@ -420,7 +379,7 @@ func NewDiscoveryClientForConfig(c *restclient.Config) (*DiscoveryClient, error)
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api"}, err
}
-// NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. If
+// NewDiscoveryClientForConfigOrDie creates a new DiscoveryClient for the given config. If
// there is an error, it panics.
func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
client, err := NewDiscoveryClientForConfig(c)
@@ -431,20 +390,11 @@ func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
}
-// New creates a new DiscoveryClient for the given RESTClient.
+// NewDiscoveryClient returns a new DiscoveryClient for the given RESTClient.
func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api"}
}
-func stringDoesntExistIn(str string, slice []string) bool {
- for _, s := range slice {
- if s == str {
- return false
- }
- }
- return true
-}
-
// RESTClient returns a RESTClient that is used to communicate
// with API server by this client implementation.
func (c *DiscoveryClient) RESTClient() restclient.Interface {