Skip to content

Commit

Permalink
Merge pull request #113797 from seans3/force-no-aggregated
Browse files Browse the repository at this point in the history
Adds field to force non-aggregated discovery

Kubernetes-commit: 418608e926049e7458f03226fe27f101e7fdc47f
  • Loading branch information
k8s-publishing-bot committed Nov 16, 2022
2 parents c8ffed3 + 3ac73ea commit 968ba8d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 12 deletions.
6 changes: 6 additions & 0 deletions discovery/cached/disk/cached_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ func (d *CachedDiscoveryClient) Invalidate() {
}
}

// WithLegacy returns current cached discovery client;
// current client does not support legacy-only discovery.
func (d *CachedDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
return d
}

// NewCachedDiscoveryClientForConfig creates a new DiscoveryClient for the given config, and wraps
// the created client in a CachedDiscoveryClient. The provided configuration is updated with a
// custom transport that understands cache responses.
Expand Down
4 changes: 4 additions & 0 deletions discovery/cached/disk/cached_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,10 @@ func (d *fakeDiscoveryClient) OpenAPIV3() openapi.Client {
panic("unimplemented")
}

func (d *fakeDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
panic("unimplemented")
}

func groupNamesFromList(groups *metav1.APIGroupList) []string {
result := []string{}
for _, group := range groups.Groups {
Expand Down
6 changes: 6 additions & 0 deletions discovery/cached/memory/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,12 @@ func (d *memCacheClient) serverResourcesForGroupVersion(groupVersion string) (*m
return r, nil
}

// WithLegacy returns current memory-cached discovery client;
// current client does not support legacy-only discovery.
func (d *memCacheClient) WithLegacy() discovery.DiscoveryInterface {
return d
}

// NewMemCacheClient creates a new CachedDiscoveryInterface which caches
// discovery information in memory and will stay up-to-date if Invalidate is
// called with regularity.
Expand Down
30 changes: 26 additions & 4 deletions discovery/discovery_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ type DiscoveryInterface interface {
ServerVersionInterface
OpenAPISchemaInterface
OpenAPIV3SchemaInterface
// Returns copy of current discovery client that will only
// receive the legacy discovery format, or pointer to current
// discovery client if it does not support legacy-only discovery.
WithLegacy() DiscoveryInterface
}

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

LegacyPrefix string
// Forces the client to request only "unaggregated" (legacy) discovery.
UseLegacyDiscovery bool
}

var _ AggregatedDiscoveryInterface = &DiscoveryClient{}
Expand Down Expand Up @@ -213,10 +219,14 @@ func (d *DiscoveryClient) GroupsAndMaybeResources() (*metav1.APIGroupList, map[s
// possible for the resource map to be nil if the server returned
// the unaggregated discovery.
func (d *DiscoveryClient) downloadLegacy() (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, error) {
accept := acceptDiscoveryFormats
if d.UseLegacyDiscovery {
accept = AcceptV1
}
var responseContentType string
body, err := d.restClient.Get().
AbsPath("/api").
SetHeader("Accept", acceptDiscoveryFormats).
SetHeader("Accept", accept).
Do(context.TODO()).
ContentType(&responseContentType).
Raw()
Expand Down Expand Up @@ -262,10 +272,14 @@ func (d *DiscoveryClient) downloadLegacy() (*metav1.APIGroupList, map[schema.Gro
// discovery resources. The returned groups will always exist, but the
// resources map may be nil.
func (d *DiscoveryClient) downloadAPIs() (*metav1.APIGroupList, map[schema.GroupVersion]*metav1.APIResourceList, error) {
accept := acceptDiscoveryFormats
if d.UseLegacyDiscovery {
accept = AcceptV1
}
var responseContentType string
body, err := d.restClient.Get().
AbsPath("/apis").
SetHeader("Accept", acceptDiscoveryFormats).
SetHeader("Accept", accept).
Do(context.TODO()).
ContentType(&responseContentType).
Raw()
Expand Down Expand Up @@ -590,6 +604,14 @@ func (d *DiscoveryClient) OpenAPIV3() openapi.Client {
return openapi.NewClient(d.restClient)
}

// WithLegacy returns copy of current discovery client that will only
// receive the legacy discovery format.
func (d *DiscoveryClient) WithLegacy() DiscoveryInterface {
client := *d
client.UseLegacyDiscovery = true
return &client
}

// withRetries retries the given recovery function in case the groups supported by the server change after ServerGroup() returns.
func withRetries(maxRetries int, f func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error)) ([]*metav1.APIGroup, []*metav1.APIResourceList, error) {
var result []*metav1.APIResourceList
Expand Down Expand Up @@ -654,7 +676,7 @@ func NewDiscoveryClientForConfigAndClient(c *restclient.Config, httpClient *http
return nil, err
}
client, err := restclient.UnversionedRESTClientForConfigAndClient(&config, httpClient)
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api"}, err
return &DiscoveryClient{restClient: client, LegacyPrefix: "/api", UseLegacyDiscovery: false}, err
}

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

// NewDiscoveryClient returns a new DiscoveryClient for the given RESTClient.
func NewDiscoveryClient(c restclient.Interface) *DiscoveryClient {
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api"}
return &DiscoveryClient{restClient: c, LegacyPrefix: "/api", UseLegacyDiscovery: false}
}

// RESTClient returns a RESTClient that is used to communicate
Expand Down
20 changes: 20 additions & 0 deletions discovery/discovery_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2297,6 +2297,26 @@ func TestAggregatedServerPreferredResources(t *testing.T) {
}
}

func TestUseLegacyDiscovery(t *testing.T) {
// Default client sends aggregated discovery accept format (first) as well as legacy format.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
acceptHeader := req.Header.Get("Accept")
assert.Equal(t, acceptDiscoveryFormats, acceptHeader)
}))
defer server.Close()
client := NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
client.ServerGroups()
// When "UseLegacyDiscovery" field is set, only the legacy discovery format is requested.
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
acceptHeader := req.Header.Get("Accept")
assert.Equal(t, AcceptV1, acceptHeader)
}))
defer server.Close()
client = NewDiscoveryClientForConfigOrDie(&restclient.Config{Host: server.URL})
client.UseLegacyDiscovery = true
client.ServerGroups()
}

