forked from kubernetes/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcut.go
172 lines (147 loc) · 6.15 KB
/
shortcut.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright 2016 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restmapper
import (
"strings"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
)
// shortcutExpander is a RESTMapper that can be used for Kubernetes resources. It expands the resource first, then invokes the wrapped
type shortcutExpander struct {
RESTMapper meta.RESTMapper
discoveryClient discovery.DiscoveryInterface
}
var _ meta.RESTMapper = &shortcutExpander{}
// NewShortcutExpander wraps a restmapper in a layer that expands shortcuts found via discovery
func NewShortcutExpander(delegate meta.RESTMapper, client discovery.DiscoveryInterface) meta.RESTMapper {
return shortcutExpander{RESTMapper: delegate, discoveryClient: client}
}
// KindFor fulfills meta.RESTMapper
func (e shortcutExpander) KindFor(resource schema.GroupVersionResource) (schema.GroupVersionKind, error) {
return e.RESTMapper.KindFor(e.expandResourceShortcut(resource))
}
// KindsFor fulfills meta.RESTMapper
func (e shortcutExpander) KindsFor(resource schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
return e.RESTMapper.KindsFor(e.expandResourceShortcut(resource))
}
// ResourcesFor fulfills meta.RESTMapper
func (e shortcutExpander) ResourcesFor(resource schema.GroupVersionResource) ([]schema.GroupVersionResource, error) {
return e.RESTMapper.ResourcesFor(e.expandResourceShortcut(resource))
}
// ResourceFor fulfills meta.RESTMapper
func (e shortcutExpander) ResourceFor(resource schema.GroupVersionResource) (schema.GroupVersionResource, error) {
return e.RESTMapper.ResourceFor(e.expandResourceShortcut(resource))
}
// ResourceSingularizer fulfills meta.RESTMapper
func (e shortcutExpander) ResourceSingularizer(resource string) (string, error) {
return e.RESTMapper.ResourceSingularizer(e.expandResourceShortcut(schema.GroupVersionResource{Resource: resource}).Resource)
}
// RESTMapping fulfills meta.RESTMapper
func (e shortcutExpander) RESTMapping(gk schema.GroupKind, versions ...string) (*meta.RESTMapping, error) {
return e.RESTMapper.RESTMapping(gk, versions...)
}
// RESTMappings fulfills meta.RESTMapper
func (e shortcutExpander) RESTMappings(gk schema.GroupKind, versions ...string) ([]*meta.RESTMapping, error) {
return e.RESTMapper.RESTMappings(gk, versions...)
}
// getShortcutMappings returns a set of tuples which holds short names for resources.
// First the list of potential resources will be taken from the API server.
// Next we will append the hardcoded list of resources - to be backward compatible with old servers.
// NOTE that the list is ordered by group priority.
func (e shortcutExpander) getShortcutMappings() ([]*metav1.APIResourceList, []resourceShortcuts, error) {
res := []resourceShortcuts{}
// get server resources
// This can return an error *and* the results it was able to find. We don't need to fail on the error.
apiResList, err := e.discoveryClient.ServerResources()
if err != nil {
klog.V(1).Infof("Error loading discovery information: %v", err)
}
for _, apiResources := range apiResList {
gv, err := schema.ParseGroupVersion(apiResources.GroupVersion)
if err != nil {
klog.V(1).Infof("Unable to parse groupversion = %s due to = %s", apiResources.GroupVersion, err.Error())
continue
}
for _, apiRes := range apiResources.APIResources {
for _, shortName := range apiRes.ShortNames {
rs := resourceShortcuts{
ShortForm: schema.GroupResource{Group: gv.Group, Resource: shortName},
LongForm: schema.GroupResource{Group: gv.Group, Resource: apiRes.Name},
}
res = append(res, rs)
}
}
}
return apiResList, res, nil
}
// expandResourceShortcut will return the expanded version of resource
// (something that a pkg/api/meta.RESTMapper can understand), if it is
// indeed a shortcut. If no match has been found, we will match on group prefixing.
// Lastly we will return resource unmodified.
func (e shortcutExpander) expandResourceShortcut(resource schema.GroupVersionResource) schema.GroupVersionResource {
// get the shortcut mappings and return on first match.
if allResources, shortcutResources, err := e.getShortcutMappings(); err == nil {
// avoid expanding if there's an exact match to a full resource name
for _, apiResources := range allResources {
gv, err := schema.ParseGroupVersion(apiResources.GroupVersion)
if err != nil {
continue
}
if len(resource.Group) != 0 && resource.Group != gv.Group {
continue
}
for _, apiRes := range apiResources.APIResources {
if resource.Resource == apiRes.Name {
return resource
}
if resource.Resource == apiRes.SingularName {
return resource
}
}
}
for _, item := range shortcutResources {
if len(resource.Group) != 0 && resource.Group != item.ShortForm.Group {
continue
}
if resource.Resource == item.ShortForm.Resource {
resource.Resource = item.LongForm.Resource
resource.Group = item.LongForm.Group
return resource
}
}
// we didn't find exact match so match on group prefixing. This allows autoscal to match autoscaling
if len(resource.Group) == 0 {
return resource
}
for _, item := range shortcutResources {
if !strings.HasPrefix(item.ShortForm.Group, resource.Group) {
continue
}
if resource.Resource == item.ShortForm.Resource {
resource.Resource = item.LongForm.Resource
resource.Group = item.LongForm.Group
return resource
}
}
}
return resource
}
// ResourceShortcuts represents a structure that holds the information how to
// transition from resource's shortcut to its full name.
type resourceShortcuts struct {
ShortForm schema.GroupResource
LongForm schema.GroupResource
}