-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
manager.go
386 lines (298 loc) · 12.1 KB
/
manager.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
Copyright 2020 the Velero contributors.
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 clientmgmt
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/sirupsen/logrus"
biav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/backupitemaction/v1"
biav2cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/backupitemaction/v2"
"github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/process"
riav1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/restoreitemaction/v1"
riav2cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/restoreitemaction/v2"
vsv1cli "github.com/vmware-tanzu/velero/pkg/plugin/clientmgmt/volumesnapshotter/v1"
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
biav1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v1"
biav2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/backupitemaction/v2"
riav1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/restoreitemaction/v1"
riav2 "github.com/vmware-tanzu/velero/pkg/plugin/velero/restoreitemaction/v2"
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
)
// Manager manages the lifecycles of plugins.
type Manager interface {
// GetObjectStore returns the ObjectStore plugin for name.
GetObjectStore(name string) (velero.ObjectStore, error)
// GetVolumeSnapshotter returns the VolumeSnapshotter plugin for name.
GetVolumeSnapshotter(name string) (vsv1.VolumeSnapshotter, error)
// GetBackupItemActions returns all v1 backup item action plugins.
GetBackupItemActions() ([]biav1.BackupItemAction, error)
// GetBackupItemAction returns the backup item action plugin for name.
GetBackupItemAction(name string) (biav1.BackupItemAction, error)
// GetBackupItemActionsV2 returns all v2 backup item action plugins (including those adapted from v1).
GetBackupItemActionsV2() ([]biav2.BackupItemAction, error)
// GetBackupItemActionV2 returns the backup item action plugin for name.
GetBackupItemActionV2(name string) (biav2.BackupItemAction, error)
// GetRestoreItemActions returns all restore item action plugins.
GetRestoreItemActions() ([]riav1.RestoreItemAction, error)
// GetRestoreItemAction returns the restore item action plugin for name.
GetRestoreItemAction(name string) (riav1.RestoreItemAction, error)
// GetRestoreItemActionsV2 returns all v2 restore item action plugins.
GetRestoreItemActionsV2() ([]riav2.RestoreItemAction, error)
// GetRestoreItemActionV2 returns the restore item action plugin for name.
GetRestoreItemActionV2(name string) (riav2.RestoreItemAction, error)
// GetDeleteItemActions returns all delete item action plugins.
GetDeleteItemActions() ([]velero.DeleteItemAction, error)
// GetDeleteItemAction returns the delete item action plugin for name.
GetDeleteItemAction(name string) (velero.DeleteItemAction, error)
// CleanupClients terminates all of the Manager's running plugin processes.
CleanupClients()
}
// Used checking for adapted plugin versions
var pluginNotFoundErrType = &process.PluginNotFoundError{}
// manager implements Manager.
type manager struct {
logger logrus.FieldLogger
logLevel logrus.Level
registry process.Registry
restartableProcessFactory process.RestartableProcessFactory
// lock guards restartableProcesses
lock sync.Mutex
restartableProcesses map[string]process.RestartableProcess
}
// NewManager constructs a manager for getting plugins.
func NewManager(logger logrus.FieldLogger, level logrus.Level, registry process.Registry) Manager {
return &manager{
logger: logger,
logLevel: level,
registry: registry,
restartableProcessFactory: process.NewRestartableProcessFactory(),
restartableProcesses: make(map[string]process.RestartableProcess),
}
}
func (m *manager) CleanupClients() {
m.lock.Lock()
for _, restartableProcess := range m.restartableProcesses {
restartableProcess.Stop()
}
m.lock.Unlock()
}
// getRestartableProcess returns a restartableProcess for a plugin identified by kind and name, creating a
// restartableProcess if it is the first time it has been requested.
func (m *manager) getRestartableProcess(kind common.PluginKind, name string) (process.RestartableProcess, error) {
m.lock.Lock()
defer m.lock.Unlock()
logger := m.logger.WithFields(logrus.Fields{
"kind": kind.String(),
"name": name,
})
logger.Debug("looking for plugin in registry")
info, err := m.registry.Get(kind, name)
if err != nil {
return nil, err
}
logger = logger.WithField("command", info.Command)
restartableProcess, found := m.restartableProcesses[info.Command]
if found {
logger.Debug("found preexisting restartable plugin process")
return restartableProcess, nil
}
logger.Debug("creating new restartable plugin process")
restartableProcess, err = m.restartableProcessFactory.NewRestartableProcess(info.Command, m.logger, m.logLevel)
if err != nil {
return nil, err
}
m.restartableProcesses[info.Command] = restartableProcess
return restartableProcess, nil
}
// GetObjectStore returns a restartableObjectStore for name.
func (m *manager) GetObjectStore(name string) (velero.ObjectStore, error) {
name = sanitizeName(name)
restartableProcess, err := m.getRestartableProcess(common.PluginKindObjectStore, name)
if err != nil {
return nil, err
}
r := NewRestartableObjectStore(name, restartableProcess)
return r, nil
}
// GetVolumeSnapshotter returns a restartableVolumeSnapshotter for name.
func (m *manager) GetVolumeSnapshotter(name string) (vsv1.VolumeSnapshotter, error) {
name = sanitizeName(name)
for _, adaptedVolumeSnapshotter := range vsv1cli.AdaptedVolumeSnapshotters() {
restartableProcess, err := m.getRestartableProcess(adaptedVolumeSnapshotter.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedVolumeSnapshotter.GetRestartable(name, restartableProcess), nil
}
return nil, fmt.Errorf("unable to get valid VolumeSnapshotter for %q", name)
}
// GetBackupItemActions returns all backup item actions as restartableBackupItemActions.
func (m *manager) GetBackupItemActions() ([]biav1.BackupItemAction, error) {
list := m.registry.List(common.PluginKindBackupItemAction)
actions := make([]biav1.BackupItemAction, 0, len(list))
for i := range list {
id := list[i]
r, err := m.GetBackupItemAction(id.Name)
if err != nil {
return nil, err
}
actions = append(actions, r)
}
return actions, nil
}
// GetBackupItemAction returns a restartableBackupItemAction for name.
func (m *manager) GetBackupItemAction(name string) (biav1.BackupItemAction, error) {
name = sanitizeName(name)
for _, adaptedBackupItemAction := range biav1cli.AdaptedBackupItemActions() {
restartableProcess, err := m.getRestartableProcess(adaptedBackupItemAction.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedBackupItemAction.GetRestartable(name, restartableProcess), nil
}
return nil, fmt.Errorf("unable to get valid BackupItemAction for %q", name)
}
// GetBackupItemActionsV2 returns all v2 backup item actions as RestartableBackupItemActions.
func (m *manager) GetBackupItemActionsV2() ([]biav2.BackupItemAction, error) {
list := m.registry.List(common.PluginKindBackupItemActionV2)
actions := make([]biav2.BackupItemAction, 0, len(list))
for i := range list {
id := list[i]
r, err := m.GetBackupItemActionV2(id.Name)
if err != nil {
return nil, err
}
actions = append(actions, r)
}
return actions, nil
}
// GetBackupItemActionV2 returns a v2 restartableBackupItemAction for name.
func (m *manager) GetBackupItemActionV2(name string) (biav2.BackupItemAction, error) {
name = sanitizeName(name)
for _, adaptedBackupItemAction := range biav2cli.AdaptedBackupItemActions() {
restartableProcess, err := m.getRestartableProcess(adaptedBackupItemAction.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedBackupItemAction.GetRestartable(name, restartableProcess), nil
}
return nil, fmt.Errorf("unable to get valid BackupItemActionV2 for %q", name)
}
// GetRestoreItemActions returns all restore item actions as restartableRestoreItemActions.
func (m *manager) GetRestoreItemActions() ([]riav1.RestoreItemAction, error) {
list := m.registry.List(common.PluginKindRestoreItemAction)
actions := make([]riav1.RestoreItemAction, 0, len(list))
for i := range list {
id := list[i]
r, err := m.GetRestoreItemAction(id.Name)
if err != nil {
return nil, err
}
actions = append(actions, r)
}
return actions, nil
}
// GetRestoreItemAction returns a restartableRestoreItemAction for name.
func (m *manager) GetRestoreItemAction(name string) (riav1.RestoreItemAction, error) {
name = sanitizeName(name)
for _, adaptedRestoreItemAction := range riav1cli.AdaptedRestoreItemActions() {
restartableProcess, err := m.getRestartableProcess(adaptedRestoreItemAction.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedRestoreItemAction.GetRestartable(name, restartableProcess), nil
}
return nil, fmt.Errorf("unable to get valid RestoreItemAction for %q", name)
}
// GetRestoreItemActionsV2 returns all v2 restore item actions as restartableRestoreItemActions.
func (m *manager) GetRestoreItemActionsV2() ([]riav2.RestoreItemAction, error) {
list := m.registry.List(common.PluginKindRestoreItemActionV2)
actions := make([]riav2.RestoreItemAction, 0, len(list))
for i := range list {
id := list[i]
r, err := m.GetRestoreItemActionV2(id.Name)
if err != nil {
return nil, err
}
actions = append(actions, r)
}
return actions, nil
}
// GetRestoreItemActionV2 returns a v2 restartableRestoreItemAction for name.
func (m *manager) GetRestoreItemActionV2(name string) (riav2.RestoreItemAction, error) {
name = sanitizeName(name)
for _, adaptedRestoreItemAction := range riav2cli.AdaptedRestoreItemActions() {
restartableProcess, err := m.getRestartableProcess(adaptedRestoreItemAction.Kind, name)
// Check if plugin was not found
if errors.As(err, &pluginNotFoundErrType) {
continue
}
if err != nil {
return nil, err
}
return adaptedRestoreItemAction.GetRestartable(name, restartableProcess), nil
}
return nil, fmt.Errorf("unable to get valid RestoreItemActionV2 for %q", name)
}
// GetDeleteItemActions returns all delete item actions as restartableDeleteItemActions.
func (m *manager) GetDeleteItemActions() ([]velero.DeleteItemAction, error) {
list := m.registry.List(common.PluginKindDeleteItemAction)
actions := make([]velero.DeleteItemAction, 0, len(list))
for i := range list {
id := list[i]
r, err := m.GetDeleteItemAction(id.Name)
if err != nil {
return nil, err
}
actions = append(actions, r)
}
return actions, nil
}
// GetDeleteItemAction returns a restartableDeleteItemAction for name.
func (m *manager) GetDeleteItemAction(name string) (velero.DeleteItemAction, error) {
name = sanitizeName(name)
restartableProcess, err := m.getRestartableProcess(common.PluginKindDeleteItemAction, name)
if err != nil {
return nil, err
}
r := NewRestartableDeleteItemAction(name, restartableProcess)
return r, nil
}
// sanitizeName adds "velero.io" to legacy plugins that weren't namespaced.
func sanitizeName(name string) string {
// Backwards compatibility with non-namespaced Velero plugins, following principle of least surprise
// since DeleteItemActions were not bundled with Velero when plugins were non-namespaced.
if !strings.Contains(name, "/") {
name = "velero.io/" + name
}
return name
}