discovery_client.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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. "context"
  16. "encoding/json"
  17. "fmt"
  18. "mime"
  19. "net/http"
  20. "net/url"
  21. "sort"
  22. "strings"
  23. "sync"
  24. "time"
  25. //nolint:staticcheck // SA1019 Keep using module since it's still being maintained and the api of google.golang.org/protobuf/proto differs
  26. "github.com/golang/protobuf/proto"
  27. openapi_v2 "github.com/google/gnostic-models/openapiv2"
  28. apidiscovery "k8s.io/api/apidiscovery/v2beta1"
  29. "k8s.io/apimachinery/pkg/api/errors"
  30. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  31. "k8s.io/apimachinery/pkg/runtime"
  32. "k8s.io/apimachinery/pkg/runtime/schema"
  33. "k8s.io/apimachinery/pkg/runtime/serializer"
  34. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  35. "k8s.io/apimachinery/pkg/version"
  36. "k8s.io/client-go/kubernetes/scheme"
  37. "k8s.io/client-go/openapi"
  38. restclient "k8s.io/client-go/rest"
  39. )
  40. const (
  41. // defaultRetries is the number of times a resource discovery is repeated if an api group disappears on the fly (e.g. CustomResourceDefinitions).
  42. defaultRetries = 2
  43. // protobuf mime type
  44. openAPIV2mimePb = "application/com.github.proto-openapi.spec.v2@v1.0+protobuf"
  45. // defaultTimeout is the maximum amount of time per request when no timeout has been set on a RESTClient.
  46. // Defaults to 32s in order to have a distinguishable length of time, relative to other timeouts that exist.
  47. defaultTimeout = 32 * time.Second
  48. // defaultBurst is the default burst to be used with the discovery client's token bucket rate limiter
  49. defaultBurst = 300
  50. AcceptV1 = runtime.ContentTypeJSON
  51. // Aggregated discovery content-type (v2beta1). NOTE: content-type parameters
  52. // MUST be ordered (g, v, as) for server in "Accept" header (BUT we are resilient
  53. // to ordering when comparing returned values in "Content-Type" header).
  54. AcceptV2Beta1 = runtime.ContentTypeJSON + ";" + "g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList"
  55. // Prioritize aggregated discovery by placing first in the order of discovery accept types.
  56. acceptDiscoveryFormats = AcceptV2Beta1 + "," + AcceptV1
  57. )
  58. // Aggregated discovery content-type GVK.
  59. var v2Beta1GVK = schema.GroupVersionKind{Group: "apidiscovery.k8s.io", Version: "v2beta1", Kind: "APIGroupDiscoveryList"}
  60. // DiscoveryInterface holds the methods that discover server-supported API groups,
  61. // versions and resources.
  62. type DiscoveryInterface interface {
  63. RESTClient() restclient.Interface
  64. ServerGroupsInterface
  65. ServerResourcesInterface
  66. ServerVersionInterface
  67. OpenAPISchemaInterface
  68. OpenAPIV3SchemaInterface
  69. // Returns copy of current discovery client that will only
  70. // receive the legacy discovery format, or pointer to current
  71. // discovery client if it does not support legacy-only discovery.
  72. WithLegacy() DiscoveryInterface
  73. }
  74. // AggregatedDiscoveryInterface extends DiscoveryInterface to include a method to possibly
  75. // return discovery resources along with the discovery groups, which is what the newer
  76. // aggregated discovery format does (APIGroupDiscoveryList).
  77. type AggregatedDiscoveryInterface interface {
  78. DiscoveryInterface
  79. GroupsAndMaybeResources() (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, map[schema.GroupVersion]error, error)
  80. }
  81. // CachedDiscoveryInterface is a DiscoveryInterface with cache invalidation and freshness.
  82. // Note that If the ServerResourcesForGroupVersion method returns a cache miss
  83. // error, the user needs to explicitly call Invalidate to clear the cache,
  84. // otherwise the same cache miss error will be returned next time.
  85. type CachedDiscoveryInterface interface {
  86. DiscoveryInterface
  87. // Fresh is supposed to tell the caller whether or not to retry if the cache
  88. // fails to find something (false = retry, true = no need to retry).
  89. //
  90. // TODO: this needs to be revisited, this interface can't be locked properly
  91. // and doesn't make a lot of sense.
  92. Fresh() bool
  93. // Invalidate enforces that no cached data that is older than the current time
  94. // is used.
  95. Invalidate()
  96. }
  97. // ServerGroupsInterface has methods for obtaining supported groups on the API server
  98. type ServerGroupsInterface interface {
  99. // ServerGroups returns the supported groups, with information like supported versions and the
  100. // preferred version.
  101. ServerGroups() (*metav1.APIGroupList, error)
  102. }
  103. // ServerResourcesInterface has methods for obtaining supported resources on the API server
  104. type ServerResourcesInterface interface {
  105. // ServerResourcesForGroupVersion returns the supported resources for a group and version.
  106. ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error)
  107. // ServerGroupsAndResources returns the supported groups and resources for all groups and versions.
  108. //
  109. // The returned group and resource lists might be non-nil with partial results even in the
  110. // case of non-nil error.
  111. ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error)
  112. // ServerPreferredResources returns the supported resources with the version preferred by the
  113. // server.
  114. //
  115. // The returned group and resource lists might be non-nil with partial results even in the
  116. // case of non-nil error.
  117. ServerPreferredResources() ([]*metav1.APIResourceList, error)
  118. // ServerPreferredNamespacedResources returns the supported namespaced resources with the
  119. // version preferred by the server.
  120. //
  121. // The returned resource list might be non-nil with partial results even in the case of
  122. // non-nil error.
  123. ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error)
  124. }
  125. // ServerVersionInterface has a method for retrieving the server's version.
  126. type ServerVersionInterface interface {
  127. // ServerVersion retrieves and parses the server's version (git version).
  128. ServerVersion() (*version.Info, error)
  129. }
  130. // OpenAPISchemaInterface has a method to retrieve the open API schema.
  131. type OpenAPISchemaInterface interface {
  132. // OpenAPISchema retrieves and parses the swagger API schema the server supports.
  133. OpenAPISchema() (*openapi_v2.Document, error)
  134. }
  135. type OpenAPIV3SchemaInterface interface {
  136. OpenAPIV3() openapi.Client
  137. }
  138. // DiscoveryClient implements the functions that discover server-supported API groups,
  139. // versions and resources.
  140. type DiscoveryClient struct {
  141. restClient restclient.Interface
  142. LegacyPrefix string
  143. // Forces the client to request only "unaggregated" (legacy) discovery.
  144. UseLegacyDiscovery bool
  145. }
  146. var _ AggregatedDiscoveryInterface = &DiscoveryClient{}
  147. // Convert metav1.APIVersions to metav1.APIGroup. APIVersions is used by legacy v1, so
  148. // group would be "".
  149. func apiVersionsToAPIGroup(apiVersions *metav1.APIVersions) (apiGroup metav1.APIGroup) {
  150. groupVersions := []metav1.GroupVersionForDiscovery{}
  151. for _, version := range apiVersions.Versions {
  152. groupVersion := metav1.GroupVersionForDiscovery{
  153. GroupVersion: version,
  154. Version: version,
  155. }
  156. groupVersions = append(groupVersions, groupVersion)
  157. }
  158. apiGroup.Versions = groupVersions
  159. // There should be only one groupVersion returned at /api
  160. apiGroup.PreferredVersion = groupVersions[0]
  161. return
  162. }
  163. // GroupsAndMaybeResources returns the discovery groups, and (if new aggregated
  164. // discovery format) the resources keyed by group/version. Merges discovery groups
  165. // and resources from /api and /apis (either aggregated or not). Legacy groups
  166. // must be ordered first. The server will either return both endpoints (/api, /apis)
  167. // as aggregated discovery format or legacy format. For safety, resources will only
  168. // be returned if both endpoints returned resources. Returned "failedGVs" can be
  169. // empty, but will only be nil in the case an error is returned.
  170. func (d *DiscoveryClient) GroupsAndMaybeResources() (
  171. *metav1.APIGroupList,
  172. map[schema.GroupVersion]*metav1.APIResourceList,
  173. map[schema.GroupVersion]error,
  174. error) {
  175. // Legacy group ordered first (there is only one -- core/v1 group). Returned groups must
  176. // be non-nil, but it could be empty. Returned resources, apiResources map could be nil.
  177. groups, resources, failedGVs, err := d.downloadLegacy()
  178. if err != nil {
  179. return nil, nil, nil, err
  180. }
  181. // Discovery groups and (possibly) resources downloaded from /apis.
  182. apiGroups, apiResources, failedApisGVs, aerr := d.downloadAPIs()
  183. if aerr != nil {
  184. return nil, nil, nil, aerr
  185. }
  186. // Merge apis groups into the legacy groups.
  187. for _, group := range apiGroups.Groups {
  188. groups.Groups = append(groups.Groups, group)
  189. }
  190. // For safety, only return resources if both endpoints returned resources.
  191. if resources != nil && apiResources != nil {
  192. for gv, resourceList := range apiResources {
  193. resources[gv] = resourceList
  194. }
  195. } else if resources != nil {
  196. resources = nil
  197. }
  198. // Merge failed GroupVersions from /api and /apis
  199. for gv, err := range failedApisGVs {
  200. failedGVs[gv] = err
  201. }
  202. return groups, resources, failedGVs, err
  203. }
  204. // downloadLegacy returns the discovery groups and possibly resources
  205. // for the legacy v1 GVR at /api, or an error if one occurred. It is
  206. // possible for the resource map to be nil if the server returned
  207. // the unaggregated discovery. Returned "failedGVs" can be empty, but
  208. // will only be nil in the case of a returned error.
  209. func (d *DiscoveryClient) downloadLegacy() (
  210. *metav1.APIGroupList,
  211. map[schema.GroupVersion]*metav1.APIResourceList,
  212. map[schema.GroupVersion]error,
  213. error) {
  214. accept := acceptDiscoveryFormats
  215. if d.UseLegacyDiscovery {
  216. accept = AcceptV1
  217. }
  218. var responseContentType string
  219. body, err := d.restClient.Get().
  220. AbsPath("/api").
  221. SetHeader("Accept", accept).
  222. Do(context.TODO()).
  223. ContentType(&responseContentType).
  224. Raw()
  225. apiGroupList := &metav1.APIGroupList{}
  226. failedGVs := map[schema.GroupVersion]error{}
  227. if err != nil {
  228. // Tolerate 404, since aggregated api servers can return it.
  229. if errors.IsNotFound(err) {
  230. // Return empty structures and no error.
  231. emptyGVMap := map[schema.GroupVersion]*metav1.APIResourceList{}
  232. return apiGroupList, emptyGVMap, failedGVs, nil
  233. } else {
  234. return nil, nil, nil, err
  235. }
  236. }
  237. var resourcesByGV map[schema.GroupVersion]*metav1.APIResourceList
  238. // Based on the content-type server responded with: aggregated or unaggregated.
  239. if isGVK, _ := ContentTypeIsGVK(responseContentType, v2Beta1GVK); isGVK {
  240. var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
  241. err = json.Unmarshal(body, &aggregatedDiscovery)
  242. if err != nil {
  243. return nil, nil, nil, err
  244. }
  245. apiGroupList, resourcesByGV, failedGVs = SplitGroupsAndResources(aggregatedDiscovery)
  246. } else {
  247. // Default is unaggregated discovery v1.
  248. var v metav1.APIVersions
  249. err = json.Unmarshal(body, &v)
  250. if err != nil {
  251. return nil, nil, nil, err
  252. }
  253. apiGroup := metav1.APIGroup{}
  254. if len(v.Versions) != 0 {
  255. apiGroup = apiVersionsToAPIGroup(&v)
  256. }
  257. apiGroupList.Groups = []metav1.APIGroup{apiGroup}
  258. }
  259. return apiGroupList, resourcesByGV, failedGVs, nil
  260. }
  261. // downloadAPIs returns the discovery groups and (if aggregated format) the
  262. // discovery resources. The returned groups will always exist, but the
  263. // resources map may be nil. Returned "failedGVs" can be empty, but will
  264. // only be nil in the case of a returned error.
  265. func (d *DiscoveryClient) downloadAPIs() (
  266. *metav1.APIGroupList,
  267. map[schema.GroupVersion]*metav1.APIResourceList,
  268. map[schema.GroupVersion]error,
  269. error) {
  270. accept := acceptDiscoveryFormats
  271. if d.UseLegacyDiscovery {
  272. accept = AcceptV1
  273. }
  274. var responseContentType string
  275. body, err := d.restClient.Get().
  276. AbsPath("/apis").
  277. SetHeader("Accept", accept).
  278. Do(context.TODO()).
  279. ContentType(&responseContentType).
  280. Raw()
  281. if err != nil {
  282. return nil, nil, nil, err
  283. }
  284. apiGroupList := &metav1.APIGroupList{}
  285. failedGVs := map[schema.GroupVersion]error{}
  286. var resourcesByGV map[schema.GroupVersion]*metav1.APIResourceList
  287. // Based on the content-type server responded with: aggregated or unaggregated.
  288. if isGVK, _ := ContentTypeIsGVK(responseContentType, v2Beta1GVK); isGVK {
  289. var aggregatedDiscovery apidiscovery.APIGroupDiscoveryList
  290. err = json.Unmarshal(body, &aggregatedDiscovery)
  291. if err != nil {
  292. return nil, nil, nil, err
  293. }
  294. apiGroupList, resourcesByGV, failedGVs = SplitGroupsAndResources(aggregatedDiscovery)
  295. } else {
  296. // Default is unaggregated discovery v1.
  297. err = json.Unmarshal(body, apiGroupList)
  298. if err != nil {
  299. return nil, nil, nil, err
  300. }
  301. }
  302. return apiGroupList, resourcesByGV, failedGVs, nil
  303. }
  304. // ContentTypeIsGVK checks of the content-type string is both
  305. // "application/json" and matches the provided GVK. An error
  306. // is returned if the content type string is malformed.
  307. // NOTE: This function is resilient to the ordering of the
  308. // content-type parameters, as well as parameters added by
  309. // intermediaries such as proxies or gateways. Examples:
  310. //
  311. // ("application/json; g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
  312. // ("application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
  313. // ("application/json; as=APIGroupDiscoveryList;v=v2beta1;g=apidiscovery.k8s.io;charset=utf-8", {apidiscovery.k8s.io, v2beta1, APIGroupDiscoveryList}) = (true, nil)
  314. // ("application/json", any GVK) = (false, nil)
  315. // ("application/json; charset=UTF-8", any GVK) = (false, nil)
  316. // ("malformed content type string", any GVK) = (false, error)
  317. func ContentTypeIsGVK(contentType string, gvk schema.GroupVersionKind) (bool, error) {
  318. base, params, err := mime.ParseMediaType(contentType)
  319. if err != nil {
  320. return false, err
  321. }
  322. gvkMatch := runtime.ContentTypeJSON == base &&
  323. params["g"] == gvk.Group &&
  324. params["v"] == gvk.Version &&
  325. params["as"] == gvk.Kind
  326. return gvkMatch, nil
  327. }
  328. // ServerGroups returns the supported groups, with information like supported versions and the
  329. // preferred version.
  330. func (d *DiscoveryClient) ServerGroups() (*metav1.APIGroupList, error) {
  331. groups, _, _, err := d.GroupsAndMaybeResources()
  332. if err != nil {
  333. return nil, err
  334. }
  335. return groups, nil
  336. }
  337. // ServerResourcesForGroupVersion returns the supported resources for a group and version.
  338. func (d *DiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (resources *metav1.APIResourceList, err error) {
  339. url := url.URL{}
  340. if len(groupVersion) == 0 {
  341. return nil, fmt.Errorf("groupVersion shouldn't be empty")
  342. }
  343. if len(d.LegacyPrefix) > 0 && groupVersion == "v1" {
  344. url.Path = d.LegacyPrefix + "/" + groupVersion
  345. } else {
  346. url.Path = "/apis/" + groupVersion
  347. }
  348. resources = &metav1.APIResourceList{
  349. GroupVersion: groupVersion,
  350. }
  351. err = d.restClient.Get().AbsPath(url.String()).Do(context.TODO()).Into(resources)
  352. if err != nil {
  353. // Tolerate core/v1 not found response by returning empty resource list;
  354. // this probably should not happen. But we should verify all callers are
  355. // not depending on this toleration before removal.
  356. if groupVersion == "v1" && errors.IsNotFound(err) {
  357. return resources, nil
  358. }
  359. return nil, err
  360. }
  361. return resources, nil
  362. }
  363. // ServerGroupsAndResources returns the supported resources for all groups and versions.
  364. func (d *DiscoveryClient) ServerGroupsAndResources() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
  365. return withRetries(defaultRetries, func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
  366. return ServerGroupsAndResources(d)
  367. })
  368. }
  369. // ErrGroupDiscoveryFailed is returned if one or more API groups fail to load.
  370. type ErrGroupDiscoveryFailed struct {
  371. // Groups is a list of the groups that failed to load and the error cause
  372. Groups map[schema.GroupVersion]error
  373. }
  374. // Error implements the error interface
  375. func (e *ErrGroupDiscoveryFailed) Error() string {
  376. var groups []string
  377. for k, v := range e.Groups {
  378. groups = append(groups, fmt.Sprintf("%s: %v", k, v))
  379. }
  380. sort.Strings(groups)
  381. return fmt.Sprintf("unable to retrieve the complete list of server APIs: %s", strings.Join(groups, ", "))
  382. }
  383. // IsGroupDiscoveryFailedError returns true if the provided error indicates the server was unable to discover
  384. // a complete list of APIs for the client to use.
  385. func IsGroupDiscoveryFailedError(err error) bool {
  386. _, ok := err.(*ErrGroupDiscoveryFailed)
  387. return err != nil && ok
  388. }
  389. func ServerGroupsAndResources(d DiscoveryInterface) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
  390. var sgs *metav1.APIGroupList
  391. var resources []*metav1.APIResourceList
  392. var failedGVs map[schema.GroupVersion]error
  393. var err error
  394. // If the passed discovery object implements the wider AggregatedDiscoveryInterface,
  395. // then attempt to retrieve aggregated discovery with both groups and the resources.
  396. if ad, ok := d.(AggregatedDiscoveryInterface); ok {
  397. var resourcesByGV map[schema.GroupVersion]*metav1.APIResourceList
  398. sgs, resourcesByGV, failedGVs, err = ad.GroupsAndMaybeResources()
  399. for _, resourceList := range resourcesByGV {
  400. resources = append(resources, resourceList)
  401. }
  402. } else {
  403. sgs, err = d.ServerGroups()
  404. }
  405. if sgs == nil {
  406. return nil, nil, err
  407. }
  408. resultGroups := []*metav1.APIGroup{}
  409. for i := range sgs.Groups {
  410. resultGroups = append(resultGroups, &sgs.Groups[i])
  411. }
  412. // resources is non-nil if aggregated discovery succeeded.
  413. if resources != nil {
  414. // Any stale Group/Versions returned by aggregated discovery
  415. // must be surfaced to the caller as failed Group/Versions.
  416. var ferr error
  417. if len(failedGVs) > 0 {
  418. ferr = &ErrGroupDiscoveryFailed{Groups: failedGVs}
  419. }
  420. return resultGroups, resources, ferr
  421. }
  422. groupVersionResources, failedGroups := fetchGroupVersionResources(d, sgs)
  423. // order results by group/version discovery order
  424. result := []*metav1.APIResourceList{}
  425. for _, apiGroup := range sgs.Groups {
  426. for _, version := range apiGroup.Versions {
  427. gv := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
  428. if resources, ok := groupVersionResources[gv]; ok {
  429. result = append(result, resources)
  430. }
  431. }
  432. }
  433. if len(failedGroups) == 0 {
  434. return resultGroups, result, nil
  435. }
  436. return resultGroups, result, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  437. }
  438. // ServerPreferredResources uses the provided discovery interface to look up preferred resources
  439. func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList, error) {
  440. var serverGroupList *metav1.APIGroupList
  441. var failedGroups map[schema.GroupVersion]error
  442. var groupVersionResources map[schema.GroupVersion]*metav1.APIResourceList
  443. var err error
  444. // If the passed discovery object implements the wider AggregatedDiscoveryInterface,
  445. // then it is attempt to retrieve both the groups and the resources. "failedGroups"
  446. // are Group/Versions returned as stale in AggregatedDiscovery format.
  447. ad, ok := d.(AggregatedDiscoveryInterface)
  448. if ok {
  449. serverGroupList, groupVersionResources, failedGroups, err = ad.GroupsAndMaybeResources()
  450. } else {
  451. serverGroupList, err = d.ServerGroups()
  452. }
  453. if err != nil {
  454. return nil, err
  455. }
  456. // Non-aggregated discovery must fetch resources from Groups.
  457. if groupVersionResources == nil {
  458. groupVersionResources, failedGroups = fetchGroupVersionResources(d, serverGroupList)
  459. }
  460. result := []*metav1.APIResourceList{}
  461. grVersions := map[schema.GroupResource]string{} // selected version of a GroupResource
  462. grAPIResources := map[schema.GroupResource]*metav1.APIResource{} // selected APIResource for a GroupResource
  463. gvAPIResourceLists := map[schema.GroupVersion]*metav1.APIResourceList{} // blueprint for a APIResourceList for later grouping
  464. for _, apiGroup := range serverGroupList.Groups {
  465. for _, version := range apiGroup.Versions {
  466. groupVersion := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
  467. apiResourceList, ok := groupVersionResources[groupVersion]
  468. if !ok {
  469. continue
  470. }
  471. // create empty list which is filled later in another loop
  472. emptyAPIResourceList := metav1.APIResourceList{
  473. GroupVersion: version.GroupVersion,
  474. }
  475. gvAPIResourceLists[groupVersion] = &emptyAPIResourceList
  476. result = append(result, &emptyAPIResourceList)
  477. for i := range apiResourceList.APIResources {
  478. apiResource := &apiResourceList.APIResources[i]
  479. if strings.Contains(apiResource.Name, "/") {
  480. continue
  481. }
  482. gv := schema.GroupResource{Group: apiGroup.Name, Resource: apiResource.Name}
  483. if _, ok := grAPIResources[gv]; ok && version.Version != apiGroup.PreferredVersion.Version {
  484. // only override with preferred version
  485. continue
  486. }
  487. grVersions[gv] = version.Version
  488. grAPIResources[gv] = apiResource
  489. }
  490. }
  491. }
  492. // group selected APIResources according to GroupVersion into APIResourceLists
  493. for groupResource, apiResource := range grAPIResources {
  494. version := grVersions[groupResource]
  495. groupVersion := schema.GroupVersion{Group: groupResource.Group, Version: version}
  496. apiResourceList := gvAPIResourceLists[groupVersion]
  497. apiResourceList.APIResources = append(apiResourceList.APIResources, *apiResource)
  498. }
  499. if len(failedGroups) == 0 {
  500. return result, nil
  501. }
  502. return result, &ErrGroupDiscoveryFailed{Groups: failedGroups}
  503. }
  504. // fetchServerResourcesForGroupVersions uses the discovery client to fetch the resources for the specified groups in parallel.
  505. func fetchGroupVersionResources(d DiscoveryInterface, apiGroups *metav1.APIGroupList) (map[schema.GroupVersion]*metav1.APIResourceList, map[schema.GroupVersion]error) {
  506. groupVersionResources := make(map[schema.GroupVersion]*metav1.APIResourceList)
  507. failedGroups := make(map[schema.GroupVersion]error)
  508. wg := &sync.WaitGroup{}
  509. resultLock := &sync.Mutex{}
  510. for _, apiGroup := range apiGroups.Groups {
  511. for _, version := range apiGroup.Versions {
  512. groupVersion := schema.GroupVersion{Group: apiGroup.Name, Version: version.Version}
  513. wg.Add(1)
  514. go func() {
  515. defer wg.Done()
  516. defer utilruntime.HandleCrash()
  517. apiResourceList, err := d.ServerResourcesForGroupVersion(groupVersion.String())
  518. // lock to record results
  519. resultLock.Lock()
  520. defer resultLock.Unlock()
  521. if err != nil {
  522. // TODO: maybe restrict this to NotFound errors
  523. failedGroups[groupVersion] = err
  524. }
  525. if apiResourceList != nil {
  526. // even in case of error, some fallback might have been returned
  527. groupVersionResources[groupVersion] = apiResourceList
  528. }
  529. }()
  530. }
  531. }
  532. wg.Wait()
  533. return groupVersionResources, failedGroups
  534. }
  535. // ServerPreferredResources returns the supported resources with the version preferred by the
  536. // server.
  537. func (d *DiscoveryClient) ServerPreferredResources() ([]*metav1.APIResourceList, error) {
  538. _, rs, err := withRetries(defaultRetries, func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
  539. rs, err := ServerPreferredResources(d)
  540. return nil, rs, err
  541. })
  542. return rs, err
  543. }
  544. // ServerPreferredNamespacedResources returns the supported namespaced resources with the
  545. // version preferred by the server.
  546. func (d *DiscoveryClient) ServerPreferredNamespacedResources() ([]*metav1.APIResourceList, error) {
  547. return ServerPreferredNamespacedResources(d)
  548. }
  549. // ServerPreferredNamespacedResources uses the provided discovery interface to look up preferred namespaced resources
  550. func ServerPreferredNamespacedResources(d DiscoveryInterface) ([]*metav1.APIResourceList, error) {
  551. all, err := ServerPreferredResources(d)
  552. return FilteredBy(ResourcePredicateFunc(func(groupVersion string, r *metav1.APIResource) bool {
  553. return r.Namespaced
  554. }), all), err
  555. }
  556. // ServerVersion retrieves and parses the server's version (git version).
  557. func (d *DiscoveryClient) ServerVersion() (*version.Info, error) {
  558. body, err := d.restClient.Get().AbsPath("/version").Do(context.TODO()).Raw()
  559. if err != nil {
  560. return nil, err
  561. }
  562. var info version.Info
  563. err = json.Unmarshal(body, &info)
  564. if err != nil {
  565. return nil, fmt.Errorf("unable to parse the server version: %v", err)
  566. }
  567. return &info, nil
  568. }
  569. // OpenAPISchema fetches the open api v2 schema using a rest client and parses the proto.
  570. func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
  571. data, err := d.restClient.Get().AbsPath("/openapi/v2").SetHeader("Accept", openAPIV2mimePb).Do(context.TODO()).Raw()
  572. if err != nil {
  573. if errors.IsForbidden(err) || errors.IsNotFound(err) || errors.IsNotAcceptable(err) {
  574. // single endpoint not found/registered in old server, try to fetch old endpoint
  575. // TODO: remove this when kubectl/client-go don't work with 1.9 server
  576. data, err = d.restClient.Get().AbsPath("/swagger-2.0.0.pb-v1").Do(context.TODO()).Raw()
  577. if err != nil {
  578. return nil, err
  579. }
  580. } else {
  581. return nil, err
  582. }
  583. }
  584. document := &openapi_v2.Document{}
  585. err = proto.Unmarshal(data, document)
  586. if err != nil {
  587. return nil, err
  588. }
  589. return document, nil
  590. }
  591. func (d *DiscoveryClient) OpenAPIV3() openapi.Client {
  592. return openapi.NewClient(d.restClient)
  593. }
  594. // WithLegacy returns copy of current discovery client that will only
  595. // receive the legacy discovery format.
  596. func (d *DiscoveryClient) WithLegacy() DiscoveryInterface {
  597. client := *d
  598. client.UseLegacyDiscovery = true
  599. return &client
  600. }
  601. // withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
  602. func withRetries(maxRetries int, f func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error)) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
  603. var result []*metav1.APIResourceList
  604. var resultGroups []*metav1.APIGroup
  605. var err error
  606. for i := 0; i < maxRetries; i++ {
  607. resultGroups, result, err = f()
  608. if err == nil {
  609. return resultGroups, result, nil
  610. }
  611. if _, ok := err.(*ErrGroupDiscoveryFailed); !ok {
  612. return nil, nil, err
  613. }
  614. }
  615. return resultGroups, result, err
  616. }
  617. func setDiscoveryDefaults(config *restclient.Config) error {
  618. config.APIPath = ""
  619. config.GroupVersion = nil
  620. if config.Timeout == 0 {
  621. config.Timeout = defaultTimeout
  622. }
  623. // if a burst limit is not already configured
  624. if config.Burst == 0 {
  625. // discovery is expected to be bursty, increase the default burst
  626. // to accommodate looking up resource info for many API groups.
  627. // matches burst set by ConfigFlags#ToDiscoveryClient().
  628. // see https://issue.k8s.io/86149
  629. config.Burst = defaultBurst
  630. }
  631. codec := runtime.NoopEncoder{Decoder: scheme.Codecs.UniversalDecoder()}
  632. config.NegotiatedSerializer = serializer.NegotiatedSerializerWrapper(runtime.SerializerInfo{Serializer: codec})
  633. if len(config.UserAgent) == 0 {
  634. config.UserAgent = restclient.DefaultKubernetesUserAgent()
  635. }
  636. return nil
  637. }
  638. // NewDiscoveryClientForConfig creates a new DiscoveryClient for the given config. This client
  639. // can be used to discover supported resources in the API server.
  640. // NewDiscoveryClientForConfig is equivalent to NewDiscoveryClientForConfigAndClient(c, httpClient),
  641. // where httpClient was generated with rest.HTTPClientFor(c).
  642. func NewDiscoveryClientForConfig(c *restclient.Config) (*DiscoveryClient, error) {
  643. config := *c
  644. if err := setDiscoveryDefaults(&config); err != nil {
  645. return nil, err
  646. }
  647. httpClient, err := restclient.HTTPClientFor(&config)
  648. if err != nil {
  649. return nil, err
  650. }
  651. return NewDiscoveryClientForConfigAndClient(&config, httpClient)
  652. }
  653. // NewDiscoveryClientForConfigAndClient creates a new DiscoveryClient for the given config. This client
  654. // can be used to discover supported resources in the API server.
  655. // Note the http client provided takes precedence over the configured transport values.
  656. func NewDiscoveryClientForConfigAndClient(c *restclient.Config, httpClient *http.Client) (*DiscoveryClient, error) {
  657. config := *c
  658. if err := setDiscoveryDefaults(&config); err != nil {
  659. return nil, err
  660. }
  661. client, err := restclient.UnversionedRESTClientForConfigAndClient(&config, httpClient)
  662. return &DiscoveryClient{restClient: client, LegacyPrefix: "/api", UseLegacyDiscovery: false}, err
  663. }
  664. // NewDiscoveryClientForConfigOrDie creates a new DiscoveryClient for the given config. If
  665. // there is an error, it panics.
  666. func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
  667. client, err := NewDiscoveryClientForConfig(c)
  668. if err != nil {
  669. panic(err)
  670. }
  671. return client
  672. }
  673. // NewDiscoveryClient returns a new DiscoveryClient for the given RESTClient.
  674. func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
  675. return &DiscoveryClient{restClient: c, LegacyPrefix: "/api", UseLegacyDiscovery: false}
  676. }
  677. // RESTClient returns a RESTClient that is used to communicate
  678. // with API server by this client implementation.
  679. func (d *DiscoveryClient) RESTClient() restclient.Interface {
  680. if d == nil {
  681. return nil
  682. }
  683. return d.restClient
  684. }