-
Notifications
You must be signed in to change notification settings - Fork 39.3k
/
snapshot_resource.go
337 lines (287 loc) · 14.2 KB
/
snapshot_resource.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
/*
Copyright 2020 The Kubernetes 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 framework
import (
"context"
"fmt"
"github.com/onsi/ginkgo"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/kubernetes/test/e2e/framework"
"k8s.io/kubernetes/test/e2e/storage/utils"
)
// SnapshotResource represents a snapshot class, a snapshot and its bound snapshot contents for a specific test case
type SnapshotResource struct {
Config *PerTestConfig
Pattern TestPattern
Vs *unstructured.Unstructured
Vscontent *unstructured.Unstructured
Vsclass *unstructured.Unstructured
}
// CreateSnapshot creates a VolumeSnapshotClass with given SnapshotDeletionPolicy and a VolumeSnapshot
// from the VolumeSnapshotClass using a dynamic client.
// Returns the unstructured VolumeSnapshotClass and VolumeSnapshot objects.
func CreateSnapshot(sDriver SnapshottableTestDriver, config *PerTestConfig, pattern TestPattern, pvcName string, pvcNamespace string, timeouts *framework.TimeoutContext, parameters map[string]string) (*unstructured.Unstructured, *unstructured.Unstructured) {
defer ginkgo.GinkgoRecover()
var err error
if pattern.SnapshotType != DynamicCreatedSnapshot && pattern.SnapshotType != PreprovisionedCreatedSnapshot {
err = fmt.Errorf("SnapshotType must be set to either DynamicCreatedSnapshot or PreprovisionedCreatedSnapshot")
framework.ExpectNoError(err)
}
dc := config.Framework.DynamicClient
ginkgo.By("creating a SnapshotClass")
sclass := sDriver.GetSnapshotClass(config, parameters)
if sclass == nil {
framework.Failf("Failed to get snapshot class based on test config")
}
sclass.Object["deletionPolicy"] = pattern.SnapshotDeletionPolicy.String()
sclass, err = dc.Resource(utils.SnapshotClassGVR).Create(context.TODO(), sclass, metav1.CreateOptions{})
framework.ExpectNoError(err)
sclass, err = dc.Resource(utils.SnapshotClassGVR).Get(context.TODO(), sclass.GetName(), metav1.GetOptions{})
framework.ExpectNoError(err)
ginkgo.By("creating a dynamic VolumeSnapshot")
// prepare a dynamically provisioned volume snapshot with certain data
snapshot := getSnapshot(pvcName, pvcNamespace, sclass.GetName())
snapshot, err = dc.Resource(utils.SnapshotGVR).Namespace(snapshot.GetNamespace()).Create(context.TODO(), snapshot, metav1.CreateOptions{})
framework.ExpectNoError(err)
return sclass, snapshot
}
// CreateSnapshotResource creates a snapshot resource for the current test. It knows how to deal with
// different test pattern snapshot provisioning and deletion policy
func CreateSnapshotResource(sDriver SnapshottableTestDriver, config *PerTestConfig, pattern TestPattern, pvcName string, pvcNamespace string, timeouts *framework.TimeoutContext, parameters map[string]string) *SnapshotResource {
var err error
r := SnapshotResource{
Config: config,
Pattern: pattern,
}
r.Vsclass, r.Vs = CreateSnapshot(sDriver, config, pattern, pvcName, pvcNamespace, timeouts, parameters)
dc := r.Config.Framework.DynamicClient
r.Vscontent = utils.GetSnapshotContentFromSnapshot(dc, r.Vs)
if pattern.SnapshotType == PreprovisionedCreatedSnapshot {
// prepare a pre-provisioned VolumeSnapshotContent with certain data
// Because this could be run with an external CSI driver, we have no way
// to pre-provision the snapshot as we normally would using their API.
// We instead dynamically take a snapshot (above step), delete the old snapshot,
// and create another snapshot using the first snapshot's snapshot handle.
ginkgo.By("updating the snapshot content deletion policy to retain")
r.Vscontent.Object["spec"].(map[string]interface{})["deletionPolicy"] = "Retain"
r.Vscontent, err = dc.Resource(utils.SnapshotContentGVR).Update(context.TODO(), r.Vscontent, metav1.UpdateOptions{})
framework.ExpectNoError(err)
ginkgo.By("recording properties of the preprovisioned snapshot")
snapshotHandle := r.Vscontent.Object["status"].(map[string]interface{})["snapshotHandle"].(string)
framework.Logf("Recording snapshot content handle: %s", snapshotHandle)
snapshotContentAnnotations := r.Vscontent.GetAnnotations()
framework.Logf("Recording snapshot content annotations: %v", snapshotContentAnnotations)
csiDriverName := r.Vsclass.Object["driver"].(string)
framework.Logf("Recording snapshot driver: %s", csiDriverName)
// If the deletion policy is retain on vscontent:
// when vs is deleted vscontent will not be deleted
// when the vscontent is manually deleted then the underlying snapshot resource will not be deleted.
// We exploit this to create a snapshot resource from which we can create a preprovisioned snapshot
ginkgo.By("deleting the snapshot and snapshot content")
err = dc.Resource(utils.SnapshotGVR).Namespace(r.Vs.GetNamespace()).Delete(context.TODO(), r.Vs.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
framework.ExpectNoError(err)
ginkgo.By("checking the Snapshot has been deleted")
err = utils.WaitForNamespacedGVRDeletion(dc, utils.SnapshotGVR, r.Vs.GetName(), r.Vs.GetNamespace(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
err = dc.Resource(utils.SnapshotContentGVR).Delete(context.TODO(), r.Vscontent.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
framework.ExpectNoError(err)
ginkgo.By("checking the Snapshot content has been deleted")
err = utils.WaitForGVRDeletion(dc, utils.SnapshotContentGVR, r.Vscontent.GetName(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
ginkgo.By("creating a snapshot content with the snapshot handle")
uuid := uuid.NewUUID()
snapName := getPreProvisionedSnapshotName(uuid)
snapcontentName := getPreProvisionedSnapshotContentName(uuid)
r.Vscontent = getPreProvisionedSnapshotContent(snapcontentName, snapshotContentAnnotations, snapName, pvcNamespace, snapshotHandle, pattern.SnapshotDeletionPolicy.String(), csiDriverName)
r.Vscontent, err = dc.Resource(utils.SnapshotContentGVR).Create(context.TODO(), r.Vscontent, metav1.CreateOptions{})
framework.ExpectNoError(err)
ginkgo.By("creating a snapshot with that snapshot content")
r.Vs = getPreProvisionedSnapshot(snapName, pvcNamespace, snapcontentName)
r.Vs, err = dc.Resource(utils.SnapshotGVR).Namespace(r.Vs.GetNamespace()).Create(context.TODO(), r.Vs, metav1.CreateOptions{})
framework.ExpectNoError(err)
err = utils.WaitForSnapshotReady(dc, r.Vs.GetNamespace(), r.Vs.GetName(), framework.Poll, timeouts.SnapshotCreate)
framework.ExpectNoError(err)
ginkgo.By("getting the snapshot and snapshot content")
r.Vs, err = dc.Resource(utils.SnapshotGVR).Namespace(r.Vs.GetNamespace()).Get(context.TODO(), r.Vs.GetName(), metav1.GetOptions{})
framework.ExpectNoError(err)
r.Vscontent, err = dc.Resource(utils.SnapshotContentGVR).Get(context.TODO(), r.Vscontent.GetName(), metav1.GetOptions{})
framework.ExpectNoError(err)
}
return &r
}
// CleanupResource cleans up the snapshot resource and ignores not found errors
func (sr *SnapshotResource) CleanupResource(timeouts *framework.TimeoutContext) error {
var err error
var cleanupErrs []error
dc := sr.Config.Framework.DynamicClient
if sr.Vs != nil {
framework.Logf("deleting snapshot %q/%q", sr.Vs.GetNamespace(), sr.Vs.GetName())
sr.Vs, err = dc.Resource(utils.SnapshotGVR).Namespace(sr.Vs.GetNamespace()).Get(context.TODO(), sr.Vs.GetName(), metav1.GetOptions{})
switch {
case err == nil:
snapshotStatus := sr.Vs.Object["status"].(map[string]interface{})
snapshotContentName := snapshotStatus["boundVolumeSnapshotContentName"].(string)
framework.Logf("received snapshotStatus %v", snapshotStatus)
framework.Logf("snapshotContentName %s", snapshotContentName)
boundVsContent, err := dc.Resource(utils.SnapshotContentGVR).Get(context.TODO(), snapshotContentName, metav1.GetOptions{})
switch {
case err == nil:
if boundVsContent.Object["spec"].(map[string]interface{})["deletionPolicy"] != "Delete" {
// The purpose of this block is to prevent physical snapshotContent leaks.
// We must update the SnapshotContent to have Delete Deletion policy,
// or else the physical snapshot content will be leaked.
boundVsContent.Object["spec"].(map[string]interface{})["deletionPolicy"] = "Delete"
boundVsContent, err = dc.Resource(utils.SnapshotContentGVR).Update(context.TODO(), boundVsContent, metav1.UpdateOptions{})
framework.ExpectNoError(err)
}
err = dc.Resource(utils.SnapshotGVR).Namespace(sr.Vs.GetNamespace()).Delete(context.TODO(), sr.Vs.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
framework.ExpectNoError(err)
err = utils.WaitForGVRDeletion(dc, utils.SnapshotContentGVR, boundVsContent.GetName(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
case apierrors.IsNotFound(err):
// the volume snapshot is not bound to snapshot content yet
err = dc.Resource(utils.SnapshotGVR).Namespace(sr.Vs.GetNamespace()).Delete(context.TODO(), sr.Vs.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
framework.ExpectNoError(err)
err = utils.WaitForNamespacedGVRDeletion(dc, utils.SnapshotGVR, sr.Vs.GetName(), sr.Vs.GetNamespace(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
default:
cleanupErrs = append(cleanupErrs, err)
}
case apierrors.IsNotFound(err):
// Hope that the underlying snapshot content and resource is gone already
default:
cleanupErrs = append(cleanupErrs, err)
}
}
if sr.Vscontent != nil {
framework.Logf("deleting snapshot content %q", sr.Vscontent.GetName())
sr.Vscontent, err = dc.Resource(utils.SnapshotContentGVR).Get(context.TODO(), sr.Vscontent.GetName(), metav1.GetOptions{})
switch {
case err == nil:
if sr.Vscontent.Object["spec"].(map[string]interface{})["deletionPolicy"] != "Delete" {
// The purpose of this block is to prevent physical snapshotContent leaks.
// We must update the SnapshotContent to have Delete Deletion policy,
// or else the physical snapshot content will be leaked.
sr.Vscontent.Object["spec"].(map[string]interface{})["deletionPolicy"] = "Delete"
sr.Vscontent, err = dc.Resource(utils.SnapshotContentGVR).Update(context.TODO(), sr.Vscontent, metav1.UpdateOptions{})
framework.ExpectNoError(err)
}
err = dc.Resource(utils.SnapshotContentGVR).Delete(context.TODO(), sr.Vscontent.GetName(), metav1.DeleteOptions{})
if apierrors.IsNotFound(err) {
err = nil
}
framework.ExpectNoError(err)
err = utils.WaitForGVRDeletion(dc, utils.SnapshotContentGVR, sr.Vscontent.GetName(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
case apierrors.IsNotFound(err):
// Hope the underlying physical snapshot resource has been deleted already
default:
cleanupErrs = append(cleanupErrs, err)
}
}
if sr.Vsclass != nil {
framework.Logf("deleting snapshot class %q", sr.Vsclass.GetName())
// typically this snapshot class has already been deleted
err = dc.Resource(utils.SnapshotClassGVR).Delete(context.TODO(), sr.Vsclass.GetName(), metav1.DeleteOptions{})
if err != nil && !apierrors.IsNotFound(err) {
framework.Failf("Error deleting snapshot class %q. Error: %v", sr.Vsclass.GetName(), err)
}
err = utils.WaitForGVRDeletion(dc, utils.SnapshotClassGVR, sr.Vsclass.GetName(), framework.Poll, timeouts.SnapshotDelete)
framework.ExpectNoError(err)
}
return utilerrors.NewAggregate(cleanupErrs)
}
func getSnapshot(claimName string, ns, snapshotClassName string) *unstructured.Unstructured {
snapshot := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "VolumeSnapshot",
"apiVersion": utils.SnapshotAPIVersion,
"metadata": map[string]interface{}{
"generateName": "snapshot-",
"namespace": ns,
},
"spec": map[string]interface{}{
"volumeSnapshotClassName": snapshotClassName,
"source": map[string]interface{}{
"persistentVolumeClaimName": claimName,
},
},
},
}
return snapshot
}
func getPreProvisionedSnapshot(snapName, ns, snapshotContentName string) *unstructured.Unstructured {
snapshot := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "VolumeSnapshot",
"apiVersion": utils.SnapshotAPIVersion,
"metadata": map[string]interface{}{
"name": snapName,
"namespace": ns,
},
"spec": map[string]interface{}{
"source": map[string]interface{}{
"volumeSnapshotContentName": snapshotContentName,
},
},
},
}
return snapshot
}
func getPreProvisionedSnapshotContent(snapcontentName string, snapshotContentAnnotations map[string]string, snapshotName, snapshotNamespace, snapshotHandle, deletionPolicy, csiDriverName string) *unstructured.Unstructured {
snapshotContent := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": "VolumeSnapshotContent",
"apiVersion": utils.SnapshotAPIVersion,
"metadata": map[string]interface{}{
"name": snapcontentName,
"annotations": snapshotContentAnnotations,
},
"spec": map[string]interface{}{
"source": map[string]interface{}{
"snapshotHandle": snapshotHandle,
},
"volumeSnapshotRef": map[string]interface{}{
"name": snapshotName,
"namespace": snapshotNamespace,
},
"driver": csiDriverName,
"deletionPolicy": deletionPolicy,
},
},
}
return snapshotContent
}
func getPreProvisionedSnapshotContentName(uuid types.UID) string {
return fmt.Sprintf("pre-provisioned-snapcontent-%s", string(uuid))
}
func getPreProvisionedSnapshotName(uuid types.UID) string {
return fmt.Sprintf("pre-provisioned-snapshot-%s", string(uuid))
}