Skip to content

Commit 968ba8d

Browse files
Merge pull request #113797 from seans3/force-no-aggregated
Adds field to force non-aggregated discovery Kubernetes-commit: 418608e926049e7458f03226fe27f101e7fdc47f
2 parents c8ffed3 + 3ac73ea commit 968ba8d

File tree

10 files changed

+87
-12
lines changed

10 files changed

+87
-12
lines changed

discovery/cached/disk/cached_discovery.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ func (d *CachedDiscoveryClient) Invalidate() {
277277
}
278278
}
279279

280+
// WithLegacy returns current cached discovery client;
281+
// current client does not support legacy-only discovery.
282+
func (d *CachedDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
283+
return d
284+
}
285+
280286
// NewCachedDiscoveryClientForConfig creates a new DiscoveryClient for the given config, and wraps
281287
// the created client in a CachedDiscoveryClient. The provided configuration is updated with a
282288
// custom transport that understands cache responses.

discovery/cached/disk/cached_discovery_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ func (d *fakeDiscoveryClient) OpenAPIV3() openapi.Client {
786786
panic("unimplemented")
787787
}
788788

789+
func (d *fakeDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
790+
panic("unimplemented")
791+
}
792+
789793
func groupNamesFromList(groups *metav1.APIGroupList) []string {
790794
result := []string{}
791795
for _, group := range groups.Groups {

discovery/cached/memory/memcache.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ func (d *memCacheClient) serverResourcesForGroupVersion(groupVersion string) (*m
279279
return r, nil
280280
}
281281

282+
// WithLegacy returns current memory-cached discovery client;
283+
// current client does not support legacy-only discovery.
284+
func (d *memCacheClient) WithLegacy() discovery.DiscoveryInterface {
285+
return d
286+
}
287+
282288
// NewMemCacheClient creates a new CachedDiscoveryInterface which caches
283289
// discovery information in memory and will stay up-to-date if Invalidate is
284290
// called with regularity.

discovery/discovery_client.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ type DiscoveryInterface interface {
7474
ServerVersionInterface
7575
OpenAPISchemaInterface
7676
OpenAPIV3SchemaInterface
77+
// Returns copy of current discovery client that will only
78+
// receive the legacy discovery format, or pointer to current
79+
// discovery client if it does not support legacy-only discovery.
80+
WithLegacy() DiscoveryInterface
7781
}
7882

7983
// AggregatedDiscoveryInterface extends DiscoveryInterface to include a method to possibly
@@ -154,6 +158,8 @@ type DiscoveryClient struct {
154158
restClient restclient.Interface
155159

156160
LegacyPrefix string
161+
// Forces the client to request only "unaggregated" (legacy) discovery.
162+
UseLegacyDiscovery bool
157163
}
158164

159165
var _ AggregatedDiscoveryInterface = &DiscoveryClient{}
@@ -213,10 +219,14 @@ func (d *DiscoveryClient) GroupsAndMaybeResources() (*metav1.APIGroupList, map[s
213219
// possible for the resource map to be nil if the server returned
214220
// the unaggregated discovery.
215221
func (d *DiscoveryClient) downloadLegacy() (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, error) {
222+
accept := acceptDiscoveryFormats
223+
if d.UseLegacyDiscovery {
224+
accept = AcceptV1
225+
}
216226
var responseContentType string
217227
body, err := d.restClient.Get().
218228
AbsPath("/api").
219-
SetHeader("Accept", acceptDiscoveryFormats).
229+
SetHeader("Accept", accept).
220230
Do(context.TODO()).
221231
ContentType(&responseContentType).
222232
Raw()
@@ -262,10 +272,14 @@ func (d *DiscoveryClient) downloadLegacy() (*metav1.APIGroupList, map[schema.Gro
262272
// discovery resources. The returned groups will always exist, but the
263273
// resources map may be nil.
264274
func (d *DiscoveryClient) downloadAPIs() (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, error) {
275+
accept := acceptDiscoveryFormats
276+
if d.UseLegacyDiscovery {
277+
accept = AcceptV1
278+
}
265279
var responseContentType string
266280
body, err := d.restClient.Get().
267281
AbsPath("/apis").
268-
SetHeader("Accept", acceptDiscoveryFormats).
282+
SetHeader("Accept", accept).
269283
Do(context.TODO()).
270284
ContentType(&responseContentType).
271285
Raw()
@@ -590,6 +604,14 @@ func (d *DiscoveryClient) OpenAPIV3() openapi.Client {
590604
return openapi.NewClient(d.restClient)
591605
}
592606

607+
// WithLegacy returns copy of current discovery client that will only
608+
// receive the legacy discovery format.
609+
func (d *DiscoveryClient) WithLegacy() DiscoveryInterface {
610+
client := *d
611+
client.UseLegacyDiscovery = true
612+
return &client
613+
}
614+
593615
// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
594616
func withRetries(maxRetries int, f func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error)) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
595617
var result []*metav1.APIResourceList
@@ -654,7 +676,7 @@ func NewDiscoveryClientForConfigAndClient(c *restclient.Config, httpClient *http
654676
return nil, err
655677
}
656678
client, err := restclient.UnversionedRESTClientForConfigAndClient(&config, httpClient)
657-
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api"}, err
679+
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api", UseLegacyDiscovery: false}, err
658680
}
659681

660682
// NewDiscoveryClientForConfigOrDie creates a new DiscoveryClient for the given config. If
@@ -670,7 +692,7 @@ func NewDiscoveryClientForConfigOrDie(c *restclient.Config) *DiscoveryClient {
670692

671693
// NewDiscoveryClient returns a new DiscoveryClient for the given RESTClient.
672694
func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
673-
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api"}
695+
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api", UseLegacyDiscovery: false}
674696
}
675697

676698
// RESTClient returns a RESTClient that is used to communicate

discovery/discovery_client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,6 +2297,26 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
22972297
}
22982298
}
22992299

2300+
func TestUseLegacyDiscovery(t *testing.T) {
2301+
// Default client sends aggregated discovery accept format (first) as well as legacy format.
2302+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2303+
acceptHeader := req.Header.Get("Accept")
2304+
assert.Equal(t, acceptDiscoveryFormats, acceptHeader)
2305+
}))
2306+
defer server.Close()
2307+
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
2308+
client.ServerGroups()
2309+
// When "UseLegacyDiscovery" field is set, only the legacy discovery format is requested.
2310+
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
2311+
acceptHeader := req.Header.Get("Accept")
2312+
assert.Equal(t, AcceptV1, acceptHeader)
2313+
}))
2314+
defer server.Close()
2315+
client = NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
2316+
client.UseLegacyDiscovery = true
2317+
client.ServerGroups()
2318+
}
2319+
23002320
func groupNames(groups []*metav1.APIGroup) []string {
23012321
result := []string{}
23022322
for _, group := range groups {

discovery/fake/discovery.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
"k8s.io/apimachinery/pkg/version"
29+
"k8s.io/client-go/discovery"
2930
"k8s.io/client-go/openapi"
3031
kubeversion "k8s.io/client-go/pkg/version"
3132
restclient "k8s.io/client-go/rest"
@@ -164,3 +165,7 @@ func (c *FakeDiscovery) OpenAPIV3() openapi.Client {
164165
func (c *FakeDiscovery) RESTClient() restclient.Interface {
165166
return nil
166167
}
168+
169+
func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface {
170+
panic("unimplemented")
171+
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ require (
2424
golang.org/x/term v0.1.0
2525
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
2626
google.golang.org/protobuf v1.28.1
27-
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f
28-
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
27+
k8s.io/api v0.0.0-20221116135212-053624e78dd8
28+
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e
2929
k8s.io/klog/v2 v2.80.1
3030
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
3131
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
@@ -59,6 +59,6 @@ require (
5959
)
6060

6161
replace (
62-
k8s.io/api => k8s.io/api v0.0.0-20221112014728-9e1815a99d4f
63-
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
62+
k8s.io/api => k8s.io/api v0.0.0-20221116135212-053624e78dd8
63+
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221116134806-067949de242e
6464
)

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
476476
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
477477
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
478478
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
479-
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f h1:ktcfuKz8wVGjfjJ+qyGhcepyyYcbsxLXwP41rZwHvGA=
480-
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f/go.mod h1:j2jT1HZpNN4eUpl6xrwjWC1amreYNCdsevVdZMhBz5o=
481-
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5 h1:iFAMJ1evvrO6X7dS7EKujS6An+bp3u/dD6opu8rn0QA=
482-
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5/go.mod h1:VXMmlsE7YRJ5vyAyWpkKIfFkEbDNpVs0ObpkuQf1WfM=
479+
k8s.io/api v0.0.0-20221116135212-053624e78dd8 h1:olCyRRIjMsLFmsBCz9l9y/3xBSe+rCsEtTyG1YumXlU=
480+
k8s.io/api v0.0.0-20221116135212-053624e78dd8/go.mod h1:AXoGyBHBHIy4QC0TrMlMqGDz+LRD8aBTeXZkITZHN+w=
481+
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e h1:TVs+bZy3ij+o1TzjRJh2ddFxFW5mjRFRrm8Juo1ywJA=
482+
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e/go.mod h1:VXMmlsE7YRJ5vyAyWpkKIfFkEbDNpVs0ObpkuQf1WfM=
483483
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
484484
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
485485
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E=

restmapper/discovery_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,10 @@ func (c *fakeFailingDiscovery) OpenAPIV3() openapi.Client {
422422
panic("implement me")
423423
}
424424

425+
func (c *fakeFailingDiscovery) WithLegacy() DiscoveryInterface {
426+
panic("implement me")
427+
}
428+
425429
type fakeCachedDiscoveryInterface struct {
426430
invalidateCalls int
427431
fresh bool
@@ -499,6 +503,10 @@ func (c *fakeCachedDiscoveryInterface) OpenAPIV3() openapi.Client {
499503
panic("implement me")
500504
}
501505

506+
func (c *fakeCachedDiscoveryInterface) WithLegacy() DiscoveryInterface {
507+
panic("implement me")
508+
}
509+
502510
var (
503511
aGroup = metav1.APIGroup{
504512
Name: "a",

restmapper/shortcut_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ func (c *fakeDiscoveryClient) OpenAPIV3() openapi.Client {
362362
panic("implement me")
363363
}
364364

365+
func (c *fakeDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
366+
panic("implement me")
367+
}
368+
365369
type fakeCachedDiscoveryClient struct {
366370
discovery.DiscoveryInterface
367371
freshHandler func() bool

0 commit comments

Comments
 (0)