Skip to content

Commit

Permalink
cleanup in openstack plan builder and client
Browse files Browse the repository at this point in the history
Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Dec 25, 2023
1 parent 2949e3b commit cea3700
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
39 changes: 22 additions & 17 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,23 +896,26 @@ func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string,
r.Log.Info("the image is not ready yet", "image", image.Name)
continue
}
var populatorName string
populatorName, err = r.ensureVolumePopulator(workload, &image, secretName)
if err != nil {
return
}
var pvc *core.PersistentVolumeClaim
pvc, err = r.ensureVolumePopulatorPVC(workload, &image, annotations, populatorName)
if err != nil {
if pvc, pvcErr := r.getCorrespondingPvc(image, workload, vmRef, annotations, secretName); pvcErr == nil {
if pvc != nil {
pvcNames = append(pvcNames, pvc.Name)
}
} else {
err = pvcErr
return
}
if pvc != nil {
pvcNames = append(pvcNames, pvc.Name)
}
}
return
}

func (r *Builder) getCorrespondingPvc(image model.Image, workload *model.Workload, vmRef ref.Ref, annotations map[string]string, secretName string) (*core.PersistentVolumeClaim, error) {
if populatorName, err := r.ensureVolumePopulator(workload, &image, secretName); err == nil {
return r.ensureVolumePopulatorPVC(workload, &image, annotations, populatorName)
} else {
return nil, err
}
}

func (r *Builder) ensureVolumePopulator(workload *model.Workload, image *model.Image, secretName string) (populatorName string, err error) {
volumePopulatorCR, err := r.getVolumePopulatorCR(image.Name)
if err != nil {
Expand All @@ -938,15 +941,17 @@ func (r *Builder) ensureVolumePopulatorPVC(workload *model.Workload, image *mode
originalVolumeDiskId = imageProperty.(string)
}
storageClassName := r.Context.Map.Storage.Spec.Map[0].Destination.StorageClass
volumeType := r.getVolumeType(workload, originalVolumeDiskId)
if volumeType != "" {
if volumeType := r.getVolumeType(workload, originalVolumeDiskId); volumeType != "" {
storageClassName, err = r.getStorageClassName(workload, volumeType)
if err != nil {
err = liberr.Wrap(err)
return
}
}
pvc, err = r.persistentVolumeClaimWithSourceRef(*image, storageClassName, populatorName, annotations)
if pvc, err = r.persistentVolumeClaimWithSourceRef(*image, storageClassName, populatorName, annotations); err != nil {
err = liberr.Wrap(err)
return
}
}
return
}
Expand Down Expand Up @@ -1082,9 +1087,9 @@ func (r *Builder) getVolumePopulatorCR(name string) (populatorCr api.OpenstackVo
return
}

func (r *Builder) getVolumePopulatorPVC(name string) (populatorPvc core.PersistentVolumeClaim, err error) {
populatorPvc = core.PersistentVolumeClaim{}
err = r.Destination.Client.Get(context.TODO(), client.ObjectKey{Namespace: r.Plan.Spec.TargetNamespace, Name: name}, &populatorPvc)
func (r *Builder) getVolumePopulatorPVC(name string) (populatorPvc *core.PersistentVolumeClaim, err error) {
populatorPvc = &core.PersistentVolumeClaim{}
err = r.Destination.Client.Get(context.TODO(), client.ObjectKey{Namespace: r.Plan.Spec.TargetNamespace, Name: name}, populatorPvc)
return
}

Expand Down
22 changes: 7 additions & 15 deletions pkg/controller/plan/adapter/openstack/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,7 @@ func (r *Client) ensureVmSnapshot(vm *libclient.VM) (ready bool, err error) {

func (r *Client) ensureImagesFromVolumesReady(vm *libclient.VM) (ready bool, err error) {
var imagesFromVolumes []libclient.Image
imagesFromVolumes, err = r.getImagesFromVolumes(vm)
if err != nil {
if imagesFromVolumes, err = r.getImagesFromVolumes(vm); err != nil {
err = liberr.Wrap(err)
r.Log.Error(err, "error while trying to get the images from the VM volumes",
"vm", vm.Name)
Expand Down Expand Up @@ -831,29 +830,25 @@ func (r *Client) ensureImageFromVolumeReady(vm *libclient.VM, image *libclient.I

func (r *Client) ensureImageUpToDate(vm *libclient.VM, image *libclient.Image) (upToDate bool, err error) {
inventoryImage := &model.Image{}
err = r.Context.Source.Inventory.Find(inventoryImage, ref.Ref{ID: image.ID})
if err != nil {
if err = r.Context.Source.Inventory.Find(inventoryImage, ref.Ref{ID: image.ID}); err != nil {
if errors.As(err, &model.NotFoundError{}) {
err = nil
r.Log.Info("the image does not exist in the inventory, waiting...",
"vm", vm.Name, "image", image.Name, "properties", image.Properties)
}
return
}
upToDate = true
if _, ok := inventoryImage.Properties[forkliftPropertyOriginalVolumeID]; !ok {
if _, upToDate = inventoryImage.Properties[forkliftPropertyOriginalVolumeID]; !upToDate {
r.Log.Info("image properties have not been synchronized, waiting...",
"vm", vm.Name, "image", inventoryImage.Name, "properties", inventoryImage.Properties)
upToDate = false
}
return

}

func (r *Client) ensureSnapshotsFromVolumes(vm *libclient.VM) (err error) {
var snapshotsFromVolumes []libclient.Snapshot
snapshotsFromVolumes, err = r.getSnapshotsFromVolumes(vm)
if err != nil {
if snapshotsFromVolumes, err = r.getSnapshotsFromVolumes(vm); err != nil {
err = liberr.Wrap(err)
return
}
Expand All @@ -878,8 +873,7 @@ func (r *Client) ensureSnapshotsFromVolumes(vm *libclient.VM) (err error) {
}

func (r *Client) ensureVolumeFromSnapshot(vm *libclient.VM, snapshot *libclient.Snapshot) (err error) {
_, err = r.getVolumeFromSnapshot(vm, snapshot.ID)
if err != nil {
if _, err = r.getVolumeFromSnapshot(vm, snapshot.ID); err != nil {
if !errors.Is(err, ResourceNotFoundError) {
err = liberr.Wrap(err)
r.Log.Error(err, "trying to get the snapshot info from the volume VM snapshot",
Expand Down Expand Up @@ -916,8 +910,7 @@ func (r *Client) ensureVolumeFromSnapshot(vm *libclient.VM, snapshot *libclient.

func (r *Client) ensureVolumesFromSnapshots(vm *libclient.VM) (err error) {
var volumesFromSnapshots []libclient.Volume
volumesFromSnapshots, err = r.getVolumesFromSnapshots(vm)
if err != nil {
if volumesFromSnapshots, err = r.getVolumesFromSnapshots(vm); err != nil {
err = liberr.Wrap(err)
return
}
Expand Down Expand Up @@ -945,8 +938,7 @@ func (r *Client) ensureVolumesFromSnapshots(vm *libclient.VM) (err error) {
}

func (r *Client) ensureImageFromVolume(vm *libclient.VM, volume *libclient.Volume) (err error) {
_, err = r.getImageFromVolume(vm, volume.ID)
if err != nil {
if _, err = r.getImageFromVolume(vm, volume.ID); err != nil {
if !errors.Is(err, ResourceNotFoundError) {
err = liberr.Wrap(err)
r.Log.Error(err, "while trying to get the image from the volume",
Expand Down

0 comments on commit cea3700

Please sign in to comment.