This repository has been archived by the owner on May 26, 2021. It is now read-only.
forked from vmware-tanzu/velero
/
manager.go
410 lines (334 loc) · 12.4 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Copyright 2017 the Heptio Ark 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 plugin
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
plugin "github.com/hashicorp/go-plugin"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/heptio/ark/pkg/backup"
"github.com/heptio/ark/pkg/cloudprovider"
"github.com/heptio/ark/pkg/restore"
)
// PluginKind is a type alias for a string that describes
// the kind of an Ark-supported plugin.
type PluginKind string
func (k PluginKind) String() string {
return string(k)
}
func baseConfig() *plugin.ClientConfig {
return &plugin.ClientConfig{
HandshakeConfig: Handshake,
AllowedProtocols: []plugin.Protocol{plugin.ProtocolGRPC},
}
}
const (
// PluginKindObjectStore is the Kind string for
// an Object Store plugin.
PluginKindObjectStore PluginKind = "objectstore"
// PluginKindBlockStore is the Kind string for
// a Block Store plugin.
PluginKindBlockStore PluginKind = "blockstore"
// PluginKindCloudProvider is the Kind string for
// a CloudProvider plugin (i.e. an Object & Block
// store).
//
// NOTE that it is highly likely that in subsequent
// versions of Ark this kind of plugin will be replaced
// with a different mechanism for providing multiple
// plugin impls within a single binary. This should
// probably not be used.
PluginKindCloudProvider PluginKind = "cloudprovider"
// PluginKindBackupItemAction is the Kind string for
// a Backup ItemAction plugin.
PluginKindBackupItemAction PluginKind = "backupitemaction"
// PluginKindRestoreItemAction is the Kind string for
// a Restore ItemAction plugin.
PluginKindRestoreItemAction PluginKind = "restoreitemaction"
pluginDir = "/plugins"
)
var AllPluginKinds = []PluginKind{
PluginKindObjectStore,
PluginKindBlockStore,
PluginKindCloudProvider,
PluginKindBackupItemAction,
PluginKindRestoreItemAction,
}
type pluginInfo struct {
kinds []PluginKind
name string
commandName string
commandArgs []string
}
// Manager exposes functions for getting implementations of the pluggable
// Ark interfaces.
type Manager interface {
// GetObjectStore returns the plugin implementation of the
// cloudprovider.ObjectStore interface with the specified name.
GetObjectStore(name string) (cloudprovider.ObjectStore, error)
// GetBlockStore returns the plugin implementation of the
// cloudprovider.BlockStore interface with the specified name.
GetBlockStore(name string) (cloudprovider.BlockStore, error)
// GetBackupItemActions returns all backup.ItemAction plugins.
// These plugin instances should ONLY be used for a single backup
// (mainly because each one outputs to a per-backup log),
// and should be terminated upon completion of the backup with
// CloseBackupItemActions().
GetBackupItemActions(backupName string) ([]backup.ItemAction, error)
// CloseBackupItemActions terminates the plugin sub-processes that
// are hosting BackupItemAction plugins for the given backup name.
CloseBackupItemActions(backupName string) error
// GetRestoreItemActions returns all restore.ItemAction plugins.
// These plugin instances should ONLY be used for a single restore
// (mainly because each one outputs to a per-restore log),
// and should be terminated upon completion of the restore with
// CloseRestoreItemActions().
GetRestoreItemActions(restoreName string) ([]restore.ItemAction, error)
// CloseRestoreItemActions terminates the plugin sub-processes that
// are hosting RestoreItemAction plugins for the given restore name.
CloseRestoreItemActions(restoreName string) error
}
type manager struct {
logger logrus.FieldLogger
logLevel logrus.Level
pluginRegistry *registry
clientStore *clientStore
}
// NewManager constructs a manager for getting plugin implementations.
func NewManager(logger logrus.FieldLogger, level logrus.Level) (Manager, error) {
m := &manager{
logger: logger,
logLevel: level,
pluginRegistry: newRegistry(),
clientStore: newClientStore(),
}
if err := m.registerPlugins(); err != nil {
return nil, err
}
return m, nil
}
func pluginForKind(kind PluginKind) plugin.Plugin {
switch kind {
case PluginKindObjectStore:
return &ObjectStorePlugin{}
case PluginKindBlockStore:
return &BlockStorePlugin{}
default:
return nil
}
}
func getPluginInstance(client *plugin.Client, kind PluginKind) (interface{}, error) {
protocolClient, err := client.Client()
if err != nil {
return nil, errors.WithStack(err)
}
plugin, err := protocolClient.Dispense(string(kind))
if err != nil {
return nil, errors.WithStack(err)
}
return plugin, nil
}
func (m *manager) registerPlugins() error {
// first, register internal plugins
for _, provider := range []string{"aws", "gcp", "azure"} {
m.pluginRegistry.register(provider, "/ark", []string{"run-plugin", "cloudprovider", provider}, PluginKindObjectStore, PluginKindBlockStore)
}
m.pluginRegistry.register("pv", "/ark", []string{"run-plugin", string(PluginKindBackupItemAction), "pv"}, PluginKindBackupItemAction)
m.pluginRegistry.register("backup-pod", "/ark", []string{"run-plugin", string(PluginKindBackupItemAction), "pod"}, PluginKindBackupItemAction)
m.pluginRegistry.register("job", "/ark", []string{"run-plugin", string(PluginKindRestoreItemAction), "job"}, PluginKindRestoreItemAction)
m.pluginRegistry.register("restore-pod", "/ark", []string{"run-plugin", string(PluginKindRestoreItemAction), "pod"}, PluginKindRestoreItemAction)
m.pluginRegistry.register("svc", "/ark", []string{"run-plugin", string(PluginKindRestoreItemAction), "svc"}, PluginKindRestoreItemAction)
// second, register external plugins (these will override internal plugins, if applicable)
if _, err := os.Stat(pluginDir); err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
files, err := ioutil.ReadDir(pluginDir)
if err != nil {
return err
}
for _, file := range files {
name, kind, err := parse(file.Name())
if err != nil {
continue
}
if kind == PluginKindCloudProvider {
m.pluginRegistry.register(name, filepath.Join(pluginDir, file.Name()), nil, PluginKindObjectStore, PluginKindBlockStore)
} else {
m.pluginRegistry.register(name, filepath.Join(pluginDir, file.Name()), nil, kind)
}
}
return nil
}
func parse(filename string) (string, PluginKind, error) {
for _, kind := range AllPluginKinds {
if prefix := fmt.Sprintf("ark-%s-", kind); strings.Index(filename, prefix) == 0 {
return strings.Replace(filename, prefix, "", -1), kind, nil
}
}
return "", "", errors.New("invalid file name")
}
// GetObjectStore returns the plugin implementation of the cloudprovider.ObjectStore
// interface with the specified name.
func (m *manager) GetObjectStore(name string) (cloudprovider.ObjectStore, error) {
pluginObj, err := m.getCloudProviderPlugin(name, PluginKindObjectStore)
if err != nil {
return nil, err
}
objStore, ok := pluginObj.(cloudprovider.ObjectStore)
if !ok {
return nil, errors.New("could not convert gRPC client to cloudprovider.ObjectStore")
}
return objStore, nil
}
// GetBlockStore returns the plugin implementation of the cloudprovider.BlockStore
// interface with the specified name.
func (m *manager) GetBlockStore(name string) (cloudprovider.BlockStore, error) {
pluginObj, err := m.getCloudProviderPlugin(name, PluginKindBlockStore)
if err != nil {
return nil, err
}
blockStore, ok := pluginObj.(cloudprovider.BlockStore)
if !ok {
return nil, errors.New("could not convert gRPC client to cloudprovider.BlockStore")
}
return blockStore, nil
}
func (m *manager) getCloudProviderPlugin(name string, kind PluginKind) (interface{}, error) {
client, err := m.clientStore.get(kind, name, "")
if err != nil {
pluginInfo, err := m.pluginRegistry.get(kind, name)
if err != nil {
return nil, err
}
// build a plugin client that can dispense all of the PluginKinds it's registered for
clientBuilder := newClientBuilder(baseConfig()).
withCommand(pluginInfo.commandName, pluginInfo.commandArgs...).
withLogger(&logrusAdapter{impl: m.logger, level: m.logLevel})
for _, kind := range pluginInfo.kinds {
clientBuilder.withPlugin(kind, pluginForKind(kind))
}
client = clientBuilder.client()
// register the plugin client for the appropriate kinds
for _, kind := range pluginInfo.kinds {
m.clientStore.add(client, kind, name, "")
}
}
pluginObj, err := getPluginInstance(client, kind)
if err != nil {
return nil, err
}
return pluginObj, nil
}
// GetBackupActions returns all backup.BackupAction plugins.
// These plugin instances should ONLY be used for a single backup
// (mainly because each one outputs to a per-backup log),
// and should be terminated upon completion of the backup with
// CloseBackupActions().
func (m *manager) GetBackupItemActions(backupName string) ([]backup.ItemAction, error) {
clients, err := m.clientStore.list(PluginKindBackupItemAction, backupName)
if err != nil {
pluginInfo, err := m.pluginRegistry.list(PluginKindBackupItemAction)
if err != nil {
return nil, err
}
// create clients for each
for _, plugin := range pluginInfo {
logger := &logrusAdapter{impl: m.logger, level: m.logLevel}
client := newClientBuilder(baseConfig()).
withCommand(plugin.commandName, plugin.commandArgs...).
withPlugin(PluginKindBackupItemAction, &BackupItemActionPlugin{log: logger}).
withLogger(logger).
client()
m.clientStore.add(client, PluginKindBackupItemAction, plugin.name, backupName)
clients = append(clients, client)
}
}
var backupActions []backup.ItemAction
for _, client := range clients {
plugin, err := getPluginInstance(client, PluginKindBackupItemAction)
if err != nil {
m.CloseBackupItemActions(backupName)
return nil, err
}
backupAction, ok := plugin.(backup.ItemAction)
if !ok {
m.CloseBackupItemActions(backupName)
return nil, errors.New("could not convert gRPC client to backup.ItemAction")
}
backupActions = append(backupActions, backupAction)
}
return backupActions, nil
}
// CloseBackupItemActions terminates the plugin sub-processes that
// are hosting BackupItemAction plugins for the given backup name.
func (m *manager) CloseBackupItemActions(backupName string) error {
return closeAll(m.clientStore, PluginKindBackupItemAction, backupName)
}
func (m *manager) GetRestoreItemActions(restoreName string) ([]restore.ItemAction, error) {
clients, err := m.clientStore.list(PluginKindRestoreItemAction, restoreName)
if err != nil {
pluginInfo, err := m.pluginRegistry.list(PluginKindRestoreItemAction)
if err != nil {
return nil, err
}
// create clients for each
for _, plugin := range pluginInfo {
logger := &logrusAdapter{impl: m.logger, level: m.logLevel}
client := newClientBuilder(baseConfig()).
withCommand(plugin.commandName, plugin.commandArgs...).
withPlugin(PluginKindRestoreItemAction, &RestoreItemActionPlugin{log: logger}).
withLogger(logger).
client()
m.clientStore.add(client, PluginKindRestoreItemAction, plugin.name, restoreName)
clients = append(clients, client)
}
}
var itemActions []restore.ItemAction
for _, client := range clients {
plugin, err := getPluginInstance(client, PluginKindRestoreItemAction)
if err != nil {
m.CloseRestoreItemActions(restoreName)
return nil, err
}
itemAction, ok := plugin.(restore.ItemAction)
if !ok {
m.CloseRestoreItemActions(restoreName)
return nil, errors.New("could not convert gRPC client to restore.ItemAction")
}
itemActions = append(itemActions, itemAction)
}
return itemActions, nil
}
// CloseRestoreItemActions terminates the plugin sub-processes that
// are hosting RestoreItemAction plugins for the given restore name.
func (m *manager) CloseRestoreItemActions(restoreName string) error {
return closeAll(m.clientStore, PluginKindRestoreItemAction, restoreName)
}
func closeAll(store *clientStore, kind PluginKind, scope string) error {
clients, err := store.list(kind, scope)
if err != nil {
return err
}
for _, client := range clients {
client.Kill()
}
store.deleteAll(kind, scope)
return nil
}