/
snapshot.go
421 lines (362 loc) · 13.3 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
Copyright 2018 The OpenEBS Authors
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.
*/
package snapshot
import (
"strings"
yaml "github.com/ghodss/yaml"
"github.com/openebs/maya/pkg/apis/openebs.io/v1alpha1"
cast "github.com/openebs/maya/pkg/castemplate/v1alpha1"
m_k8s_client "github.com/openebs/maya/pkg/client/k8s"
menv "github.com/openebs/maya/pkg/env/v1alpha1"
"github.com/pkg/errors"
v1_storage "k8s.io/api/storage/v1"
mach_apis_meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
)
// options contains the options with respect to
// snapshot related operations
type options struct {
// k8sClient will make K8s API calls
k8sClient *m_k8s_client.K8sClient
snapOptions *v1alpha1.SnapshotOptions
}
// snapshot exposes methods with respect to snapshot related operations
// e.g. read, create, delete.
type snapshot struct {
// options has the options to various snapshot related
// operations
options
}
// Snapshot returns a new instance of snapshot
func Snapshot(opts *v1alpha1.SnapshotOptions) (*snapshot, error) {
if len(opts.Namespace) == 0 {
return nil, errors.Errorf("failed to instantiate snapshot operation: missing run namespace")
}
kc, err := m_k8s_client.NewK8sClient(opts.Namespace)
if err != nil {
return nil, err
}
return &snapshot{
options: options{
k8sClient: kc,
snapOptions: opts,
},
}, nil
}
// Create creates an OpenEBS snapshot of a volume
func (s *snapshot) Create() (*v1alpha1.CASSnapshot, error) {
if s.k8sClient == nil {
return nil, errors.Errorf("unable to create snapshot: nil k8s client")
}
// fetch the pv specifications
pv, err := s.k8sClient.GetPV(s.snapOptions.VolumeName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
storageEngine := pv.Labels[string(v1alpha1.CASTypeKey)]
scName := pv.Labels[string(v1alpha1.StorageClassKey)]
if len(scName) == 0 {
scName = pv.Spec.StorageClassName
}
if len(scName) == 0 {
return nil, errors.Errorf("unable to create snapshot %s: missing storage class in PV %s", s.snapOptions.Name, s.snapOptions.VolumeName)
}
// fetch the storage class specifications
sc, err := s.k8sClient.GetStorageV1SC(scName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
castName := getCreateCASTemplate(storageEngine, sc)
if len(castName) == 0 {
return nil, errors.Errorf("unable to create snapshot %s: missing cas template for create snapshot", s.snapOptions.Name)
}
// fetch read cas template specifications
cast, err := s.k8sClient.GetOEV1alpha1CAST(castName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
snapshotLabels := map[string]interface{}{
string(v1alpha1.OwnerVTP): s.snapOptions.Name,
string(v1alpha1.VolumeSTP): s.snapOptions.VolumeName,
string(v1alpha1.RunNamespaceVTP): s.snapOptions.Namespace,
}
// extract the cas volume config from storage class
casConfigSC := sc.Annotations[string(v1alpha1.CASConfigKey)]
// provision CAS snapshot via CAS snapshot specific CAS template engine
engine, err := SnapshotEngine("", casConfigSC, cast, string(v1alpha1.SnapshotTLP), snapshotLabels)
if err != nil {
return nil, err
}
// create the snapshot
data, err := engine.Run()
if err != nil {
return nil, err
}
// unmarshall into openebs snapshot
snap := &v1alpha1.CASSnapshot{}
err = yaml.Unmarshal(data, snap)
if err != nil {
return nil, err
}
return snap, nil
}
// Get the openebs snapshot details
func (s *snapshot) Read() (*v1alpha1.CASSnapshot, error) {
if s.k8sClient == nil {
return nil, errors.Errorf("unable to read snapshot: nil k8s client")
}
// fetch the pv specifications
pv, err := s.k8sClient.GetPV(s.snapOptions.VolumeName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
storageEngine := pv.Labels[string(v1alpha1.CASTypeKey)]
scName := pv.Labels[string(v1alpha1.StorageClassKey)]
if len(scName) == 0 {
scName = pv.Spec.StorageClassName
}
if len(scName) == 0 {
return nil, errors.Errorf("unable to read snapshot %s: missing storage class in PV %s", s.snapOptions.Name, s.snapOptions.VolumeName)
}
// fetch the storage class specifications
sc, err := s.k8sClient.GetStorageV1SC(scName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
castName := getReadCASTemplate(storageEngine, sc)
if len(castName) == 0 {
return nil, errors.Errorf("unable to read snapshot %s: missing cas template for read snapshot", s.snapOptions.Name)
}
// fetch read cas template specifications
castObj, err := s.k8sClient.GetOEV1alpha1CAST(castName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
snapshotLabels := map[string]interface{}{
string(v1alpha1.OwnerVTP): s.snapOptions.Name,
string(v1alpha1.RunNamespaceVTP): s.snapOptions.Namespace,
string(v1alpha1.VolumeSTP): s.snapOptions.VolumeName,
}
// read cas volume via cas template engine
engine, err := cast.Engine(castObj, string(v1alpha1.SnapshotTLP), snapshotLabels)
if err != nil {
return nil, err
}
// read cas snapshot by executing engine
data, err := engine.Run()
if err != nil {
return nil, err
}
// unmarshall into openebs snapshot
snap := &v1alpha1.CASSnapshot{}
err = yaml.Unmarshal(data, snap)
if err != nil {
return nil, err
}
return snap, nil
}
// Get the openebs snapshot details
func (s *snapshot) Delete() (*v1alpha1.CASSnapshot, error) {
if s.k8sClient == nil {
return nil, errors.Errorf("unable to delete snapshot: nil k8s client")
}
// fetch the pv specifications
pv, err := s.k8sClient.GetPV(s.snapOptions.VolumeName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
snap := &v1alpha1.CASSnapshot{}
// Return success for delete snapshot request in case volume snapshot belongs to a
// migrated CSI based persistent volume. Later as part of migration snapshot
// can be migrated to CSI snapshots using a migration or manual steps
if pv.Spec.CSI != nil {
if string(pv.Spec.CSI.Driver) == string(v1alpha1.CSIDriverName) {
klog.Infof("skip backend snapshot delete request of migrated CSI volume: %q", s.snapOptions.VolumeName)
return snap, nil
}
}
storageEngine := pv.Labels[string(v1alpha1.CASTypeKey)]
scName := pv.Labels[string(v1alpha1.StorageClassKey)]
if len(scName) == 0 {
scName = pv.Spec.StorageClassName
}
if len(scName) == 0 {
return nil, errors.Errorf("unable to delete snapshot %s: missing storage class in PV %s", s.snapOptions.Name, s.snapOptions.VolumeName)
}
// fetch the storage class specifications
sc, err := s.k8sClient.GetStorageV1SC(scName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
castName := getDeleteCASTemplate(storageEngine, sc)
if len(castName) == 0 {
return nil, errors.Errorf("unable to delete snapshot %s: missing cas template for delete snapshot", s.snapOptions.Name)
}
// fetch read cas template specifications
cast, err := s.k8sClient.GetOEV1alpha1CAST(castName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
snapshotLabels := map[string]interface{}{
string(v1alpha1.OwnerVTP): s.snapOptions.Name,
string(v1alpha1.RunNamespaceVTP): s.snapOptions.Namespace,
string(v1alpha1.VolumeSTP): s.snapOptions.VolumeName,
}
// extract the cas volume config from storage class
casConfigSC := sc.Annotations[string(v1alpha1.CASConfigKey)]
// provision CAS snapshot via CAS snapshot specific CAS template engine
engine, err := SnapshotEngine("", casConfigSC, cast, string(v1alpha1.SnapshotTLP), snapshotLabels)
if err != nil {
return nil, err
}
// delete cas snapshot by executing engine
data, err := engine.Run()
if err != nil {
return nil, err
}
// unmarshall into openebs snapshot
err = yaml.Unmarshal(data, snap)
if err != nil {
return nil, err
}
return snap, nil
}
func (s *snapshot) List() (*v1alpha1.CASSnapshotList, error) {
if s.k8sClient == nil {
return nil, errors.Errorf("unable to list snapshot: nil k8s client")
}
// fetch the pv specifications
pv, err := s.k8sClient.GetPV(s.snapOptions.VolumeName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
storageEngine := pv.Labels[string(v1alpha1.CASTypeKey)]
scName := pv.Labels[string(v1alpha1.StorageClassKey)]
if len(scName) == 0 {
scName = pv.Spec.StorageClassName
}
if len(scName) == 0 {
return nil, errors.Errorf("unable to list snapshot: missing storage class in PV %s", s.snapOptions.VolumeName)
}
// fetch the storage class specifications
sc, err := s.k8sClient.GetStorageV1SC(scName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
castName := getListCASTemplate(storageEngine, sc)
if len(castName) == 0 {
return nil, errors.Errorf("unable to list snapshots: missing cas template for list snapshot")
}
// fetch read cas template specifications
castObj, err := s.k8sClient.GetOEV1alpha1CAST(castName, mach_apis_meta_v1.GetOptions{})
if err != nil {
return nil, err
}
snapshotLabels := map[string]interface{}{
string(v1alpha1.RunNamespaceVTP): s.snapOptions.Namespace,
string(v1alpha1.VolumeSTP): s.snapOptions.VolumeName,
}
// list cas volume via cas template engine
engine, err := cast.Engine(castObj, string(v1alpha1.SnapshotTLP), snapshotLabels)
if err != nil {
return nil, err
}
// list cas snapshots by executing the engine
data, err := engine.Run()
if err != nil {
return nil, err
}
// unmarshall into openebs snapshot
snapList := &v1alpha1.CASSnapshotList{}
err = yaml.Unmarshal(data, snapList)
if err != nil {
return nil, err
}
return snapList, nil
}
func getReadCASTemplate(defaultCasType string, sc *v1_storage.StorageClass) string {
castName := sc.Annotations[string(v1alpha1.CASTemplateKeyForSnapshotRead)]
// if cas template for the given operation is empty then fetch from environment variables
if len(castName) == 0 {
casType := strings.ToLower(sc.Annotations[string(v1alpha1.CASTypeKey)])
// if casType is missing in sc annotation then use the default cas type
if casType == "" {
casType = strings.ToLower(defaultCasType)
}
// check for casType, if cstor, set read cas template to cstor,
// if jiva or absent then default to jiva
if casType == string(v1alpha1.CstorVolume) {
castName = menv.Get(menv.CASTemplateToReadCStorSnapshotENVK)
} else if casType == string(v1alpha1.JivaVolume) || casType == "" {
castName = menv.Get(menv.CASTemplateToReadJivaSnapshotENVK)
}
}
return castName
}
func getCreateCASTemplate(defaultCasType string, sc *v1_storage.StorageClass) string {
castName := sc.Annotations[string(v1alpha1.CASTemplateKeyForSnapshotCreate)]
// if cas template for the given operation is empty then fetch from environment variables
if len(castName) == 0 {
casType := strings.ToLower(sc.Annotations[string(v1alpha1.CASTypeKey)])
// if casType is missing in sc annotation then use the default cas type
if casType == "" {
casType = strings.ToLower(defaultCasType)
}
// check for casType, if cstor, set create cas template to cstor,
// if jiva or absent then default to jiva
if casType == string(v1alpha1.CstorVolume) {
castName = menv.Get(menv.CASTemplateToCreateCStorSnapshotENVK)
} else if casType == string(v1alpha1.JivaVolume) || casType == "" {
castName = menv.Get(menv.CASTemplateToCreateJivaSnapshotENVK)
}
}
return castName
}
func getDeleteCASTemplate(defaultCasType string, sc *v1_storage.StorageClass) string {
castName := sc.Annotations[string(v1alpha1.CASTemplateKeyForSnapshotDelete)]
// if cas template for the given operation is empty then fetch from environment variables
if len(castName) == 0 {
casType := strings.ToLower(sc.Annotations[string(v1alpha1.CASTypeKey)])
// if casType is missing in sc annotation then use the default cas type
if casType == "" {
casType = strings.ToLower(defaultCasType)
}
// check for casType, if cstor, set delete cas template to cstor,
// if jiva or absent then default to jiva
if casType == string(v1alpha1.CstorVolume) {
castName = menv.Get(menv.CASTemplateToDeleteCStorSnapshotENVK)
} else if casType == string(v1alpha1.JivaVolume) || casType == "" {
castName = menv.Get(menv.CASTemplateToDeleteJivaSnapshotENVK)
}
}
return castName
}
func getListCASTemplate(defaultCasType string, sc *v1_storage.StorageClass) string {
castName := sc.Annotations[string(v1alpha1.CASTemplateKeyForSnapshotList)]
// if cas template for the given operation is empty then fetch from environment variables
if len(castName) == 0 {
casType := strings.ToLower(sc.Annotations[string(v1alpha1.CASTypeKey)])
// if casType is missing in sc annotation then use the default cas type
if casType == "" {
casType = strings.ToLower(defaultCasType)
}
// check for casType, if cstor, set list cas template to cstor,
// if jiva or absent then default to jiva
if casType == string(v1alpha1.CstorVolume) {
castName = menv.Get(menv.CASTemplateToListCStorSnapshotENVK)
} else if casType == string(v1alpha1.JivaVolume) || casType == "" {
castName = menv.Get(menv.CASTemplateToListJivaSnapshotENVK)
}
}
return castName
}