@@ -17,7 +17,6 @@ package gcecloudprovider
17
17
import (
18
18
"context"
19
19
"fmt"
20
- "strconv"
21
20
"strings"
22
21
23
22
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -37,6 +36,7 @@ const (
37
36
Timestamp = "2018-09-05T15:17:08.270-07:00"
38
37
BasePath = "https://www.googleapis.com/compute/v1/projects/"
39
38
snapshotURITemplateGlobal = "%s/global/snapshots/%s" //{gce.projectID}/global/snapshots/{snapshot.Name}"
39
+ imageURITemplateGlobal = "%s/global/images/%s" //{gce.projectID}/global/images/{image.Name}"
40
40
)
41
41
42
42
type FakeCloudProvider struct {
@@ -47,6 +47,7 @@ type FakeCloudProvider struct {
47
47
pageTokens map [string ]sets.String
48
48
instances map [string ]* computev1.Instance
49
49
snapshots map [string ]* computev1.Snapshot
50
+ images map [string ]* computev1.Image
50
51
51
52
// marker to set disk status during InsertDisk operation.
52
53
mockDiskStatus string
@@ -61,6 +62,7 @@ func CreateFakeCloudProvider(project, zone string, cloudDisks []*CloudDisk) (*Fa
61
62
disks : map [string ]* CloudDisk {},
62
63
instances : map [string ]* computev1.Instance {},
63
64
snapshots : map [string ]* computev1.Snapshot {},
65
+ images : map [string ]* computev1.Image {},
64
66
pageTokens : map [string ]sets.String {},
65
67
// A newly created disk is marked READY by default.
66
68
mockDiskStatus : "READY" ,
@@ -122,7 +124,7 @@ func (cloud *FakeCloudProvider) ListDisks(ctx context.Context) ([]*computev1.Dis
122
124
return d , "" , nil
123
125
}
124
126
125
- func (cloud * FakeCloudProvider ) ListSnapshots (ctx context.Context , filter string , maxEntries int64 , pageToken string ) ([]* computev1.Snapshot , string , error ) {
127
+ func (cloud * FakeCloudProvider ) ListSnapshots (ctx context.Context , filter string ) ([]* computev1.Snapshot , string , error ) {
126
128
var sourceDisk string
127
129
snapshots := []* computev1.Snapshot {}
128
130
if len (filter ) > 0 {
@@ -141,45 +143,7 @@ func (cloud *FakeCloudProvider) ListSnapshots(ctx context.Context, filter string
141
143
snapshots = append (snapshots , snapshot )
142
144
}
143
145
144
- var (
145
- ulenSnapshots = len (snapshots )
146
- startingToken int
147
- )
148
-
149
- if len (pageToken ) > 0 {
150
- i , err := strconv .ParseUint (pageToken , 10 , 32 )
151
- if err != nil {
152
- return nil , "" , invalidError ()
153
- }
154
- startingToken = int (i )
155
- }
156
-
157
- if startingToken > ulenSnapshots {
158
- return nil , "" , invalidError ()
159
- }
160
-
161
- // Discern the number of remaining entries.
162
- rem := ulenSnapshots - startingToken
163
-
164
- // If maxEntries is 0 or greater than the number of remaining entries then
165
- // set maxEntries to the number of remaining entries.
166
- max := int (maxEntries )
167
- if max == 0 || max > rem {
168
- max = rem
169
- }
170
-
171
- results := []* computev1.Snapshot {}
172
- j := startingToken
173
- for i := 0 ; i < max ; i ++ {
174
- results = append (results , snapshots [j ])
175
- j ++
176
- }
177
-
178
- var nextToken string
179
- if j < ulenSnapshots {
180
- nextToken = fmt .Sprintf ("%d" , j )
181
- }
182
- return results , nextToken , nil
146
+ return snapshots , "" , nil
183
147
}
184
148
185
149
// Disk Methods
@@ -238,6 +202,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
238
202
Type : cloud .GetDiskTypeURI (project , volKey , params .DiskType ),
239
203
SourceSnapshotId : snapshotID ,
240
204
SourceDiskId : volumeContentSourceVolumeID ,
205
+ SourceImageId : snapshotID ,
241
206
Status : cloud .mockDiskStatus ,
242
207
Labels : params .Labels ,
243
208
}
@@ -399,6 +364,71 @@ func (cloud *FakeCloudProvider) DeleteSnapshot(ctx context.Context, project, sna
399
364
return nil
400
365
}
401
366
367
+ func (cloud * FakeCloudProvider ) ListImages (ctx context.Context , filter string ) ([]* computev1.Image , string , error ) {
368
+ var sourceDisk string
369
+ images := []* computev1.Image {}
370
+ if len (filter ) > 0 {
371
+ filterSplits := strings .Fields (filter )
372
+ if len (filterSplits ) != 3 || filterSplits [0 ] != "sourceDisk" {
373
+ return nil , "" , invalidError ()
374
+ }
375
+ sourceDisk = filterSplits [2 ]
376
+ }
377
+ for _ , image := range cloud .images {
378
+ if len (sourceDisk ) > 0 {
379
+ if image .SourceDisk == sourceDisk {
380
+ continue
381
+ }
382
+ }
383
+ images = append (images , image )
384
+ }
385
+
386
+ return images , "" , nil
387
+ }
388
+
389
+ func (cloud * FakeCloudProvider ) GetImage (ctx context.Context , project , imageName string ) (* computev1.Image , error ) {
390
+ image , ok := cloud .images [imageName ]
391
+ if ! ok {
392
+ return nil , notFoundError ()
393
+ }
394
+ image .Status = "READY"
395
+ return image , nil
396
+ }
397
+
398
+ func (cloud * FakeCloudProvider ) CreateImage (ctx context.Context , project string , volKey * meta.Key , imageName string , snapshotParams common.SnapshotParameters ) (* computev1.Image , error ) {
399
+ if image , ok := cloud .images [imageName ]; ok {
400
+ return image , nil
401
+ }
402
+
403
+ imageToCreate := & computev1.Image {
404
+ CreationTimestamp : Timestamp ,
405
+ DiskSizeGb : int64 (DiskSizeGb ),
406
+ Family : snapshotParams .ImageFamily ,
407
+ Name : imageName ,
408
+ SelfLink : cloud .getGlobalImageURI (project , imageName ),
409
+ SourceType : "RAW" ,
410
+ Status : "PENDING" ,
411
+ StorageLocations : snapshotParams .StorageLocations ,
412
+ }
413
+
414
+ switch volKey .Type () {
415
+ case meta .Zonal :
416
+ imageToCreate .SourceDisk = cloud .getZonalDiskSourceURI (project , volKey .Name , volKey .Zone )
417
+ case meta .Regional :
418
+ imageToCreate .SourceDisk = cloud .getRegionalDiskSourceURI (project , volKey .Name , volKey .Region )
419
+ default :
420
+ return nil , fmt .Errorf ("could not create image, disk key was neither zonal nor regional, instead got: %v" , volKey .String ())
421
+ }
422
+
423
+ cloud .images [imageName ] = imageToCreate
424
+ return imageToCreate , nil
425
+ }
426
+
427
+ func (cloud * FakeCloudProvider ) DeleteImage (ctx context.Context , project , imageName string ) error {
428
+ delete (cloud .images , imageName )
429
+ return nil
430
+ }
431
+
402
432
func (cloud * FakeCloudProvider ) ValidateExistingSnapshot (resp * computev1.Snapshot , volKey * meta.Key ) error {
403
433
if resp == nil {
404
434
return fmt .Errorf ("disk does not exist" )
@@ -447,6 +477,13 @@ func (cloud *FakeCloudProvider) getGlobalSnapshotURI(project, snapshotName strin
447
477
snapshotName )
448
478
}
449
479
480
+ func (cloud * FakeCloudProvider ) getGlobalImageURI (project , imageName string ) string {
481
+ return BasePath + fmt .Sprintf (
482
+ imageURITemplateGlobal ,
483
+ project ,
484
+ imageName )
485
+ }
486
+
450
487
func (cloud * FakeCloudProvider ) UpdateDiskStatus (s string ) {
451
488
cloud .mockDiskStatus = s
452
489
}
@@ -467,6 +504,13 @@ func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, proj
467
504
return cloud .FakeCloudProvider .CreateSnapshot (ctx , project , volKey , snapshotName , snapshotParams )
468
505
}
469
506
507
+ func (cloud * FakeBlockingCloudProvider ) CreateImage (ctx context.Context , project string , volKey * meta.Key , imageName string , snapshotParams common.SnapshotParameters ) (* computev1.Image , error ) {
508
+ executeCreateSnapshot := make (chan struct {})
509
+ cloud .ReadyToExecute <- executeCreateSnapshot
510
+ <- executeCreateSnapshot
511
+ return cloud .FakeCloudProvider .CreateImage (ctx , project , volKey , imageName , snapshotParams )
512
+ }
513
+
470
514
func notFoundError () * googleapi.Error {
471
515
return & googleapi.Error {
472
516
Errors : []googleapi.ErrorItem {
0 commit comments