2018-05-13 13:28:09 +00:00
|
|
|
/*
|
2019-03-20 19:32:48 +00:00
|
|
|
Copyright 2018 the Velero contributors.
|
2018-05-13 13:28:09 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2019-03-15 18:32:11 +00:00
|
|
|
|
|
|
|
package clientmgmt
|
2018-05-13 13:28:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2018-10-23 14:36:11 +00:00
|
|
|
|
2019-03-15 01:25:52 +00:00
|
|
|
"github.com/heptio/velero/pkg/plugin/framework"
|
2019-03-14 20:35:06 +00:00
|
|
|
"github.com/heptio/velero/pkg/plugin/velero"
|
2018-05-13 13:28:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// restartableRestoreItemAction is a restore item action for a given implementation (such as "pod"). It is associated with
|
|
|
|
// a restartableProcess, which may be shared and used to run multiple plugins. At the beginning of each method
|
|
|
|
// call, the restartableRestoreItemAction asks its restartableProcess to restart itself if needed (e.g. if the
|
|
|
|
// process terminated for any reason), then it proceeds with the actual call.
|
|
|
|
type restartableRestoreItemAction struct {
|
|
|
|
key kindAndName
|
|
|
|
sharedPluginProcess RestartableProcess
|
|
|
|
config map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newRestartableRestoreItemAction returns a new restartableRestoreItemAction.
|
|
|
|
func newRestartableRestoreItemAction(name string, sharedPluginProcess RestartableProcess) *restartableRestoreItemAction {
|
|
|
|
r := &restartableRestoreItemAction{
|
2019-03-15 01:25:52 +00:00
|
|
|
key: kindAndName{kind: framework.PluginKindRestoreItemAction, name: name},
|
2018-05-13 13:28:09 +00:00
|
|
|
sharedPluginProcess: sharedPluginProcess,
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// getRestoreItemAction returns the restore item action for this restartableRestoreItemAction. It does *not* restart the
|
|
|
|
// plugin process.
|
2019-03-14 20:35:06 +00:00
|
|
|
func (r *restartableRestoreItemAction) getRestoreItemAction() (velero.RestoreItemAction, error) {
|
2018-05-13 13:28:09 +00:00
|
|
|
plugin, err := r.sharedPluginProcess.getByKindAndName(r.key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-03-14 20:35:06 +00:00
|
|
|
restoreItemAction, ok := plugin.(velero.RestoreItemAction)
|
2018-05-13 13:28:09 +00:00
|
|
|
if !ok {
|
2019-03-14 20:35:06 +00:00
|
|
|
return nil, errors.Errorf("%T is not a RestoreItemAction!", plugin)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return restoreItemAction, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDelegate restarts the plugin process (if needed) and returns the restore item action for this restartableRestoreItemAction.
|
2019-03-14 20:35:06 +00:00
|
|
|
func (r *restartableRestoreItemAction) getDelegate() (velero.RestoreItemAction, error) {
|
2018-05-13 13:28:09 +00:00
|
|
|
if err := r.sharedPluginProcess.resetIfNeeded(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.getRestoreItemAction()
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppliesTo restarts the plugin's process if needed, then delegates the call.
|
2019-03-14 20:35:06 +00:00
|
|
|
func (r *restartableRestoreItemAction) AppliesTo() (velero.ResourceSelector, error) {
|
2018-05-13 13:28:09 +00:00
|
|
|
delegate, err := r.getDelegate()
|
|
|
|
if err != nil {
|
2019-03-14 20:35:06 +00:00
|
|
|
return velero.ResourceSelector{}, err
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return delegate.AppliesTo()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute restarts the plugin's process if needed, then delegates the call.
|
2019-03-14 20:35:06 +00:00
|
|
|
func (r *restartableRestoreItemAction) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) {
|
2018-05-13 13:28:09 +00:00
|
|
|
delegate, err := r.getDelegate()
|
|
|
|
if err != nil {
|
2018-12-05 13:22:04 +00:00
|
|
|
return nil, err
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 13:22:04 +00:00
|
|
|
return delegate.Execute(input)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|