/
restore.go
executable file
·100 lines (89 loc) · 2.83 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
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
package lib
import (
"context"
"fmt"
"github.com/onsi/ginkgo/v2"
velero "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type restoreOpts func(*velero.Restore)
func WithIncludedResources(resources []string) restoreOpts {
return func(restore *velero.Restore) {
restore.Spec.IncludedResources = resources
}
}
func WithExcludedResources(resources []string) restoreOpts {
return func(restore *velero.Restore) {
restore.Spec.ExcludedResources = resources
}
}
func WithIncludedNamespaces(namespaces []string) restoreOpts {
return func(restore *velero.Restore) {
restore.Spec.IncludedNamespaces = namespaces
}
}
func WithExcludedNamespaces(namespaces []string) restoreOpts {
return func(restore *velero.Restore) {
restore.Spec.ExcludedNamespaces = namespaces
}
}
func CreateRestoreFromBackup(ocClient client.Client, veleroNamespace, backupName, restoreName string, opts ...restoreOpts) (velero.Restore, error) {
restore := velero.Restore{
ObjectMeta: metav1.ObjectMeta{
Name: restoreName,
Namespace: veleroNamespace,
},
Spec: velero.RestoreSpec{
BackupName: backupName,
},
}
for _, opt := range opts {
opt(&restore)
}
err := ocClient.Create(context.Background(), &restore)
return restore, err
}
func IsRestoreDone(ocClient client.Client, veleroNamespace, name string) wait.ConditionFunc {
return func() (bool, error) {
restore := velero.Restore{}
err := ocClient.Get(context.Background(), client.ObjectKey{
Namespace: veleroNamespace,
Name: name,
}, &restore)
if err != nil {
return false, err
}
if len(restore.Status.Phase) > 0 {
ginkgo.GinkgoWriter.Write([]byte(fmt.Sprintf("restore phase: %s\n", restore.Status.Phase)))
}
var phasesNotDone = []velero.RestorePhase{
velero.RestorePhaseNew,
velero.RestorePhaseInProgress,
velero.RestorePhaseWaitingForPluginOperations,
velero.RestorePhaseWaitingForPluginOperationsPartiallyFailed,
"",
}
for _, notDonePhase := range phasesNotDone {
if restore.Status.Phase == notDonePhase {
return false, nil
}
}
return true, nil
}
}
func IsRestoreCompletedSuccessfully(ocClient client.Client, veleroNamespace, name string) (bool, error) {
restore := velero.Restore{}
err := ocClient.Get(context.Background(), client.ObjectKey{
Namespace: veleroNamespace,
Name: name,
}, &restore)
if err != nil {
return false, err
}
if restore.Status.Phase == velero.RestorePhaseCompleted {
return true, nil
}
return false, fmt.Errorf("restore phase is: %s; expected: %s\nfailure reason: %s\nvalidation errors: %v\nvelero failure logs: %v", restore.Status.Phase, velero.RestorePhaseCompleted, restore.Status.FailureReason, restore.Status.ValidationErrors, GetVeleroContainerFailureLogs(veleroNamespace))
}