func groupNames(groups []*metav1.APIGroup) []string {
result := []string{}
for _, group := range groups {
Expand Down
5 changes: 5 additions & 0 deletions discovery/fake/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
"k8s.io/client-go/openapi"
kubeversion "k8s.io/client-go/pkg/version"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -164,3 +165,7 @@ func (c *FakeDiscovery) OpenAPIV3() openapi.Client {
func (c *FakeDiscovery) RESTClient() restclient.Interface {
return nil
}

func (c *FakeDiscovery) WithLegacy() discovery.DiscoveryInterface {
panic("unimplemented")
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ require (
golang.org/x/term v0.1.0
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
google.golang.org/protobuf v1.28.1
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
k8s.io/api v0.0.0-20221116135212-053624e78dd8
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e
k8s.io/klog/v2 v2.80.1
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
k8s.io/utils v0.0.0-20221107191617-1a15be271d1d
Expand Down Expand Up @@ -59,6 +59,6 @@ require (
)

replace (
k8s.io/api => k8s.io/api v0.0.0-20221112014728-9e1815a99d4f
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5
k8s.io/api => k8s.io/api v0.0.0-20221116135212-053624e78dd8
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20221116134806-067949de242e
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,10 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f h1:ktcfuKz8wVGjfjJ+qyGhcepyyYcbsxLXwP41rZwHvGA=
k8s.io/api v0.0.0-20221112014728-9e1815a99d4f/go.mod h1:j2jT1HZpNN4eUpl6xrwjWC1amreYNCdsevVdZMhBz5o=
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5 h1:iFAMJ1evvrO6X7dS7EKujS6An+bp3u/dD6opu8rn0QA=
k8s.io/apimachinery v0.0.0-20221108055230-fd8a60496be5/go.mod h1:VXMmlsE7YRJ5vyAyWpkKIfFkEbDNpVs0ObpkuQf1WfM=
k8s.io/api v0.0.0-20221116135212-053624e78dd8 h1:olCyRRIjMsLFmsBCz9l9y/3xBSe+rCsEtTyG1YumXlU=
k8s.io/api v0.0.0-20221116135212-053624e78dd8/go.mod h1:AXoGyBHBHIy4QC0TrMlMqGDz+LRD8aBTeXZkITZHN+w=
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e h1:TVs+bZy3ij+o1TzjRJh2ddFxFW5mjRFRrm8Juo1ywJA=
k8s.io/apimachinery v0.0.0-20221116134806-067949de242e/go.mod h1:VXMmlsE7YRJ5vyAyWpkKIfFkEbDNpVs0ObpkuQf1WfM=
k8s.io/klog/v2 v2.80.1 h1:atnLQ121W371wYYFawwYx1aEY2eUfs4l3J72wtgAwV4=
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280 h1:+70TFaan3hfJzs+7VK2o+OGxg8HsuBr/5f6tVAjDu6E=
Expand Down
8 changes: 8 additions & 0 deletions restmapper/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,10 @@ func (c *fakeFailingDiscovery) OpenAPIV3() openapi.Client {
panic("implement me")
}

func (c *fakeFailingDiscovery) WithLegacy() DiscoveryInterface {
panic("implement me")
}

type fakeCachedDiscoveryInterface struct {
invalidateCalls int
fresh bool
Expand Down Expand Up @@ -499,6 +503,10 @@ func (c *fakeCachedDiscoveryInterface) OpenAPIV3() openapi.Client {
panic("implement me")
}

func (c *fakeCachedDiscoveryInterface) WithLegacy() DiscoveryInterface {
panic("implement me")
}

var (
aGroup = metav1.APIGroup{
Name: "a",
Expand Down
4 changes: 4 additions & 0 deletions restmapper/shortcut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ func (c *fakeDiscoveryClient) OpenAPIV3() openapi.Client {
panic("implement me")
}

func (c *fakeDiscoveryClient) WithLegacy() discovery.DiscoveryInterface {
panic("implement me")
}

type fakeCachedDiscoveryClient struct {
discovery.DiscoveryInterface
freshHandler func() bool
Expand Down

0 comments on commit 968ba8d

Please sign in to comment.