From 8e9549344f9a46112bf4114c5fb68ff4df5cfa30 Mon Sep 17 00:00:00 2001 From: Ricardo Katz Date: Thu, 24 Oct 2024 10:06:54 -0300 Subject: [PATCH] Fix return messages to contain proper VM information --- pkg/services/govmomi/power.go | 2 +- pkg/services/govmomi/service.go | 20 ++++++++++---------- pkg/services/govmomi/util.go | 8 ++++---- pkg/services/govmomi/vcenter/clone.go | 24 ++++++++++++------------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pkg/services/govmomi/power.go b/pkg/services/govmomi/power.go index 933071db1d..9b69616f25 100644 --- a/pkg/services/govmomi/power.go +++ b/pkg/services/govmomi/power.go @@ -44,7 +44,7 @@ func (vms *VMService) getPowerState(ctx context.Context, virtualMachineCtx *virt case types.VirtualMachinePowerStateSuspended: return infrav1.VirtualMachinePowerStateSuspended, nil default: - return "", errors.Errorf("unexpected power state %q for vm %s", powerState, ctx) + return "", errors.Errorf("unexpected power state %q for vm %s", powerState, virtualMachineCtx) } } diff --git a/pkg/services/govmomi/service.go b/pkg/services/govmomi/service.go index 92256bf2a5..8393317fd2 100644 --- a/pkg/services/govmomi/service.go +++ b/pkg/services/govmomi/service.go @@ -335,7 +335,7 @@ func (vms *VMService) reconcileMetadata(ctx context.Context, virtualMachineCtx * log.Info("Updating VM metadata") taskRef, err := vms.setMetadata(ctx, virtualMachineCtx, newMetadata) if err != nil { - return false, errors.Wrapf(err, "unable to set metadata on vm %s", ctx) + return false, errors.Wrapf(err, "unable to set metadata on vm %s", virtualMachineCtx) } virtualMachineCtx.VSphereVM.Status.TaskRef = taskRef @@ -356,7 +356,7 @@ func (vms *VMService) reconcilePowerState(ctx context.Context, virtualMachineCtx task, err := virtualMachineCtx.Obj.PowerOn(ctx) if err != nil { conditions.MarkFalse(virtualMachineCtx.VSphereVM, infrav1.VMProvisionedCondition, infrav1.PoweringOnFailedReason, clusterv1.ConditionSeverityWarning, err.Error()) - return false, errors.Wrapf(err, "failed to trigger power on op for vm %s", ctx) + return false, errors.Wrapf(err, "failed to trigger power on op for vm %s", virtualMachineCtx) } conditions.MarkFalse(virtualMachineCtx.VSphereVM, infrav1.VMProvisionedCondition, infrav1.PoweringOnReason, clusterv1.ConditionSeverityInfo, "") @@ -376,7 +376,7 @@ func (vms *VMService) reconcilePowerState(ctx context.Context, virtualMachineCtx log.Info("VM is powered on") return true, nil default: - return false, errors.Errorf("unexpected power state %q for vm %s", powerState, ctx) + return false, errors.Errorf("unexpected power state %q for vm %s", powerState, virtualMachineCtx) } } @@ -465,7 +465,7 @@ func (vms *VMService) reconcileStoragePolicy(ctx context.Context, virtualMachine DeviceChange: changes, }) if err != nil { - return errors.Wrapf(err, "unable to set storagePolicy on vm %s", ctx) + return errors.Wrapf(err, "unable to set storagePolicy on vm %s", virtualMachineCtx) } virtualMachineCtx.VSphereVM.Status.TaskRef = task.Reference().Value } @@ -492,7 +492,7 @@ func (vms *VMService) reconcileHardwareVersion(ctx context.Context, virtualMachi log.Info("Upgrading hardware version", "fromVersion", virtualMachine.Config.Version, "toVersion", virtualMachineCtx.VSphereVM.Spec.HardwareVersion) task, err := virtualMachineCtx.Obj.UpgradeVM(ctx, virtualMachineCtx.VSphereVM.Spec.HardwareVersion) if err != nil { - return false, errors.Wrapf(err, "error trigging upgrade op for machine %s", ctx) + return false, errors.Wrapf(err, "error trigging upgrade op for machine %s", virtualMachineCtx) } virtualMachineCtx.VSphereVM.Status.TaskRef = task.Reference().Value return false, nil @@ -535,7 +535,7 @@ func (vms *VMService) reconcilePCIDevices(ctx context.Context, virtualMachineCtx } log.Info("PCI devices to be added", "number", len(specsToBeAdded)) if err := virtualMachineCtx.Obj.AddDevice(ctx, pci.ConstructDeviceSpecs(specsToBeAdded)...); err != nil { - return errors.Wrapf(err, "error adding pci devices for %q", ctx) + return errors.Wrapf(err, "error adding pci devices for %q", virtualMachineCtx) } } return nil @@ -550,7 +550,7 @@ func (vms *VMService) getMetadata(ctx context.Context, virtualMachineCtx *virtua ) if err := pc.RetrieveOne(ctx, virtualMachineCtx.Ref, props, &obj); err != nil { - return "", errors.Wrapf(err, "unable to fetch props %v for vm %s", props, ctx) + return "", errors.Wrapf(err, "unable to fetch props %v for vm %s", props, virtualMachineCtx) } if obj.Config == nil { return "", nil @@ -571,7 +571,7 @@ func (vms *VMService) getMetadata(ctx context.Context, virtualMachineCtx *virtua metadataBuf, err := base64.StdEncoding.DecodeString(metadataBase64) if err != nil { - return "", errors.Wrapf(err, "unable to decode metadata for %s", ctx) + return "", errors.Wrapf(err, "unable to decode metadata for %s", virtualMachineCtx) } return string(metadataBuf), nil @@ -599,7 +599,7 @@ func (vms *VMService) setMetadata(ctx context.Context, virtualMachineCtx *virtua ExtraConfig: extraConfig, }) if err != nil { - return "", errors.Wrapf(err, "unable to set metadata on vm %s", ctx) + return "", errors.Wrapf(err, "unable to set metadata on vm %s", virtualMachineCtx) } return task.Reference().Value, nil @@ -641,7 +641,7 @@ func (vms *VMService) getBootstrapData(ctx context.Context, vmCtx *capvcontext.V Name: vmCtx.VSphereVM.Spec.BootstrapRef.Name, } if err := vmCtx.Client.Get(ctx, secretKey, secret); err != nil { - return nil, "", errors.Wrapf(err, "failed to get bootstrap data secret for %s", ctx) + return nil, "", errors.Wrapf(err, "failed to get bootstrap data secret for %s", vmCtx) } format, ok := secret.Data["format"] diff --git a/pkg/services/govmomi/util.go b/pkg/services/govmomi/util.go index afc6c691b0..ee7927c9c8 100644 --- a/pkg/services/govmomi/util.go +++ b/pkg/services/govmomi/util.go @@ -194,11 +194,11 @@ func reconcileVSphereVMWhenNetworkIsReady(ctx context.Context, virtualMachineCtx // Wait for the VM to be powered on. powerOnTaskInfo, err := powerOnTask.WaitForResult(ctx) if err != nil && powerOnTaskInfo == nil { - return nil, nil, errors.Wrapf(err, "failed to wait for power on op for vm %s", ctx) + return nil, nil, errors.Wrapf(err, "failed to wait for power on op for vm %s", virtualMachineCtx) } powerState, err := virtualMachineCtx.Obj.PowerState(ctx) if err != nil { - return nil, nil, errors.Wrapf(err, "failed to get power state for vm %s", ctx) + return nil, nil, errors.Wrapf(err, "failed to get power state for vm %s", virtualMachineCtx) } if powerState != types.VirtualMachinePowerStatePoweredOn { return nil, nil, errors.Errorf( @@ -208,7 +208,7 @@ func reconcileVSphereVMWhenNetworkIsReady(ctx context.Context, virtualMachineCtx // Wait for all NICs to have valid MAC addresses. if err := waitForMacAddresses(ctx, virtualMachineCtx); err != nil { - return nil, nil, errors.Wrapf(err, "failed to wait for mac addresses for vm %s", ctx) + return nil, nil, errors.Wrapf(err, "failed to wait for mac addresses for vm %s", virtualMachineCtx) } // Get all the MAC addresses. This is done separately from waiting @@ -217,7 +217,7 @@ func reconcileVSphereVMWhenNetworkIsReady(ctx context.Context, virtualMachineCtx // specs, and not the propery change order. _, macToDeviceIndex, deviceToMacIndex, err := getMacAddresses(ctx, virtualMachineCtx) if err != nil { - return nil, nil, errors.Wrapf(err, "failed to get mac addresses for vm %s", ctx) + return nil, nil, errors.Wrapf(err, "failed to get mac addresses for vm %s", virtualMachineCtx) } // Wait for the IP addresses to show up for the VM. diff --git a/pkg/services/govmomi/vcenter/clone.go b/pkg/services/govmomi/vcenter/clone.go index 91f64eb28d..3b0baf4fd3 100644 --- a/pkg/services/govmomi/vcenter/clone.go +++ b/pkg/services/govmomi/vcenter/clone.go @@ -120,17 +120,17 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by folder, err := vmCtx.Session.Finder.FolderOrDefault(ctx, vmCtx.VSphereVM.Spec.Folder) if err != nil { - return errors.Wrapf(err, "unable to get folder for %q", ctx) + return errors.Wrapf(err, "unable to get folder for %q", vmCtx) } pool, err := vmCtx.Session.Finder.ResourcePoolOrDefault(ctx, vmCtx.VSphereVM.Spec.ResourcePool) if err != nil { - return errors.Wrapf(err, "unable to get resource pool for %q", ctx) + return errors.Wrapf(err, "unable to get resource pool for %q", vmCtx) } devices, err := tpl.Device(ctx) if err != nil { - return errors.Wrapf(err, "error getting devices for %q", ctx) + return errors.Wrapf(err, "error getting devices for %q", vmCtx) } // Create a new list of device specs for cloning the VM. @@ -140,14 +140,14 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by if snapshotRef == nil { diskSpecs, err := getDiskSpec(vmCtx, devices) if err != nil { - return errors.Wrapf(err, "error getting disk spec for %q", ctx) + return errors.Wrapf(err, "error getting disk spec for %q", vmCtx) } deviceSpecs = append(deviceSpecs, diskSpecs...) } networkSpecs, err := getNetworkSpecs(ctx, vmCtx, devices) if err != nil { - return errors.Wrapf(err, "error getting network specs for %q", ctx) + return errors.Wrapf(err, "error getting network specs for %q", vmCtx) } deviceSpecs = append(deviceSpecs, networkSpecs...) @@ -207,7 +207,7 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by if vmCtx.VSphereVM.Spec.Datastore != "" { datastore, err := vmCtx.Session.Finder.Datastore(ctx, vmCtx.VSphereVM.Spec.Datastore) if err != nil { - return errors.Wrapf(err, "unable to get datastore %s for %q", vmCtx.VSphereVM.Spec.Datastore, ctx) + return errors.Wrapf(err, "unable to get datastore %s for %q", vmCtx.VSphereVM.Spec.Datastore, vmCtx) } datastoreRef = types.NewReference(datastore.Reference()) spec.Location.Datastore = datastoreRef @@ -217,12 +217,12 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by if vmCtx.VSphereVM.Spec.StoragePolicyName != "" { pbmClient, err := pbm.NewClient(ctx, vmCtx.Session.Client.Client) if err != nil { - return errors.Wrapf(err, "unable to create pbm client for %q", ctx) + return errors.Wrapf(err, "unable to create pbm client for %q", vmCtx) } storageProfileID, err = pbmClient.ProfileIDByName(ctx, vmCtx.VSphereVM.Spec.StoragePolicyName) if err != nil { - return errors.Wrapf(err, "unable to get storageProfileID from name %s for %q", vmCtx.VSphereVM.Spec.StoragePolicyName, ctx) + return errors.Wrapf(err, "unable to get storageProfileID from name %s for %q", vmCtx.VSphereVM.Spec.StoragePolicyName, vmCtx) } var hubs []pbmTypes.PbmPlacementHub @@ -279,7 +279,7 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by // if no datastore defined through VM spec or storage policy, use default datastore, err := vmCtx.Session.Finder.DefaultDatastore(ctx) if err != nil { - return errors.Wrapf(err, "unable to get default datastore for %q", ctx) + return errors.Wrapf(err, "unable to get default datastore for %q", vmCtx) } datastoreRef = types.NewReference(datastore.Reference()) } @@ -292,7 +292,7 @@ func Clone(ctx context.Context, vmCtx *capvcontext.VMContext, bootstrapData []by log.Info(fmt.Sprintf("Cloning Machine with clone mode %s", vmCtx.VSphereVM.Status.CloneMode)) task, err := tpl.Clone(ctx, folder, vmCtx.VSphereVM.Name, spec) if err != nil { - return errors.Wrapf(err, "error trigging clone op for machine %s", ctx) + return errors.Wrapf(err, "error trigging clone op for machine %s", vmCtx) } vmCtx.VSphereVM.Status.TaskRef = task.Reference().Value @@ -415,11 +415,11 @@ func getNetworkSpecs(ctx context.Context, vmCtx *capvcontext.VMContext, devices } backing, err := ref.EthernetCardBackingInfo(ctx) if err != nil { - return nil, errors.Wrapf(err, "unable to create new ethernet card backing info for network %q on %q", netSpec.NetworkName, ctx) + return nil, errors.Wrapf(err, "unable to create new ethernet card backing info for network %q on %q", netSpec.NetworkName, vmCtx) } dev, err := object.EthernetCardTypes().CreateEthernetCard(ethCardType, backing) if err != nil { - return nil, errors.Wrapf(err, "unable to create new ethernet card %q for network %q on %q", ethCardType, netSpec.NetworkName, ctx) + return nil, errors.Wrapf(err, "unable to create new ethernet card %q for network %q on %q", ethCardType, netSpec.NetworkName, vmCtx) } // Get the actual NIC object. This is safe to assert without a check