discovery_client.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package discovery
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "net/url"
  18. "sort"
  19. "strings"
  20. "github.com/emicklei/go-restful/swagger"
  21. "k8s.io/apimachinery/pkg/api/errors"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. "k8s.io/apimachinery/pkg/runtime"
  24. "k8s.io/apimachinery/pkg/runtime/schema"
  25. "k8s.io/apimachinery/pkg/runtime/serializer"
  26. "k8s.io/apimachinery/pkg/version"
  27. "k8s.io/client-go/pkg/api"
  28. "k8s.io/client-go/pkg/api/v1"
  29. restclient "k8s.io/client-go/rest"
  30. )
  31. // defaultRetries is the number of times a resource discovery is repeated if an api group disappears on the fly (e.g. ThirdPartyResources).
  32. const defaultRetries = 2
  33. // DiscoveryInterface holds the methods that discover server-supported API groups,
  34. // versions and resources.
  35. type DiscoveryInterface interface {
  36. RESTClient() restclient.Interface
  37. ServerGroupsInterface
  38. ServerResourcesInterface
  39. ServerVersionInterface
  40. SwaggerSchemaInterface
  41. }
  42. // CachedDiscoveryInterface is a DiscoveryInterface with cache invalidation and freshness.
  43. type CachedDiscoveryInterface interface {
  44. DiscoveryInterface
  45. // Fresh returns true if no cached data was used that had been retrieved before the instantiation.
  46. Fresh() bool
  47. // Invalidate enforces that no cached data is used in the future that is older than the current time.
  48. Invalidate()
  49. }
  50. // ServerGroupsInterface has methods for obtaining supported groups on the API server
  51. type ServerGroupsInterface interface {
  52. // ServerGroups returns the supported groups, with information like supported versions and the
  53. // preferred version.
  54. ServerGroups() (*metav1.APIGroupList, error)
  55. }
  56. // ServerResourcesInterface has methods for obtaining supported resources on the API server
  57. type ServerResourcesInterface interface {
  58. // ServerResourcesForGroupVersion returns the supported resources for a group and version.
  59. ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error)
  60. // ServerResources returns the supported resources for all groups and versions.
  61. ServerResources() ([]*metav1.APIResourceList, error)
  62. // ServerPreferredResources returns the supported resources with the version preferred by the
  63. // server.
  64. ServerPreferredResources() ([]*metav1.APIResourceList, error)
  65. // ServerPreferredNamespacedResources returns the supported namespaced resources with the
  66. // version preferred by the server.
  67. ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error)
  68. }
  69. // ServerVersionInterface has a method for retrieving the server's version.
  70. type ServerVersionInterface interface {
  71. // ServerVersion retrieves and parses the server's version (git version).
  72. ServerVersion() (*version.Info, error)
  73. }
  74. // SwaggerSchemaInterface has a method to retrieve the swagger schema.
  75. type SwaggerSchemaInterface interface {
  76. // SwaggerSchema retrieves and parses the swagger API schema the server supports.
  77. SwaggerSchema(version schema.GroupVersion) (*swagger.ApiDeclaration, error)
  78. }
  79. // DiscoveryClient implements the functions that discover server-supported API groups,
  80. // versions and resources.
  81. type DiscoveryClient struct {
  82. restClient restclient.Interface
  83. LegacyPrefix string
  84. }
  85. // Convert metav1.APIVersions to metav1.APIGroup. APIVersions is used by legacy v1, so
  86. // group would be "".
  87. func apiVersionsToAPIGroup(apiVersions *metav1.APIVersions) (apiGroup metav1.APIGroup) {
  88. groupVersions := []metav1.GroupVersionForDiscovery{}
  89. for _, version := range apiVersions.Versions {
  90. groupVersion := metav1.GroupVersionForDiscovery{
  91. GroupVersion: version,
  92. Version: version,
  93. }
  94. groupVersions = append(groupVersions, groupVersion)
  95. }
  96. apiGroup.Versions = groupVersions
  97. // There should be only one groupVersion returned at /api
  98. apiGroup.PreferredVersion = groupVersions[0]
  99. return
  100. }
  101. // ServerGroups returns the supported groups, with information like supported versions and the
  102. // preferred version.
  103. func (d *DiscoveryClient) ServerGroups() (apiGroupList *metav1.APIGroupList, err error) {
  104. // Get the groupVersions exposed at /api
  105. v := &metav1.APIVersions{}
  106. err = d.restClient.Get().AbsPath(d.LegacyPrefix).Do().Into(v)
  107. apiGroup := metav1.APIGroup{}
  108. if err == nil && len(v.Versions) != 0 {
  109. apiGroup = apiVersionsToAPIGroup(v)
  110. }
  111. if err != nil && !errors.IsNotFound(err) && !errors.IsForbidden(err) {
  112. return nil, err
  113. }
  114. // Get the groupVersions exposed at /apis
  115. apiGroupList = &metav1.APIGroupList{}
  116. err = d.restClient.Get().AbsPath("/apis").Do().Into(apiGroupList)
  117. if err != nil && !errors.IsNotFound(err) && !errors.IsForbidden(err) {
  118. return nil, err
  119. }
  120. // to be compatible with a v1.0 server, if it's a 403 or 404, ignore and return whatever we got from /api
  121. if err != nil && (errors.IsNotFound(err) || errors.IsForbidden(err)) {
  122. apiGroupList = &metav1.APIGroupList{}
  123. }
  124. // append the group retrieved from /api to the list if not empty
  125. if len(v.Versions) != 0 {
  126. apiGroupList.Groups = append(apiGroupList.Groups, apiGroup)
  127. }
  128. return apiGroupList, nil
  129. }
  130. // ServerResourcesForGroupVersion returns the supported resources for a group and version.
  131. func (d *DiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (resources *metav1.APIResourceList, err error) {
  132. url := url.URL{}
  133. if len(groupVersion) == 0 {
  134. return nil, fmt.Errorf("groupVersion shouldn't be empty")
  135. }
  136. if len(d.LegacyPrefix) > 0 && groupVersion == "v1" {
  137. url.Path = d.LegacyPrefix + "/" + groupVersion
  138. } else {
  139. url.Path = "/apis/" + groupVersion
  140. }
  141. resources = &metav1.APIResourceList{
  142. GroupVersion: groupVersion,
  143. }
  144. err = d.restClient.Get().AbsPath(url.String()).Do().Into(resources)
  145. if err != nil {
  146. // ignore 403 or 404 error to be compatible with an v1.0 server.
  147. if groupVersion == "v1" && (errors.IsNotFound(err) || errors.IsForbidden(err)) {
  148. return resources, nil
  149. }
  150. return nil, err
  151. }
  152. return resources, nil
  153. }
  154. // serverResources returns the supported resources for all groups and versions.
  155. func (d *DiscoveryClient) serverResources(failEarly bool) ([]*metav1.APIResourceList, error) {
  156. apiGroups, err := d.ServerGroups()
  157. if err != nil {
  158. return nil, err
  159. }
  160. result := []*metav1.APIResourceList{}
  161. failedGroups := make(map[schema.GroupVersion]error)
  162. for _, apiGroup := range apiGroups.Groups {
  163. for _, version := range apiGroup.Versions {
  164. gv := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
  165. resources, err := d.ServerResourcesForGroupVersion(version.GroupVersion)
  166. if err != nil {
  167. // TODO: maybe restrict this to NotFound errors
  168. failedGroups[gv] = err
  169. if failEarly {
  170. return nil, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  171. }
  172. continue
  173. }
  174. result = append(result, resources)
  175. }
  176. }
  177. if len(failedGroups) == 0 {
  178. return result, nil
  179. }
  180. return result, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  181. }
  182. // ServerResources returns the supported resources for all groups and versions.
  183. func (d *DiscoveryClient) ServerResources() ([]*metav1.APIResourceList, error) {
  184. return withRetries(defaultRetries, d.serverResources)
  185. }
  186. // ErrGroupDiscoveryFailed is returned if one or more API groups fail to load.
  187. type ErrGroupDiscoveryFailed struct {
  188. // Groups is a list of the groups that failed to load and the error cause
  189. Groups map[schema.GroupVersion]error
  190. }
  191. // Error implements the error interface
  192. func (e *ErrGroupDiscoveryFailed) Error() string {
  193. var groups []string
  194. for k, v := range e.Groups {
  195. groups = append(groups, fmt.Sprintf("%s: %v", k, v))
  196. }
  197. sort.Strings(groups)
  198. return fmt.Sprintf("unable to retrieve the complete list of server APIs: %s", strings.Join(groups, ", "))
  199. }
  200. // IsGroupDiscoveryFailedError returns true if the provided error indicates the server was unable to discover
  201. // a complete list of APIs for the client to use.
  202. func IsGroupDiscoveryFailedError(err error) bool {
  203. _, ok := err.(*ErrGroupDiscoveryFailed)
  204. return err != nil && ok
  205. }
  206. // serverPreferredResources returns the supported resources with the version preferred by the server.
  207. func (d *DiscoveryClient) serverPreferredResources(failEarly bool) ([]*metav1.APIResourceList, error) {
  208. serverGroupList, err := d.ServerGroups()
  209. if err != nil {
  210. return nil, err
  211. }
  212. result := []*metav1.APIResourceList{}
  213. failedGroups := make(map[schema.GroupVersion]error)
  214. grVersions := map[schema.GroupResource]string{} // selected version of a GroupResource
  215. grApiResources := map[schema.GroupResource]*metav1.APIResource{} // selected APIResource for a GroupResource
  216. gvApiResourceLists := map[schema.GroupVersion]*metav1.APIResourceList{} // blueprint for a APIResourceList for later grouping
  217. for _, apiGroup := range serverGroupList.Groups {
  218. for _, version := range apiGroup.Versions {
  219. groupVersion := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
  220. apiResourceList, err := d.ServerResourcesForGroupVersion(version.GroupVersion)
  221. if err != nil {
  222. // TODO: maybe restrict this to NotFound errors
  223. failedGroups[groupVersion] = err
  224. if failEarly {
  225. return nil, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  226. }
  227. continue
  228. }
  229. // create empty list which is filled later in another loop
  230. emptyApiResourceList := metav1.APIResourceList{
  231. GroupVersion: version.GroupVersion,
  232. }
  233. gvApiResourceLists[groupVersion] = &emptyApiResourceList
  234. result = append(result, &emptyApiResourceList)
  235. for i := range apiResourceList.APIResources {
  236. apiResource := &apiResourceList.APIResources[i]
  237. if strings.Contains(apiResource.Name, "/") {
  238. continue
  239. }
  240. gv := schema.GroupResource{Group: apiGroup.Name, Resource: apiResource.Name}
  241. if _, ok := grApiResources[gv]; ok && version.Version != apiGroup.PreferredVersion.Version {
  242. // only override with preferred version
  243. continue
  244. }
  245. grVersions[gv] = version.Version
  246. grApiResources[gv] = apiResource
  247. }
  248. }
  249. }
  250. // group selected APIResources according to GroupVersion into APIResourceLists
  251. for groupResource, apiResource := range grApiResources {
  252. version := grVersions[groupResource]
  253. groupVersion := schema.GroupVersion{Group: groupResource.Group, Version: version}
  254. apiResourceList := gvApiResourceLists[groupVersion]
  255. apiResourceList.APIResources = append(apiResourceList.APIResources, *apiResource)
  256. }
  257. if len(failedGroups) == 0 {
  258. return result, nil
  259. }
  260. return result, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  261. }
  262. // ServerPreferredResources returns the supported resources with the version preferred by the
  263. // server.
  264. func (d *DiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
  265. return withRetries(defaultRetries, func(retryEarly bool) ([]*metav1.APIResourceList, error) {
  266. return d.serverPreferredResources(retryEarly)
  267. })
  268. }
  269. // ServerPreferredNamespacedResources returns the supported namespaced resources with the
  270. // version preferred by the server.
  271. func (d *DiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
  272. all, err := d.ServerPreferredResources()
  273. return FilteredBy(ResourcePredicateFunc(func(groupVersion string, r *metav1.APIResource) bool {
  274. return r.Namespaced
  275. }), all), err
  276. }
  277. // ServerVersion retrieves and parses the server's version (git version).
  278. func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
  279. body, err := d.restClient.Get().AbsPath("/version").Do().Raw()
  280. if err != nil {
  281. return nil, err
  282. }
  283. var info version.Info
  284. err = json.Unmarshal(body, &info)
  285. if err != nil {
  286. return nil, fmt.Errorf("got '%s': %v", string(body), err)
  287. }
  288. return &info, nil
  289. }
  290. // SwaggerSchema retrieves and parses the swagger API schema the server supports.
  291. func (d *DiscoveryClient) SwaggerSchema(version schema.GroupVersion) (*swagger.ApiDeclaration, error) {
  292. if version.Empty() {
  293. return nil, fmt.Errorf("groupVersion cannot be empty")
  294. }
  295. groupList, err := d.ServerGroups()
  296. if err != nil {
  297. return nil, err
  298. }
  299. groupVersions := metav1.ExtractGroupVersions(groupList)
  300. // This check also takes care the case that kubectl is newer than the running endpoint
  301. if stringDoesntExistIn(version.String(), groupVersions) {
  302. return nil, fmt.Errorf("API version: %v is not supported by the server. Use one of: %v", version, groupVersions)
  303. }
  304. var path string
  305. if len(d.LegacyPrefix) > 0 && version == v1.SchemeGroupVersion {
  306. path = "/swaggerapi" + d.LegacyPrefix + "/" + version.Version
  307. } else {
  308. path = "/swaggerapi/apis/" + version.Group + "/" + version.Version
  309. }
  310. body, err := d.restClient.Get().AbsPath(path).Do().Raw()
  311. if err != nil {
  312. return nil, err
  313. }
  314. var schema swagger.ApiDeclaration
  315. err = json.Unmarshal(body, &schema)
  316. if err != nil {
  317. return nil, fmt.Errorf("got '%s': %v", string(body), err)
  318. }
  319. return &schema, nil
  320. }
  321. // withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
  322. func withRetries(maxRetries int, f func(failEarly bool) ([]*metav1.APIResourceList, error)) ([]*metav1.APIResourceList, error) {
  323. var result []*metav1.APIResourceList
  324. var err error
  325. for i := 0; i < maxRetries; i++ {
  326. failEarly := i < maxRetries-1
  327. result, err = f(failEarly)
  328. if err == nil {
  329. return result, nil
  330. }
  331. if _, ok := err.(*ErrGroupDiscoveryFailed); !ok {
  332. return nil, err
  333. }
  334. }
  335. return result, err
  336. }
  337. func setDiscoveryDefaults(config *restclient.Config) error {
  338. config.APIPath = ""
  339. config.GroupVersion = nil
  340. codec := runtime.NoopEncoder{Decoder: api.Codecs.UniversalDecoder()}
  341. config.NegotiatedSerializer = serializer.NegotiatedSerializerWrapper(runtime.SerializerInfo{Serializer: codec})
  342. if len(config.UserAgent) == 0 {
  343. config.UserAgent = restclient.DefaultKubernetesUserAgent()
  344. }
  345. return nil
  346. }
  347. // NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. This client
  348. // can be used to discover supported resources in the API server.
  349. func NewDiscoveryClientForConfig(c *restclient.Config) (*DiscoveryClient, error) {
  350. config := *c
  351. if err := setDiscoveryDefaults(&config); err != nil {
  352. return nil, err
  353. }
  354. client, err := restclient.UnversionedRESTClientFor(&config)
  355. return &DiscoveryClient{restClient: client, LegacyPrefix: "/api"}, err
  356. }
  357. // NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. If
  358. // there is an error, it panics.
  359. func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
  360. client, err := NewDiscoveryClientForConfig(c)
  361. if err != nil {
  362. panic(err)
  363. }
  364. return client
  365. }
  366. // New creates a new DiscoveryClient for the given RESTClient.
  367. func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
  368. return &DiscoveryClient{restClient: c, LegacyPrefix: "/api"}
  369. }
  370. func stringDoesntExistIn(str string, slice []string) bool {
  371. for _, s := range slice {
  372. if s == str {
  373. return false
  374. }
  375. }
  376. return true
  377. }
  378. // RESTClient returns a RESTClient that is used to communicate
  379. // with API server by this client implementation.
  380. func (c *DiscoveryClient) RESTClient() restclient.Interface {
  381. if c == nil {
  382. return nil
  383. }
  384. return c.restClient
  385. }