Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions pkg/cloud/openstack/clients/machineservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,28 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
}

if bootfromvolume.SourceType(config.RootVolume.SourceType) == bootfromvolume.SourceImage {
// Name the volume after the instance
volumeName := name

// if source type is "image" then we have to create a volume from the image first
klog.Infof("Creating a bootable volume from image %v.", config.RootVolume.SourceUUID)
klog.Infof("Creating bootable volume with name %q from image %v.", volumeName, config.RootVolume.SourceUUID)

// Deleting any volumes with the same name, as they may
// be leftovers from a previous failed try.
{
volumeIDs, err := volumeIDsFromName(is.volumeClient, volumeName)
if err != nil {
klog.Infof("unable to list volumes with name %q: %v.", volumeName, err)
}

for _, volumeID := range volumeIDs {
if err := volumes.Delete(is.volumeClient, volumeID, nil).ExtractErr(); err != nil {
klog.Infof("unable to delete volume with ID %q: %v.", volumeID, err)
} else {
klog.Infof("deleted volume with name %q and ID %q", volumeName, volumeID)
}
}
}

imageID, err := imageutils.IDFromName(is.imagesClient, config.RootVolume.SourceUUID)
if err != nil {
Expand All @@ -717,11 +737,10 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust

// Create a volume first
volumeCreateOpts := volumes.CreateOpts{
Size: config.RootVolume.Size,
VolumeType: config.RootVolume.VolumeType,
ImageID: imageID,
// The same name as the instance
Name: name,
Size: config.RootVolume.Size,
VolumeType: config.RootVolume.VolumeType,
ImageID: imageID,
Name: volumeName,
AvailabilityZone: config.RootVolume.Zone,
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/cloud/openstack/clients/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/utils/openstack/clientconfig"
machinev1 "github.com/openshift/machine-api-operator/pkg/apis/machine/v1beta1"

Expand Down Expand Up @@ -106,3 +107,26 @@ func GetProviderClient(cloud clientconfig.Cloud, cert []byte) (*gophercloud.Prov

return provider, nil
}

// volumeIDsFromName returns zero or more IDs corresponding to a name. The returned
// error is only non-nil in case of failure.
func volumeIDsFromName(client *gophercloud.ServiceClient, name string) ([]string, error) {
pages, err := volumes.List(client, volumes.ListOpts{
Name: name,
}).AllPages()
if err != nil {
return nil, err
}

all, err := volumes.ExtractVolumes(pages)
if err != nil {
return nil, err
}

IDs := make([]string, len(all))
for i := range all {
IDs[i] = all[i].ID
}

return IDs, nil
}