/
snapshot.go
147 lines (128 loc) · 4.65 KB
/
snapshot.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
package snapshotUtils
import (
"context"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/builder"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
backupdriverv1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/apis/backupdriver/v1alpha1"
v1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/generated/clientset/versioned/typed/backupdriver/v1alpha1"
core_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)
/*
This will wait until one of the specified waitForPhases is reached, the timeout is reached or the context is canceled.
Note: Canceling the context cancels the wait, it does not cancel the snapshot operation. Use CancelSnapshot to cancel
an in-progress snapshot
*/
type waitResult struct {
item interface{}
err error
}
type BackupRepository struct {
backupRepositoryName string
}
func NewBackupRepository(backupRepositoryName string) *BackupRepository {
return &BackupRepository{
backupRepositoryName,
}
}
func checkPhasesAndSendResult(waitForPhases []backupdriverv1.SnapshotPhase, snapshot *backupdriverv1.Snapshot,
results chan waitResult) {
for _, checkPhase := range waitForPhases {
if snapshot.Status.Phase == checkPhase {
results <- waitResult{
item: snapshot,
err: nil,
}
}
}
}
// Create a Snapshot record in the specified namespace.
func SnapshotRef(ctx context.Context,
clientSet *v1.BackupdriverV1alpha1Client,
objectToSnapshot core_v1.TypedLocalObjectReference,
namespace string,
repository BackupRepository,
labels map[string]string,
waitForPhases []backupdriverv1.SnapshotPhase,
logger logrus.FieldLogger) (*backupdriverv1.Snapshot, error) {
snapshotUUID, err := uuid.NewRandom()
if err != nil {
return nil, errors.Wrapf(err, "Could not create snapshot UUID")
}
snapshotName := "snap-" + snapshotUUID.String()
snapshotReq := builder.ForSnapshot(namespace, snapshotName, labels).
BackupRepository(repository.backupRepositoryName).
ObjectReference(objectToSnapshot).
CancelState(false).Result()
writtenSnapshot, err := clientSet.Snapshots(namespace).Create(context.TODO(), snapshotReq, metav1.CreateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to create snapshot record")
}
logger.Infof("Snapshot record, %s, created", writtenSnapshot.Name)
writtenSnapshot.Status.Phase = backupdriverv1.SnapshotPhaseNew
writtenSnapshot, err = clientSet.Snapshots(namespace).UpdateStatus(context.TODO(), writtenSnapshot, metav1.UpdateOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to update status of snapshot record")
}
logger.Infof("Snapshot record, %s, status updated to %s", writtenSnapshot.Name, writtenSnapshot.Status.Phase)
updatedSnapshot, err := WaitForPhases(ctx, clientSet, *writtenSnapshot, waitForPhases, namespace, logger)
if err != nil {
return nil, errors.Wrapf(err, "Failed to wait for expected snapshot phases")
}
return updatedSnapshot, err
}
func WaitForPhases(ctx context.Context, clientSet *v1.BackupdriverV1alpha1Client, snapshotToWait backupdriverv1.Snapshot, waitForPhases []backupdriverv1.SnapshotPhase, namespace string, logger logrus.FieldLogger) (*backupdriverv1.Snapshot, error) {
results := make(chan waitResult)
watchlist := cache.NewListWatchFromClient(clientSet.RESTClient(), "snapshots", namespace,
fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&backupdriverv1.Snapshot{},
time.Second*0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
snapshot := obj.(*backupdriverv1.Snapshot)
if snapshot.Name != snapshotToWait.Name {
return
}
logger.Infof("snapshot added: %v", snapshot)
logger.Infof("phase = %s", snapshot.Status.Phase)
checkPhasesAndSendResult(waitForPhases, snapshot, results)
},
DeleteFunc: func(obj interface{}) {
snapshot := obj.(*backupdriverv1.Snapshot)
if snapshot.Name != snapshotToWait.Name {
return
}
logger.Infof("snapshot deleted: %s", obj)
results <- waitResult{
item: nil,
err: errors.New("Snapshot deleted"),
}
},
UpdateFunc: func(oldObj, newObj interface{}) {
snapshot := newObj.(*backupdriverv1.Snapshot)
if snapshot.Name != snapshotToWait.Name {
return
}
logger.Infof("snapshot= %s changed, phase = %s", snapshot.Name, snapshot.Status.Phase)
checkPhasesAndSendResult(waitForPhases, snapshot, results)
},
},
)
stop := make(chan struct{})
defer close(stop)
go controller.Run(stop)
select {
case <-ctx.Done():
stop <- struct{}{}
return nil, ctx.Err()
case result := <-results:
return result.item.(*backupdriverv1.Snapshot), result.err
}
}