Skip to content

Commit f4d10a3

Browse files
Fedosink8s-infra-cherrypick-robot
authored andcommitted
Use DynamicRESTMapperOption to enable Lazy Restmapper
Instead of creating the instance of the mapper directly, we will use WithExperimentalLazyMapper option for Dynamic Restmapper.
1 parent b3f4882 commit f4d10a3

File tree

3 files changed

+34
-59
lines changed

3 files changed

+34
-59
lines changed

pkg/client/apiutil/dynamicrestmapper.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type dynamicRESTMapper struct {
4040
// Used for lazy init.
4141
inited uint32
4242
initMtx sync.Mutex
43+
44+
useLazyRestmapper bool
4345
}
4446

4547
// DynamicRESTMapperOption is a functional option on the dynamicRESTMapper.
@@ -60,6 +62,12 @@ var WithLazyDiscovery DynamicRESTMapperOption = func(drm *dynamicRESTMapper) err
6062
return nil
6163
}
6264

65+
// WithExperimentalLazyMapper enables experimental more advanced Lazy Restmapping mechanism.
66+
var WithExperimentalLazyMapper DynamicRESTMapperOption = func(drm *dynamicRESTMapper) error {
67+
drm.useLazyRestmapper = true
68+
return nil
69+
}
70+
6371
// WithCustomMapper supports setting a custom RESTMapper refresher instead of
6472
// the default method, which uses a discovery client.
6573
//
@@ -95,6 +103,9 @@ func NewDynamicRESTMapper(cfg *rest.Config, opts ...DynamicRESTMapperOption) (me
95103
return nil, err
96104
}
97105
}
106+
if drm.useLazyRestmapper {
107+
return newLazyRESTMapperWithClient(client)
108+
}
98109
if !drm.lazy {
99110
if err := drm.setStaticMapper(); err != nil {
100111
return nil, err

pkg/client/apiutil/lazyrestmapper.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ import (
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime/schema"
2626
"k8s.io/client-go/discovery"
27-
"k8s.io/client-go/rest"
2827
"k8s.io/client-go/restmapper"
2928
)
3029

31-
// LazyRESTMapper is a RESTMapper that will lazily query the provided
30+
// lazyRESTMapper is a RESTMapper that will lazily query the provided
3231
// client for discovery information to do REST mappings.
33-
type LazyRESTMapper struct {
32+
type lazyRESTMapper struct {
3433
mapper meta.RESTMapper
3534
client *discovery.DiscoveryClient
3635
knownGroups map[string]*restmapper.APIGroupResources
@@ -40,27 +39,17 @@ type LazyRESTMapper struct {
4039
mu sync.Mutex
4140
}
4241

43-
// NewLazyRESTMapper initializes a LazyRESTMapper.
44-
func NewLazyRESTMapper(c *rest.Config) (meta.RESTMapper, error) {
45-
discoveryClient, err := discovery.NewDiscoveryClientForConfig(c)
46-
if err != nil {
47-
return nil, fmt.Errorf("failed to create discovery client: %w", err)
48-
}
49-
50-
return NewLazyRESTMapperWithClient(discoveryClient)
51-
}
52-
53-
// NewLazyRESTMapperWithClient initializes a LazyRESTMapper with a custom discovery client.
54-
func NewLazyRESTMapperWithClient(discoveryClient *discovery.DiscoveryClient) (meta.RESTMapper, error) {
55-
return &LazyRESTMapper{
42+
// newLazyRESTMapperWithClient initializes a LazyRESTMapper with a custom discovery client.
43+
func newLazyRESTMapperWithClient(discoveryClient *discovery.DiscoveryClient) (meta.RESTMapper, error) {
44+
return &lazyRESTMapper{
5645
mapper: restmapper.NewDiscoveryRESTMapper([]*restmapper.APIGroupResources{}),
5746
client: discoveryClient,
5847
knownGroups: map[string]*restmapper.APIGroupResources{},
5948
}, nil
6049
}
6150

6251
// KindFor implements Mapper.KindFor.
63-
func (m *LazyRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
52+
func (m *lazyRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
6453
res, err := m.mapper.KindFor(resource)
6554
if meta.IsNoMatchError(err) {
6655
if err = m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
@@ -74,7 +63,7 @@ func (m *LazyRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.G
7463
}
7564

7665
// KindsFor implements Mapper.KindsFor.
77-
func (m *LazyRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
66+
func (m *lazyRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
7867
res, err := m.mapper.KindsFor(resource)
7968
if meta.IsNoMatchError(err) {
8069
if err = m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
@@ -88,7 +77,7 @@ func (m *LazyRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schem
8877
}
8978

9079
// ResourceFor implements Mapper.ResourceFor.
91-
func (m *LazyRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
80+
func (m *lazyRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
9281
res, err := m.mapper.ResourceFor(input)
9382
if meta.IsNoMatchError(err) {
9483
if err = m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
@@ -102,7 +91,7 @@ func (m *LazyRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.
10291
}
10392

10493
// ResourcesFor implements Mapper.ResourcesFor.
105-
func (m *LazyRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
94+
func (m *lazyRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
10695
res, err := m.mapper.ResourcesFor(input)
10796
if meta.IsNoMatchError(err) {
10897
if err = m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
@@ -116,7 +105,7 @@ func (m *LazyRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]sche
116105
}
117106

118107
// RESTMapping implements Mapper.RESTMapping.
119-
func (m *LazyRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
108+
func (m *lazyRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
120109
res, err := m.mapper.RESTMapping(gk, versions...)
121110
if meta.IsNoMatchError(err) {
122111
if err = m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
@@ -130,7 +119,7 @@ func (m *LazyRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*
130119
}
131120

132121
// RESTMappings implements Mapper.RESTMappings.
133-
func (m *LazyRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
122+
func (m *lazyRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
134123
res, err := m.mapper.RESTMappings(gk, versions...)
135124
if meta.IsNoMatchError(err) {
136125
if err = m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
@@ -144,13 +133,13 @@ func (m *LazyRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) (
144133
}
145134

146135
// ResourceSingularizer implements Mapper.ResourceSingularizer.
147-
func (m *LazyRESTMapper) ResourceSingularizer(resource string) (string, error) {
136+
func (m *lazyRESTMapper) ResourceSingularizer(resource string) (string, error) {
148137
return m.mapper.ResourceSingularizer(resource)
149138
}
150139

151140
// addKnownGroupAndReload reloads the mapper with updated information about missing API group.
152141
// versions can be specified for partial updates, for instance for v1beta1 version only.
153-
func (m *LazyRESTMapper) addKnownGroupAndReload(groupName string, versions ...string) error {
142+
func (m *lazyRESTMapper) addKnownGroupAndReload(groupName string, versions ...string) error {
154143
m.mu.Lock()
155144
defer m.mu.Unlock()
156145

@@ -209,7 +198,7 @@ func (m *LazyRESTMapper) addKnownGroupAndReload(groupName string, versions ...st
209198
}
210199

211200
// findAPIGroupByName returns API group by its name.
212-
func (m *LazyRESTMapper) findAPIGroupByName(groupName string) (metav1.APIGroup, error) {
201+
func (m *lazyRESTMapper) findAPIGroupByName(groupName string) (metav1.APIGroup, error) {
213202
// Ensure that required info about existing API groups is received and stored in the mapper.
214203
// It will make 2 API calls to /api and /apis, but only once.
215204
if m.apiGroups == nil {
@@ -234,7 +223,7 @@ func (m *LazyRESTMapper) findAPIGroupByName(groupName string) (metav1.APIGroup,
234223
}
235224

236225
// fetchGroupVersionResources fetches the resources for the specified group and its versions.
237-
func (m *LazyRESTMapper) fetchGroupVersionResources(groupName string, versions ...string) (map[schema.GroupVersion]*metav1.APIResourceList, error) {
226+
func (m *lazyRESTMapper) fetchGroupVersionResources(groupName string, versions ...string) (map[schema.GroupVersion]*metav1.APIResourceList, error) {
238227
groupVersionResources := make(map[schema.GroupVersion]*metav1.APIResourceList)
239228
failedGroups := make(map[schema.GroupVersion]error)
240229

pkg/client/apiutil/lazyrestmapper_test.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
gmg "github.com/onsi/gomega"
2525

2626
"k8s.io/apimachinery/pkg/runtime/schema"
27-
"k8s.io/client-go/discovery"
2827
"k8s.io/client-go/rest"
2928
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3029
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -96,10 +95,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
9695
crt := newCountingRoundTripper(httpClient.Transport)
9796
httpClient.Transport = crt
9897

99-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
100-
g.Expect(err).NotTo(gmg.HaveOccurred())
101-
102-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
98+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
10399
g.Expect(err).NotTo(gmg.HaveOccurred())
104100

105101
// There are no requests before any call
@@ -148,10 +144,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
148144
crt := newCountingRoundTripper(httpClient.Transport)
149145
httpClient.Transport = crt
150146

151-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
152-
g.Expect(err).NotTo(gmg.HaveOccurred())
153-
154-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
147+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
155148
g.Expect(err).NotTo(gmg.HaveOccurred())
156149

157150
g.Expect(crt.GetRequestCount()).To(gmg.Equal(0))
@@ -188,10 +181,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
188181
crt := newCountingRoundTripper(httpClient.Transport)
189182
httpClient.Transport = crt
190183

191-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
192-
g.Expect(err).NotTo(gmg.HaveOccurred())
193-
194-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
184+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
195185
g.Expect(err).NotTo(gmg.HaveOccurred())
196186

197187
g.Expect(crt.GetRequestCount()).To(gmg.Equal(0))
@@ -227,10 +217,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
227217
crt := newCountingRoundTripper(httpClient.Transport)
228218
httpClient.Transport = crt
229219

230-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
231-
g.Expect(err).NotTo(gmg.HaveOccurred())
232-
233-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
220+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
234221
g.Expect(err).NotTo(gmg.HaveOccurred())
235222

236223
g.Expect(crt.GetRequestCount()).To(gmg.Equal(0))
@@ -265,10 +252,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
265252
crt := newCountingRoundTripper(httpClient.Transport)
266253
httpClient.Transport = crt
267254

268-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
269-
g.Expect(err).NotTo(gmg.HaveOccurred())
270-
271-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
255+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
272256
g.Expect(err).NotTo(gmg.HaveOccurred())
273257

274258
g.Expect(crt.GetRequestCount()).To(gmg.Equal(0))
@@ -312,10 +296,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
312296
crt := newCountingRoundTripper(httpClient.Transport)
313297
httpClient.Transport = crt
314298

315-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
316-
g.Expect(err).NotTo(gmg.HaveOccurred())
317-
318-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
299+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
319300
g.Expect(err).NotTo(gmg.HaveOccurred())
320301

321302
_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "INVALID1"}, "v1")
@@ -354,10 +335,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
354335
crt := newCountingRoundTripper(httpClient.Transport)
355336
httpClient.Transport = crt
356337

357-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
358-
g.Expect(err).NotTo(gmg.HaveOccurred())
359-
360-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
338+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
361339
g.Expect(err).NotTo(gmg.HaveOccurred())
362340

363341
_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "apps", Kind: "INVALID"})
@@ -396,10 +374,7 @@ func TestLazyRestMapperProvider(t *testing.T) {
396374
crt := newCountingRoundTripper(httpClient.Transport)
397375
httpClient.Transport = crt
398376

399-
discoveryClient, err := discovery.NewDiscoveryClientForConfigAndClient(restCfg, httpClient)
400-
g.Expect(err).NotTo(gmg.HaveOccurred())
401-
402-
lazyRestMapper, err := apiutil.NewLazyRESTMapperWithClient(discoveryClient)
377+
lazyRestMapper, err := apiutil.NewDynamicRESTMapper(restCfg, httpClient, apiutil.WithExperimentalLazyMapper)
403378
g.Expect(err).NotTo(gmg.HaveOccurred())
404379

405380
_, err = lazyRestMapper.RESTMapping(schema.GroupKind{Group: "apps", Kind: "deployment"}, "INVALID")

0 commit comments

Comments
 (0)