@@ -88,12 +88,28 @@ type ServerResourcesInterface interface {
88
88
// ServerResourcesForGroupVersion returns the supported resources for a group and version.
89
89
ServerResourcesForGroupVersion (groupVersion string ) (* metav1.APIResourceList , error )
90
90
// ServerResources returns the supported resources for all groups and versions.
91
+ //
92
+ // The returned resource list might be non-nil with partial results even in the case of
93
+ // non-nil error.
94
+ //
95
+ // Deprecated: use ServerGroupsAndResources instead.
91
96
ServerResources () ([]* metav1.APIResourceList , error )
97
+ // ServerResources returns the supported groups and resources for all groups and versions.
98
+ //
99
+ // The returned group and resource lists might be non-nil with partial results even in the
100
+ // case of non-nil error.
101
+ ServerGroupsAndResources () ([]* metav1.APIGroup , []* metav1.APIResourceList , error )
92
102
// ServerPreferredResources returns the supported resources with the version preferred by the
93
103
// server.
104
+ //
105
+ // The returned group and resource lists might be non-nil with partial results even in the
106
+ // case of non-nil error.
94
107
ServerPreferredResources () ([]* metav1.APIResourceList , error )
95
108
// ServerPreferredNamespacedResources returns the supported namespaced resources with the
96
109
// version preferred by the server.
110
+ //
111
+ // The returned resource list might be non-nil with partial results even in the case of
112
+ // non-nil error.
97
113
ServerPreferredNamespacedResources () ([]* metav1.APIResourceList , error )
98
114
}
99
115
@@ -191,14 +207,18 @@ func (d *DiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (r
191
207
return resources , nil
192
208
}
193
209
194
- // serverResources returns the supported resources for all groups and versions.
195
- func (d * DiscoveryClient ) serverResources () ([]* metav1.APIResourceList , error ) {
196
- return ServerResources (d )
197
- }
198
-
199
210
// ServerResources returns the supported resources for all groups and versions.
211
+ // Deprecated: use ServerGroupsAndResources instead.
200
212
func (d * DiscoveryClient ) ServerResources () ([]* metav1.APIResourceList , error ) {
201
- return withRetries (defaultRetries , d .serverResources )
213
+ _ , rs , err := d .ServerGroupsAndResources ()
214
+ return rs , err
215
+ }
216
+
217
+ // ServerGroupsAndResources returns the supported resources for all groups and versions.
218
+ func (d * DiscoveryClient ) ServerGroupsAndResources () ([]* metav1.APIGroup , []* metav1.APIResourceList , error ) {
219
+ return withRetries (defaultRetries , func () ([]* metav1.APIGroup , []* metav1.APIResourceList , error ) {
220
+ return ServerGroupsAndResources (d )
221
+ })
202
222
}
203
223
204
224
// ErrGroupDiscoveryFailed is returned if one or more API groups fail to load.
@@ -224,23 +244,28 @@ func IsGroupDiscoveryFailedError(err error) bool {
224
244
return err != nil && ok
225
245
}
226
246
227
- // serverPreferredResources returns the supported resources with the version preferred by the server.
228
- func (d * DiscoveryClient ) serverPreferredResources () ([]* metav1.APIResourceList , error ) {
229
- return ServerPreferredResources (d )
230
- }
231
-
232
247
// ServerResources uses the provided discovery interface to look up supported resources for all groups and versions.
248
+ // Deprecated: use ServerGroupsAndResources instead.
233
249
func ServerResources (d DiscoveryInterface ) ([]* metav1.APIResourceList , error ) {
234
- apiGroups , err := d .ServerGroups ()
235
- if err != nil {
236
- return nil , err
250
+ _ , rs , err := ServerGroupsAndResources (d )
251
+ return rs , err
252
+ }
253
+
254
+ func ServerGroupsAndResources (d DiscoveryInterface ) ([]* metav1.APIGroup , []* metav1.APIResourceList , error ) {
255
+ sgs , err := d .ServerGroups ()
256
+ if sgs == nil {
257
+ return nil , nil , err
258
+ }
259
+ resultGroups := []* metav1.APIGroup {}
260
+ for i := range sgs .Groups {
261
+ resultGroups = append (resultGroups , & sgs .Groups [i ])
237
262
}
238
263
239
- groupVersionResources , failedGroups := fetchGroupVersionResources (d , apiGroups )
264
+ groupVersionResources , failedGroups := fetchGroupVersionResources (d , sgs )
240
265
241
266
// order results by group/version discovery order
242
267
result := []* metav1.APIResourceList {}
243
- for _ , apiGroup := range apiGroups .Groups {
268
+ for _ , apiGroup := range sgs .Groups {
244
269
for _ , version := range apiGroup .Versions {
245
270
gv := schema.GroupVersion {Group : apiGroup .Name , Version : version .Version }
246
271
if resources , ok := groupVersionResources [gv ]; ok {
@@ -250,10 +275,10 @@ func ServerResources(d DiscoveryInterface) ([]*metav1.APIResourceList, error) {
250
275
}
251
276
252
277
if len (failedGroups ) == 0 {
253
- return result , nil
278
+ return resultGroups , result , nil
254
279
}
255
280
256
- return result , & ErrGroupDiscoveryFailed {Groups : failedGroups }
281
+ return resultGroups , result , & ErrGroupDiscoveryFailed {Groups : failedGroups }
257
282
}
258
283
259
284
// ServerPreferredResources uses the provided discovery interface to look up preferred resources
@@ -317,7 +342,7 @@ func ServerPreferredResources(d DiscoveryInterface) ([]*metav1.APIResourceList,
317
342
return result , & ErrGroupDiscoveryFailed {Groups : failedGroups }
318
343
}
319
344
320
- // fetchServerResourcesForGroupVersions uses the discovery client to fetch the resources for the specified groups in parallel
345
+ // fetchServerResourcesForGroupVersions uses the discovery client to fetch the resources for the specified groups in parallel.
321
346
func fetchGroupVersionResources (d DiscoveryInterface , apiGroups * metav1.APIGroupList ) (map [schema.GroupVersion ]* metav1.APIResourceList , map [schema.GroupVersion ]error ) {
322
347
groupVersionResources := make (map [schema.GroupVersion ]* metav1.APIResourceList )
323
348
failedGroups := make (map [schema.GroupVersion ]error )
@@ -341,7 +366,9 @@ func fetchGroupVersionResources(d DiscoveryInterface, apiGroups *metav1.APIGroup
341
366
if err != nil {
342
367
// TODO: maybe restrict this to NotFound errors
343
368
failedGroups [groupVersion ] = err
344
- } else {
369
+ }
370
+ if apiResourceList != nil {
371
+ // even in case of error, some fallback might have been returned
345
372
groupVersionResources [groupVersion ] = apiResourceList
346
373
}
347
374
}()
@@ -355,7 +382,11 @@ func fetchGroupVersionResources(d DiscoveryInterface, apiGroups *metav1.APIGroup
355
382
// ServerPreferredResources returns the supported resources with the version preferred by the
356
383
// server.
357
384
func (d * DiscoveryClient ) ServerPreferredResources () ([]* metav1.APIResourceList , error ) {
358
- return withRetries (defaultRetries , d .serverPreferredResources )
385
+ _ , rs , err := withRetries (defaultRetries , func () ([]* metav1.APIGroup , []* metav1.APIResourceList , error ) {
386
+ rs , err := ServerPreferredResources (d )
387
+ return nil , rs , err
388
+ })
389
+ return rs , err
359
390
}
360
391
361
392
// ServerPreferredNamespacedResources returns the supported namespaced resources with the
@@ -410,19 +441,20 @@ func (d *DiscoveryClient) OpenAPISchema() (*openapi_v2.Document, error) {
410
441
}
411
442
412
443
// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
413
- func withRetries (maxRetries int , f func () ([]* metav1.APIResourceList , error )) ([]* metav1.APIResourceList , error ) {
444
+ func withRetries (maxRetries int , f func () ([]* metav1.APIGroup , [] * metav1. APIResourceList , error )) ([] * metav1. APIGroup , []* metav1.APIResourceList , error ) {
414
445
var result []* metav1.APIResourceList
446
+ var resultGroups []* metav1.APIGroup
415
447
var err error
416
448
for i := 0 ; i < maxRetries ; i ++ {
417
- result , err = f ()
449
+ resultGroups , result , err = f ()
418
450
if err == nil {
419
- return result , nil
451
+ return resultGroups , result , nil
420
452
}
421
453
if _ , ok := err .(* ErrGroupDiscoveryFailed ); ! ok {
422
- return nil , err
454
+ return nil , nil , err
423
455
}
424
456
}
425
- return result , err
457
+ return resultGroups , result , err
426
458
}
427
459
428
460
func setDiscoveryDefaults (config * restclient.Config ) error {
0 commit comments