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: fix cognitive complexity #491

Merged
merged 5 commits into from
Dec 25, 2023
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
2 changes: 1 addition & 1 deletion pkg/controller/plan/adapter/base/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Builder interface {
// check whether the builder supports Volume Populators
SupportsVolumePopulators() bool
// Build populator volumes
PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) (pvcNames []string, err error)
PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) ([]*core.PersistentVolumeClaim, error)
// Transferred bytes
PopulatorTransferredBytes(persistentVolumeClaim *core.PersistentVolumeClaim) (transferredBytes int64, err error)
// Set the populator PVC labels
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/plan/adapter/ocp/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ func (r *Builder) SupportsVolumePopulators() bool {
return false
}

func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) (pvcNames []string, err error) {
func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) (pvcs []*core.PersistentVolumeClaim, err error) {
err = planbase.VolumePopulatorNotSupportedError
return
}
Expand Down
100 changes: 55 additions & 45 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,20 +870,18 @@ func (r *Builder) SupportsVolumePopulators() bool {
return true
}

func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) (pvcNames []string, err error) {
func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string, secretName string) (pvcs []*core.PersistentVolumeClaim, err error) {
workload := &model.Workload{}
err = r.Source.Inventory.Find(workload, vmRef)
if err != nil {
err = liberr.Wrap(err)
return
}

images, err := r.getImagesFromVolumes(workload)
if err != nil {
err = liberr.Wrap(err)
return
}

if workload.ImageID != "" {
var image model.Image
image, err = r.getVMSnapshotImage(workload)
Expand All @@ -893,56 +891,64 @@ func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string,
}
images = append(images, image)
}

for _, image := range images {

if image.Status != string(ImageStatusActive) {
r.Log.Info("the image is not ready yet", "image", image.Name)
continue
}

originalVolumeDiskId := image.Name
if imageProperty, ok := image.Properties[forkliftPropertyOriginalVolumeID]; ok {
originalVolumeDiskId = imageProperty.(string)
if pvc, pvcErr := r.getCorrespondingPvc(image, workload, vmRef, annotations, secretName); pvcErr == nil {
pvcs = append(pvcs, pvc)
} else {
err = pvcErr
return
}
}
return
}

_, err = r.getVolumePopulator(image.Name)
if err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
return
}
}
func (r *Builder) getCorrespondingPvc(image model.Image, workload *model.Workload, vmRef ref.Ref, annotations map[string]string, secretName string) (pvc *core.PersistentVolumeClaim, err error) {
populatorCR, err := r.ensureVolumePopulator(workload, &image, secretName)
if err != nil {
return
}
return r.ensureVolumePopulatorPVC(workload, &image, annotations, populatorCR.Name)
}

var populatorName string
populatorName, err = r.createVolumePopulatorCR(image, secretName, vmRef.ID)
if err != nil {
func (r *Builder) ensureVolumePopulator(workload *model.Workload, image *model.Image, secretName string) (populatorCR *api.OpenstackVolumePopulator, err error) {
volumePopulatorCR, err := r.getVolumePopulatorCR(image.Name)
if err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
return
}
return r.createVolumePopulatorCR(*image, secretName, workload.ID)
}
populatorCR = &volumePopulatorCR
return
}

func (r *Builder) ensureVolumePopulatorPVC(workload *model.Workload, image *model.Image, annotations map[string]string, populatorName string) (pvc *core.PersistentVolumeClaim, err error) {
if pvc, err = r.getVolumePopulatorPVC(image.ID); err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
return
}
originalVolumeDiskId := image.Name
if imageProperty, ok := image.Properties[forkliftPropertyOriginalVolumeID]; ok {
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
}
}

var pvc *core.PersistentVolumeClaim
pvc, err = r.persistentVolumeClaimWithSourceRef(image, storageClassName, populatorName, annotations)
if err != nil {
if !k8serr.IsAlreadyExists(err) {
err = liberr.Wrap(err, "couldn't build the PVC",
"image", image.Name, "storageClassName", storageClassName, "populatorName", populatorName)
return
}
err = nil
continue
if pvc, err = r.persistentVolumeClaimWithSourceRef(*image, storageClassName, populatorName, annotations); err != nil {
err = liberr.Wrap(err)
return
}
pvcNames = append(pvcNames, pvc.Name)
}
return
}
Expand Down Expand Up @@ -989,8 +995,8 @@ func (r *Builder) getImagesFromVolumes(workload *model.Workload) (images []model
return
}

func (r *Builder) createVolumePopulatorCR(image model.Image, secretName, vmId string) (name string, err error) {
populatorCR := &api.OpenstackVolumePopulator{
func (r *Builder) createVolumePopulatorCR(image model.Image, secretName, vmId string) (populatorCR *api.OpenstackVolumePopulator, err error) {
populatorCR = &api.OpenstackVolumePopulator{
ObjectMeta: meta.ObjectMeta{
Name: image.Name,
Namespace: r.Plan.Spec.TargetNamespace,
Expand All @@ -1005,14 +1011,9 @@ func (r *Builder) createVolumePopulatorCR(image model.Image, secretName, vmId st
}
err = r.Context.Client.Create(context.TODO(), populatorCR, &client.CreateOptions{})
if err != nil {
if !k8serr.IsAlreadyExists(err) {
err = liberr.Wrap(err)
ahadas marked this conversation as resolved.
Show resolved Hide resolved
return
} else {
err = nil
}
err = liberr.Wrap(err)
return
}
name = populatorCR.Name
return
}

Expand Down Expand Up @@ -1077,12 +1078,18 @@ func (r *Builder) getVolumeAndAccessMode(storageClassName string) ([]core.Persis
}

// Get the OpenstackVolumePopulator CustomResource based on the image name.
func (r *Builder) getVolumePopulator(name string) (populatorCr api.OpenstackVolumePopulator, err error) {
func (r *Builder) getVolumePopulatorCR(name string) (populatorCr api.OpenstackVolumePopulator, err error) {
populatorCr = api.OpenstackVolumePopulator{}
err = r.Destination.Client.Get(context.TODO(), client.ObjectKey{Namespace: r.Plan.Spec.TargetNamespace, Name: name}, &populatorCr)
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)
return
}

func (r *Builder) persistentVolumeClaimWithSourceRef(image model.Image, storageClassName string,
populatorName string, annotations map[string]string) (pvc *core.PersistentVolumeClaim, err error) {

Expand Down Expand Up @@ -1134,6 +1141,9 @@ func (r *Builder) persistentVolumeClaimWithSourceRef(image model.Image, storageC
}

err = r.Client.Create(context.TODO(), pvc, &client.CreateOptions{})
if err != nil {
err = liberr.Wrap(err)
}
return
}

Expand All @@ -1142,7 +1152,7 @@ func (r *Builder) PopulatorTransferredBytes(persistentVolumeClaim *core.Persiste
if err != nil {
return
}
populatorCr, err := r.getVolumePopulator(image.Name)
populatorCr, err := r.getVolumePopulatorCR(image.Name)
if err != nil {
return
}
Expand Down Expand Up @@ -1194,7 +1204,7 @@ func (r *Builder) SetPopulatorDataSourceLabels(vmRef ref.Ref, pvcs []core.Persis
}
migrationID := string(r.Plan.Status.Migration.ActiveSnapshot().Migration.UID)
for _, image := range images {
populatorCr, err := r.getVolumePopulator(image.Name)
populatorCr, err := r.getVolumePopulatorCR(image.Name)
if err != nil {
continue
}
Expand Down
Loading
Loading