-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
registry.go
274 lines (220 loc) · 9.67 KB
/
registry.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright 2022 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 registry
import (
"sync"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime/schema"
kerrors "k8s.io/apimachinery/pkg/util/errors"
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
)
// ExtensionRegistry defines the funcs of a RuntimeExtension registry.
type ExtensionRegistry interface {
// WarmUp can be used to initialize a "cold" RuntimeExtension registry with all
// known runtimev1.ExtensionConfigs at a given time.
// After WarmUp completes the RuntimeExtension registry is considered ready.
WarmUp(extensionConfigList *runtimev1.ExtensionConfigList) error
// IsReady returns true if the RuntimeExtension registry is ready for usage.
// This happens after WarmUp is completed.
IsReady() bool
// Add adds all RuntimeExtensions of the given ExtensionConfig.
// Please note that if the ExtensionConfig has been added before, the
// corresponding registry entries will get updated/replaced with the
// one from the newly provided ExtensionConfig.
Add(extensionConfig *runtimev1.ExtensionConfig) error
// Remove removes all RuntimeExtensions corresponding to the provided ExtensionConfig.
Remove(extensionConfig *runtimev1.ExtensionConfig) error
// List lists all registered RuntimeExtensions for a given catalog.GroupHook.
List(gh runtimecatalog.GroupHook) ([]*ExtensionRegistration, error)
// Get gets the RuntimeExtensions with the given name.
Get(name string) (*ExtensionRegistration, error)
}
// ExtensionRegistration contains information about a registered RuntimeExtension.
type ExtensionRegistration struct {
// Name is the unique name of the RuntimeExtension.
Name string
// ExtensionConfigName is the name of the corresponding ExtensionConfig.
ExtensionConfigName string
// GroupVersionHook is the GroupVersionHook that the RuntimeExtension implements.
GroupVersionHook runtimecatalog.GroupVersionHook
// NamespaceSelector limits the objects by namespace for which a Runtime Extension is called.
NamespaceSelector labels.Selector
// ClientConfig is the ClientConfig to communicate with the RuntimeExtension.
ClientConfig runtimev1.ClientConfig
// TimeoutSeconds is the timeout duration used for calls to the RuntimeExtension.
TimeoutSeconds *int32
// FailurePolicy defines how failures in calls to the RuntimeExtension should be handled by a client.
FailurePolicy *runtimev1.FailurePolicy
// Settings captures additional information sent in call to the RuntimeExtensions.
Settings map[string]string
}
// extensionRegistry is an implementation of ExtensionRegistry.
type extensionRegistry struct {
// ready represents if the registry has been warmed up.
ready bool
// items contains the registry entries.
items map[string]*ExtensionRegistration
// lock is used to synchronize access to fields of the extensionRegistry.
lock sync.RWMutex
}
// New returns a new ExtensionRegistry.
func New() ExtensionRegistry {
return &extensionRegistry{
items: map[string]*ExtensionRegistration{},
}
}
// WarmUp can be used to initialize a "cold" RuntimeExtension registry with all
// known runtimev1.ExtensionConfigs at a given time.
// After WarmUp completes the RuntimeExtension registry is considered ready.
func (r *extensionRegistry) WarmUp(extensionConfigList *runtimev1.ExtensionConfigList) error {
if extensionConfigList == nil {
return errors.New("failed to warm up registry: invalid argument: when calling WarmUp ExtensionConfigList must not be nil")
}
r.lock.Lock()
defer r.lock.Unlock()
if r.ready {
return errors.New("failed to warm up registry: invalid operation: WarmUp cannot be called on a registry which has already been warmed up")
}
var allErrs []error
for i := range extensionConfigList.Items {
if err := r.add(&extensionConfigList.Items[i]); err != nil {
allErrs = append(allErrs, err)
}
}
if len(allErrs) > 0 {
// Reset the map, so that the next WarmUp can start with an empty map
// and doesn't inherit entries from this failed WarmUp.
r.items = map[string]*ExtensionRegistration{}
return errors.Wrapf(kerrors.NewAggregate(allErrs), "failed to warm up registry")
}
r.ready = true
return nil
}
// IsReady returns true if the RuntimeExtension registry is ready for usage.
// This happens after WarmUp is completed.
func (r *extensionRegistry) IsReady() bool {
r.lock.RLock()
defer r.lock.RUnlock()
return r.ready
}
// Add adds all RuntimeExtensions of the given ExtensionConfig.
// Please note that if the ExtensionConfig has been added before, the
// corresponding registry entries will get updated/replaced with the
// one from the newly provided ExtensionConfig.
func (r *extensionRegistry) Add(extensionConfig *runtimev1.ExtensionConfig) error {
if extensionConfig == nil {
return errors.New("failed to add ExtensionConfig to registry: invalid argument: when calling Add extensionConfig must not be nil")
}
r.lock.Lock()
defer r.lock.Unlock()
if !r.ready {
return errors.Errorf("failed to add ExtensionConfig %q to registry: invalid operation: Add cannot be called on a registry which has not been warmed up", extensionConfig.Name)
}
return r.add(extensionConfig)
}
// Remove removes all RuntimeExtensions corresponding to the provided ExtensionConfig.
func (r *extensionRegistry) Remove(extensionConfig *runtimev1.ExtensionConfig) error {
if extensionConfig == nil {
return errors.New("failed to remove ExtensionConfig from registry: invalid argument: when calling Remove ExtensionConfig must not be nil")
}
r.lock.Lock()
defer r.lock.Unlock()
if !r.ready {
return errors.Errorf("failed to remove ExtensionConfig %q from registry: invalid operation: Remove cannot be called on a registry which has not been warmed up", extensionConfig.Name)
}
r.remove(extensionConfig)
return nil
}
func (r *extensionRegistry) remove(extensionConfig *runtimev1.ExtensionConfig) {
for _, e := range r.items {
if e.ExtensionConfigName == extensionConfig.Name {
delete(r.items, e.Name)
}
}
}
// List lists all registered RuntimeExtensions for a given catalog.GroupHook.
func (r *extensionRegistry) List(gh runtimecatalog.GroupHook) ([]*ExtensionRegistration, error) {
if gh.Group == "" {
return nil, errors.New("failed to list extension handlers: invalid argument: when calling List gh.Group must not be empty")
}
if gh.Hook == "" {
return nil, errors.New("failed to list extension handlers: invalid argument: when calling List gh.Hook must not be empty")
}
r.lock.RLock()
defer r.lock.RUnlock()
if !r.ready {
return nil, errors.Errorf("failed to list extension handlers for GroupHook %q: invalid operation: List cannot be called on a registry which has not been warmed up", gh.String())
}
l := []*ExtensionRegistration{}
for _, registration := range r.items {
if registration.GroupVersionHook.Group == gh.Group && registration.GroupVersionHook.Hook == gh.Hook {
l = append(l, registration)
}
}
return l, nil
}
// Get gets the RuntimeExtensions with the given name.
func (r *extensionRegistry) Get(name string) (*ExtensionRegistration, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.ready {
return nil, errors.Errorf("failed to get extension handler %q from registry: invalid operation: Get cannot be called on a registry not yet ready", name)
}
registration, ok := r.items[name]
if !ok {
return nil, errors.Errorf("failed to get extension handler %q from registry: handler with name %q has not been registered", name, name)
}
return registration, nil
}
func (r *extensionRegistry) add(extensionConfig *runtimev1.ExtensionConfig) error {
r.remove(extensionConfig)
// Create a selector from the NamespaceSelector defined in the extensionConfig spec.
selector, err := metav1.LabelSelectorAsSelector(extensionConfig.Spec.NamespaceSelector)
if err != nil {
return errors.Wrapf(err, "failed to add ExtensionConfig %q to registry: failed to create namespaceSelector", extensionConfig.Name)
}
var allErrs []error
registrations := []*ExtensionRegistration{}
for _, e := range extensionConfig.Status.Handlers {
gv, err := schema.ParseGroupVersion(e.RequestHook.APIVersion)
if err != nil {
allErrs = append(allErrs, errors.Wrapf(err, "failed to add extension handler %q to registry: failed to parse GroupVersion %q of handler %q", e.Name, e.RequestHook.APIVersion, e.Name))
continue
}
// Registrations will only be added to the registry if no errors occur (all or nothing).
registrations = append(registrations, &ExtensionRegistration{
ExtensionConfigName: extensionConfig.Name,
Name: e.Name,
GroupVersionHook: runtimecatalog.GroupVersionHook{
Group: gv.Group,
Version: gv.Version,
Hook: e.RequestHook.Hook,
},
NamespaceSelector: selector,
ClientConfig: extensionConfig.Spec.ClientConfig,
TimeoutSeconds: e.TimeoutSeconds,
FailurePolicy: e.FailurePolicy,
Settings: extensionConfig.Spec.Settings,
})
}
if len(allErrs) > 0 {
return errors.Wrapf(kerrors.NewAggregate(allErrs), "failed to add ExtensionConfig %q to registry", extensionConfig.Name)
}
for _, registration := range registrations {
r.items[registration.Name] = registration
}
return nil
}