Skip to content

Commit cf746c2

Browse files
committed
Provide a truly lazy restmapper
This commit adds a rest mapper that will lazily query the provided client for discovery information to do REST mappings.
1 parent 9fd5774 commit cf746c2

File tree

3 files changed

+704
-0
lines changed

3 files changed

+704
-0
lines changed
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package lazyrestmapper
18+
19+
import (
20+
"fmt"
21+
"sync"
22+
23+
"k8s.io/apimachinery/pkg/api/meta"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime/schema"
26+
"k8s.io/client-go/discovery"
27+
"k8s.io/client-go/rest"
28+
"k8s.io/client-go/restmapper"
29+
)
30+
31+
// LazyRESTMapper is a RESTMapper that will lazily query the provided
32+
// client for discovery information to do REST mappings.
33+
type LazyRESTMapper struct {
34+
mapper meta.RESTMapper
35+
client *discovery.DiscoveryClient
36+
knownGroups map[string]*restmapper.APIGroupResources
37+
apiGroups *metav1.APIGroupList
38+
39+
// mutex to provide thread-safe mapper reloading.
40+
mu sync.Mutex
41+
}
42+
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{
56+
mapper: restmapper.NewDiscoveryRESTMapper([]*restmapper.APIGroupResources{}),
57+
client: discoveryClient,
58+
knownGroups: map[string]*restmapper.APIGroupResources{},
59+
}, nil
60+
}
61+
62+
// KindFor implements Mapper.KindFor.
63+
func (m *LazyRESTMapper) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
64+
res, err := m.mapper.KindFor(resource)
65+
if meta.IsNoMatchError(err) {
66+
if err = m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
67+
return res, err
68+
}
69+
70+
res, err = m.mapper.KindFor(resource)
71+
}
72+
73+
return res, err
74+
}
75+
76+
// KindsFor implements Mapper.KindsFor.
77+
func (m *LazyRESTMapper) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
78+
res, err := m.mapper.KindsFor(resource)
79+
if meta.IsNoMatchError(err) {
80+
if err = m.addKnownGroupAndReload(resource.Group, resource.Version); err != nil {
81+
return res, err
82+
}
83+
84+
res, err = m.mapper.KindsFor(resource)
85+
}
86+
87+
return res, err
88+
}
89+
90+
// ResourceFor implements Mapper.ResourceFor.
91+
func (m *LazyRESTMapper) ResourceFor(input schema.GroupVersionResource) (schema.GroupVersionResource, error) {
92+
res, err := m.mapper.ResourceFor(input)
93+
if meta.IsNoMatchError(err) {
94+
if err = m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
95+
return res, err
96+
}
97+
98+
res, err = m.mapper.ResourceFor(input)
99+
}
100+
101+
return res, err
102+
}
103+
104+
// ResourcesFor implements Mapper.ResourcesFor.
105+
func (m *LazyRESTMapper) ResourcesFor(input schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
106+
res, err := m.mapper.ResourcesFor(input)
107+
if meta.IsNoMatchError(err) {
108+
if err = m.addKnownGroupAndReload(input.Group, input.Version); err != nil {
109+
return res, err
110+
}
111+
112+
res, err = m.mapper.ResourcesFor(input)
113+
}
114+
115+
return res, err
116+
}
117+
118+
// RESTMapping implements Mapper.RESTMapping.
119+
func (m *LazyRESTMapper) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
120+
res, err := m.mapper.RESTMapping(gk, versions...)
121+
if meta.IsNoMatchError(err) {
122+
if err = m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
123+
return res, err
124+
}
125+
126+
res, err = m.mapper.RESTMapping(gk, versions...)
127+
}
128+
129+
return res, err
130+
}
131+
132+
// RESTMappings implements Mapper.RESTMappings.
133+
func (m *LazyRESTMapper) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
134+
res, err := m.mapper.RESTMappings(gk, versions...)
135+
if meta.IsNoMatchError(err) {
136+
if err = m.addKnownGroupAndReload(gk.Group, versions...); err != nil {
137+
return res, err
138+
}
139+
140+
res, err = m.mapper.RESTMappings(gk, versions...)
141+
}
142+
143+
return res, err
144+
}
145+
146+
// ResourceSingularizer implements Mapper.ResourceSingularizer.
147+
func (m *LazyRESTMapper) ResourceSingularizer(resource string) (string, error) {
148+
return m.mapper.ResourceSingularizer(resource)
149+
}
150+
151+
// addKnownGroupAndReload reloads the mapper with updated information about missing API group.
152+
// versions can be specified for partial updates, for instance for v1beta1 version only.
153+
func (m *LazyRESTMapper) addKnownGroupAndReload(groupName string, versions ...string) error {
154+
m.mu.Lock()
155+
defer m.mu.Unlock()
156+
157+
// First, find information about requested group and its versions. Fail immediately if there is no such group.
158+
apiGroup, err := m.findAPIGroupByName(groupName)
159+
if err != nil {
160+
return err
161+
}
162+
163+
// If no specific versions are set by user, we will scan all available ones for the API group.
164+
if len(versions) == 0 {
165+
for _, version := range apiGroup.Versions {
166+
versions = append(versions, version.Version)
167+
}
168+
}
169+
170+
// Second, get resources. The number of API calls is equal to the number of versions: /apis/<group>/<version>.
171+
groupVersionResources, err := m.fetchGroupVersionResources(apiGroup.Name, versions...)
172+
if err != nil {
173+
return fmt.Errorf("failed to get API group resources: %w", err)
174+
}
175+
176+
groupResources := &restmapper.APIGroupResources{
177+
Group: apiGroup,
178+
VersionedResources: make(map[string][]metav1.APIResource),
179+
}
180+
for version, resources := range groupVersionResources {
181+
groupResources.VersionedResources[version.Version] = resources.APIResources
182+
}
183+
184+
// Add new known API group or just append the resources to the existing group.
185+
if _, ok := m.knownGroups[groupName]; !ok {
186+
m.knownGroups[groupName] = groupResources
187+
} else {
188+
for version, resources := range groupResources.VersionedResources {
189+
m.knownGroups[groupName].VersionedResources[version] = resources
190+
}
191+
}
192+
193+
// Finally, update the group with received information and regenerate the mapper.
194+
updatedGroupResources := make([]*restmapper.APIGroupResources, 0, len(m.knownGroups))
195+
for _, v := range m.knownGroups {
196+
updatedGroupResources = append(updatedGroupResources, v)
197+
}
198+
199+
m.mapper = restmapper.NewDiscoveryRESTMapper(updatedGroupResources)
200+
201+
return nil
202+
}
203+
204+
// findAPIGroupByName returns API group by its name.
205+
func (m *LazyRESTMapper) findAPIGroupByName(groupName string) (metav1.APIGroup, error) {
206+
// Ensure that required info about existing API groups is received and stored in the mapper.
207+
// It will make 2 API calls to /api and /apis, but only once.
208+
if m.apiGroups == nil {
209+
apiGroups, err := m.client.ServerGroups()
210+
if err != nil {
211+
return metav1.APIGroup{}, fmt.Errorf("failed to get server groups: %w", err)
212+
}
213+
if len(apiGroups.Groups) == 0 {
214+
return metav1.APIGroup{}, fmt.Errorf("received an empty API groups list")
215+
}
216+
217+
m.apiGroups = apiGroups
218+
}
219+
220+
for i := range m.apiGroups.Groups {
221+
if groupName == (&m.apiGroups.Groups[i]).Name {
222+
return m.apiGroups.Groups[i], nil
223+
}
224+
}
225+
226+
return metav1.APIGroup{}, fmt.Errorf("failed to find API group %s", groupName)
227+
}
228+
229+
// fetchGroupVersionResources fetches the resources for the specified group and its versions.
230+
func (m *LazyRESTMapper) fetchGroupVersionResources(groupName string, versions ...string) (map[schema.GroupVersion]*metav1.APIResourceList, error) {
231+
groupVersionResources := make(map[schema.GroupVersion]*metav1.APIResourceList)
232+
failedGroups := make(map[schema.GroupVersion]error)
233+
234+
for _, version := range versions {
235+
groupVersion := schema.GroupVersion{Group: groupName, Version: version}
236+
237+
apiResourceList, err := m.client.ServerResourcesForGroupVersion(groupVersion.String())
238+
if err != nil {
239+
failedGroups[groupVersion] = err
240+
}
241+
if apiResourceList != nil {
242+
// even in case of error, some fallback might have been returned.
243+
groupVersionResources[groupVersion] = apiResourceList
244+
}
245+
}
246+
247+
if len(failedGroups) > 0 {
248+
return nil, &discovery.ErrGroupDiscoveryFailed{Groups: failedGroups}
249+
}
250+
251+
return groupVersionResources, nil
252+
}

0 commit comments

Comments
 (0)