@@ -17,7 +17,6 @@ package gcecloudprovider
1717import (
1818 "context"
1919 "fmt"
20- "strconv"
2120 "strings"
2221
2322 "github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
@@ -37,6 +36,7 @@ const (
3736 Timestamp = "2018-09-05T15:17:08.270-07:00"
3837 BasePath = "https://www.googleapis.com/compute/v1/projects/"
3938 snapshotURITemplateGlobal = "%s/global/snapshots/%s" //{gce.projectID}/global/snapshots/{snapshot.Name}"
39+ imageURITemplateGlobal = "%s/global/images/%s" //{gce.projectID}/global/images/{image.Name}"
4040)
4141
4242type FakeCloudProvider struct {
@@ -47,6 +47,7 @@ type FakeCloudProvider struct {
4747 pageTokens map [string ]sets.String
4848 instances map [string ]* computev1.Instance
4949 snapshots map [string ]* computev1.Snapshot
50+ images map [string ]* computev1.Image
5051
5152 // marker to set disk status during InsertDisk operation.
5253 mockDiskStatus string
@@ -61,6 +62,7 @@ func CreateFakeCloudProvider(project, zone string, cloudDisks []*CloudDisk) (*Fa
6162 disks : map [string ]* CloudDisk {},
6263 instances : map [string ]* computev1.Instance {},
6364 snapshots : map [string ]* computev1.Snapshot {},
65+ images : map [string ]* computev1.Image {},
6466 pageTokens : map [string ]sets.String {},
6567 // A newly created disk is marked READY by default.
6668 mockDiskStatus : "READY" ,
@@ -122,7 +124,7 @@ func (cloud *FakeCloudProvider) ListDisks(ctx context.Context) ([]*computev1.Dis
122124 return d , "" , nil
123125}
124126
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 ) {
126128 var sourceDisk string
127129 snapshots := []* computev1.Snapshot {}
128130 if len (filter ) > 0 {
@@ -141,45 +143,7 @@ func (cloud *FakeCloudProvider) ListSnapshots(ctx context.Context, filter string
141143 snapshots = append (snapshots , snapshot )
142144 }
143145
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
183147}
184148
185149// Disk Methods
@@ -238,6 +202,7 @@ func (cloud *FakeCloudProvider) InsertDisk(ctx context.Context, project string,
238202 Type : cloud .GetDiskTypeURI (project , volKey , params .DiskType ),
239203 SourceSnapshotId : snapshotID ,
240204 SourceDiskId : volumeContentSourceVolumeID ,
205+ SourceImageId : snapshotID ,
241206 Status : cloud .mockDiskStatus ,
242207 Labels : params .Labels ,
243208 }
@@ -399,6 +364,71 @@ func (cloud *FakeCloudProvider) DeleteSnapshot(ctx context.Context, project, sna
399364 return nil
400365}
401366
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+
402432func (cloud * FakeCloudProvider ) ValidateExistingSnapshot (resp * computev1.Snapshot , volKey * meta.Key ) error {
403433 if resp == nil {
404434 return fmt .Errorf ("disk does not exist" )
@@ -447,6 +477,13 @@ func (cloud *FakeCloudProvider) getGlobalSnapshotURI(project, snapshotName strin
447477 snapshotName )
448478}
449479
480+ func (cloud * FakeCloudProvider ) getGlobalImageURI (project , imageName string ) string {
481+ return BasePath + fmt .Sprintf (
482+ imageURITemplateGlobal ,
483+ project ,
484+ imageName )
485+ }
486+
450487func (cloud * FakeCloudProvider ) UpdateDiskStatus (s string ) {
451488 cloud .mockDiskStatus = s
452489}
@@ -467,6 +504,13 @@ func (cloud *FakeBlockingCloudProvider) CreateSnapshot(ctx context.Context, proj
467504 return cloud .FakeCloudProvider .CreateSnapshot (ctx , project , volKey , snapshotName , snapshotParams )
468505}
469506
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+
470514func notFoundError () * googleapi.Error {
471515 return & googleapi.Error {
472516 Errors : []googleapi.ErrorItem {
0 commit comments