/
restore.go
46 lines (37 loc) · 1.67 KB
/
restore.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
package cronjob
import (
"encoding/json"
"github.com/konveyor/openshift-velero-plugin/velero-plugins/common"
"github.com/sirupsen/logrus"
"github.com/vmware-tanzu/velero/pkg/plugin/velero"
batchv1beta1API "k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// RestorePlugin is a restore item action plugin for Velero
type RestorePlugin struct {
Log logrus.FieldLogger
}
// AppliesTo returns a velero.ResourceSelector that applies to cronjobs
func (p *RestorePlugin) AppliesTo() (velero.ResourceSelector, error) {
return velero.ResourceSelector{
IncludedResources: []string{"cronjobs"},
}, nil
}
// Execute action for the restore plugin for the cronjob resource
func (p *RestorePlugin) Execute(input *velero.RestoreItemActionExecuteInput) (*velero.RestoreItemActionExecuteOutput, error) {
p.Log.Info("[cronjob-restore] Entering CronJob restore plugin")
cronjob := batchv1beta1API.CronJob{}
itemMarshal, _ := json.Marshal(input.Item)
json.Unmarshal(itemMarshal, &cronjob)
p.Log.Infof("[cronjob-restore] cronjob: %s", cronjob.Name)
backupRegistry, registry, err := common.GetSrcAndDestRegistryInfo(input.Item)
if err != nil {
return nil, err
}
common.SwapContainerImageRefs(cronjob.Spec.JobTemplate.Spec.Template.Spec.Containers, backupRegistry, registry, p.Log, input.Restore.Spec.NamespaceMapping)
common.SwapContainerImageRefs(cronjob.Spec.JobTemplate.Spec.Template.Spec.InitContainers, backupRegistry, registry, p.Log, input.Restore.Spec.NamespaceMapping)
var out map[string]interface{}
objrec, _ := json.Marshal(cronjob)
json.Unmarshal(objrec, &out)
return velero.NewRestoreItemActionExecuteOutput(&unstructured.Unstructured{Object: out}), nil
}