github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/image.go (about) 1 package ibmcloud 2 3 import ( 4 "net/http" 5 "strings" 6 7 "github.com/IBM/vpc-go-sdk/vpcv1" 8 "github.com/pkg/errors" 9 ) 10 11 const imageTypeName = "image" 12 13 // listImages lists images in the vpc 14 func (o *ClusterUninstaller) listImages() (cloudResources, error) { 15 o.Logger.Debugf("Listing images") 16 ctx, cancel := o.contextWithTimeout() 17 defer cancel() 18 19 options := o.vpcSvc.NewListImagesOptions() 20 resources, _, err := o.vpcSvc.ListImagesWithContext(ctx, options) 21 if err != nil { 22 return nil, errors.Wrapf(err, "failed to list images") 23 } 24 25 result := []cloudResource{} 26 for _, image := range resources.Images { 27 if strings.Contains(*image.Name, o.InfraID) { 28 result = append(result, cloudResource{ 29 key: *image.ID, 30 name: *image.Name, 31 status: *image.Status, 32 typeName: imageTypeName, 33 id: *image.ID, 34 }) 35 } 36 } 37 38 return cloudResources{}.insert(result...), nil 39 } 40 41 func (o *ClusterUninstaller) deleteImage(item cloudResource) error { 42 if item.status == vpcv1.ImageStatusDeletingConst { 43 o.Logger.Debugf("Waiting for image %q to delete", item.name) 44 return nil 45 } 46 47 o.Logger.Debugf("Deleting image %q", item.name) 48 ctx, cancel := o.contextWithTimeout() 49 defer cancel() 50 51 options := o.vpcSvc.NewDeleteImageOptions(item.id) 52 details, err := o.vpcSvc.DeleteImageWithContext(ctx, options) 53 54 if err != nil && details != nil && details.StatusCode == http.StatusNotFound { 55 // The resource is gone 56 o.deletePendingItems(item.typeName, []cloudResource{item}) 57 o.Logger.Infof("Deleted image %q", item.name) 58 return nil 59 } 60 61 if err != nil && details != nil && details.StatusCode != http.StatusNotFound { 62 return errors.Wrapf(err, "Failed to delete image %s", item.name) 63 } 64 65 return nil 66 } 67 68 // destroyImages removes all image resources that have a name prefixed 69 // with the cluster's infra ID. 70 func (o *ClusterUninstaller) destroyImages() error { 71 found, err := o.listImages() 72 if err != nil { 73 return err 74 } 75 76 items := o.insertPendingItems(imageTypeName, found.list()) 77 for _, item := range items { 78 if _, ok := found[item.key]; !ok { 79 // This item has finished deletion. 80 o.deletePendingItems(item.typeName, []cloudResource{item}) 81 o.Logger.Infof("Deleted image %q", item.name) 82 continue 83 } 84 err := o.deleteImage(item) 85 if err != nil { 86 o.errorTracker.suppressWarning(item.key, err, o.Logger) 87 } 88 } 89 90 if items = o.getPendingItems(imageTypeName); len(items) > 0 { 91 return errors.Errorf("%d items pending", len(items)) 92 } 93 return nil 94 }