-
Notifications
You must be signed in to change notification settings - Fork 874
/
mapper.go
141 lines (123 loc) · 4.33 KB
/
mapper.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
/*
Copyright 2021 The KubeVela 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 discoverymapper
import (
"sync"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
)
// DiscoveryMapper is a interface for refresh and discovery resources from GVK.
type DiscoveryMapper interface {
GetMapper() (meta.RESTMapper, error)
Refresh() (meta.RESTMapper, error)
RESTMapping(gk schema.GroupKind, version ...string) (*meta.RESTMapping, error)
KindsFor(input schema.GroupVersionResource) ([]schema.GroupVersionKind, error)
ResourcesFor(input schema.GroupVersionKind) (schema.GroupVersionResource, error)
}
var _ DiscoveryMapper = &DefaultDiscoveryMapper{}
// DefaultDiscoveryMapper is a K8s resource mapper for discovery, it will cache the result
type DefaultDiscoveryMapper struct {
dc *discovery.DiscoveryClient
mapper meta.RESTMapper
mutex sync.RWMutex
}
// New will create a new DefaultDiscoveryMapper by giving a K8s rest config
func New(c *rest.Config) (DiscoveryMapper, error) {
dc, err := discovery.NewDiscoveryClientForConfig(c)
if err != nil {
return nil, err
}
return &DefaultDiscoveryMapper{
dc: dc,
}, nil
}
// GetMapper will get the cached restmapper, if nil, it will create one by refresh
// Prefer lazy discovery, because resources created after refresh can not be found
func (d *DefaultDiscoveryMapper) GetMapper() (meta.RESTMapper, error) {
d.mutex.RLock()
mapper := d.mapper
d.mutex.RUnlock()
if mapper == nil {
return d.Refresh()
}
return mapper, nil
}
// Refresh will re-create the mapper by getting the new resource from K8s API by using discovery client
func (d *DefaultDiscoveryMapper) Refresh() (meta.RESTMapper, error) {
gr, err := restmapper.GetAPIGroupResources(d.dc)
if err != nil {
return nil, err
}
d.mutex.Lock()
defer d.mutex.Unlock()
d.mapper = restmapper.NewDiscoveryRESTMapper(gr)
return d.mapper, nil
}
// RESTMapping will mapping resources from GVK, if not found, it will refresh from APIServer and try once again
func (d *DefaultDiscoveryMapper) RESTMapping(gk schema.GroupKind, version ...string) (*meta.RESTMapping, error) {
mapper, err := d.GetMapper()
if err != nil {
return nil, err
}
mapping, err := mapper.RESTMapping(gk, version...)
if meta.IsNoMatchError(err) {
// if no kind match err, refresh and try once more.
mapper, err = d.Refresh()
if err != nil {
return nil, err
}
mapping, err = mapper.RESTMapping(gk, version...)
}
return mapping, err
}
// KindsFor will get kinds from GroupVersionResource, if version not set, all resources matched will be returned.
func (d *DefaultDiscoveryMapper) KindsFor(input schema.GroupVersionResource) ([]schema.GroupVersionKind, error) {
mapper, err := d.GetMapper()
if err != nil {
return nil, err
}
mapping, err := mapper.KindsFor(input)
if meta.IsNoMatchError(err) {
// if no kind match err, refresh and try once more.
mapper, err = d.Refresh()
if err != nil {
return nil, err
}
mapping, err = mapper.KindsFor(input)
}
return mapping, err
}
// ResourcesFor will get a resource from GroupVersionKind
func (d *DefaultDiscoveryMapper) ResourcesFor(input schema.GroupVersionKind) (schema.GroupVersionResource, error) {
var gvr schema.GroupVersionResource
mapping, err := d.RESTMapping(input.GroupKind(), input.Version)
if err != nil {
return gvr, err
}
gvr = mapping.Resource
return gvr, nil
}
// IsNamespacedScope discover the resources supported by API server and check
// whether a resource is namespaced-scope.
func IsNamespacedScope(dm DiscoveryMapper, gk schema.GroupKind) (bool, error) {
restMapping, err := dm.RESTMapping(gk)
if err != nil {
return false, errors.WithMessage(err, "cannot check resource scope")
}
isNamespaced := restMapping.Scope.Name() == meta.RESTScopeNameNamespace
return isNamespaced, nil
}