/
disks.go
417 lines (371 loc) · 11.6 KB
/
disks.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package gce
import (
"fmt"
"strings"
"sync"
"github.com/juju/errors"
"github.com/juju/utils"
"github.com/juju/utils/set"
"github.com/juju/juju/provider/gce/google"
"github.com/juju/juju/storage"
)
const (
storageProviderType = storage.ProviderType("gce")
)
// StorageProviderTypes implements storage.ProviderRegistry.
func (env *environ) StorageProviderTypes() ([]storage.ProviderType, error) {
return []storage.ProviderType{storageProviderType}, nil
}
// StorageProvider implements storage.ProviderRegistry.
func (env *environ) StorageProvider(t storage.ProviderType) (storage.Provider, error) {
if t == storageProviderType {
return &storageProvider{env}, nil
}
return nil, errors.NotFoundf("storage provider %q", t)
}
type storageProvider struct {
env *environ
}
var _ storage.Provider = (*storageProvider)(nil)
func (g *storageProvider) ValidateConfig(cfg *storage.Config) error {
return nil
}
func (g *storageProvider) Supports(k storage.StorageKind) bool {
return k == storage.StorageKindBlock
}
func (g *storageProvider) Scope() storage.Scope {
return storage.ScopeEnviron
}
func (g *storageProvider) Dynamic() bool {
return true
}
func (g *storageProvider) DefaultPools() []*storage.Config {
// TODO(perrito666) Add explicit pools.
return nil
}
func (g *storageProvider) FilesystemSource(providerConfig *storage.Config) (storage.FilesystemSource, error) {
return nil, errors.NotSupportedf("filesystems")
}
type volumeSource struct {
gce gceConnection
envName string // non-unique, informational only
modelUUID string
}
func (g *storageProvider) VolumeSource(cfg *storage.Config) (storage.VolumeSource, error) {
environConfig := g.env.Config()
source := &volumeSource{
gce: g.env.gce,
envName: environConfig.Name(),
modelUUID: environConfig.UUID(),
}
return source, nil
}
type instanceCache map[string]google.Instance
func (c instanceCache) update(gceClient gceConnection, ids ...string) error {
if len(ids) == 1 {
if _, ok := c[ids[0]]; ok {
return nil
}
}
idMap := make(map[string]int, len(ids))
for _, id := range ids {
idMap[id] = 0
}
instances, err := gceClient.Instances("", google.StatusRunning)
if err != nil {
return errors.Annotate(err, "querying instance details")
}
for _, instance := range instances {
if _, ok := idMap[instance.ID]; !ok {
continue
}
c[instance.ID] = instance
}
return nil
}
func (c instanceCache) get(id string) (google.Instance, error) {
inst, ok := c[id]
if !ok {
return google.Instance{}, errors.Errorf("cannot attach to non-running instance %v", id)
}
return inst, nil
}
func (v *volumeSource) CreateVolumes(params []storage.VolumeParams) (_ []storage.CreateVolumesResult, err error) {
results := make([]storage.CreateVolumesResult, len(params))
instanceIds := set.NewStrings()
for i, p := range params {
if err := v.ValidateVolumeParams(p); err != nil {
results[i].Error = err
continue
}
instanceIds.Add(string(p.Attachment.InstanceId))
}
instances := make(instanceCache)
if instanceIds.Size() > 1 {
if err := instances.update(v.gce, instanceIds.Values()...); err != nil {
logger.Debugf("querying running instances: %v", err)
// We ignore the error, because we don't want an invalid
// InstanceId reference from one VolumeParams to prevent
// the creation of another volume.
}
}
for i, p := range params {
if results[i].Error != nil {
continue
}
volume, attachment, err := v.createOneVolume(p, instances)
if err != nil {
results[i].Error = err
logger.Errorf("could not create one volume (or attach it): %v", err)
continue
}
results[i].Volume = volume
results[i].VolumeAttachment = attachment
}
return results, nil
}
// mibToGib converts mebibytes to gibibytes.
// GCE expects GiB, we work in MiB; round up
// to nearest GiB.
func mibToGib(m uint64) uint64 {
return (m + 1023) / 1024
}
func nameVolume(zone string) (string, error) {
volumeUUID, err := utils.NewUUID()
if err != nil {
return "", errors.Annotate(err, "cannot generate uuid to name the volume")
}
// type-zone-uuid
volumeName := fmt.Sprintf("%s--%s", zone, volumeUUID.String())
return volumeName, nil
}
func (v *volumeSource) createOneVolume(p storage.VolumeParams, instances instanceCache) (volume *storage.Volume, volumeAttachment *storage.VolumeAttachment, err error) {
var volumeName, zone string
defer func() {
if err == nil || volumeName == "" {
return
}
if err := v.gce.RemoveDisk(zone, volumeName); err != nil {
logger.Errorf("error cleaning up volume %v: %v", volumeName, err)
}
}()
instId := string(p.Attachment.InstanceId)
if err := instances.update(v.gce, instId); err != nil {
return nil, nil, errors.Annotatef(err, "cannot add %q to instance cache", instId)
}
inst, err := instances.get(instId)
if err != nil {
// Can't create the volume without the instance,
// because we need to know what its AZ is.
return nil, nil, errors.Annotatef(err, "cannot obtain %q from instance cache", instId)
}
persistentType, ok := p.Attributes["type"].(google.DiskType)
if !ok {
persistentType = google.DiskPersistentStandard
}
zone = inst.ZoneName
volumeName, err = nameVolume(zone)
if err != nil {
return nil, nil, errors.Annotate(err, "cannot create a new volume name")
}
// TODO(perrito666) the volumeName is arbitrary and it was crafted this
// way to help solve the need to have zone all over the place.
disk := google.DiskSpec{
SizeHintGB: mibToGib(p.Size),
Name: volumeName,
PersistentDiskType: persistentType,
Description: v.modelUUID,
}
gceDisks, err := v.gce.CreateDisks(zone, []google.DiskSpec{disk})
if err != nil {
return nil, nil, errors.Annotate(err, "cannot create disk")
}
if len(gceDisks) != 1 {
return nil, nil, errors.New(fmt.Sprintf("unexpected number of disks created: %d", len(gceDisks)))
}
gceDisk := gceDisks[0]
// TODO(perrito666) Tag, there are no tags in gce, how do we fix it?
attachedDisk, err := v.attachOneVolume(gceDisk.Name, google.ModeRW, inst.ID)
if err != nil {
return nil, nil, errors.Annotatef(err, "attaching %q to %q", gceDisk.Name, instId)
}
volume = &storage.Volume{
p.Tag,
storage.VolumeInfo{
VolumeId: gceDisk.Name,
Size: gceDisk.Size,
Persistent: true,
},
}
volumeAttachment = &storage.VolumeAttachment{
p.Tag,
p.Attachment.Machine,
storage.VolumeAttachmentInfo{
DeviceLink: fmt.Sprintf(
"/dev/disk/by-id/google-%s",
attachedDisk.DeviceName,
),
},
}
return volume, volumeAttachment, nil
}
func (v *volumeSource) DestroyVolumes(volNames []string) ([]error, error) {
var wg sync.WaitGroup
wg.Add(len(volNames))
results := make([]error, len(volNames))
for i, volumeName := range volNames {
go func(i int, volumeName string) {
defer wg.Done()
results[i] = v.destroyOneVolume(volumeName)
}(i, volumeName)
}
wg.Wait()
return results, nil
}
func parseVolumeId(volName string) (string, string, error) {
idRest := strings.SplitN(volName, "--", 2)
if len(idRest) != 2 {
return "", "", errors.New(fmt.Sprintf("malformed volume id %q", volName))
}
zone := idRest[0]
volumeUUID := idRest[1]
return zone, volumeUUID, nil
}
func isValidVolume(volumeName string) bool {
_, _, err := parseVolumeId(volumeName)
return err == nil
}
func (v *volumeSource) destroyOneVolume(volName string) error {
zone, _, err := parseVolumeId(volName)
if err != nil {
return errors.Annotatef(err, "invalid volume id %q", volName)
}
if err := v.gce.RemoveDisk(zone, volName); err != nil {
return errors.Annotatef(err, "cannot destroy volume %q", volName)
}
return nil
}
func (v *volumeSource) ListVolumes() ([]string, error) {
azs, err := v.gce.AvailabilityZones("")
if err != nil {
return nil, errors.Annotate(err, "cannot determine availability zones")
}
var volumes []string
for _, zone := range azs {
disks, err := v.gce.Disks(zone.Name())
if err != nil {
// maybe use available and status also.
logger.Errorf("cannot get disks for %q zone", zone.Name())
continue
}
for _, disk := range disks {
// Blank disk description means an older disk or a disk
// not created by storage, we should not touch it.
if disk.Description != v.modelUUID && disk.Description != "" {
continue
}
// We don't want to lay hands on disks we did not create.
if isValidVolume(disk.Name) {
volumes = append(volumes, disk.Name)
}
}
}
return volumes, nil
}
func (v *volumeSource) DescribeVolumes(volNames []string) ([]storage.DescribeVolumesResult, error) {
results := make([]storage.DescribeVolumesResult, len(volNames))
for i, vol := range volNames {
res, err := v.describeOneVolume(vol)
if err != nil {
return nil, errors.Annotate(err, "cannot describe volumes")
}
results[i] = res
}
return results, nil
}
func (v *volumeSource) describeOneVolume(volName string) (storage.DescribeVolumesResult, error) {
zone, _, err := parseVolumeId(volName)
if err != nil {
return storage.DescribeVolumesResult{}, errors.Annotatef(err, "cannot describe %q", volName)
}
disk, err := v.gce.Disk(zone, volName)
if err != nil {
return storage.DescribeVolumesResult{}, errors.Annotatef(err, "cannot get volume %q", volName)
}
desc := storage.DescribeVolumesResult{
&storage.VolumeInfo{
Size: disk.Size,
VolumeId: disk.Name,
},
nil,
}
return desc, nil
}
// TODO(perrito666) These rules are yet to be defined.
func (v *volumeSource) ValidateVolumeParams(params storage.VolumeParams) error {
return nil
}
func (v *volumeSource) AttachVolumes(attachParams []storage.VolumeAttachmentParams) ([]storage.AttachVolumesResult, error) {
results := make([]storage.AttachVolumesResult, len(attachParams))
for i, attachment := range attachParams {
volumeName := attachment.VolumeId
mode := google.ModeRW
if attachment.ReadOnly {
mode = google.ModeRW
}
instanceId := attachment.InstanceId
attached, err := v.attachOneVolume(volumeName, mode, string(instanceId))
if err != nil {
logger.Errorf("could not attach %q to %q: %v", volumeName, instanceId, err)
results[i].Error = err
continue
}
results[i].VolumeAttachment = &storage.VolumeAttachment{
attachment.Volume,
attachment.Machine,
storage.VolumeAttachmentInfo{
DeviceName: attached.DeviceName,
},
}
}
return results, nil
}
func (v *volumeSource) attachOneVolume(volumeName string, mode google.DiskMode, instanceId string) (*google.AttachedDisk, error) {
zone, _, err := parseVolumeId(volumeName)
if err != nil {
return nil, errors.Annotate(err, "invalid volume name")
}
instanceDisks, err := v.gce.InstanceDisks(zone, instanceId)
if err != nil {
return nil, errors.Annotate(err, "cannot verify if the disk is already in the instance")
}
// Is it already attached?
for _, disk := range instanceDisks {
if disk.VolumeName == volumeName {
return disk, nil
}
}
attachment, err := v.gce.AttachDisk(zone, volumeName, instanceId, mode)
if err != nil {
return nil, errors.Annotate(err, "cannot attach volume")
}
return attachment, nil
}
func (v *volumeSource) DetachVolumes(attachParams []storage.VolumeAttachmentParams) ([]error, error) {
result := make([]error, len(attachParams))
for i, volumeAttachment := range attachParams {
result[i] = v.detachOneVolume(volumeAttachment)
}
return result, nil
}
func (v *volumeSource) detachOneVolume(attachParam storage.VolumeAttachmentParams) error {
instId := attachParam.InstanceId
volumeName := attachParam.VolumeId
zone, _, err := parseVolumeId(volumeName)
if err != nil {
return errors.Annotatef(err, "%q is not a valid volume id", volumeName)
}
return v.gce.DetachDisk(zone, string(instId), volumeName)
}