Skip to content

Commit

Permalink
openstack: fix cognitive complexity
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Martín <[email protected]>
  • Loading branch information
mmartinv committed Aug 3, 2023
1 parent bf680ca commit b630f54
Show file tree
Hide file tree
Showing 3 changed files with 291 additions and 251 deletions.
1 change: 1 addition & 0 deletions pkg/controller/plan/adapter/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/api/resource",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/labels",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema",
"//vendor/k8s.io/apimachinery/pkg/types",
"//vendor/kubevirt.io/api/core/v1:core",
"//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1",
Expand Down
83 changes: 44 additions & 39 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,13 +876,11 @@ func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string,
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 @@ -892,34 +890,52 @@ 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)
var populatorName string
populatorName, err = r.ensureVolumePopulator(workload, &image, secretName)
if err != nil {
return
}

_, err = r.getVolumePopulator(image.Name)
var pvc *core.PersistentVolumeClaim
pvc, err = r.ensureVolumePopulatorPVC(workload, &image, annotations, populatorName)
if err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
return
}
return
}
if pvc != nil {
pvcNames = append(pvcNames, pvc.Name)
}
}
return
}

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) (populatorName string, 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)
}
populatorName = volumePopulatorCR.Name
return
}

func (r *Builder) ensureVolumePopulatorPVC(workload *model.Workload, image *model.Image, annotations map[string]string, populatorName string) (pvc *core.PersistentVolumeClaim, err error) {
_, err = r.getVolumePopulatorPVC(image.ID)
if 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 != "" {
Expand All @@ -929,19 +945,7 @@ func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string,
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
}
pvcNames = append(pvcNames, pvc.Name)
pvc, err = r.persistentVolumeClaimWithSourceRef(*image, storageClassName, populatorName, annotations)
}
return
}
Expand Down Expand Up @@ -1004,12 +1008,7 @@ 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)
return
} else {
err = nil
}
return
}
name = populatorCR.Name
return
Expand Down Expand Up @@ -1074,12 +1073,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 @@ -1142,7 +1147,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 +1199,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

0 comments on commit b630f54

Please sign in to comment.