Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openstack: check image is in the inventory before progressing #693

Merged
merged 2 commits into from
Jan 2, 2024
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
9 changes: 5 additions & 4 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,8 +1101,7 @@ func (r *Builder) persistentVolumeClaimWithSourceRef(image model.Image, storageC
}

var accessModes []core.PersistentVolumeAccessMode
var volumeMode *core.PersistentVolumeMode
accessModes, volumeMode, err = r.getVolumeAndAccessMode(storageClassName)
accessModes, volumeMode, err := r.getVolumeAndAccessMode(storageClassName)
if err != nil {
err = liberr.Wrap(err)
return
Expand All @@ -1113,9 +1112,11 @@ func (r *Builder) persistentVolumeClaimWithSourceRef(image model.Image, storageC
if originalVolumeDiskId, ok := image.Properties["forklift_original_volume_id"]; ok {
annotations[AnnImportDiskId] = originalVolumeDiskId.(string)
r.Log.Info("the image comes from a volume", "volumeID", originalVolumeDiskId)
} else if originalImageId, ok := image.Properties["forklift_original_image_id"]; ok {
annotations[AnnImportDiskId] = originalImageId.(string)
r.Log.Info("the image comes from a vm snapshot", "imageID", originalImageId)
} else {
annotations[AnnImportDiskId] = image.ID
r.Log.Info("the image comes from a vm snapshot", "imageID", image.ID)
r.Log.Error(nil, "the image has no volume or vm snapshot associated to it", "image", image.Name)
}
ahadas marked this conversation as resolved.
Show resolved Hide resolved

pvc = &core.PersistentVolumeClaim{
Expand Down
42 changes: 35 additions & 7 deletions pkg/controller/plan/adapter/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,17 @@ func (r *Client) createVmSnapshotImage(vm *libclient.VM) (vmImage *libclient.Ima
err = liberr.Wrap(err)
return
}
// The vm is image based and we need to create the snapsots of the
// The vm is image based and we need to create the snapshots of the
// volumes attached to it.
if _, ok := vm.Image["id"]; ok {
if imageID, ok := vm.Image["id"]; ok {
// Update property for image based
imageUpdateOpts := &libclient.ImageUpdateOpts{}
imageUpdateOpts.AddImageProperty(forkliftPropertyOriginalImageID, imageID.(string))
err = r.Update(vmImage, imageUpdateOpts)
if err != nil {
err = liberr.Wrap(err)
return
}
for _, attachedVolume := range vm.AttachedVolumes {
var volume *libclient.Volume
volume, err = r.getVolume(ref.Ref{ID: attachedVolume.ID})
Expand Down Expand Up @@ -796,6 +804,14 @@ func (r *Client) ensureVmSnapshot(vm *libclient.VM) (ready bool, err error) {
r.Log.Info("the VM snapshot image is ready!",
"vm", vm.Name, "image", vmSnapshotImage.Name, "imageID", vmSnapshotImage.ID)
ready = true
if _, ok := vm.Image["id"]; ok {
ready, err = r.ensureImageUpToDate(vm, vmSnapshotImage, vmTypeImageBased)
if err != nil {
r.Log.Error(err, "checking the VM snapshot image properties", "vm", vm.Name, "image", vmSnapshotImage.Name)
return
}
}
return
case ImageStatusImporting, ImageStatusQueued, ImageStatusUploading, ImageStatusSaving:
r.Log.Info("the VM snapshot image is not ready yet, skipping...",
"vm", vm.Name, "image", vmSnapshotImage.Name, "imageID", vmSnapshotImage.ID)
Expand All @@ -806,7 +822,6 @@ func (r *Client) ensureVmSnapshot(vm *libclient.VM) (ready bool, err error) {
"vm", vm.Name, "image", vmSnapshotImage.Name, "imageID", vmSnapshotImage.ID, "status", vmSnapshotImage.Status)
return
}
return
}

func (r *Client) ensureImagesFromVolumesReady(vm *libclient.VM) (ready bool, err error) {
Expand Down Expand Up @@ -862,7 +877,7 @@ func (r *Client) ensureImageFromVolumeReady(vm *libclient.VM, image *libclient.I
r.Log.Info("the image properties have been updated",
"vm", vm.Name, "image", image.Name, "properties", image.Properties)
var imageUpToDate bool
imageUpToDate, err = r.ensureImageUpToDate(vm, image)
imageUpToDate, err = r.ensureImageUpToDate(vm, image, vmTypeVolumeBased)
if err != nil || !imageUpToDate {
return
}
Expand All @@ -877,7 +892,12 @@ func (r *Client) ensureImageFromVolumeReady(vm *libclient.VM, image *libclient.I
return
}

func (r *Client) ensureImageUpToDate(vm *libclient.VM, image *libclient.Image) (upToDate bool, err error) {
type vmType string

var vmTypeImageBased vmType = "imageBased"
var vmTypeVolumeBased vmType = "volumeBased"

func (r *Client) ensureImageUpToDate(vm *libclient.VM, image *libclient.Image, vmType vmType) (upToDate bool, err error) {
inventoryImage := &model.Image{}
if err = r.Context.Source.Inventory.Find(inventoryImage, ref.Ref{ID: image.ID}); err != nil {
if errors.As(err, &model.NotFoundError{}) {
Expand All @@ -887,12 +907,20 @@ func (r *Client) ensureImageUpToDate(vm *libclient.VM, image *libclient.Image) (
}
return
}
if _, upToDate = inventoryImage.Properties[forkliftPropertyOriginalVolumeID]; !upToDate {

switch vmType {
case vmTypeImageBased:
_, upToDate = inventoryImage.Properties[forkliftPropertyOriginalImageID]
case vmTypeVolumeBased:
_, upToDate = inventoryImage.Properties[forkliftPropertyOriginalVolumeID]
}

if !upToDate {
r.Log.Info("image properties have not been synchronized, waiting...",
"vm", vm.Name, "image", inventoryImage.Name, "properties", inventoryImage.Properties)
}
return

return
}

func (r *Client) ensureSnapshotsFromVolumes(vm *libclient.VM) (err error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/plan/adapter/openstack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package openstack

import (
"fmt"

plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
)

const (
forkliftPropertyOriginalVolumeID = "forklift_original_volume_id"
forkliftPropertyOriginalImageID = "forklift_original_image_id"
)

func getMigrationName(ctx *plancontext.Context) string {
Expand Down
Loading