diff --git a/.golangci.yml b/.golangci.yml index 68a3dc994..3b8a00ab2 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -5,6 +5,7 @@ linters: - nilnil - bodyclose - gofmt + - goimports linters-settings: ginkgolinter: forbid-focus-container: true diff --git a/pkg/controller/hook/regexp.go b/pkg/controller/hook/regexp.go index 044f65d5d..dd9b18ae5 100644 --- a/pkg/controller/hook/regexp.go +++ b/pkg/controller/hook/regexp.go @@ -1,3 +1,4 @@ +//nolint:unused package hook import ( diff --git a/pkg/controller/host/handler/vsphere/handler.go b/pkg/controller/host/handler/vsphere/handler.go index 7cbf51801..26a583e94 100644 --- a/pkg/controller/host/handler/vsphere/handler.go +++ b/pkg/controller/host/handler/vsphere/handler.go @@ -1,6 +1,9 @@ package vsphere import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*vsphere.Host) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list HostMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/host/validation.go b/pkg/controller/host/validation.go index 42422b723..d1551f4c1 100644 --- a/pkg/controller/host/validation.go +++ b/pkg/controller/host/validation.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" adapter "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/vsphere" "github.com/konveyor/forklift-controller/pkg/controller/provider/web" diff --git a/pkg/controller/map/network/handler/ocp/handler.go b/pkg/controller/map/network/handler/ocp/handler.go index 31d5e488f..2b6d63f26 100644 --- a/pkg/controller/map/network/handler/ocp/handler.go +++ b/pkg/controller/map/network/handler/ocp/handler.go @@ -1,6 +1,8 @@ package ocp import ( + "path" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ocp" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,7 +10,6 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" ) @@ -70,6 +71,7 @@ func (r *Handler) changed(network *ocp.NetworkAttachmentDefinition) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list NetworkMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/network/handler/openstack/handler.go b/pkg/controller/map/network/handler/openstack/handler.go index 194908017..c34f8ab0e 100644 --- a/pkg/controller/map/network/handler/openstack/handler.go +++ b/pkg/controller/map/network/handler/openstack/handler.go @@ -80,6 +80,7 @@ func (r *Handler) changed(models ...*openstack.Network) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list NetworkMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/network/handler/ova/handler.go b/pkg/controller/map/network/handler/ova/handler.go index ab1f1f6b7..a80aad589 100644 --- a/pkg/controller/map/network/handler/ova/handler.go +++ b/pkg/controller/map/network/handler/ova/handler.go @@ -80,6 +80,7 @@ func (r *Handler) changed(models ...*ova.Network) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list NetworkMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/network/handler/ovirt/handler.go b/pkg/controller/map/network/handler/ovirt/handler.go index 3ea454f1d..17ddf85c4 100644 --- a/pkg/controller/map/network/handler/ovirt/handler.go +++ b/pkg/controller/map/network/handler/ovirt/handler.go @@ -1,6 +1,9 @@ package ovirt import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ovirt" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*ovirt.Network) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list NetworkMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/network/handler/vsphere/handler.go b/pkg/controller/map/network/handler/vsphere/handler.go index 26e81a535..7c334cb8f 100644 --- a/pkg/controller/map/network/handler/vsphere/handler.go +++ b/pkg/controller/map/network/handler/vsphere/handler.go @@ -1,6 +1,9 @@ package vsphere import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*vsphere.Network) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list NetworkMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/storage/handler/ocp/handler.go b/pkg/controller/map/storage/handler/ocp/handler.go index 247941773..557263cf3 100644 --- a/pkg/controller/map/storage/handler/ocp/handler.go +++ b/pkg/controller/map/storage/handler/ocp/handler.go @@ -1,6 +1,8 @@ package ocp import ( + "path" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ocp" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,7 +10,6 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" ) @@ -68,6 +69,7 @@ func (r *Handler) changed(storageClass *ocp.StorageClass) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list StorageMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/storage/handler/openstack/handler.go b/pkg/controller/map/storage/handler/openstack/handler.go index 352427ead..bf3758018 100644 --- a/pkg/controller/map/storage/handler/openstack/handler.go +++ b/pkg/controller/map/storage/handler/openstack/handler.go @@ -78,6 +78,7 @@ func (r *Handler) changed(models ...*openstack.VolumeType) { err := r.List(context.TODO(), storageMapList) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list StorageMap CRs") return } for _, storageMap := range storageMapList.Items { diff --git a/pkg/controller/map/storage/handler/ova/handler.go b/pkg/controller/map/storage/handler/ova/handler.go index b3358130b..88f60eaed 100644 --- a/pkg/controller/map/storage/handler/ova/handler.go +++ b/pkg/controller/map/storage/handler/ova/handler.go @@ -80,6 +80,7 @@ func (r *Handler) changed(models ...*ova.Disk) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list storage maps") return } for i := range list.Items { diff --git a/pkg/controller/map/storage/handler/ovirt/handler.go b/pkg/controller/map/storage/handler/ovirt/handler.go index 2e9b7592b..3d462d8f2 100644 --- a/pkg/controller/map/storage/handler/ovirt/handler.go +++ b/pkg/controller/map/storage/handler/ovirt/handler.go @@ -1,6 +1,9 @@ package ovirt import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ovirt" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*ovirt.StorageDomain) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list StorageMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/map/storage/handler/vsphere/handler.go b/pkg/controller/map/storage/handler/vsphere/handler.go index 8446ceb61..70bfff738 100644 --- a/pkg/controller/map/storage/handler/vsphere/handler.go +++ b/pkg/controller/map/storage/handler/vsphere/handler.go @@ -1,6 +1,9 @@ package vsphere import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*vsphere.Datastore) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list StorageMap CRs") return } for i := range list.Items { diff --git a/pkg/controller/migration/predicate.go b/pkg/controller/migration/predicate.go index 34d9ceab4..3305d23d2 100644 --- a/pkg/controller/migration/predicate.go +++ b/pkg/controller/migration/predicate.go @@ -70,11 +70,7 @@ func (r PlanPredicate) Update(e event.UpdateEvent) bool { func (r PlanPredicate) Delete(e event.DeleteEvent) bool { _, cast := e.Object.(*api.Plan) - if cast { - return true - } - - return false + return cast } func (r PlanPredicate) Generic(e event.GenericEvent) bool { diff --git a/pkg/controller/migration/validation.go b/pkg/controller/migration/validation.go index 5d86a3004..f0572743e 100644 --- a/pkg/controller/migration/validation.go +++ b/pkg/controller/migration/validation.go @@ -3,6 +3,7 @@ package migration import ( "context" "errors" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" plancnt "github.com/konveyor/forklift-controller/pkg/controller/plan" "github.com/konveyor/forklift-controller/pkg/controller/provider/web" diff --git a/pkg/controller/plan/adapter/ocp/builder.go b/pkg/controller/plan/adapter/ocp/builder.go index 136ab3db5..199812ce6 100644 --- a/pkg/controller/plan/adapter/ocp/builder.go +++ b/pkg/controller/plan/adapter/ocp/builder.go @@ -38,8 +38,7 @@ const ( type Builder struct { *plancontext.Context - macConflictsMap map[string]string - sourceClient client.Client + sourceClient client.Client } // ConfigMap implements base.Builder diff --git a/pkg/controller/plan/adapter/openstack/BUILD.bazel b/pkg/controller/plan/adapter/openstack/BUILD.bazel index 3f305c324..7530a494b 100644 --- a/pkg/controller/plan/adapter/openstack/BUILD.bazel +++ b/pkg/controller/plan/adapter/openstack/BUILD.bazel @@ -34,7 +34,7 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/labels", "//vendor/k8s.io/apimachinery/pkg/types", "//vendor/k8s.io/apimachinery/pkg/util/wait", - "//vendor/k8s.io/utils/pointer", + "//vendor/k8s.io/utils/ptr", "//vendor/kubevirt.io/api/core/v1:core", "//vendor/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1", "//vendor/sigs.k8s.io/controller-runtime/pkg/client", diff --git a/pkg/controller/plan/adapter/openstack/builder.go b/pkg/controller/plan/adapter/openstack/builder.go index f079cb8da..12843fe01 100644 --- a/pkg/controller/plan/adapter/openstack/builder.go +++ b/pkg/controller/plan/adapter/openstack/builder.go @@ -24,7 +24,7 @@ import ( "k8s.io/apimachinery/pkg/api/resource" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" - "k8s.io/utils/pointer" + "k8s.io/utils/ptr" cnv "kubevirt.io/api/core/v1" cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -539,7 +539,7 @@ func (r *Builder) mapDisks(vm *model.Workload, persistentVolumeClaims []*core.Pe } if bootable, err := strconv.ParseBool(volume.Bootable); err == nil && bootable { r.Log.Info("bootable volume found", "volumeID", volumeID) - bootOrder = pointer.Uint(1) + bootOrder = ptr.To[uint](1) } } @@ -592,7 +592,7 @@ func (r *Builder) mapDisks(vm *model.Workload, persistentVolumeClaims []*core.Pe r.Log.Info("No bootable volume found, falling back to image", "image", imagePVC.Name) for i, disk := range kDisks { if disk.Name == fmt.Sprintf("vol-%s", imagePVC.Annotations[AnnImportDiskId]) { - kDisks[i].BootOrder = pointer.Uint(1) + kDisks[i].BootOrder = ptr.To[uint](1) r.Log.Info("Boot order set to 1 on", "disk", kDisks[i], "ann", imagePVC.Annotations[AnnImportDiskId]) break } @@ -1236,6 +1236,7 @@ func (r *Builder) PopulatorTransferredBytes(persistentVolumeClaim *core.Persiste if err != nil { transferredBytes = 0 err = nil + //nolint:nilerr return } diff --git a/pkg/controller/plan/adapter/openstack/client.go b/pkg/controller/plan/adapter/openstack/client.go index b0d9540e0..865caf62a 100644 --- a/pkg/controller/plan/adapter/openstack/client.go +++ b/pkg/controller/plan/adapter/openstack/client.go @@ -582,7 +582,9 @@ func (r *Client) getImageFromVolume(vm *libclient.VM, volumeID string) (image *l volume, err := r.getVolume(volumeRef) if err != nil { err = liberr.Wrap(err) + return } + originalVolumeID := volume.Metadata[forkliftPropertyOriginalVolumeID] imageName := getImageFromVolumeName(r.Context, vm.ID, originalVolumeID) images := []libclient.Image{} diff --git a/pkg/controller/plan/adapter/openstack/common.go b/pkg/controller/plan/adapter/openstack/common.go index bce29b1a7..ffe1ce242 100644 --- a/pkg/controller/plan/adapter/openstack/common.go +++ b/pkg/controller/plan/adapter/openstack/common.go @@ -11,9 +11,6 @@ const ( forkliftPropertyOriginalImageID = "forklift_original_image_id" ) -func getMigrationName(ctx *plancontext.Context) string { - return ctx.Migration.GetName() -} func getMigrationID(ctx *plancontext.Context) string { return string(ctx.Migration.GetUID()) } diff --git a/pkg/controller/plan/adapter/ova/builder.go b/pkg/controller/plan/adapter/ova/builder.go index 768459f93..28732dd82 100644 --- a/pkg/controller/plan/adapter/ova/builder.go +++ b/pkg/controller/plan/adapter/ova/builder.go @@ -61,7 +61,7 @@ const ( // Regex which matches the snapshot identifier suffix of a // OVA disk backing file. -var backingFilePattern = regexp.MustCompile("-\\d\\d\\d\\d\\d\\d.vmdk") +var backingFilePattern = regexp.MustCompile(`-\d\d\d\d\d\d.vmdk`) // OVA builder. type Builder struct { @@ -161,8 +161,7 @@ func (r *Builder) DataVolumes(vmRef ref.Ref, secret *core.Secret, _ *core.Config return nil, err } storageClass := mapped.Destination.StorageClass - var dvSource cdi.DataVolumeSource - dvSource = cdi.DataVolumeSource{ + dvSource := cdi.DataVolumeSource{ Blank: &cdi.DataVolumeBlankImage{}, } dvSpec := cdi.DataVolumeSpec{ diff --git a/pkg/controller/plan/adapter/ova/client.go b/pkg/controller/plan/adapter/ova/client.go index 7a1df17cd..b94ef9003 100644 --- a/pkg/controller/plan/adapter/ova/client.go +++ b/pkg/controller/plan/adapter/ova/client.go @@ -96,7 +96,6 @@ func (r *Client) DetachDisks(vmRef ref.Ref) (err error) { } func (r Client) Finalize(vms []*planapi.VMStatus, planName string) { - return } func (r *Client) PreTransferActions(vmRef ref.Ref) (ready bool, err error) { diff --git a/pkg/controller/plan/adapter/ova/ova_capacity_test.go b/pkg/controller/plan/adapter/ova/ova_capacity_test.go index c173a8d82..f26c41612 100644 --- a/pkg/controller/plan/adapter/ova/ova_capacity_test.go +++ b/pkg/controller/plan/adapter/ova/ova_capacity_test.go @@ -37,7 +37,7 @@ func TestGetResourceCapacity(t *testing.T) { if testCase.expectError { g.Expect(err).To(gomega.HaveOccurred(), fmt.Sprintf("expected an error for input: %v", testCase.units)) } else { - g.Expect(err).To(gomega.BeNil(), fmt.Sprintf("did not expect an error for input: %v, but got: %v", testCase.units, err)) + g.Expect(err).ToNot(gomega.HaveOccurred(), fmt.Sprintf("did not expect an error for input: %v, but got: %v", testCase.units, err)) g.Expect(result).To(gomega.Equal(testCase.expected), fmt.Sprintf("expected %v, but got %v", testCase.expected, result)) } }) diff --git a/pkg/controller/plan/adapter/ovirt/client.go b/pkg/controller/plan/adapter/ovirt/client.go index 9f2dcf03c..5a2394ff7 100644 --- a/pkg/controller/plan/adapter/ovirt/client.go +++ b/pkg/controller/plan/adapter/ovirt/client.go @@ -370,26 +370,6 @@ func (r *Client) getDiskSnapshot(diskID, targetSnapshotID string) (diskSnapshotI return } -func (r *Client) isDiskBeingTransferred(diskID string) (bool, error) { - transfers, err := r.connection.SystemService().ImageTransfersService().List().Send() - if err != nil { - err = liberr.Wrap(err) - return false, err - } - - for _, transfer := range transfers.MustImageTransfer().Slice() { - phase, _ := transfer.Phase() - if phase != ovirtsdk.IMAGETRANSFERPHASE_FINISHED_FAILURE && phase != ovirtsdk.IMAGETRANSFERPHASE_FINISHED_SUCCESS { - transferDiskID, _ := transfer.MustImage().Id() - if transferDiskID == diskID { - return true, nil - } - } - } - - return false, nil -} - // Connect to the oVirt API. func (r *Client) connect() (err error) { URL := r.Source.Provider.Spec.URL @@ -429,8 +409,8 @@ func (r *Client) cacert() []byte { func (r Client) Finalize(vms []*planapi.VMStatus, planName string) { defer func() { - if p := recover(); p != nil { - r.Log.Info("Recovered from panic:", p) + if err := recover(); err != nil { + r.Log.Info("recovered from panic:", "err", err) } }() @@ -439,7 +419,12 @@ func (r Client) Finalize(vms []*planapi.VMStatus, planName string) { return } - r.connect() + err := r.connect() + if err != nil { + r.Log.Error(err, "Failed to connect to oVirt API") + return + } + defer r.Close() var wg sync.WaitGroup wg.Add(len(vms)) diff --git a/pkg/controller/plan/adapter/ovirt/destinationclient.go b/pkg/controller/plan/adapter/ovirt/destinationclient.go index c1b23394d..cece1578f 100644 --- a/pkg/controller/plan/adapter/ovirt/destinationclient.go +++ b/pkg/controller/plan/adapter/ovirt/destinationclient.go @@ -23,10 +23,13 @@ type DestinationClient struct { func (r *DestinationClient) DeletePopulatorDataSource(vm *plan.VMStatus) error { populatorCrList, err := r.getPopulatorCrList() if err != nil { - return err + return liberr.Wrap(err) } for _, populatorCr := range populatorCrList.Items { err = r.DeleteObject(&populatorCr, vm, "Deleted OvirtPopulator CR.", "OvirtVolumePopulator") + if err != nil { + return liberr.Wrap(err) + } } return nil } diff --git a/pkg/controller/plan/adapter/vsphere/builder.go b/pkg/controller/plan/adapter/vsphere/builder.go index 6995e92f4..850d69f75 100644 --- a/pkg/controller/plan/adapter/vsphere/builder.go +++ b/pkg/controller/plan/adapter/vsphere/builder.go @@ -122,7 +122,7 @@ var osMap = map[string]string{ // Regex which matches the snapshot identifier suffix of a // vSphere disk backing file. -var backingFilePattern = regexp.MustCompile("-\\d\\d\\d\\d\\d\\d.vmdk") +var backingFilePattern = regexp.MustCompile(`-\d\d\d\d\d\d.vmdk`) // vSphere builder. type Builder struct { diff --git a/pkg/controller/plan/adapter/vsphere/client.go b/pkg/controller/plan/adapter/vsphere/client.go index 67466d88b..842981247 100644 --- a/pkg/controller/plan/adapter/vsphere/client.go +++ b/pkg/controller/plan/adapter/vsphere/client.go @@ -221,7 +221,6 @@ func (r *Client) Close() { } func (c *Client) Finalize(vms []*planapi.VMStatus, planName string) { - return } func (r *Client) PreTransferActions(vmRef ref.Ref) (ready bool, err error) { diff --git a/pkg/controller/plan/adapter/vsphere/host.go b/pkg/controller/plan/adapter/vsphere/host.go index 1cd4133f8..5549fe702 100644 --- a/pkg/controller/plan/adapter/vsphere/host.go +++ b/pkg/controller/plan/adapter/vsphere/host.go @@ -40,27 +40,6 @@ func (r *EsxHost) TestConnection() (err error) { return } -// Translate network ID. -func (r *EsxHost) networkID(network *model.Network) (id string, err error) { - ctx := context.Background() - ctx, cancel := context.WithCancel(ctx) - defer cancel() - err = r.connect(ctx) - if err != nil { - return - } - defer r.close() - object, fErr := r.finder.Network(ctx, network.Name) - if fErr != nil { - err = liberr.Wrap(fErr) - return - } - - id = object.Reference().Value - - return -} - // Translate datastore ID. func (r *EsxHost) DatastoreID(ds *model.Datastore) (id string, err error) { ctx := context.Background() diff --git a/pkg/controller/plan/controller.go b/pkg/controller/plan/controller.go index f90adf375..743c22250 100644 --- a/pkg/controller/plan/controller.go +++ b/pkg/controller/plan/controller.go @@ -276,7 +276,11 @@ func (r *Reconciler) setPopulatorDataSourceLabels(plan *api.Plan) { } plan.Annotations[AnnPopulatorLabels] = "True" patch := client.MergeFrom(planCopy) - r.Client.Patch(context.TODO(), plan, patch) + err = r.Client.Patch(context.TODO(), plan, patch) + if err != nil { + r.Log.Error(err, "Couldn't patch plan with populator labels.") + } + // Restore Referenced data that is not returned back from the server plan.Referenced = planCopy.Referenced // Restore original status with staged conditions @@ -387,8 +391,7 @@ func (r *Reconciler) execute(plan *api.Plan) (reQ time.Duration, err error) { } // // Find pending migrations. - pending := []*api.Migration{} - pending, err = r.pendingMigrations(plan) + pending, err := r.pendingMigrations(plan) if err != nil { return } diff --git a/pkg/controller/plan/handler/ocp/handler.go b/pkg/controller/plan/handler/ocp/handler.go index f1898f221..8bff62602 100644 --- a/pkg/controller/plan/handler/ocp/handler.go +++ b/pkg/controller/plan/handler/ocp/handler.go @@ -1,6 +1,8 @@ package ocp import ( + "path" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ocp" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,7 +10,6 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" ) @@ -70,6 +71,7 @@ func (r *Handler) changed(vm *ocp.VM) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list Plan CRs") return } for i := range list.Items { diff --git a/pkg/controller/plan/handler/openstack/handler.go b/pkg/controller/plan/handler/openstack/handler.go index 5da4d50c8..e5bca4818 100644 --- a/pkg/controller/plan/handler/openstack/handler.go +++ b/pkg/controller/plan/handler/openstack/handler.go @@ -80,6 +80,7 @@ func (r *Handler) changed(models ...*openstack.VM) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list Plan CRs") return } for i := range list.Items { diff --git a/pkg/controller/plan/handler/ova/handler.go b/pkg/controller/plan/handler/ova/handler.go index bc113911a..f78a950b1 100644 --- a/pkg/controller/plan/handler/ova/handler.go +++ b/pkg/controller/plan/handler/ova/handler.go @@ -80,6 +80,7 @@ func (r *Handler) changed(models ...*ova.VM) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list Plan CRs") return } for i := range list.Items { diff --git a/pkg/controller/plan/handler/ovirt/handler.go b/pkg/controller/plan/handler/ovirt/handler.go index 67d7338f5..a4e69c428 100644 --- a/pkg/controller/plan/handler/ovirt/handler.go +++ b/pkg/controller/plan/handler/ovirt/handler.go @@ -1,6 +1,9 @@ package ovirt import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/ovirt" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*ovirt.VM) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "List Plan CRs failed.") return } for i := range list.Items { diff --git a/pkg/controller/plan/handler/vsphere/handler.go b/pkg/controller/plan/handler/vsphere/handler.go index cd9e93e27..028513e93 100644 --- a/pkg/controller/plan/handler/vsphere/handler.go +++ b/pkg/controller/plan/handler/vsphere/handler.go @@ -1,6 +1,9 @@ package vsphere import ( + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" "github.com/konveyor/forklift-controller/pkg/controller/watch/handler" @@ -8,9 +11,7 @@ import ( libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" "github.com/konveyor/forklift-controller/pkg/lib/logging" "golang.org/x/net/context" - "path" "sigs.k8s.io/controller-runtime/pkg/event" - "strings" ) // Package logger. @@ -79,6 +80,7 @@ func (r *Handler) changed(models ...*vsphere.VM) { err := r.List(context.TODO(), &list) if err != nil { err = liberr.Wrap(err) + log.Error(err, "failed to list plans") return } for i := range list.Items { diff --git a/pkg/controller/plan/hook.go b/pkg/controller/plan/hook.go index cec6ed637..296b6e5b9 100644 --- a/pkg/controller/plan/hook.go +++ b/pkg/controller/plan/hook.go @@ -3,6 +3,9 @@ package plan import ( "context" "encoding/base64" + "path" + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" planapi "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan" plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context" @@ -14,10 +17,8 @@ import ( meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/kubernetes/scheme" - "path" "sigs.k8s.io/controller-runtime/pkg/client" k8sutil "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "strings" ) // Labels diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index 4d10a61a3..4e37a8191 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -1565,7 +1565,7 @@ func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.Confi } for i, v := range vmVolumes { - pvc, _ := pvcsByName[v.PersistentVolumeClaim.ClaimName] + pvc := pvcsByName[v.PersistentVolumeClaim.ClaimName] vol := core.Volume{ Name: pvc.Name, VolumeSource: core.VolumeSource{ diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 5e221c5be..f08288026 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -364,7 +364,6 @@ func (r *Migration) Archive() { } _ = r.cleanup(vm, dontFailOnError) } - return } func (r *Migration) SetPopulatorDataSourceLabels() { @@ -1427,7 +1426,7 @@ func (r *Migration) updateCopyProgress(vm *plan.VMStatus, step *plan.Step) (err if err != nil { return } - if dvs == nil || len(dvs) == 0 { + if len(dvs) == 0 { pvcs, err = r.kubevirt.getPVCs(vm.Ref) if err != nil { return diff --git a/pkg/controller/plan/predicate.go b/pkg/controller/plan/predicate.go index 44a588e8d..0b49e62cd 100644 --- a/pkg/controller/plan/predicate.go +++ b/pkg/controller/plan/predicate.go @@ -146,11 +146,7 @@ func (r NetMapPredicate) Update(e event.UpdateEvent) bool { func (r NetMapPredicate) Delete(e event.DeleteEvent) bool { _, cast := e.Object.(*api.NetworkMap) - if cast { - return true - } - - return false + return cast } func (r NetMapPredicate) Generic(e event.GenericEvent) bool { @@ -183,11 +179,7 @@ func (r DsMapPredicate) Update(e event.UpdateEvent) bool { func (r DsMapPredicate) Delete(e event.DeleteEvent) bool { _, cast := e.Object.(*api.StorageMap) - if cast { - return true - } - - return false + return cast } func (r DsMapPredicate) Generic(e event.GenericEvent) bool { @@ -220,11 +212,7 @@ func (r HookPredicate) Update(e event.UpdateEvent) bool { func (r HookPredicate) Delete(e event.DeleteEvent) bool { _, cast := e.Object.(*api.Hook) - if cast { - return true - } - - return false + return cast } func (r HookPredicate) Generic(e event.GenericEvent) bool { diff --git a/pkg/controller/plan/scheduler/doc.go b/pkg/controller/plan/scheduler/doc.go index 737b422de..bf34e2b8a 100644 --- a/pkg/controller/plan/scheduler/doc.go +++ b/pkg/controller/plan/scheduler/doc.go @@ -49,7 +49,7 @@ func New(ctx *plancontext.Context) (scheduler Scheduler, err error) { MaxInFlight: settings.Settings.MaxInFlight, } default: - liberr.New("provider not supported.") + err = liberr.New("provider not supported.") } return diff --git a/pkg/controller/plan/scheduler/vsphere/scheduler.go b/pkg/controller/plan/scheduler/vsphere/scheduler.go index 1c2abc3e0..100130c90 100644 --- a/pkg/controller/plan/scheduler/vsphere/scheduler.go +++ b/pkg/controller/plan/scheduler/vsphere/scheduler.go @@ -3,9 +3,10 @@ package vsphere import ( "context" "errors" - "github.com/konveyor/forklift-controller/pkg/controller/provider/web" "sync" + "github.com/konveyor/forklift-controller/pkg/controller/provider/web" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan" plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context" diff --git a/pkg/controller/plan/scheduler/vsphere/scheduler_test.go b/pkg/controller/plan/scheduler/vsphere/scheduler_test.go index 4f09de7b3..ffeef6fdc 100644 --- a/pkg/controller/plan/scheduler/vsphere/scheduler_test.go +++ b/pkg/controller/plan/scheduler/vsphere/scheduler_test.go @@ -1,8 +1,9 @@ package vsphere import ( - "github.com/onsi/gomega" "testing" + + "github.com/onsi/gomega" ) func TestScheduler(t *testing.T) { diff --git a/pkg/controller/provider/container/ova/resource.go b/pkg/controller/provider/container/ova/resource.go index 122133088..45d310a8e 100644 --- a/pkg/controller/provider/container/ova/resource.go +++ b/pkg/controller/provider/container/ova/resource.go @@ -1,8 +1,6 @@ package ova import ( - "strconv" - model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/ova" ) @@ -12,22 +10,6 @@ type Base struct { Description string `json:"Description"` } -func (b *Base) bool(s string) (v bool) { - v, _ = strconv.ParseBool(s) - return -} - -func (b *Base) int32(s string) (v int32) { - n, _ := strconv.ParseInt(s, 10, 32) - v = int32(n) - return -} - -func (b *Base) int64(s string) (v int64) { - v, _ = strconv.ParseInt(s, 10, 64) - return -} - // VM. type VM struct { Name string `json:"Name"` diff --git a/pkg/controller/provider/container/ovirt/collector.go b/pkg/controller/provider/container/ovirt/collector.go index 89fd0d5da..e6c66ad9b 100644 --- a/pkg/controller/provider/container/ovirt/collector.go +++ b/pkg/controller/provider/container/ovirt/collector.go @@ -427,20 +427,30 @@ func (r *Collector) refresh(ctx *Context) (err error) { "event", event) var changeSet []Updater - changeSet, applyErr := r.changeSet(ctx, event) - if applyErr == nil { - applyErr = r.apply(changeSet) - r.log.V(3).Info( - "Event applied.", + changeSet, changeSetErr := r.changeSet(ctx, event) + if changeSetErr == nil { + r.log.Error( + changeSetErr, + "Apply event failed.", "event", event) - } else { + continue + } + err = r.apply(changeSet) + if err != nil { r.log.Error( - applyErr, - "Apply event failed.", + err, + "Apply changeSet failed.", "event", event) + + continue } + + r.log.V(3).Info( + "Event applied.", + "event", + event) } return @@ -483,7 +493,7 @@ func (r *Collector) apply(changeSet []Updater) (err error) { func (r *Collector) listEvent() (list []Event, err error) { eventList := EventList{} codes := []string{} - for n, _ := range adapterMap { + for n := range adapterMap { codes = append(codes, fmt.Sprintf("type=%d", n)) } search := strings.Join(codes, " or ") diff --git a/pkg/controller/provider/container/vsphere/collector.go b/pkg/controller/provider/container/vsphere/collector.go index f34287db6..f7077d7c7 100644 --- a/pkg/controller/provider/container/vsphere/collector.go +++ b/pkg/controller/provider/container/vsphere/collector.go @@ -383,7 +383,13 @@ func (r *Collector) getUpdates(ctx context.Context) error { response, err := methods.WaitForUpdatesEx(ctx, r.client, &req) if err != nil { if ctx.Err() == context.Canceled { - pc.CancelWaitForUpdates(context.Background()) + err = pc.CancelWaitForUpdates(context.Background()) + if err != nil { + r.log.Error( + err, + "cancel wait for updates failed.") + } + break } return liberr.Wrap(err) diff --git a/pkg/controller/provider/container/vsphere/model.go b/pkg/controller/provider/container/vsphere/model.go index 394355b90..bcead2414 100644 --- a/pkg/controller/provider/container/vsphere/model.go +++ b/pkg/controller/provider/container/vsphere/model.go @@ -1,12 +1,13 @@ package vsphere import ( - model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere" - libref "github.com/konveyor/forklift-controller/pkg/lib/ref" - "github.com/vmware/govmomi/vim25/types" "net/url" "sort" "strings" + + model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere" + libref "github.com/konveyor/forklift-controller/pkg/lib/ref" + "github.com/vmware/govmomi/vim25/types" ) // Model adapter. diff --git a/pkg/controller/provider/model/base/model.go b/pkg/controller/provider/model/base/model.go index 7e90a85cc..cccc94fb7 100644 --- a/pkg/controller/provider/model/base/model.go +++ b/pkg/controller/provider/model/base/model.go @@ -2,6 +2,7 @@ package base import ( "fmt" + libmodel "github.com/konveyor/forklift-controller/pkg/lib/inventory/model" ) diff --git a/pkg/controller/provider/validation.go b/pkg/controller/provider/validation.go index 351606871..31a4e3844 100644 --- a/pkg/controller/provider/validation.go +++ b/pkg/controller/provider/validation.go @@ -153,7 +153,7 @@ func (r *Reconciler) validateURL(provider *api.Provider) error { Status: True, Reason: Malformed, Category: Critical, - Message: fmt.Sprintf("The NFS path is malformed"), + Message: "The NFS path is malformed", }) } return nil diff --git a/pkg/controller/provider/web/base/tree.go b/pkg/controller/provider/web/base/tree.go index 778b6cc8a..9ccd8e4fc 100644 --- a/pkg/controller/provider/web/base/tree.go +++ b/pkg/controller/provider/web/base/tree.go @@ -1,10 +1,11 @@ package base import ( + "time" + model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/base" libmodel "github.com/konveyor/forklift-controller/pkg/lib/inventory/model" "github.com/konveyor/forklift-controller/pkg/lib/logging" - "time" ) var log = logging.WithName("web|tree") diff --git a/pkg/controller/provider/web/ocp/client.go b/pkg/controller/provider/web/ocp/client.go index be1afda4c..285b9ca92 100644 --- a/pkg/controller/provider/web/ocp/client.go +++ b/pkg/controller/provider/web/ocp/client.go @@ -22,29 +22,24 @@ type Resolver struct { // Resolve the URL path. func (r *Resolver) Path(object interface{}, id string) (path string, err error) { provider := r.Provider - switch object.(type) { + switch r := object.(type) { case *Provider: - r := Provider{} r.UID = id r.Link() path = r.SelfLink case *Namespace: - r := Namespace{} r.UID = id r.Link(provider) path = r.SelfLink case *StorageClass: - r := StorageClass{} r.UID = id r.Link(provider) path = r.SelfLink case *NetworkAttachmentDefinition: - r := NetworkAttachmentDefinition{} r.UID = id r.Link(provider) path = r.SelfLink case *VM: - r := VM{} r.UID = id r.Link(provider) path = r.SelfLink @@ -79,7 +74,7 @@ func (r *Finder) With(client base.Client) base.Finder { // NotFoundErr // RefNotUniqueErr func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { - switch resource.(type) { + switch res := resource.(type) { case *NetworkAttachmentDefinition: id := ref.ID if id != "" { @@ -116,7 +111,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*NetworkAttachmentDefinition) = list[0] + *res = list[0] } case *StorageClass: id := ref.ID @@ -148,7 +143,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*StorageClass) = list[0] + *res = list[0] } case *VM: id := ref.ID @@ -194,7 +189,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*VM) = list[0] + *res = list[0] } } diff --git a/pkg/controller/provider/web/openstack/base.go b/pkg/controller/provider/web/openstack/base.go index 401415ba5..a66ec9271 100644 --- a/pkg/controller/provider/web/openstack/base.go +++ b/pkg/controller/provider/web/openstack/base.go @@ -3,12 +3,13 @@ package openstack import ( "strings" + pathlib "path" + "github.com/gin-gonic/gin" model "github.com/konveyor/forklift-controller/pkg/controller/provider/model/openstack" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/base" libmodel "github.com/konveyor/forklift-controller/pkg/lib/inventory/model" "github.com/konveyor/forklift-controller/pkg/lib/logging" - pathlib "path" ) // Package logger. @@ -65,9 +66,9 @@ func (r *PathBuilder) Path(m model.Model) (path string) { if r.cache == nil { r.cache = map[string]interface{}{} } - switch m.(type) { + switch m := m.(type) { case *model.Project: - project := m.(*model.Project) + project := m path = project.Name if project.IsDomain { return @@ -79,7 +80,7 @@ func (r *PathBuilder) Path(m model.Model) (path string) { } case *model.VM: - vm := m.(*model.VM) + vm := m var project *model.Project project, err = r.getProject(vm.TenantID) if err != nil { diff --git a/pkg/controller/provider/web/openstack/client.go b/pkg/controller/provider/web/openstack/client.go index cbe9770a4..c20f35dbb 100644 --- a/pkg/controller/provider/web/openstack/client.go +++ b/pkg/controller/provider/web/openstack/client.go @@ -21,57 +21,46 @@ type Resolver struct { // Build the URL path. func (r *Resolver) Path(resource interface{}, id string) (path string, err error) { provider := r.Provider - switch resource.(type) { + switch r := resource.(type) { case *Provider: - r := Provider{} r.UID = id r.Link() path = r.SelfLink case *Region: - r := Region{} r.ID = id r.Link(provider) path = r.SelfLink case *Project: - r := Project{} r.ID = id r.Link(provider) case *Image: - r := Image{} r.ID = id r.Link(provider) path = r.SelfLink case *Flavor: - r := Flavor{} r.ID = id r.Link(provider) case *VM: - r := VM{} r.ID = id r.Link(provider) path = r.SelfLink case *Snapshot: - r := Snapshot{} r.ID = id r.Link(provider) path = r.SelfLink case *Volume: - r := Volume{} r.ID = id r.Link(provider) path = r.SelfLink case *VolumeType: - r := VolumeType{} r.ID = id r.Link(provider) path = r.SelfLink case *Network: - r := Network{} r.ID = id r.Link(provider) path = r.SelfLink case *Workload: - r := Workload{} r.ID = id r.Link(provider) path = r.SelfLink @@ -107,7 +96,7 @@ func (r *Finder) With(client base.Client) base.Finder { // NotFoundErr // RefNotUniqueErr func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { - switch resource.(type) { + switch res := resource.(type) { case *VM: id := ref.ID if id != "" { @@ -138,7 +127,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*VM) = list[0] + *res = list[0] } case *Workload: id := ref.ID @@ -170,7 +159,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Workload) = list[0] + *res = list[0] } case *Network: id := ref.ID @@ -202,7 +191,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Network) = list[0] + *res = list[0] } case *VolumeType: id := ref.ID @@ -234,7 +223,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*VolumeType) = list[0] + *res = list[0] } case *Snapshot: id := ref.ID @@ -266,7 +255,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Snapshot) = list[0] + *res = list[0] } case *Volume: id := ref.ID @@ -298,7 +287,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Volume) = list[0] + *res = list[0] } case *Image: id := ref.ID @@ -330,7 +319,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Image) = list[0] + *res = list[0] } default: err = liberr.Wrap( diff --git a/pkg/controller/provider/web/openstack/flavor.go b/pkg/controller/provider/web/openstack/flavor.go index cfc85c3b8..7a3f14f3c 100644 --- a/pkg/controller/provider/web/openstack/flavor.go +++ b/pkg/controller/provider/web/openstack/flavor.go @@ -191,7 +191,7 @@ func (r *Flavor) With(m *model.Flavor) { r.Description = m.Description r.Disk = m.Disk r.RAM = m.RAM - r.RxTxFactor, _ = strconv.ParseFloat(m.RxTxFactor, 8) + r.RxTxFactor, _ = strconv.ParseFloat(m.RxTxFactor, 64) r.Swap = m.Swap r.VCPUs = m.VCPUs r.IsPublic = m.IsPublic diff --git a/pkg/controller/provider/web/openstack/network.go b/pkg/controller/provider/web/openstack/network.go index 5f5fbc61e..fcf8c4c80 100644 --- a/pkg/controller/provider/web/openstack/network.go +++ b/pkg/controller/provider/web/openstack/network.go @@ -184,7 +184,7 @@ type Network struct { ProjectID string `json:"projectID"` Shared bool `json:"shared"` AvailabilityZoneHints []string `json:"availabilityZoneHints,omitempty"` - Tags []string `json:"tags,omitEmpty"` + Tags []string `json:"tags,omitempty"` RevisionNumber int `json:"revisionNumber"` } diff --git a/pkg/controller/provider/web/openstack/tree.go b/pkg/controller/provider/web/openstack/tree.go index 937c03eba..0ad4b5141 100644 --- a/pkg/controller/provider/web/openstack/tree.go +++ b/pkg/controller/provider/web/openstack/tree.go @@ -125,9 +125,9 @@ type BranchNavigator struct { // Next (children) on the branch. func (n *BranchNavigator) Next(p libmodel.Model) (r []model.Model, err error) { - switch p.(type) { + switch p := p.(type) { case *model.Project: - m := p.(*model.Project) + m := p vmList, nErr := n.listVM(m) if nErr == nil { for i := range vmList { diff --git a/pkg/controller/provider/web/ova/base.go b/pkg/controller/provider/web/ova/base.go index 6983a9a00..6bafe8a4a 100644 --- a/pkg/controller/provider/web/ova/base.go +++ b/pkg/controller/provider/web/ova/base.go @@ -65,19 +65,15 @@ func (r *PathBuilder) Path(m model.Model) (path string) { if r.cache == nil { r.cache = map[string]string{} } - switch m.(type) { + switch m := m.(type) { case *model.VM: - vm := m.(*model.VM) - path = pathlib.Join(vm.UUID) + path = pathlib.Join(m.UUID) case *model.Network: - net := m.(*model.Network) - path = pathlib.Join(net.ID) + path = pathlib.Join(m.ID) case *model.Disk: - disk := m.(*model.Disk) - path = pathlib.Join(disk.ID) + path = pathlib.Join(m.ID) case *model.Storage: - storage := m.(*model.Storage) - path = pathlib.Join(storage.ID) + path = pathlib.Join(m.ID) } if err != nil { diff --git a/pkg/controller/provider/web/ova/client.go b/pkg/controller/provider/web/ova/client.go index d52bb0a59..0756dd52a 100644 --- a/pkg/controller/provider/web/ova/client.go +++ b/pkg/controller/provider/web/ova/client.go @@ -21,42 +21,37 @@ type Resolver struct { // Build the URL path. func (r *Resolver) Path(resource interface{}, id string) (path string, err error) { provider := r.Provider - switch resource.(type) { + switch res := resource.(type) { case *Provider: - r := Provider{} - r.UID = id - r.Link() - path = r.SelfLink + res.UID = id + res.Link() + path = res.SelfLink case *Network: - r := Network{} - r.ID = id - r.Link(provider) - path = r.SelfLink + res.ID = id + res.Link(provider) + path = res.SelfLink case *VM: - r := VM{} - r.ID = id - r.Link(provider) - path = r.SelfLink + res.ID = id + res.Link(provider) + path = res.SelfLink case *Disk: - r := Disk{} - r.ID = id - r.Link(provider) - path = r.SelfLink + res.ID = id + res.Link(provider) + path = res.SelfLink case *Workload: - r := Workload{} - r.ID = id - r.Link(provider) - path = r.SelfLink + res.ID = id + res.Link(provider) + path = res.SelfLink case *Storage: - r := Storage{} - r.ID = id - r.Link(provider) - path = r.SelfLink + res.ID = id + res.Link(provider) + path = res.SelfLink default: err = liberr.Wrap( base.ResourceNotResolvedError{ Object: resource, - }) + }, + ) } path = strings.TrimRight(path, "/") @@ -83,7 +78,7 @@ func (r *Finder) With(client base.Client) base.Finder { // NotFoundErr // RefNotUniqueErr func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { - switch resource.(type) { + switch res := resource.(type) { case *Network: id := ref.ID if id != "" { @@ -114,7 +109,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Network) = list[0] + *res = list[0] } case *VM: id := ref.ID @@ -146,7 +141,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*VM) = list[0] + *res = list[0] } case *Disk: id := ref.ID @@ -178,7 +173,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Disk) = list[0] + *res = list[0] } case *Workload: id := ref.ID @@ -210,7 +205,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Workload) = list[0] + *res = list[0] } case *Storage: id := ref.ID @@ -242,7 +237,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Storage) = list[0] + *res = list[0] } default: err = liberr.Wrap( diff --git a/pkg/controller/provider/web/ovirt/datacenter.go b/pkg/controller/provider/web/ovirt/datacenter.go index 730bf9f03..12a3ce12a 100644 --- a/pkg/controller/provider/web/ovirt/datacenter.go +++ b/pkg/controller/provider/web/ovirt/datacenter.go @@ -22,8 +22,6 @@ const ( // DataCenter handler. type DataCenterHandler struct { Handler - // Selected DataCenter. - datacenter *model.DataCenter } // Add routes to the `gin` router. diff --git a/pkg/controller/provider/web/ovirt/tree.go b/pkg/controller/provider/web/ovirt/tree.go index fac9bec37..d7ca20703 100644 --- a/pkg/controller/provider/web/ovirt/tree.go +++ b/pkg/controller/provider/web/ovirt/tree.go @@ -125,9 +125,9 @@ type BranchNavigator struct { // Next (children) on the branch. func (n *BranchNavigator) Next(p libmodel.Model) (r []model.Model, err error) { - switch p.(type) { + switch p := p.(type) { case *model.DataCenter: - list, nErr := n.listCluster(p.(*model.DataCenter)) + list, nErr := n.listCluster(p) if nErr == nil { for i := range list { m := &list[i] @@ -137,7 +137,7 @@ func (n *BranchNavigator) Next(p libmodel.Model) (r []model.Model, err error) { err = nErr } case *model.Cluster: - m := p.(*model.Cluster) + m := p hostList, nErr := n.listHost(m) if nErr == nil { for i := range hostList { diff --git a/pkg/controller/provider/web/vsphere/client.go b/pkg/controller/provider/web/vsphere/client.go index 7c5a748d6..049fcf52d 100644 --- a/pkg/controller/provider/web/vsphere/client.go +++ b/pkg/controller/provider/web/vsphere/client.go @@ -1,10 +1,11 @@ package vsphere import ( + "strings" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web/base" liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - "strings" ) // Errors. @@ -97,7 +98,7 @@ func (r *Finder) With(client base.Client) base.Finder { // NotFoundErr // RefNotUniqueErr func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { - switch resource.(type) { + switch res := resource.(type) { case *Network: id := ref.ID if id != "" { @@ -128,7 +129,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Network) = list[0] + *res = list[0] } case *Datastore: id := ref.ID @@ -160,7 +161,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Datastore) = list[0] + *res = list[0] } case *Host: id := ref.ID @@ -192,7 +193,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Host) = list[0] + *res = list[0] } case *VM: id := ref.ID @@ -224,7 +225,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*VM) = list[0] + *res = list[0] } case *Workload: id := ref.ID @@ -256,7 +257,7 @@ func (r *Finder) ByRef(resource interface{}, ref base.Ref) (err error) { err = liberr.Wrap(RefNotUniqueError{Ref: ref}) break } - *resource.(*Workload) = list[0] + *res = list[0] } default: err = liberr.Wrap( diff --git a/pkg/controller/provider/web/vsphere/folder.go b/pkg/controller/provider/web/vsphere/folder.go index 9e1fac69f..2ab3dec8a 100644 --- a/pkg/controller/provider/web/vsphere/folder.go +++ b/pkg/controller/provider/web/vsphere/folder.go @@ -22,8 +22,6 @@ const ( // Folder handler. type FolderHandler struct { Handler - // Selected folder. - folder *model.Folder } // Add routes to the `gin` router. diff --git a/pkg/controller/provider/web/vsphere/host.go b/pkg/controller/provider/web/vsphere/host.go index 618a41e2d..a8ad6232d 100644 --- a/pkg/controller/provider/web/vsphere/host.go +++ b/pkg/controller/provider/web/vsphere/host.go @@ -331,8 +331,6 @@ func (r AdapterBuilder) withPG(host *Host, vNIC *model.VNIC, adapter *NetworkAda break } } - - return } // Build with distributed virtual Switch & PortGroup. diff --git a/pkg/controller/provider/web/vsphere/tree.go b/pkg/controller/provider/web/vsphere/tree.go index 5c84cc36e..968665449 100644 --- a/pkg/controller/provider/web/vsphere/tree.go +++ b/pkg/controller/provider/web/vsphere/tree.go @@ -210,11 +210,11 @@ type HostNavigator struct { // Next (children) on the branch. func (n *HostNavigator) Next(p libmodel.Model) (r []libmodel.Model, err error) { - switch p.(type) { + switch p := p.(type) { case *model.Datacenter: m := &model.Folder{ Base: model.Base{ - ID: p.(*model.Datacenter).Clusters.ID, + ID: p.Clusters.ID, }, } err = n.db.Get(m) @@ -300,10 +300,10 @@ type VMNavigator struct { // Next (children) on the branch. func (n *VMNavigator) Next(p libmodel.Model) (r []libmodel.Model, err error) { - switch p.(type) { + switch p := p.(type) { case *model.Datacenter: m := &model.Folder{ - Base: model.Base{ID: p.(*model.Datacenter).Clusters.ID}, + Base: model.Base{ID: p.Clusters.ID}, } err = n.db.Get(m) if err == nil { diff --git a/pkg/controller/validation/policy/client.go b/pkg/controller/validation/policy/client.go index 25c8c57ea..7d9a1218c 100644 --- a/pkg/controller/validation/policy/client.go +++ b/pkg/controller/validation/policy/client.go @@ -266,7 +266,9 @@ func (r *Task) String() string { // Notify result handler the task has completed. func (r *Task) notify() { func() { - recover() + if err := recover(); err != nil { + log.Info("recovered from panic: ", "err", err) + } }() if !r.canceled() { r.Result <- r diff --git a/pkg/controller/watch/handler/handler.go b/pkg/controller/watch/handler/handler.go index d117c2a5b..703e0a48f 100644 --- a/pkg/controller/watch/handler/handler.go +++ b/pkg/controller/watch/handler/handler.go @@ -46,7 +46,9 @@ func (r *Handler) Inventory() web.Client { // Enqueue reconcile request. func (r *Handler) Enqueue(event event.GenericEvent) { defer func() { - recover() + if err := recover(); err != nil { + log.Info("recovered from panic: ", "err", err) + } }() r.channel <- event } diff --git a/pkg/controller/watch/handler/watch.go b/pkg/controller/watch/handler/watch.go index fc4e22092..6e1233dd1 100644 --- a/pkg/controller/watch/handler/watch.go +++ b/pkg/controller/watch/handler/watch.go @@ -1,12 +1,13 @@ package handler import ( + "sync" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/provider/web" libweb "github.com/konveyor/forklift-controller/pkg/lib/inventory/web" libref "github.com/konveyor/forklift-controller/pkg/lib/ref" "k8s.io/apimachinery/pkg/types" - "sync" ) // Watch map keyed by resource kind. diff --git a/pkg/forklift-api/webhooks/mutating-webhook/mutating-webhook.go b/pkg/forklift-api/webhooks/mutating-webhook/mutating-webhook.go index 0cfe73ba4..54ae201c7 100644 --- a/pkg/forklift-api/webhooks/mutating-webhook/mutating-webhook.go +++ b/pkg/forklift-api/webhooks/mutating-webhook/mutating-webhook.go @@ -51,5 +51,4 @@ func Serve(resp http.ResponseWriter, req *http.Request, m mutator) { log.Error(err, "mutating Serve error, failed to write response") resp.WriteHeader(http.StatusBadRequest) } - return } diff --git a/pkg/forklift-api/webhooks/mutating-webhook/mutators/secret-mutator.go b/pkg/forklift-api/webhooks/mutating-webhook/mutators/secret-mutator.go index 559307be9..ac9777f97 100644 --- a/pkg/forklift-api/webhooks/mutating-webhook/mutators/secret-mutator.go +++ b/pkg/forklift-api/webhooks/mutating-webhook/mutators/secret-mutator.go @@ -98,6 +98,7 @@ func (mutator *SecretMutator) mutateProviderSecret() *admissionv1.AdmissionRespo log.Error(err, "mutating webhook error, failed to send request for CA certificate retrieval") return util.ToAdmissionResponseError(err) } + defer response.Body.Close() cert, err := io.ReadAll(response.Body) if err != nil { diff --git a/pkg/forklift-api/webhooks/validating-webhook/BUILD.bazel b/pkg/forklift-api/webhooks/validating-webhook/BUILD.bazel index 5f206e1f2..dc17d5010 100644 --- a/pkg/forklift-api/webhooks/validating-webhook/BUILD.bazel +++ b/pkg/forklift-api/webhooks/validating-webhook/BUILD.bazel @@ -7,7 +7,6 @@ go_library( visibility = ["//visibility:public"], deps = [ "//pkg/forklift-api/webhooks/util", - "//pkg/lib/logging", "//vendor/k8s.io/api/admission/v1beta1", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta", "//vendor/k8s.io/apimachinery/pkg/runtime", diff --git a/pkg/forklift-api/webhooks/validating-webhook/validating-webhook.go b/pkg/forklift-api/webhooks/validating-webhook/validating-webhook.go index c154cc3f7..59e91aae7 100644 --- a/pkg/forklift-api/webhooks/validating-webhook/validating-webhook.go +++ b/pkg/forklift-api/webhooks/validating-webhook/validating-webhook.go @@ -10,11 +10,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "github.com/konveyor/forklift-controller/pkg/forklift-api/webhooks/util" - "github.com/konveyor/forklift-controller/pkg/lib/logging" ) -var log = logging.WithName("validating_webhooks") - type Admitter interface { Admit(*admissionv1.AdmissionReview) *admissionv1.AdmissionResponse } @@ -82,14 +79,11 @@ func Serve(resp http.ResponseWriter, req *http.Request, admitter Admitter) { responseBytes, err := json.Marshal(response) if err != nil { - //log.Log.Reason(err).Errorf("failed json encode webhook response") resp.WriteHeader(http.StatusBadRequest) return } if _, err := resp.Write(responseBytes); err != nil { - //log.Log.Reason(err).Errorf("failed to write webhook response") resp.WriteHeader(http.StatusBadRequest) return } - return } diff --git a/pkg/lib/cmd/inventory/main.go b/pkg/lib/cmd/inventory/main.go index 045950ec3..4c95e1ae5 100644 --- a/pkg/lib/cmd/inventory/main.go +++ b/pkg/lib/cmd/inventory/main.go @@ -69,13 +69,13 @@ func (h *EventHandler) Started(wid uint64) { h.wid = wid h.started = true - fmt.Printf("[%d] Event (started)\n", wid) + log.Info(fmt.Sprintf("[%d] Event (started)\n", wid)) } func (h *EventHandler) Parity() { h.parity = true - fmt.Printf("[%d] Event (parity)\n", h.wid) + log.Info(fmt.Sprintf("[%d] Event (parity)\n", h.wid)) } func (h *EventHandler) Created(e web.Event) { @@ -83,7 +83,7 @@ func (h *EventHandler) Created(e web.Event) { h.created = append(h.created, object.ID) } - fmt.Printf("[%d] Event (created): %v\n", h.wid, e) + log.Info(fmt.Sprintf("[%d] Event (created): %v\n", h.wid, e)) } func (h *EventHandler) Updated(e web.Event) { @@ -91,27 +91,27 @@ func (h *EventHandler) Updated(e web.Event) { h.updated = append(h.updated, object.ID) } - fmt.Printf("[%d] Event (updated): %v\n", h.wid, e) + log.Info(fmt.Sprintf("[%d] Event (updated): %v\n", h.wid, e)) } func (h *EventHandler) Deleted(e web.Event) { if object, cast := e.Resource.(*Model); cast { h.deleted = append(h.deleted, object.ID) } - fmt.Printf("[%d] Event (deleted): %v\n", h.wid, e) + log.Info(fmt.Sprintf("[%d] Event (deleted): %v\n", h.wid, e)) } func (h *EventHandler) Error(w *web.Watch, err error) { h.err = append(h.err, err) _ = w.Repair() - fmt.Printf("[%d] Event (error): %v\n", h.wid, err) + log.Info(fmt.Sprintf("[%d] Event (error): %v\n", h.wid, err)) } func (h *EventHandler) End() { h.done = true - fmt.Printf("[%d] Event (end)\n", h.wid) + log.Info(fmt.Sprintf("[%d] Event (end)\n", h.wid)) } type Endpoint struct { @@ -258,10 +258,10 @@ func list(client *web.Client) { panic(liberr.New(http.StatusText(status))) } - fmt.Println("List") - fmt.Println("___________________________") + log.Info("List") + log.Info("___________________________") for _, m := range list { - fmt.Println(m) + log.Info("model: ", m) } } @@ -275,7 +275,7 @@ func get(client *web.Client) { panic(liberr.New(http.StatusText(status))) } - fmt.Printf("\nGet: %v\n", m) + log.Info("Get: ", "m", m) } func watch(client *web.Client, snapshot bool) (watch *web.Watch) { @@ -294,13 +294,13 @@ func watch(client *web.Client, snapshot bool) (watch *web.Watch) { panic(liberr.New(http.StatusText(status))) } - fmt.Printf("\nWatch started: %d (snapshot=%v)\n", watch.ID(), snapshot) + log.Info(fmt.Sprintf("Watch started: %d (snapshot=%v)", watch.ID(), snapshot)) return } func endWatch(w *web.Watch) { - fmt.Printf("\nEnd watch: %d\n", w.ID()) + log.Info(fmt.Sprintf("nEnd watch: %d", w.ID())) w.End() wait(500) } @@ -331,7 +331,7 @@ func testB(client *web.Client, n int) { func testC(client *web.Client, n int) { for i := 0; i < n; i++ { w := watch(client, true) - fmt.Println(w.ID()) + log.Info("Watch ID", "ID", w.ID()) wait(500) w = nil runtime.GC() @@ -362,14 +362,14 @@ func testD(db model.DB, client *web.Client) { func testE(db model.DB, client *web.Client) { w := watch(client, true) wait(100) - fmt.Println(w.ID()) + log.Info("Watch ID", "ID", w.ID()) _ = db.Close(false) } // Main. func main() { db, _ := setup() - fmt.Println(db) + log.Info("DB", "db", db) client := &web.Client{ Transport: http.DefaultTransport, } diff --git a/pkg/lib/condition/condition.go b/pkg/lib/condition/condition.go index 3d84d820e..0d5f4b59f 100644 --- a/pkg/lib/condition/condition.go +++ b/pkg/lib/condition/condition.go @@ -1,9 +1,10 @@ package condition import ( - "k8s.io/apimachinery/pkg/apis/meta/v1" "reflect" "time" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Types diff --git a/pkg/lib/error/error_test.go b/pkg/lib/error/error_test.go index 9d33d4b86..f84fe527d 100644 --- a/pkg/lib/error/error_test.go +++ b/pkg/lib/error/error_test.go @@ -2,9 +2,10 @@ package error import ( "errors" + "testing" + "github.com/onsi/gomega" errors2 "github.com/pkg/errors" - "testing" ) func TestError(t *testing.T) { @@ -41,7 +42,7 @@ func TestError(t *testing.T) { g.Expect(le4.(*Error).Error()).To( gomega.Equal("Failed to create user. caused by: 'help: failed'")) g.Expect(le4.(*Error).Context()).ToNot(gomega.BeNil()) - g.Expect(len(le4.(*Error).Context())).To(gomega.Equal(4)) + g.Expect(le4.(*Error).Context()).To(gomega.HaveLen(4)) le5 := Wrap( le4, "Web POST failed.", @@ -50,7 +51,7 @@ func TestError(t *testing.T) { g.Expect(le5.(*Error).Error()).To( gomega.Equal("Web POST failed. caused by: 'Failed to create user.' caused by: 'help: failed'")) g.Expect(le5.(*Error).Context()).ToNot(gomega.BeNil()) - g.Expect(len(le5.(*Error).Context())).To(gomega.Equal(8)) + g.Expect(le5.(*Error).Context()).To(gomega.HaveLen(8)) println(le.Stack()) } diff --git a/pkg/lib/error/wrap.go b/pkg/lib/error/wrap.go index b7cb72b93..9bfe1977b 100644 --- a/pkg/lib/error/wrap.go +++ b/pkg/lib/error/wrap.go @@ -19,7 +19,7 @@ func New(m string, kvpair ...interface{}) error { // Returns `err` when err is `nil` or *Error. func Wrap(err error, kvpair ...interface{}) error { if err == nil { - return err + return nil } if le, cast := err.(*Error); cast { le.append(kvpair) diff --git a/pkg/lib/filebacked/file.go b/pkg/lib/filebacked/file.go index b7b3b6663..48019d60e 100644 --- a/pkg/lib/filebacked/file.go +++ b/pkg/lib/filebacked/file.go @@ -13,13 +13,14 @@ import ( "bytes" "encoding/binary" "encoding/gob" - "github.com/google/uuid" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - "github.com/konveyor/forklift-controller/pkg/lib/logging" "io" "os" pathlib "path" "runtime" + + "github.com/google/uuid" + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" + "github.com/konveyor/forklift-controller/pkg/lib/logging" ) var log = logging.WithName("filebacked") @@ -73,8 +74,6 @@ func (w *Writer) Append(object interface{}) { w.path, "kind", kind) - - return } // Build a reader. @@ -156,8 +155,6 @@ func (w *Writer) open() { "writer: opened.", "path", w.path) - - return } // Write entry. @@ -189,6 +186,7 @@ func (w *Writer) writeEntry(kind uint16, bfr bytes.Buffer) (offset int64) { } if n != nWrite { err = liberr.New("Write failed.") + panic(err) } log.V(6).Info( "writer: write entry.", @@ -287,8 +285,6 @@ func (r *Reader) AtWith(index int, object interface{}) { r.path, "index", index) - - return } // Close the reader. @@ -363,6 +359,4 @@ func (r *Reader) open() { "reader: opened.", "path", r.path) - - return } diff --git a/pkg/lib/filebacked/iterator.go b/pkg/lib/filebacked/iterator.go index 26615f41c..2eac47cb6 100644 --- a/pkg/lib/filebacked/iterator.go +++ b/pkg/lib/filebacked/iterator.go @@ -85,7 +85,6 @@ func (*EmptyIterator) At(int) interface{} { // Object at index. func (*EmptyIterator) AtWith(int, interface{}) { - return } // Next object. diff --git a/pkg/lib/filebacked/list.go b/pkg/lib/filebacked/list.go index 2c8a81e89..afb76921e 100644 --- a/pkg/lib/filebacked/list.go +++ b/pkg/lib/filebacked/list.go @@ -86,9 +86,9 @@ type List struct { // Append an object. func (l *List) Append(object interface{}) { - switch object.(type) { + switch o := object.(type) { case Iterator: - itr := object.(Iterator) + itr := o for { object, hasNext := itr.Next() if hasNext { @@ -119,7 +119,6 @@ func (l *List) At(index int) (object interface{}) { func (l *List) AtWith(index int, object interface{}) { reader := l.writer.Reader(true) reader.AtWith(index, object) - return } // Get an iterator. diff --git a/pkg/lib/filebacked/list_test.go b/pkg/lib/filebacked/list_test.go index d15b465a4..7ab84cf34 100644 --- a/pkg/lib/filebacked/list_test.go +++ b/pkg/lib/filebacked/list_test.go @@ -2,9 +2,10 @@ package filebacked import ( "fmt" - "github.com/onsi/gomega" "testing" "time" + + "github.com/onsi/gomega" ) func TestList(t *testing.T) { @@ -192,7 +193,9 @@ func TestList(t *testing.T) { } // Disabled by default. -func __TestListPerf(t *testing.T) { +func TestListPerf(t *testing.T) { + t.Skip("Skipping performance test") + list := NewList() defer list.Close() diff --git a/pkg/lib/inventory/container/collection.go b/pkg/lib/inventory/container/collection.go index a6c395de8..d87b15bdf 100644 --- a/pkg/lib/inventory/container/collection.go +++ b/pkg/lib/inventory/container/collection.go @@ -6,9 +6,10 @@ with the collection of desired models. package container import ( + "reflect" + fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" "github.com/konveyor/forklift-controller/pkg/lib/inventory/model" - "reflect" ) // Model shepherd. diff --git a/pkg/lib/inventory/container/collection_test.go b/pkg/lib/inventory/container/collection_test.go index c92627014..b5fe54168 100644 --- a/pkg/lib/inventory/container/collection_test.go +++ b/pkg/lib/inventory/container/collection_test.go @@ -2,11 +2,12 @@ package container import ( "errors" + "strconv" + "testing" + fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" "github.com/konveyor/forklift-controller/pkg/lib/inventory/model" "github.com/onsi/gomega" - "strconv" - "testing" ) type TestObject2 struct { @@ -25,7 +26,7 @@ func TestCollection(t *testing.T) { g := gomega.NewGomegaWithT(t) DB := model.New("/tmp/test2.db", &TestObject2{}) err = DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) desired := []TestObject2{} for i := 0; i < 10; i++ { @@ -35,9 +36,9 @@ func TestCollection(t *testing.T) { Age: i, } err = DB.Insert(&m) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) desired = append(desired, m) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } // @@ -47,12 +48,12 @@ func TestCollection(t *testing.T) { model.ListOptions{ Detail: model.MaxDetail, }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) collection := Collection{ Stored: stored, } err = collection.Reconcile(asIter(desired)) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(collection.Added).To(gomega.Equal(0)) g.Expect(collection.Updated).To(gomega.Equal(0)) g.Expect(collection.Deleted).To(gomega.Equal(0)) @@ -64,6 +65,7 @@ func TestCollection(t *testing.T) { model.ListOptions{ Detail: model.MaxDetail, }) + g.Expect(err).ToNot(gomega.HaveOccurred()) for i := 11; i < 15; i++ { desired = append( desired, TestObject2{ @@ -81,6 +83,7 @@ func TestCollection(t *testing.T) { Tx: tx, } err = collection.Add(asIter(desired)) + g.Expect(err).ToNot(gomega.HaveOccurred()) _ = tx.Commit() g.Expect(collection.Added).To(gomega.Equal(4)) g.Expect(collection.Updated).To(gomega.Equal(0)) @@ -93,6 +96,7 @@ func TestCollection(t *testing.T) { model.ListOptions{ Detail: model.MaxDetail, }) + g.Expect(err).ToNot(gomega.HaveOccurred()) desired[6].Name = "Larry" desired[8].Age = 100 tx, _ = DB.Begin() @@ -104,17 +108,18 @@ func TestCollection(t *testing.T) { Tx: tx, } err = collection.Update(asIter(desired)) + g.Expect(err).ToNot(gomega.HaveOccurred()) _ = tx.Commit() g.Expect(collection.Added).To(gomega.Equal(0)) g.Expect(collection.Updated).To(gomega.Equal(2)) g.Expect(collection.Deleted).To(gomega.Equal(0)) updated := &TestObject2{ID: 6} err = DB.Get(updated) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(updated.Name).To(gomega.Equal("Larry")) updated = &TestObject2{ID: 8} err = DB.Get(updated) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(updated.Age).To(gomega.Equal(100)) // @@ -124,6 +129,7 @@ func TestCollection(t *testing.T) { model.ListOptions{ Detail: model.MaxDetail, }) + g.Expect(err).ToNot(gomega.HaveOccurred()) desired = desired[2:] tx, _ = DB.Begin() defer func() { @@ -134,6 +140,7 @@ func TestCollection(t *testing.T) { Tx: tx, } err = collection.Delete(asIter(desired)) + g.Expect(err).ToNot(gomega.HaveOccurred()) _ = tx.Commit() g.Expect(collection.Added).To(gomega.Equal(0)) g.Expect(collection.Updated).To(gomega.Equal(0)) @@ -151,6 +158,7 @@ func TestCollection(t *testing.T) { model.ListOptions{ Detail: model.MaxDetail, }) + g.Expect(err).ToNot(gomega.HaveOccurred()) // delete desired = desired[2:] // update @@ -174,6 +182,7 @@ func TestCollection(t *testing.T) { Tx: tx, } err = collection.Reconcile(asIter(desired)) + g.Expect(err).ToNot(gomega.HaveOccurred()) _ = tx.Commit() g.Expect(collection.Added).To(gomega.Equal(5)) g.Expect(collection.Updated).To(gomega.Equal(2)) diff --git a/pkg/lib/inventory/container/ocp/collection.go b/pkg/lib/inventory/container/ocp/collection.go index e642e43ff..438f68508 100644 --- a/pkg/lib/inventory/container/ocp/collection.go +++ b/pkg/lib/inventory/container/ocp/collection.go @@ -2,6 +2,7 @@ package ocp import ( "context" + "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/predicate" ) diff --git a/pkg/lib/inventory/container/ocp/collector.go b/pkg/lib/inventory/container/ocp/collector.go index 5aaa8ebbe..0077d953c 100644 --- a/pkg/lib/inventory/container/ocp/collector.go +++ b/pkg/lib/inventory/container/ocp/collector.go @@ -239,7 +239,9 @@ func (r *Collector) Shutdown() { // Terminate coroutines. func (r *Collector) terminate() { defer func() { - recover() + if err := recover(); err != nil { + r.log.V(4).Info("recovered from panic: ", "err", err) + } }() close(r.eventChannel) } diff --git a/pkg/lib/inventory/model/client.go b/pkg/lib/inventory/model/client.go index 1fbbcf56f..0d39dca72 100644 --- a/pkg/lib/inventory/model/client.go +++ b/pkg/lib/inventory/model/client.go @@ -517,8 +517,7 @@ func (r *Tx) Insert(model Model) (err error) { // Update the model. func (r *Tx) Update(model Model, predicate ...Predicate) (err error) { mark := time.Now() - current := model - current = Clone(model) + current := Clone(model) err = Table{r.real}.Get(current) if err != nil { return diff --git a/pkg/lib/inventory/model/field.go b/pkg/lib/inventory/model/field.go index 8af32b6bc..02bdced67 100644 --- a/pkg/lib/inventory/model/field.go +++ b/pkg/lib/inventory/model/field.go @@ -3,12 +3,13 @@ package model import ( "encoding/json" "fmt" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - "github.com/pkg/errors" "reflect" "regexp" "strconv" "strings" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" + "github.com/pkg/errors" ) const ( @@ -391,7 +392,7 @@ func (f *Field) AsValue(object interface{}) (value interface{}, err error) { reflect.Int32, reflect.Int64: n := val.Int() - value = strconv.FormatInt(n, 0) + value = strconv.FormatInt(n, 10) default: err = liberr.Wrap(PredicateValueErr) } diff --git a/pkg/lib/inventory/model/inspect.go b/pkg/lib/inventory/model/inspect.go index be8dfd40b..3f94a1dec 100644 --- a/pkg/lib/inventory/model/inspect.go +++ b/pkg/lib/inventory/model/inspect.go @@ -1,10 +1,11 @@ package model import ( - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" "reflect" "strings" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" + fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" ) func Inspect(model interface{}) (md *Definition, err error) { @@ -105,7 +106,7 @@ func (r *Definition) Field(name string) *Field { // Match (case-insensitive) by kind. func (r *Definition) IsKind(kind string) bool { - return strings.ToLower(kind) == strings.ToLower(r.Kind) + return strings.EqualFold(kind, r.Kind) } // Get the table name for the model. @@ -173,7 +174,7 @@ func (r *Definition) fields(model interface{}) (fields []*Field, err error) { } else { nested, nErr := r.fields(fv.Addr().Interface()) if nErr != nil { - return + return nil, nErr } fields = append(fields, nested...) } @@ -413,5 +414,4 @@ func (r *Definitions) Reverse() { reversed = append(reversed, (*r)[i]) } *r = reversed - return } diff --git a/pkg/lib/inventory/model/journal.go b/pkg/lib/inventory/model/journal.go index 04ad16783..aeaa35f69 100644 --- a/pkg/lib/inventory/model/journal.go +++ b/pkg/lib/inventory/model/journal.go @@ -202,7 +202,9 @@ func (w *Watch) Match(model Model) bool { // Queue event. func (w *Watch) notify(itr fb.Iterator) { defer func() { - recover() + if err := recover(); err != nil { + w.log.Info("recovered from panic: ", "err", err) + } }() select { case w.queue <- itr: diff --git a/pkg/lib/inventory/model/model.go b/pkg/lib/inventory/model/model.go index f489e4f68..0a6e9c374 100644 --- a/pkg/lib/inventory/model/model.go +++ b/pkg/lib/inventory/model/model.go @@ -2,9 +2,10 @@ package model import ( "database/sql" + "reflect" + "github.com/konveyor/forklift-controller/pkg/lib/logging" "github.com/konveyor/forklift-controller/pkg/lib/ref" - "reflect" ) // Package logger. diff --git a/pkg/lib/inventory/model/model_test.go b/pkg/lib/inventory/model/model_test.go index 28bc369c4..38a6c49b2 100644 --- a/pkg/lib/inventory/model/model_test.go +++ b/pkg/lib/inventory/model/model_test.go @@ -1,14 +1,16 @@ +//nolint:errcheck package model import ( "errors" "fmt" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - "github.com/konveyor/forklift-controller/pkg/lib/ref" - "github.com/onsi/gomega" "math" "testing" "time" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" + "github.com/konveyor/forklift-controller/pkg/lib/ref" + "github.com/onsi/gomega" ) // Adjust default. @@ -110,7 +112,7 @@ type TestObject struct { } func (m *TestObject) Pk() string { - return fmt.Sprintf("%s", m.PK) + return m.PK } func (m *TestObject) String() string { @@ -237,7 +239,6 @@ func (w *MutatingHandler) Deleted(e Event) { } func (w *MutatingHandler) Error(err error) { - return } func (w *MutatingHandler) End() { @@ -259,7 +260,7 @@ func TestDefinition(t *testing.T) { var err error g := gomega.NewGomegaWithT(t) md, err := Inspect(&TestObject{}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // ALL g.Expect(fieldNames(md.Fields)).To(gomega.Equal( []string{ @@ -298,18 +299,17 @@ func TestCRUD(t *testing.T) { &PlainObject{}, &TestObject{}) err = DB.Open(true) - g.Expect(err).To(gomega.BeNil()) - + g.Expect(err).ToNot(gomega.HaveOccurred()) plainA := &PlainObject{ ID: 18, Name: "Ashley", Age: 17, } err = DB.Insert(plainA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) plainB := &PlainObject{ID: 18} err = DB.Get(plainB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(plainA.Pk()).To(gomega.Equal(plainB.Pk())) g.Expect(plainA.ID).To(gomega.Equal(plainB.ID)) g.Expect(plainA.Name).To(gomega.Equal(plainB.Name)) @@ -360,35 +360,35 @@ func TestCRUD(t *testing.T) { } // Insert err = DB.Insert(objA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(objA.Rev).To(gomega.Equal(1)) objB := &TestObject{ID: objA.ID} // Get err = DB.Get(objB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) assertEqual(objA, objB) // Update objA.Name = "Larry" objA.Age = 21 objA.Bool = false err = DB.Update(objA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(objA.Rev).To(gomega.Equal(2)) // Update with predicate. objA.Name = "Fred" objA.Age = 14 err = DB.Update(objA, Eq("Age", 21)) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(objA.Rev).To(gomega.Equal(3)) // Get objB = &TestObject{ID: objA.ID} err = DB.Get(objB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) assertEqual(objA, objB) // Delete objA = &TestObject{ID: objA.ID} err = DB.Delete(objA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // Get (not found) objB = &TestObject{ID: objA.ID} err = DB.Get(objB) @@ -406,7 +406,7 @@ func TestCascade(t *testing.T) { &DetailB{}, &DetailA{}) err = DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) id := func() int { return int(serial.next(-1)) @@ -420,28 +420,28 @@ func TestCascade(t *testing.T) { Age: 18, } err = DB.Insert(treeA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for a := 0; a < 3; a++ { detailA := &DetailA{ PK: id(), FK: treeA.ID, } err = DB.Insert(detailA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for b := 0; b < 3; b++ { detailB := &DetailB{ PK: id(), FK: detailA.PK, } err = DB.Insert(detailB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for c := 0; c < 3; c++ { detailC := &DetailC{ PK: id(), FK: detailB.PK, } err = DB.Insert(detailC) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } } } @@ -453,28 +453,28 @@ func TestCascade(t *testing.T) { Age: 17, } err = DB.Insert(treeB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for a := 10; a < 13; a++ { detailA := &DetailA{ PK: id(), FK: treeB.ID, } err = DB.Insert(detailA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for b := 10; b < 13; b++ { detailB := &DetailB{ PK: id(), FK: detailA.PK, } err = DB.Insert(detailB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for c := 10; c < 13; c++ { detailC := &DetailC{ PK: id(), FK: detailB.PK, } err = DB.Insert(detailC) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } } } @@ -497,7 +497,7 @@ func TestCascade(t *testing.T) { // Delete tree A. err = DB.Delete(treeA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // // Tree A gone. @@ -526,18 +526,18 @@ func TestTransactions(t *testing.T) { "/tmp/test-transactions.db", &TestObject{}) err := DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for i := 0; i < 10; i++ { // Begin tx, err := DB.Begin() defer tx.End() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) object := &TestObject{ ID: i, Name: "Elmer", } err = tx.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // Get (not found) object = &TestObject{ID: object.ID} err = DB.Get(object) @@ -546,7 +546,7 @@ func TestTransactions(t *testing.T) { // Get (found) object = &TestObject{ID: object.ID} err = DB.Get(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } } @@ -556,7 +556,7 @@ func TestWithTxSucceeded(t *testing.T) { "/tmp/test-withtx-succeeded.db", &TestObject{}) err := DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) labels := []string{"A", "B"} n := 10 // @@ -569,7 +569,7 @@ func TestWithTxSucceeded(t *testing.T) { Name: "Elmer", } err = tx.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) object = &TestObject{ID: object.ID} } return @@ -577,10 +577,12 @@ func TestWithTxSucceeded(t *testing.T) { // // Test committed. err = DB.With(insert, labels...) + g.Expect(err).ToNot(gomega.HaveOccurred()) + for i := 0; i < n; i++ { object := &TestObject{ID: i} err = DB.Get(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } } @@ -590,14 +592,14 @@ func TestWithTxFailed(t *testing.T) { "/tmp/test-withtx-failed.db", &TestObject{}) err := DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // // Insert in TX with duplicate key error. fakeErr := liberr.New("Faked") insert := func(tx *Tx) (err error) { object := &TestObject{} err = tx.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) err = fakeErr return } @@ -606,7 +608,7 @@ func TestWithTxFailed(t *testing.T) { err = DB.With(insert) g.Expect(errors.Is(err, fakeErr)).To(gomega.BeTrue()) n, err := DB.Count(&TestObject{}, nil) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(n).To(gomega.Equal(int64(0))) } @@ -617,7 +619,7 @@ func TestList(t *testing.T) { "/tmp/test-list.db", &TestObject{}) err = DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) N := 10 for i := 0; i < N; i++ { object := &TestObject{ @@ -640,12 +642,12 @@ func TestList(t *testing.T) { }, } err = DB.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } // List detail level=0 list := []TestObject{} err = DB.List(&list, ListOptions{}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("")) g.Expect(list[0].Slice).To(gomega.BeNil()) @@ -656,7 +658,7 @@ func TestList(t *testing.T) { // List detail level=1 list = []TestObject{} err = DB.List(&list, ListOptions{Detail: 1}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("")) g.Expect(list[0].Slice).To(gomega.BeNil()) @@ -667,7 +669,7 @@ func TestList(t *testing.T) { // List detail level=2 list = []TestObject{} err = DB.List(&list, ListOptions{Detail: 2}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("")) g.Expect(list[0].Slice).To(gomega.BeNil()) @@ -678,7 +680,7 @@ func TestList(t *testing.T) { // List detail level=3 list = []TestObject{} err = DB.List(&list, ListOptions{Detail: 3}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("")) g.Expect(list[0].Slice).To(gomega.BeNil()) @@ -689,7 +691,7 @@ func TestList(t *testing.T) { // List detail level=4 list = []TestObject{} err = DB.List(&list, ListOptions{Detail: 4}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("")) g.Expect(list[0].Slice).To(gomega.BeNil()) @@ -700,7 +702,7 @@ func TestList(t *testing.T) { // List detail level=10 list = []TestObject{} err = DB.List(&list, ListOptions{Detail: MaxDetail}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(10)) g.Expect(list[0].Name).To(gomega.Equal("Elmer")) g.Expect(len(list[0].Slice)).To(gomega.Equal(2)) @@ -715,7 +717,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Eq("ID", 0), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(1)) g.Expect(list[0].ID).To(gomega.Equal(0)) // List = (multiple). @@ -725,7 +727,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Eq("ID", []int{2, 4}), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) g.Expect(list[0].ID).To(gomega.Equal(2)) g.Expect(list[1].ID).To(gomega.Equal(4)) @@ -742,7 +744,7 @@ func TestList(t *testing.T) { Neq("ID", 7), Neq("ID", 9)), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(5)) g.Expect(list[0].ID).To(gomega.Equal(0)) g.Expect(list[1].ID).To(gomega.Equal(2)) @@ -758,7 +760,7 @@ func TestList(t *testing.T) { Eq("ID", 0), Eq("ID", 6)), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) g.Expect(list[0].ID).To(gomega.Equal(0)) g.Expect(list[1].ID).To(gomega.Equal(6)) @@ -769,7 +771,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Lt("ID", 2), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) g.Expect(list[0].ID).To(gomega.Equal(0)) g.Expect(list[1].ID).To(gomega.Equal(1)) @@ -780,7 +782,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Gt("ID", 7), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) g.Expect(list[0].ID).To(gomega.Equal(8)) g.Expect(list[1].ID).To(gomega.Equal(9)) @@ -792,7 +794,7 @@ func TestList(t *testing.T) { Predicate: Gt("RowID", N/2), Detail: MaxDetail, }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(N / 2)) g.Expect(list[0].RowID).To(gomega.Equal(int64(N/2) + 1)) // List (Eq) Field values. @@ -803,7 +805,7 @@ func TestList(t *testing.T) { Predicate: Eq("RowID", Field{Name: "int8"}), Detail: MaxDetail, }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(1)) g.Expect(list[0].RowID).To(gomega.Equal(int64(8))) // List (nEq) Field values. @@ -813,7 +815,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Neq("RowID", Field{Name: "int8"}), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(N - 1)) // List (Lt) Field values. list = []TestObject{} @@ -822,7 +824,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Lt("int8", Field{Name: "int16"}), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(N)) // List (Gt) Field values. list = []TestObject{} @@ -831,7 +833,7 @@ func TestList(t *testing.T) { ListOptions{ Predicate: Gt("RowID", Field{Name: "int8"}), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) // By label. list = []TestObject{} @@ -843,17 +845,17 @@ func TestList(t *testing.T) { Match(Labels{"id": "v4"}), Eq("ID", 8)), }) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(2)) g.Expect(list[0].ID).To(gomega.Equal(4)) g.Expect(list[1].ID).To(gomega.Equal(8)) // Test count all. count, err := DB.Count(&TestObject{}, nil) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(count).To(gomega.Equal(int64(10))) // Test count with predicate. count, err = DB.Count(&TestObject{}, Gt("ID", 0)) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(count).To(gomega.Equal(int64(9))) } @@ -864,7 +866,7 @@ func TestFind(t *testing.T) { "/tmp/test-iter.db", &TestObject{}) err = DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) N := 10 for i := 0; i < N; i++ { object := &TestObject{ @@ -884,19 +886,19 @@ func TestFind(t *testing.T) { }, } err = DB.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } // List all; detail level=0 itr, err := DB.Find( &TestObject{}, ListOptions{}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(itr.Len()).To(gomega.Equal(10)) var list []TestObject for { object := TestObject{} if itr.NextWith(&object) { - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) list = append(list, object) } else { break @@ -907,9 +909,9 @@ func TestFind(t *testing.T) { itr, err = DB.Find( &TestObject{}, ListOptions{}) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for object, hasNext := itr.Next(); hasNext; object, hasNext = itr.Next() { - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) _, cast := object.(Model) g.Expect(cast).To(gomega.BeTrue()) } @@ -922,14 +924,14 @@ func TestWatch(t *testing.T) { defer func() { _ = DB.Close(false) }() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // Handler A handlerA := &TestHandler{ options: WatchOptions{Snapshot: true}, name: "A", } watchA, err := DB.Watch(&TestObject{}, handlerA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchA).ToNot(gomega.BeNil()) g.Expect(watchA.Alive()).To(gomega.BeTrue()) N := 10 @@ -940,7 +942,7 @@ func TestWatch(t *testing.T) { Name: "Elmer", } err = DB.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } // Handler B handlerB := &TestHandler{ @@ -948,7 +950,7 @@ func TestWatch(t *testing.T) { name: "B", } watchB, err := DB.Watch(&TestObject{}, handlerB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchB).ToNot(gomega.BeNil()) // Update for i := 0; i < N; i++ { @@ -968,7 +970,7 @@ func TestWatch(t *testing.T) { "id": fmt.Sprintf("v%d", i), } err = DB.Update(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } // Handler C handlerC := &TestHandler{ @@ -976,12 +978,12 @@ func TestWatch(t *testing.T) { name: "C", } watchC, err := DB.Watch(&TestObject{}, handlerC) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchC).ToNot(gomega.BeNil()) // Handler D (no snapshot) handlerD := &TestHandler{name: "D"} watchD, err := DB.Watch(&TestObject{}, handlerD) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchC).ToNot(gomega.BeNil()) // Delete for i := 0; i < N; i++ { @@ -989,7 +991,7 @@ func TestWatch(t *testing.T) { ID: i, } err = DB.Delete(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } for i := 0; i < N; i++ { time.Sleep(time.Millisecond * 10) @@ -1151,6 +1153,7 @@ func TestWatch(t *testing.T) { g.Expect(handlerD.done).To(gomega.BeTrue()) } +//nolint:errcheck func TestCloseDB(t *testing.T) { g := gomega.NewGomegaWithT(t) DB := New("/tmp/test-close-db.db", &TestObject{}) @@ -1158,7 +1161,7 @@ func TestCloseDB(t *testing.T) { defer func() { _ = DB.Close(false) }() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) handler := &TestHandler{ options: WatchOptions{Snapshot: true}, name: "A", @@ -1193,7 +1196,7 @@ func TestMutatingWatch(t *testing.T) { DB := New("/tmp/test-mutating-watch.db", &TestObject{}) err := DB.Open(true) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) // Handler A handlerA := &MutatingHandler{ @@ -1202,7 +1205,7 @@ func TestMutatingWatch(t *testing.T) { DB: DB, } watchA, err := DB.Watch(&TestObject{}, handlerA) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchA).ToNot(gomega.BeNil()) // Handler B handlerB := &MutatingHandler{ @@ -1211,7 +1214,7 @@ func TestMutatingWatch(t *testing.T) { DB: DB, } watchB, err := DB.Watch(&TestObject{}, handlerB) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(watchB).ToNot(gomega.BeNil()) N := 10 // Insert @@ -1221,7 +1224,7 @@ func TestMutatingWatch(t *testing.T) { Name: "Elmer", } err = DB.Insert(object) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } for { @@ -1245,11 +1248,11 @@ func TestExecute(t *testing.T) { _ = DB.Close(false) }() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) result, err := DB.Execute( "INSERT INTO Person (id, name) values (0, 'john');") - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(result.RowsAffected()).To(gomega.Equal(int64(1))) } @@ -1271,8 +1274,8 @@ func TestSession(t *testing.T) { } } -// Remove leading __ to enable. -func __TestDbLocked(t *testing.T) { +func TestDbLocked(t *testing.T) { + t.Skip("Skipping DB locked test") g := gomega.NewGomegaWithT(t) DB := New("/tmp/test-db-locked.db", &TestObject{}, &PlainObject{}) err := DB.Open(true) @@ -1305,19 +1308,16 @@ func __TestDbLocked(t *testing.T) { defer close(endChan) n := int64(0) for err = range errChan { - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) n, err = DB.Count(&TestObject{}, nil) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) } fmt.Printf("Count:%d", n) }() - - for _ = range endChan { - } } -// Remove leading __ to enable. -func __TestConcurrency(t *testing.T) { +func TestConcurrency(t *testing.T) { + t.Skip("Skipping Concurrency test") var err error DB := New("/tmp/test-concurrency.db", &TestObject{}) diff --git a/pkg/lib/inventory/model/predicate.go b/pkg/lib/inventory/model/predicate.go index 642b75a7e..9c839ca56 100644 --- a/pkg/lib/inventory/model/predicate.go +++ b/pkg/lib/inventory/model/predicate.go @@ -2,10 +2,11 @@ package model import ( "bytes" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" "reflect" "strings" "text/template" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" ) // Label SQL. diff --git a/pkg/lib/inventory/model/relation.go b/pkg/lib/inventory/model/relation.go index a9101c516..be84928b9 100644 --- a/pkg/lib/inventory/model/relation.go +++ b/pkg/lib/inventory/model/relation.go @@ -95,7 +95,6 @@ func (r *FkRelation) sort() { } } r.sorted.Reverse() - return } // Find next model definition that references the diff --git a/pkg/lib/inventory/model/session.go b/pkg/lib/inventory/model/session.go index 465872dff..1f8768fe2 100644 --- a/pkg/lib/inventory/model/session.go +++ b/pkg/lib/inventory/model/session.go @@ -2,6 +2,7 @@ package model import ( "database/sql" + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" _ "github.com/mattn/go-sqlite3" ) diff --git a/pkg/lib/inventory/model/table.go b/pkg/lib/inventory/model/table.go index 34f490c5a..4458815e7 100644 --- a/pkg/lib/inventory/model/table.go +++ b/pkg/lib/inventory/model/table.go @@ -8,12 +8,13 @@ import ( "encoding/hex" "errors" "fmt" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" - fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" - "github.com/mattn/go-sqlite3" "reflect" "strings" "text/template" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" + fb "github.com/konveyor/forklift-controller/pkg/lib/filebacked" + "github.com/mattn/go-sqlite3" ) // DDL templates. @@ -176,23 +177,20 @@ func (t Table) DDL(model interface{}, dm *DataModel) (list []string, err error) if err != nil { return } - for _, stmt := range ddl { - list = append(list, stmt) - } + + list = append(list, ddl...) ddl, err = t.KeyIndexDDL(md) if err != nil { return } - for _, stmt := range ddl { - list = append(list, stmt) - } + + list = append(list, ddl...) ddl, err = t.IndexDDL(md) if err != nil { return } - for _, stmt := range ddl { - list = append(list, stmt) - } + + list = append(list, ddl...) return } @@ -669,7 +667,7 @@ func (t Table) EnsurePk(md *Definition) { h := sha1.New() for _, f := range md.Fields { name := strings.ToLower(f.Name) - if matched, _ := withFields[name]; !matched { + if matched := withFields[name]; !matched { continue } f.Pull() @@ -683,7 +681,11 @@ func (t Table) EnsurePk(md *Definition) { reflect.Int32, reflect.Int64: bfr := new(bytes.Buffer) - binary.Write(bfr, binary.BigEndian, f.int) + err := binary.Write(bfr, binary.BigEndian, f.int) + if err != nil { + panic(err) + } + h.Write(bfr.Bytes()) } } diff --git a/pkg/lib/inventory/web/client.go b/pkg/lib/inventory/web/client.go index 8470e638e..e3c3e6cb2 100644 --- a/pkg/lib/inventory/web/client.go +++ b/pkg/lib/inventory/web/client.go @@ -265,6 +265,9 @@ func (r *Client) Watch(url string, resource interface{}, h EventHandler) (status return } } + + defer response.Body.Close() + if pErr != nil { pErr = liberr.Wrap( pErr, diff --git a/pkg/lib/inventory/web/web.go b/pkg/lib/inventory/web/web.go index 6b8d62427..8541be962 100644 --- a/pkg/lib/inventory/web/web.go +++ b/pkg/lib/inventory/web/web.go @@ -2,12 +2,13 @@ package web import ( "fmt" + "regexp" + "time" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" "github.com/konveyor/forklift-controller/pkg/lib/inventory/container" "github.com/konveyor/forklift-controller/pkg/lib/logging" - "regexp" - "time" ) // Package logger. diff --git a/pkg/lib/itinerary/simple_test.go b/pkg/lib/itinerary/simple_test.go index b49b8dc86..2d8dc8359 100644 --- a/pkg/lib/itinerary/simple_test.go +++ b/pkg/lib/itinerary/simple_test.go @@ -2,8 +2,9 @@ package itinerary import ( "errors" - "github.com/onsi/gomega" "testing" + + "github.com/onsi/gomega" ) var ( @@ -41,7 +42,7 @@ func TestGet(t *testing.T) { } current, err := itinerary.Get("TWO") - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(current.Name).To(gomega.Equal("TWO")) } @@ -59,17 +60,17 @@ func TestNext(t *testing.T) { // ONE next, done, err := itinerary.Next("ONE") - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeFalse()) g.Expect(next.Name).To(gomega.Equal("TWO")) // TWO next, done, err = itinerary.Next(next.Name) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeFalse()) g.Expect(next.Name).To(gomega.Equal("THREE")) // THREE next, done, err = itinerary.Next(next.Name) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeTrue()) } @@ -90,21 +91,21 @@ func TestNextWithPredicate(t *testing.T) { // ONE next, done, err := itinerary.Next("ONE") - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeFalse()) g.Expect(next.Name).To(gomega.Equal("TWO")) // TWO next, done, err = itinerary.Next(next.Name) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeFalse()) g.Expect(next.Name).To(gomega.Equal("THREE")) // THREE next, done, err = itinerary.Next(next.Name) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(done).To(gomega.BeTrue()) // Step Not Found - next, done, err = itinerary.Next("unknown") + next, _, err = itinerary.Next("unknown") g.Expect(errors.Is(err, StepNotFound)).To(gomega.BeTrue()) } @@ -126,7 +127,7 @@ func TestFirst(t *testing.T) { // First step, err := itinerary.First() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(step.Name).To(gomega.Equal("ONE")) } @@ -146,7 +147,7 @@ func TestList(t *testing.T) { itinerary.Predicate = &TestPredicate{} list, err := itinerary.List() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(len(list)).To(gomega.Equal(3)) g.Expect(list[0].Name).To(gomega.Equal("ONE")) g.Expect(list[1].Name).To(gomega.Equal("TWO")) @@ -170,10 +171,10 @@ func TestProgress(t *testing.T) { // First list, err := itinerary.List() - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) for i, step := range list { report, err := itinerary.Progress(step.Name) - g.Expect(err).To(gomega.BeNil()) + g.Expect(err).ToNot(gomega.HaveOccurred()) g.Expect(report.Total).To(gomega.Equal(int64(len(list)))) g.Expect(report.Completed).To(gomega.Equal(int64(i))) } diff --git a/pkg/lib/logging/logger.go b/pkg/lib/logging/logger.go index 68dcc7e83..f52b92d96 100644 --- a/pkg/lib/logging/logger.go +++ b/pkg/lib/logging/logger.go @@ -131,7 +131,6 @@ func (l *Logger) Error(err error, message string, kvpair ...interface{}) { } le, wrapped := err.(*liberr.Error) if wrapped { - err = le.Unwrap() if context := le.Context(); context != nil { context = append( context, diff --git a/pkg/lib/ref/labels.go b/pkg/lib/ref/labels.go index 8958aa9bc..4728c621e 100644 --- a/pkg/lib/ref/labels.go +++ b/pkg/lib/ref/labels.go @@ -1,8 +1,9 @@ package ref import ( - "k8s.io/apimachinery/pkg/apis/meta/v1" "strings" + + v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) // Labels diff --git a/pkg/lib/ref/mapping.go b/pkg/lib/ref/mapping.go index e4dae40ac..7f416233a 100644 --- a/pkg/lib/ref/mapping.go +++ b/pkg/lib/ref/mapping.go @@ -1,8 +1,9 @@ package ref import ( - "k8s.io/api/core/v1" "sync" + + v1 "k8s.io/api/core/v1" ) // A resource that contains an ObjectReference. @@ -21,10 +22,8 @@ type RefMap struct { func (r *RefMap) Add(owner Owner, target Target) { r.mutex.Lock() defer r.mutex.Unlock() - owners, found := r.Content[target] - if !found { - owners = map[Owner]bool{} - r.Content[target] = owners + if _, found := r.Content[target]; !found { + r.Content[target] = map[Owner]bool{} } r.Content[target][owner] = true diff --git a/pkg/lib/ref/predicate.go b/pkg/lib/ref/predicate.go index c3717fd7a..6c42627ef 100644 --- a/pkg/lib/ref/predicate.go +++ b/pkg/lib/ref/predicate.go @@ -1,8 +1,9 @@ package ref import ( - "k8s.io/api/core/v1" "reflect" + + v1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/event" ) diff --git a/pkg/lib/ref/ref.go b/pkg/lib/ref/ref.go index 9b7fb0459..b3f4185ba 100644 --- a/pkg/lib/ref/ref.go +++ b/pkg/lib/ref/ref.go @@ -1,9 +1,10 @@ package ref import ( - "github.com/konveyor/forklift-controller/pkg/lib/logging" - "k8s.io/api/core/v1" "reflect" + + "github.com/konveyor/forklift-controller/pkg/lib/logging" + v1 "k8s.io/api/core/v1" ) // Global diff --git a/pkg/lib/ref/ref_test.go b/pkg/lib/ref/ref_test.go index 0a77d1f4c..27771550b 100644 --- a/pkg/lib/ref/ref_test.go +++ b/pkg/lib/ref/ref_test.go @@ -1,13 +1,14 @@ package ref import ( + "testing" + "github.com/onsi/gomega" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/controller-runtime/pkg/event" - "testing" ) type _ThingSpec struct { diff --git a/pkg/settings/role.go b/pkg/settings/role.go index e46d2c686..7f974c386 100644 --- a/pkg/settings/role.go +++ b/pkg/settings/role.go @@ -2,9 +2,10 @@ package settings import ( "fmt" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" "os" "strings" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" ) // Environment variables & roles. diff --git a/tests/suit/forklift_test.go b/tests/suit/forklift_test.go index 0baae420b..2f1c4efce 100644 --- a/tests/suit/forklift_test.go +++ b/tests/suit/forklift_test.go @@ -15,7 +15,7 @@ const ( AnnPopulatorLabels = "populatorLabels" ) -var _ = FDescribe("Forklift", func() { +var _ = Describe("Forklift", func() { f := framework.NewFramework("migration-func-test") var vmData *framework.OvirtVM var secret *v1.Secret diff --git a/tests/suit/framework/exec_util.go b/tests/suit/framework/exec_util.go index 3c8c3be87..ed724fa54 100644 --- a/tests/suit/framework/exec_util.go +++ b/tests/suit/framework/exec_util.go @@ -153,7 +153,7 @@ func execute(method string, url *url.URL, config *restclient.Config, stdin io.Re if err != nil { return err } - return exec.Stream(remotecommand.StreamOptions{ + return exec.StreamWithContext(context.TODO(), remotecommand.StreamOptions{ Stdin: stdin, Stdout: stdout, Stderr: stderr, diff --git a/tests/suit/framework/framework.go b/tests/suit/framework/framework.go index 39d4e6a9d..f6eb59ee7 100644 --- a/tests/suit/framework/framework.go +++ b/tests/suit/framework/framework.go @@ -169,8 +169,6 @@ func (f *Framework) AfterEach() { if ginkgo.CurrentGinkgoTestDescription().Failed { f.reporter.FailureCount++ } - - return } // CreateNamespace instantiates a new namespace object with a unique name and the passed-in label(s). @@ -191,7 +189,7 @@ func (f *Framework) CreateNamespace(prefix string, labels map[string]string) (*v var nsObj *v1.Namespace c := f.K8sClient - err := wait.PollImmediate(2*time.Second, nsCreateTime, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, nsCreateTime, true, func(context.Context) (bool, error) { var err error nsObj, err = c.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{}) if err == nil || apierrs.IsAlreadyExists(err) { @@ -374,7 +372,7 @@ func (f *Framework) CreateQuotaInSpecifiedNs(ns string, requestCPU, requestMemor if err != nil { ginkgo.Fail("Unable to set resource quota " + err.Error()) } - return wait.PollImmediate(2*time.Second, nsDeleteTime, func() (bool, error) { + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, nsCreateTime, true, func(context.Context) (bool, error) { quota, err := f.K8sClient.CoreV1().ResourceQuotas(ns).Get(context.TODO(), "test-quota", metav1.GetOptions{}) if err != nil { return false, err @@ -425,7 +423,7 @@ func (f *Framework) CreateStorageQuota(numPVCs, requestStorage int64) error { if err != nil { ginkgo.Fail("Unable to set resource quota " + err.Error()) } - return wait.PollImmediate(2*time.Second, nsDeleteTime, func() (bool, error) { + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, nsCreateTime, true, func(context.Context) (bool, error) { quota, err := f.K8sClient.CoreV1().ResourceQuotas(ns).Get(context.TODO(), "test-storage-quota", metav1.GetOptions{}) if err != nil { fmt.Fprintf(ginkgo.GinkgoWriter, "ERROR: GET ResourceQuota failed once, retrying: %v\n", err.Error()) @@ -453,7 +451,7 @@ func (f *Framework) UpdateStorageQuota(numPVCs, requestStorage int64) error { if err != nil { ginkgo.Fail("Unable to set resource quota " + err.Error()) } - return wait.PollImmediate(5*time.Second, nsDeleteTime, func() (bool, error) { + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, nsCreateTime, true, func(context.Context) (bool, error) { quota, err := f.K8sClient.CoreV1().ResourceQuotas(f.Namespace.GetName()).Get(context.TODO(), "test-storage-quota", metav1.GetOptions{}) if err != nil { fmt.Fprintf(ginkgo.GinkgoWriter, "ERROR: GET ResourceQuota failed once, retrying: %v\n", err.Error()) @@ -467,7 +465,7 @@ func (f *Framework) UpdateStorageQuota(numPVCs, requestStorage int64) error { // DeleteStorageQuota an existing storage quota in the current test namespace. func (f *Framework) DeleteStorageQuota() error { - return wait.PollImmediate(3*time.Second, time.Minute, func() (bool, error) { + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, nsCreateTime, true, func(context.Context) (bool, error) { err := f.K8sClient.CoreV1().ResourceQuotas(f.Namespace.GetName()).Delete(context.TODO(), "test-storage-quota", metav1.DeleteOptions{}) if err != nil { if apierrs.IsNotFound(err) { diff --git a/tests/suit/framework/openstack.go b/tests/suit/framework/openstack.go index 5bffc5ead..0709c311c 100644 --- a/tests/suit/framework/openstack.go +++ b/tests/suit/framework/openstack.go @@ -4,6 +4,7 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "github.com/konveyor/forklift-controller/tests/suit/utils" ) @@ -17,6 +18,10 @@ func (r *OpenStackClient) SetupClient(vmName string, networkName string, volumeT // LoadCA - Load CA from openstack func (r *OpenStackClient) LoadCA(f *Framework, namespace string, contName string) (ca string, err error) { pod, err := utils.FindPodByPrefix(f.K8sClient, namespace, contName, fmt.Sprintf("app=%s", contName)) + if err != nil { + return "", fmt.Errorf("error finding pod for %s - %v", contName, err) + } + caCert, err := r.getFileContent(f, namespace, pod.Name, contName, "/etc/pki/ca-trust/source/anchors/packstack-ca.pem") if err != nil { return "", fmt.Errorf("error getting CA Certificate - %v", err) diff --git a/tests/suit/openstack_extended_test.go b/tests/suit/openstack_extended_test.go index a9686b261..af6367a0b 100644 --- a/tests/suit/openstack_extended_test.go +++ b/tests/suit/openstack_extended_test.go @@ -1,3 +1,4 @@ +//nolint:errcheck package suit_test import ( @@ -25,8 +26,9 @@ var _ = Describe("[level:component]Migration Extended tests for OpenStack provid By("Load Source VM Details from OpenStack") packstackCA, err := f.Clients.OpenStackClient.LoadCA(f, packstackNameSpace, "packstack") - err = utils.TestHttpsCA(keystoneSecureURL, packstackCA, false) + Expect(err).ToNot(HaveOccurred()) + err = utils.TestHttpsCA(keystoneSecureURL, packstackCA, false) Expect(err).ToNot(HaveOccurred()) By("Create Secret from Definition") @@ -86,6 +88,7 @@ var _ = Describe("[level:component]Migration Extended tests for OpenStack provid By("Load Source VM Details from OpenStack") packstackCA, err := f.Clients.OpenStackClient.LoadCA(f, packstackNameSpace, "packstack") + Expect(err).ToNot(HaveOccurred()) err = utils.UpdateLocalCA(packstackCA) Expect(err).ToNot(HaveOccurred()) @@ -125,6 +128,7 @@ var _ = Describe("[level:component]Migration Extended tests for OpenStack provid By("Load Source VM Details from OpenStack") packstackCA, err := f.Clients.OpenStackClient.LoadCA(f, packstackNameSpace, "packstack") + Expect(err).ToNot(HaveOccurred()) err = utils.UpdateLocalCA(packstackCA) Expect(err).ToNot(HaveOccurred()) diff --git a/tests/suit/utils/common.go b/tests/suit/utils/common.go index f7f50cbc3..48425c0d7 100644 --- a/tests/suit/utils/common.go +++ b/tests/suit/utils/common.go @@ -48,21 +48,6 @@ func GetDefaultStorageClass(client *kubernetes.Clientset) *storagev1.StorageClas return nil } -func getDefaultStorageClassCsiDriver(client *kubernetes.Clientset) *storagev1.CSIDriver { - if DefaultStorageClass != nil { - csidrivers, err := client.StorageV1().CSIDrivers().List(context.TODO(), metav1.ListOptions{}) - if err != nil { - ginkgo.Fail(fmt.Sprintf("Unable to get csi driver: %v", err)) - } - for _, driver := range csidrivers.Items { - if driver.Name == DefaultStorageClass.Provisioner { - return &driver - } - } - } - return nil -} - // CacheTestsData fetch and cache data required for tests func CacheTestsData(client *kubernetes.Clientset, cdiNs string) { if DefaultStorageClass == nil { diff --git a/tests/suit/utils/http.go b/tests/suit/utils/http.go index 77b797a86..4f34ce05c 100644 --- a/tests/suit/utils/http.go +++ b/tests/suit/utils/http.go @@ -4,30 +4,27 @@ import ( "crypto/tls" "crypto/x509" "fmt" - liberr "github.com/konveyor/forklift-controller/pkg/lib/error" "net" "net/http" "time" + + liberr "github.com/konveyor/forklift-controller/pkg/lib/error" ) func TestHttpsCA(url string, caCert string, isInsecure bool) (err error) { - var TLSClientConfig *tls.Config - - TLSClientConfig = &tls.Config{InsecureSkipVerify: isInsecure} - cacert := []byte(caCert) roots := x509.NewCertPool() ok := roots.AppendCertsFromPEM(cacert) if !ok { - fmt.Sprintf("the CA certificate is malformed or was not provided, falling back to system CA cert pool") + fmt.Println("the CA certificate is malformed or was not provided, falling back to system CA cert pool") roots, err = x509.SystemCertPool() if err != nil { err = liberr.New("failed to configure the system's cert pool") return } } - TLSClientConfig = &tls.Config{RootCAs: roots} + TLSClientConfig := &tls.Config{RootCAs: roots} HTTPClient := &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, @@ -42,6 +39,12 @@ func TestHttpsCA(url string, caCert string, isInsecure bool) (err error) { TLSClientConfig: TLSClientConfig, }, } - _, err = HTTPClient.Get(url) + resp, err := HTTPClient.Get(url) + if err != nil { + return err + } + + defer resp.Body.Close() + return } diff --git a/tests/suit/utils/migration.go b/tests/suit/utils/migration.go index 187904293..924cd5e63 100644 --- a/tests/suit/utils/migration.go +++ b/tests/suit/utils/migration.go @@ -2,6 +2,7 @@ package utils import ( "context" + "errors" "fmt" "time" @@ -17,8 +18,7 @@ import ( // CreateMigrationFromDefinition is used by tests to create a Plan func CreateMigrationFromDefinition(cl crclient.Client, def *forkliftv1.Migration) error { - var err error - err = cl.Create(context.TODO(), def, &crclient.CreateOptions{}) + err := cl.Create(context.TODO(), def, &crclient.CreateOptions{}) if err == nil || apierrs.IsAlreadyExists(err) { return nil @@ -52,7 +52,7 @@ func WaitForMigrationSucceededWithTimeout(cl crclient.Client, namespace string, returnedMap := &forkliftv1.Migration{} - err := wait.PollImmediate(3*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(context.Context) (bool, error) { err := cl.Get(context.TODO(), migrationIdentifier, returnedMap) //terminate the retry if migration failed @@ -71,7 +71,7 @@ func WaitForMigrationSucceededWithTimeout(cl crclient.Client, namespace string, return true, nil }) if err != nil { - return fmt.Errorf("Migrtation %s not ready within %v, error: %w", migrationName, timeout, err) + return fmt.Errorf("migrtation %s not ready within %v, error: %w", migrationName, timeout, err) } return nil } @@ -86,5 +86,6 @@ func GetImportedVm(cl crclient.Client, namespace string, isImportedVm func(cnv.V return &vm, nil } } - return nil, nil + + return nil, errors.New("no imported VM found") } diff --git a/tests/suit/utils/networkmap.go b/tests/suit/utils/networkmap.go index 4e0e6550b..02be764a8 100644 --- a/tests/suit/utils/networkmap.go +++ b/tests/suit/utils/networkmap.go @@ -18,8 +18,7 @@ import ( // CreateNetworkMapFromDefinition is used by tests to create a NetworkMap func CreateNetworkMapFromDefinition(cl crclient.Client, def *forkliftv1.NetworkMap) error { - var err error - err = cl.Create(context.TODO(), def, &crclient.CreateOptions{}) + err := cl.Create(context.TODO(), def, &crclient.CreateOptions{}) if err == nil || apierrs.IsAlreadyExists(err) { return nil @@ -68,7 +67,7 @@ func WaitForNetworkMapReadyWithTimeout(cl crclient.Client, namespace, networkMap returnedNetworkMap := &forkliftv1.NetworkMap{} - err := wait.PollImmediate(3*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(context.Context) (bool, error) { err := cl.Get(context.TODO(), networkMapIdentifier, returnedNetworkMap) if err != nil || !returnedNetworkMap.Status.Conditions.IsReady() { return false, err diff --git a/tests/suit/utils/plan.go b/tests/suit/utils/plan.go index ce57e681c..3d6d366f7 100644 --- a/tests/suit/utils/plan.go +++ b/tests/suit/utils/plan.go @@ -102,7 +102,7 @@ func WaitForPlanReadyWithTimeout(cl crclient.Client, namespace, planName string, returnedMap := &forkliftv1.Plan{} - err := wait.PollImmediate(3*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(context.Context) (bool, error) { err := cl.Get(context.TODO(), planIdentifier, returnedMap) if err != nil || !returnedMap.Status.Conditions.IsReady() { return false, err diff --git a/tests/suit/utils/pod.go b/tests/suit/utils/pod.go index 1f656aec7..1ae7e773c 100644 --- a/tests/suit/utils/pod.go +++ b/tests/suit/utils/pod.go @@ -8,8 +8,6 @@ import ( "github.com/onsi/ginkgo" k8sv1 "k8s.io/api/core/v1" - v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" @@ -41,7 +39,7 @@ func DeleteVerifierPod(clientSet *kubernetes.Clientset, namespace string) error // CreatePod calls the Kubernetes API to create a Pod func CreatePod(clientSet *kubernetes.Clientset, namespace string, podDef *k8sv1.Pod) (*k8sv1.Pod, error) { - err := wait.PollImmediate(2*time.Second, podCreateTime, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, podCreateTime, true, func(context.Context) (bool, error) { var err error _, err = clientSet.CoreV1().Pods(namespace).Create(context.TODO(), podDef, metav1.CreateOptions{}) if err != nil { @@ -57,9 +55,9 @@ func DeletePodByName(clientSet *kubernetes.Clientset, podName, namespace string, _ = clientSet.CoreV1().Pods(namespace).Delete(context.TODO(), podName, metav1.DeleteOptions{ GracePeriodSeconds: gracePeriod, }) - return wait.PollImmediate(2*time.Second, podDeleteTime, func() (bool, error) { + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, podDeleteTime, true, func(context.Context) (bool, error) { _, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) - if errors.IsNotFound(err) { + if k8serrors.IsNotFound(err) { return true, nil } return false, err @@ -100,14 +98,15 @@ func FindPodByPrefixOnce(clientSet *kubernetes.Clientset, namespace, prefix, lab func findPodByCompFunc(clientSet *kubernetes.Clientset, namespace, prefix, labelSelector string, compFunc func(string, string) bool) (*k8sv1.Pod, error) { var result *k8sv1.Pod var err error - wait.PollImmediate(2*time.Second, podCreateTime, func() (bool, error) { + + err = wait.PollUntilContextCancel(context.TODO(), 2*time.Second, true, func(ctx context.Context) (bool, error) { result, err = findPodByCompFuncOnce(clientSet, namespace, prefix, labelSelector, compFunc) if result != nil { return true, err } - // If no result yet, continue polling even if there is an error return false, nil }) + return result, err } @@ -126,12 +125,12 @@ func findPodByCompFuncOnce(clientSet *kubernetes.Clientset, namespace, prefix, l } else { fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: First pod name %s in namespace %s\n", result.Name, result.Namespace) fmt.Fprintf(ginkgo.GinkgoWriter, "INFO: Second pod name %s in namespace %s\n", pod.Name, pod.Namespace) - return result, fmt.Errorf("Multiple pods starting with prefix %q in namespace %q", prefix, namespace) + return result, fmt.Errorf("multiple pods starting with prefix %q in namespace %q", prefix, namespace) } } } if result == nil { - return nil, errors.NewNotFound(v1.Resource("pod"), prefix) + return nil, k8serrors.NewNotFound(k8sv1.Resource("pod"), prefix) } return result, nil } @@ -158,16 +157,16 @@ func WaitTimeoutForPodFailed(clientSet *kubernetes.Clientset, podName, namespace // WaitTimeoutForPodStatus waits for the given pod to be created and have a expected status func WaitTimeoutForPodStatus(clientSet *kubernetes.Clientset, podName, namespace string, status k8sv1.PodPhase, timeout time.Duration) error { - return wait.PollImmediate(2*time.Second, timeout, podStatus(clientSet, podName, namespace, status)) + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, timeout, true, podStatus(clientSet, podName, namespace, status)) } // WaitTimeoutForPodCondition waits for the given pod to be created and have an expected condition func WaitTimeoutForPodCondition(clientSet *kubernetes.Clientset, podName, namespace string, conditionType k8sv1.PodConditionType, pollperiod, timeout time.Duration) error { - return wait.PollImmediate(pollperiod, timeout, podCondition(clientSet, podName, namespace, conditionType)) + return wait.PollUntilContextTimeout(context.TODO(), pollperiod, timeout, true, podCondition(clientSet, podName, namespace, conditionType)) } -func podStatus(clientSet *kubernetes.Clientset, podName, namespace string, status k8sv1.PodPhase) wait.ConditionFunc { - return func() (bool, error) { +func podStatus(clientSet *kubernetes.Clientset, podName, namespace string, status k8sv1.PodPhase) wait.ConditionWithContextFunc { + return func(context.Context) (bool, error) { pod, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { @@ -184,8 +183,8 @@ func podStatus(clientSet *kubernetes.Clientset, podName, namespace string, statu } } -func podCondition(clientSet *kubernetes.Clientset, podName, namespace string, conditionType k8sv1.PodConditionType) wait.ConditionFunc { - return func() (bool, error) { +func podCondition(clientSet *kubernetes.Clientset, podName, namespace string, conditionType k8sv1.PodConditionType) wait.ConditionWithContextFunc { + return func(context.Context) (bool, error) { pod, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { @@ -218,7 +217,7 @@ func PodGetNode(clientSet *kubernetes.Clientset, podName, namespace string) (str // returns whether the pod is deleted along with any error func WaitPodDeleted(clientSet *kubernetes.Clientset, podName, namespace string, timeout time.Duration) (bool, error) { var result bool - err := wait.PollImmediate(2*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, timeout, true, func(context.Context) (bool, error) { _, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { @@ -234,12 +233,12 @@ func WaitPodDeleted(clientSet *kubernetes.Clientset, podName, namespace string, // IsExpectedNode waits to check if the specified pod is schedule on the specified node func IsExpectedNode(clientSet *kubernetes.Clientset, nodeName, podName, namespace string, timeout time.Duration) error { - return wait.PollImmediate(2*time.Second, timeout, isExpectedNode(clientSet, nodeName, podName, namespace)) + return wait.PollUntilContextTimeout(context.TODO(), 2*time.Second, timeout, true, isExpectedNode(clientSet, nodeName, podName, namespace)) } // returns true is the specified pod running on the specified nodeName. Otherwise returns false -func isExpectedNode(clientSet *kubernetes.Clientset, nodeName, podName, namespace string) wait.ConditionFunc { - return func() (bool, error) { +func isExpectedNode(clientSet *kubernetes.Clientset, nodeName, podName, namespace string) wait.ConditionWithContextFunc { + return func(context.Context) (bool, error) { pod, err := clientSet.CoreV1().Pods(namespace).Get(context.TODO(), podName, metav1.GetOptions{}) if err != nil { if k8serrors.IsNotFound(err) { @@ -256,7 +255,7 @@ func isExpectedNode(clientSet *kubernetes.Clientset, nodeName, podName, namespac } // GetSchedulableNode return a schedulable node from a nodes list -func GetSchedulableNode(nodes *v1.NodeList) *string { +func GetSchedulableNode(nodes *k8sv1.NodeList) *string { for _, node := range nodes.Items { if node.Spec.Taints == nil { return &node.Name diff --git a/tests/suit/utils/provider.go b/tests/suit/utils/provider.go index 8d05c51fb..201ab3687 100644 --- a/tests/suit/utils/provider.go +++ b/tests/suit/utils/provider.go @@ -53,7 +53,7 @@ func WaitForProviderReadyWithTimeout(cl crclient.Client, namespace string, provi providerIdentifier := types.NamespacedName{Namespace: namespace, Name: providerName} returnedProvider := &forkliftv1.Provider{} - err := wait.PollImmediate(3*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(context.Context) (bool, error) { err := cl.Get(context.TODO(), providerIdentifier, returnedProvider) if err != nil || !returnedProvider.Status.Conditions.IsReady() { return false, err @@ -62,7 +62,7 @@ func WaitForProviderReadyWithTimeout(cl crclient.Client, namespace string, provi }) if err != nil { conditions := returnedProvider.Status.Conditions.List - return nil, fmt.Errorf("Provider %s not ready within %v - Phase/condition: %v/%v", + return nil, fmt.Errorf("provider %s not ready within %v - Phase/condition: %v/%v", providerName, timeout, returnedProvider.Status.Phase, conditions[len(conditions)-1].Message) } return returnedProvider, nil diff --git a/tests/suit/utils/secrets.go b/tests/suit/utils/secrets.go index 47b1befd9..737eee7c2 100644 --- a/tests/suit/utils/secrets.go +++ b/tests/suit/utils/secrets.go @@ -41,7 +41,7 @@ func CreateSecretFromDefinition(c *kubernetes.Clientset, definition *v1.Secret) // DeleteSecret ... func DeleteSecret(clientSet *kubernetes.Clientset, namespace string, secret v1.Secret) error { - e := wait.PollImmediate(secretPollInterval, secretPollPeriod, func() (bool, error) { + e := wait.PollUntilContextTimeout(context.TODO(), secretPollInterval, secretPollPeriod, true, func(context.Context) (bool, error) { err := clientSet.CoreV1().Secrets(namespace).Delete(context.TODO(), secret.GetName(), metav1.DeleteOptions{}) if err == nil || apierrs.IsNotFound(err) { return true, nil diff --git a/tests/suit/utils/storagemap.go b/tests/suit/utils/storagemap.go index 1e8a525f7..4ad2584d7 100644 --- a/tests/suit/utils/storagemap.go +++ b/tests/suit/utils/storagemap.go @@ -77,7 +77,7 @@ func WaitForStorageMapReadyWithTimeout(cl crclient.Client, namespace string, sto returnedStorageMap := &forkliftv1.StorageMap{} - err := wait.PollImmediate(3*time.Second, timeout, func() (bool, error) { + err := wait.PollUntilContextTimeout(context.TODO(), 3*time.Second, timeout, true, func(context.Context) (bool, error) { err := cl.Get(context.TODO(), storageMapIdentifier, returnedStorageMap) if err != nil || !returnedStorageMap.Status.Conditions.IsReady() { return false, err