/
backup_repository.go
338 lines (308 loc) · 13.7 KB
/
backup_repository.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
package backuprepository
import (
"context"
"encoding/json"
"fmt"
jsonpatch "github.com/evanphx/json-patch"
"github.com/vmware-tanzu/astrolabe/pkg/s3repository"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/constants"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/generated/clientset/versioned"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/utils"
"k8s.io/apimachinery/pkg/types"
"reflect"
"time"
"k8s.io/client-go/rest"
"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"
"github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/builder"
backupdriverTypedV1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/generated/clientset/versioned/typed/backupdriver/v1alpha1"
v1 "github.com/vmware-tanzu/velero-plugin-for-vsphere/pkg/generated/clientset/versioned/typed/backupdriver/v1alpha1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/tools/cache"
)
type waitBRCResult struct {
backupRepositoryName string
err error
}
/*
This will either create a new BackupRepositoryClaim record or update/use an existing BackupRepositoryClaim record. In
either case, it does not return until the BackupRepository is assigned and ready to use or the context is canceled
(in which case it will return an error).
*/
func ClaimBackupRepository(ctx context.Context,
repositoryDriver string,
repositoryParameters map[string]string,
allowedNamespaces []string,
ns string,
backupdriverV1Client *v1.BackupdriverV1alpha1Client,
logger logrus.FieldLogger) (string, error) {
// The map holds all the BRCs that match the parameters.
// Any BR that references any of the BRC in the map can be returned.
brcMap := make(map[string]*backupdriverv1.BackupRepositoryClaim)
brcList, err := backupdriverV1Client.BackupRepositoryClaims(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return "", errors.Errorf("Failed to list backup repository claims from the %v namespace", ns)
}
logger.Infof("Found %d BackupRepositoryClaims", len(brcList.Items))
// Pre-existing BackupRepositoryClaims found.
for _, repositoryClaimItem := range brcList.Items {
// Process the BRC only if its they match all the params.
repoMatch := compareBackupRepositoryClaim(repositoryDriver, repositoryParameters, allowedNamespaces, &repositoryClaimItem, logger)
if repoMatch {
if repositoryClaimItem.BackupRepository == "" {
logger.Infof("Found matching BRC for the parameters with no BR reference, BRC: %s", repositoryClaimItem.Name)
logger.Infof("Continuing processing other BRC, the backupdriver is expected to process this BRC later")
brcMap[repositoryClaimItem.Name] = &repositoryClaimItem
} else {
logger.Infof("Found matching BRC for the parameters, BRC: %s", repositoryClaimItem.Name)
// Retrieve the BackupRepository and return.
backupRepositoryNameFound := repositoryClaimItem.BackupRepository
return backupRepositoryNameFound, nil
}
}
}
if len(brcMap) == 0 {
// No existing BackupRepositoryClaim in the specified guest cluster namespace,
// Or no matching BackupRepositoryClaim among them.
// Create a new BackupRepositoryClaim to indicate the params.
backupRepoClaimUUID, err := uuid.NewRandom()
if err != nil {
return "", errors.Errorf("Failed to generate backup repository claim name")
}
backupRepoClaimName := "brc-" + backupRepoClaimUUID.String()
backupRepositoryClaimReq := builder.ForBackupRepositoryClaim(ns, backupRepoClaimName).
RepositoryParameters(repositoryParameters).RepositoryDriver().
AllowedNamespaces(allowedNamespaces).Result()
backupRepositoryClaim, err := backupdriverV1Client.BackupRepositoryClaims(ns).Create(context.TODO(), backupRepositoryClaimReq, metav1.CreateOptions{})
if err != nil {
return "", errors.Errorf("Failed to create backup repository claim with name %v in namespace %v", backupRepoClaimName, ns)
}
brcMap[backupRepoClaimName] = backupRepositoryClaim
}
results := make(chan waitBRCResult)
watchlist := cache.NewListWatchFromClient(backupdriverV1Client.RESTClient(),
"backuprepositoryclaims", ns, fields.Everything())
_, controller := cache.NewInformer(
watchlist,
&backupdriverv1.BackupRepositoryClaim{},
time.Second*0,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
backupRepoClaim := obj.(*backupdriverv1.BackupRepositoryClaim)
logger.Debugf("Backup Repository Claim added: %s", backupRepoClaim.Name)
checkIfBackupRepositoryClaimIsReferenced(brcMap, backupRepoClaim, results, logger)
},
UpdateFunc: func(oldObj, newObj interface{}) {
backupRepoClaimNew := newObj.(*backupdriverv1.BackupRepositoryClaim)
logger.Debugf("Backup Repository Claim: %s updated", backupRepoClaimNew.Name)
checkIfBackupRepositoryClaimIsReferenced(brcMap, backupRepoClaimNew, results, logger)
},
})
stop := make(chan struct{})
go controller.Run(stop)
// Wait here till a BR is assigned or created for the BRC.
select {
case <-ctx.Done():
stop <- struct{}{}
return "", ctx.Err()
case result := <-results:
return result.backupRepositoryName, nil
}
}
// Creates a BackupRepository with the parameters.
func CreateBackupRepository(ctx context.Context,
brc *backupdriverv1.BackupRepositoryClaim,
svcBrName string,
backupdriverV1Client *v1.BackupdriverV1alpha1Client,
logger logrus.FieldLogger) (*backupdriverv1.BackupRepository, error) {
logger.Infof("Creating BackupRepository for the BackupRepositoryClaim %s", brc.Name)
backupRepoName := GetBackupRepositoryNameForBackupRepositoryClaim(brc)
var backupRepoReq *backupdriverv1.BackupRepository
// Check if the BackupRepository already exists.
backupRepoReq, err := backupdriverV1Client.BackupRepositories().
Get(context.TODO(), backupRepoName, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
logger.Errorf("Error occurred trying to retrieve BackupRepository API object %s: %v",
backupRepoName, err)
return nil, errors.Errorf("failed to retrieve BackupRepository API object %s: %v", backupRepoName, err)
}
backupRepoReq = nil
}
// BackupRepository not found. Create a new one
if backupRepoReq == nil {
backupRepoReq = builder.ForBackupRepository(backupRepoName).
BackupRepositoryClaim(brc.Name).
AllowedNamespaces(brc.AllowedNamespaces).
RepositoryParameters(brc.RepositoryParameters).
RepositoryDriver().
SvcBackupRepositoryName(svcBrName).Result()
newBackupRepo, err := backupdriverV1Client.BackupRepositories().Create(context.TODO(), backupRepoReq, metav1.CreateOptions{})
if err != nil {
logger.Errorf("Failed to create the BackupRepository API object: %v", err)
return nil, err
}
logger.Infof("Successfully created BackupRepository API object %s for the BackupRepositoryClaim %s",
backupRepoName, brc.Name)
return newBackupRepo, nil
}
logger.Infof("Found BackupRepository %s for the BackupRepositoryClaim %s", backupRepoReq.Name, brc.Name)
return backupRepoReq, nil
}
// Construct a unique name for BackupRepository based on UID of BackupRepositoryClaim
func GetBackupRepositoryNameForBackupRepositoryClaim(brc *backupdriverv1.BackupRepositoryClaim) string {
return "br-" + string(brc.UID)
}
/*
* Only called from Guest Cluster. This will either create a new
* BackupRepositoryClaim record or update/use an existing BackupRepositoryClaim
* record in the Supervisor namespace. In either case, it does not return until
* the BackupRepository is assigned in the supervisor cluster.
*/
func ClaimSvcBackupRepository(ctx context.Context,
brc *backupdriverv1.BackupRepositoryClaim,
svcConfig *rest.Config,
svcNamespace string,
logger logrus.FieldLogger) (string, error) {
svcBackupdriverClient, err := backupdriverTypedV1.NewForConfig(svcConfig)
if err != nil {
return "", errors.WithStack(err)
}
return ClaimBackupRepository(ctx, brc.RepositoryDriver, brc.RepositoryParameters, []string{svcNamespace}, svcNamespace, svcBackupdriverClient, logger)
}
func checkIfBackupRepositoryClaimIsReferenced(
brcMap map[string]*backupdriverv1.BackupRepositoryClaim,
backupRepositoryClaim *backupdriverv1.BackupRepositoryClaim,
result chan waitBRCResult,
logger logrus.FieldLogger) {
brcName := backupRepositoryClaim.Name
brRefName := backupRepositoryClaim.BackupRepository
if brRefName != "" {
// Check if the BRC is among any of the ones we are interested in.
if _, ok := brcMap[brcName]; ok {
logger.Infof("Found matching BackupRepositoryClaim: %s with referenced BackupRepository: %s",
brcName, brRefName)
result <- waitBRCResult{
backupRepositoryName: brRefName,
err: nil,
}
}
} else {
logger.Infof("Received BackupRepositoryClaim: %s/%s with no BackupRepository reference, ignoring it now, as it"+
"is expected to be patched later", backupRepositoryClaim.Namespace, backupRepositoryClaim.Name)
}
}
// Patch the BackupRepositoryClaim with the BackupRepository name.
func PatchBackupRepositoryClaim(backupRepositoryClaim *backupdriverv1.BackupRepositoryClaim,
backRepositoryName string,
ns string,
backupdriverV1Client *v1.BackupdriverV1alpha1Client) error {
mutate := func(r *backupdriverv1.BackupRepositoryClaim) {
backupRepositoryClaim.BackupRepository = backRepositoryName
}
_, err := patchBackupRepositoryClaimInt(backupRepositoryClaim, mutate, backupdriverV1Client.BackupRepositoryClaims(ns))
if err != nil {
return errors.Errorf("Failed to patch backup repository claim %v with backup repository %v in namespace %v", backupRepositoryClaim.Name, backRepositoryName, ns)
}
return nil
}
func patchBackupRepositoryClaimInt(req *backupdriverv1.BackupRepositoryClaim,
mutate func(*backupdriverv1.BackupRepositoryClaim),
backupRepoClaimClient v1.BackupRepositoryClaimInterface) (*backupdriverv1.BackupRepositoryClaim, error) {
// Record original json
oldData, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrapf(err, "Failed to marshall original BackupRepositoryClaim")
}
// Mutate
mutate(req)
// Record new json
newData, err := json.Marshal(req)
if err != nil {
return nil, errors.Wrapf(err, "Failed to marshall updated BackupRepositoryClaim")
}
patchBytes, err := jsonpatch.CreateMergePatch(oldData, newData)
if err != nil {
return nil, errors.Wrapf(err, "Failed to creat json merge patch for BackupRepositoryClaim")
}
req, err = backupRepoClaimClient.Patch(context.TODO(), req.Name, types.MergePatchType, patchBytes, metav1.PatchOptions{})
if err != nil {
return nil, errors.Wrapf(err, "Failed to patch BackupRepositoryClaim")
}
return req, nil
}
func compareBackupRepositoryClaim(repositoryDriver string,
repositoryParameters map[string]string,
allowedNamespaces []string,
backupRepositoryClaim *backupdriverv1.BackupRepositoryClaim,
logger logrus.FieldLogger) bool {
// Compare the params. RepositoryDriver is always same.
logger.Infof("Comparing BRC: %s/%s", backupRepositoryClaim.Namespace, backupRepositoryClaim.Name)
equal := reflect.DeepEqual(repositoryParameters, backupRepositoryClaim.RepositoryParameters)
if !equal {
logger.Infof("repositoryParameters not matched")
return false
}
equal = repositoryDriver == backupRepositoryClaim.RepositoryDriver
if !equal {
logger.Infof("repositoryDriver not matched")
return false
}
equal = reflect.DeepEqual(allowedNamespaces, backupRepositoryClaim.AllowedNamespaces)
if !equal {
return false
}
return true
}
func GetRepositoryFromBackupRepository(backupRepository *backupdriverv1.BackupRepository, logger logrus.FieldLogger) (*s3repository.ProtectedEntityTypeManager, error) {
switch backupRepository.RepositoryDriver {
case constants.S3RepositoryDriver:
params := make(map[string]interface{})
for k, v := range backupRepository.RepositoryParameters {
params[k] = v
}
return utils.GetS3PETMFromParamsMap(params, logger)
default:
errMsg := fmt.Sprintf("Unsupported backuprepository driver type: %s. Only support %s.", backupRepository.RepositoryDriver, constants.S3RepositoryDriver)
return nil, errors.New(errMsg)
}
}
func GetBackupRepositoryFromBackupRepositoryName(backupRepositoryName string) (*backupdriverv1.BackupRepository, error) {
config, err := utils.GetKubeClientConfig()
if err != nil {
return nil, errors.Wrap(err, "Failed to get k8s client config")
}
pluginClient, err := versioned.NewForConfig(config)
if err != nil {
errMsg := fmt.Sprintf("Failed to get k8s clientset from the given config: %v ", config)
return nil, errors.Wrapf(err, errMsg)
}
backupRepositoryCR, err := pluginClient.BackupdriverV1alpha1().BackupRepositories().Get(context.TODO(), backupRepositoryName, metav1.GetOptions{})
if err != nil {
errMsg := fmt.Sprintf("Error while retrieving the backup repository CR %v", backupRepositoryName)
return nil, errors.Wrapf(err, errMsg)
}
return backupRepositoryCR, nil
}
func RetrieveBackupRepositoryFromBSL(ctx context.Context, bslName string, pvcNamespace string, veleroNs string,
backupdriverClient *v1.BackupdriverV1alpha1Client, restConfig *rest.Config, logger logrus.FieldLogger) (string, error) {
var backupRepositoryName string
logger.Info("Claiming backup repository")
repositoryParameters := make(map[string]string)
err := utils.RetrieveParamsFromBSL(repositoryParameters, bslName, restConfig, logger)
if err != nil {
logger.Errorf("Failed to translate BSL to repository parameters: %v", err)
return backupRepositoryName, errors.WithStack(err)
}
backupRepositoryName, err = ClaimBackupRepository(ctx, constants.S3RepositoryDriver, repositoryParameters,
[]string{pvcNamespace}, veleroNs, backupdriverClient, logger)
if err != nil {
logger.Errorf("Failed to claim backup repository: %v", err)
return backupRepositoryName, errors.WithStack(err)
}
return backupRepositoryName, nil
}