forked from kubernetes-retired/external-storage
/
openstack_snapshots.go
261 lines (221 loc) · 7.91 KB
/
openstack_snapshots.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
/*
Copyright 2016 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 openstack
import (
"fmt"
"github.com/gophercloud/gophercloud"
snapshotsV2 "github.com/gophercloud/gophercloud/openstack/blockstorage/v2/snapshots"
"github.com/golang/glog"
ctrlsnap "github.com/kubernetes-incubator/external-storage/snapshot/pkg/controller/snapshotter"
)
// SnapshotsV2 is the Cinder V2 Snapshot service from gophercloud
type SnapshotsV2 struct {
blockstorage *gophercloud.ServiceClient
opts BlockStorageOpts
}
// Snapshot is the representation of the Cinder Snapshot object
type Snapshot struct {
ID string
Name string
Status string
SourceVolumeID string
Description string
Metadata map[string]string
}
// SnapshotCreateOpts are the valid create options for Cinder Snapshots
type SnapshotCreateOpts struct {
VolumeID string
Name string
Description string
Force bool
Metadata map[string]string
}
// SnapshotListOpts are the valid list options for Cinder Snapshots
type SnapshotListOpts struct {
Name string
Status string
VolumeID string
}
type snapshotService interface {
createSnapshot(opts SnapshotCreateOpts) (string, string, error)
deleteSnapshot(snapshotName string) error
getSnapshot(snapshotID string) (Snapshot, error)
listSnapshots(opts SnapshotListOpts) ([]Snapshot, error)
}
func (snapshots *SnapshotsV2) createSnapshot(opts SnapshotCreateOpts) (string, string, error) {
createOpts := snapshotsV2.CreateOpts{
VolumeID: opts.VolumeID,
Force: false,
Name: opts.Name,
Description: opts.Description,
Metadata: opts.Metadata,
}
snap, err := snapshotsV2.Create(snapshots.blockstorage, createOpts).Extract()
if err != nil {
return "", "", err
}
return snap.ID, snap.Status, nil
}
func (snapshots *SnapshotsV2) getSnapshot(snapshotID string) (Snapshot, error) {
var snap Snapshot
glog.Infof("getSnapshot for snapshotID: %s.", snapshotID)
snapshot, err := snapshotsV2.Get(snapshots.blockstorage, snapshotID).Extract()
if err != nil {
return snap, err
}
glog.Infof("Snapshot details: %#v", snapshot)
snap.ID = snapshot.ID
snap.Name = snapshot.Name
snap.Status = snapshot.Status
snap.SourceVolumeID = snapshot.VolumeID
return snap, nil
}
func (snapshots *SnapshotsV2) deleteSnapshot(snapshotID string) error {
err := snapshotsV2.Delete(snapshots.blockstorage, snapshotID).ExtractErr()
if err != nil {
glog.Errorf("Cannot delete snapshot %s: %v", snapshotID, err)
}
return err
}
func (snapshots *SnapshotsV2) listSnapshots(opts SnapshotListOpts) ([]Snapshot, error) {
var snaplist []Snapshot
listOpts := snapshotsV2.ListOpts{
Name: opts.Name,
Status: opts.Status,
VolumeID: opts.VolumeID,
}
snapPages, err := snapshotsV2.List(snapshots.blockstorage, listOpts).AllPages()
if err != nil {
return snaplist, err
}
allSnaps, err := snapshotsV2.ExtractSnapshots(snapPages)
if err != nil {
return snaplist, err
}
for _, snapshot := range allSnaps {
glog.Infof("Snapshot details: %#v", snapshot)
var snap Snapshot
snap.ID = snapshot.ID
snap.Name = snapshot.Name
snap.Status = snapshot.Status
snap.SourceVolumeID = snapshot.VolumeID
snap.Description = snapshot.Description
snap.Metadata = snapshot.Metadata
snaplist = append(snaplist, snap)
}
return snaplist, nil
}
// CreateSnapshot from the specified volume
func (os *OpenStack) CreateSnapshot(sourceVolumeID, name, description string, tags map[string]string) (string, string, error) {
snapshots, err := os.snapshotService()
if err != nil || snapshots == nil {
glog.Errorf("Unable to initialize cinder client for region: %s", os.region)
return "", "", fmt.Errorf("Failed to create snapshot for volume %s: %v", sourceVolumeID, err)
}
opts := SnapshotCreateOpts{
VolumeID: sourceVolumeID,
Name: name,
Description: description,
}
if tags != nil {
opts.Metadata = tags
}
snapshotID, status, err := snapshots.createSnapshot(opts)
if err != nil {
glog.Errorf("Failed to snapshot volume %s : %v", sourceVolumeID, err)
return "", "", err
}
glog.Infof("Created snapshot %v from volume: %v", snapshotID, sourceVolumeID)
return snapshotID, status, nil
}
// DeleteSnapshot deletes the specified snapshot
func (os *OpenStack) DeleteSnapshot(snapshotID string) error {
snapshots, err := os.snapshotService()
if err != nil || snapshots == nil {
glog.Errorf("Unable to initialize cinder client for region: %s", os.region)
return err
}
err = snapshots.deleteSnapshot(snapshotID)
if err != nil {
glog.Errorf("Cannot delete snapshot %s: %v", snapshotID, err)
}
return nil
}
// DescribeSnapshot returns the status of the snapshot
// FIXME(j-griffith): Name doesn't fit at all here, this is actually more like is `IsAvailable`
func (os *OpenStack) DescribeSnapshot(snapshotID string) (status string, isCompleted bool, err error) {
ss, err := os.snapshotService()
if err != nil || ss == nil {
glog.Errorf("Unable to initialize cinder client for region: %s", os.region)
return "", false, fmt.Errorf("Failed to describe snapshot %s: %v", snapshotID, err)
}
snap, err := ss.getSnapshot(snapshotID)
if err != nil {
glog.Errorf("error requesting snapshot %s: %v", snapshotID, err)
}
if err != nil {
return "", false, err
}
if snap.Status != "available" {
return snap.Status, false, nil
}
return snap.Status, true, nil
}
// FindSnapshot finds snapshot by metadata
func (os *OpenStack) FindSnapshot(tags map[string]string) ([]string, []string, error) {
var snapshotIDs, statuses []string
ss, err := os.snapshotService()
if err != nil || ss == nil {
glog.Errorf("Unable to initialize cinder client for region: %s", os.region)
return snapshotIDs, statuses, fmt.Errorf("Failed to find snapshot by tags %v: %v", tags, err)
}
opts := SnapshotListOpts{}
snapshots, err := ss.listSnapshots(opts)
if err != nil {
glog.Errorf("Failed to list snapshots. Error: %v", err)
return snapshotIDs, statuses, err
}
glog.Infof("Listed [%v] snapshots.", len(snapshots))
glog.Infof("Looking for matching tags [%#v] in snapshots.", tags)
// Loop around to find the snapshot with the matching input metadata
// NOTE(xyang): Metadata based filtering for snapshots is supported by Cinder volume API
// microversion 3.21 and above. Currently the OpenStack Cloud Provider only supports V2.0.
// Revisit this later when V3.0 is supported.
for _, snapshot := range snapshots {
glog.Infof("Looking for matching tags in snapshot [%#v].", snapshot)
namespaceVal, ok := snapshot.Metadata[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotNamespaceTag]
if ok {
if tags[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotNamespaceTag] == namespaceVal {
nameVal, ok := snapshot.Metadata[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotNameTag]
if ok {
if tags[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotNameTag] == nameVal {
uidVal, ok := snapshot.Metadata[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotUIDTag]
if ok {
if tags[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotUIDTag] == uidVal {
timeVal, ok := snapshot.Metadata[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotTimestampTag]
if ok {
if tags[ctrlsnap.CloudSnapshotCreatedForVolumeSnapshotTimestampTag] == timeVal {
snapshotIDs = append(snapshotIDs, snapshot.ID)
statuses = append(statuses, snapshot.Status)
glog.Infof("Add snapshot [%#v].", snapshot)
}
}
}
}
}
}
}
}
}
return snapshotIDs, statuses, nil
}