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 616391a
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 249 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
76 changes: 39 additions & 37 deletions pkg/controller/plan/adapter/openstack/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
k8serr "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
cnv "kubevirt.io/api/core/v1"
cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
Expand Down Expand Up @@ -894,55 +895,61 @@ func (r *Builder) PopulatorVolumes(vmRef ref.Ref, annotations map[string]string,
}

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)
}

_, err = r.getVolumePopulator(image.Name)
var pvc *core.PersistentVolumeClaim
pvc, err = r.ensureVolumePopulator(workload, &image, annotations, secretName)
if err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
if !k8serr.IsAlreadyExists(err) {
err = liberr.Wrap(err, "couldn't build the PVC",
"image", image.Name)
return
}
err = nil
continue
}
pvcNames = append(pvcNames, pvc.Name)
}
return
}

func (r *Builder) ensureVolumePopulator(workload *model.Workload, image *model.Image, annotations map[string]string, secretName string) (pvc *core.PersistentVolumeClaim, err error) {
populator, err := r.getVolumePopulator(image.Name)
if err != nil {
if !k8serr.IsNotFound(err) {
err = liberr.Wrap(err)
return
}
var populatorName string
populatorName, err = r.createVolumePopulatorCR(image, secretName, vmRef.ID)
populatorName, err = r.createVolumePopulatorCR(*image, secretName, workload.ID)
if err != nil {
err = liberr.Wrap(err)
return
}
pvc, err = r.ensurePopulatorPVC(workload, image, annotations, populatorName, secretName)
return
}
err = k8serr.NewAlreadyExists(schema.ParseGroupResource("openstackvolumepopulator.forklift.konveyor.io"), populator.Name)
return
}

storageClassName := r.Context.Map.Storage.Spec.Map[0].Destination.StorageClass
volumeType := r.getVolumeType(workload, originalVolumeDiskId)
if 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)
func (r *Builder) ensurePopulatorPVC(workload *model.Workload, image *model.Image, annotations map[string]string, populatorName, secretName string) (pvc *core.PersistentVolumeClaim, err error) {
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 != "" {
storageClassName, err = r.getStorageClassName(workload, volumeType)
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
err = liberr.Wrap(err)
return
}
pvcNames = append(pvcNames, pvc.Name)
}
pvc, err = r.persistentVolumeClaimWithSourceRef(*image, storageClassName, populatorName, annotations)
return
}

Expand Down Expand Up @@ -1004,12 +1011,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
Loading

0 comments on commit 616391a

Please sign in to comment.