Skip to content

Commit

Permalink
Merge pull request #2413 from Ankitasw/vspheremachine-logging
Browse files Browse the repository at this point in the history
🌱 Removal of if block usage wrt logging in vsphere machine controller
  • Loading branch information
k8s-ci-robot authored Oct 12, 2023
2 parents ebba8af + e9fdbc5 commit 6923f2b
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions pkg/services/vmoperator/vmopmachine.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,17 @@ func (v *VmopMachineService) ReconcileDelete(ctx context.Context, machineCtx cap
}
log.V(2).Info("Destroying VM")

// If debug logging is enabled, report the number of vms in the cluster before and after the reconcile
if log.V(5).Enabled() {
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.V(5).Info("Trace Destroy PRE: VirtualMachines", "vmcount", len(vms), "error", err)
defer func() {
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.Info("Trace Destroy PRE: VirtualMachines", "vmcount", len(vms), "error", err)
defer func() {
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.Info("Trace Destroy POST: VirtualMachines", "vmcount", len(vms), "error", err)
}()
}
log.V(5).Info("Trace Destroy POST: VirtualMachines", "vmcount", len(vms), "error", err)
}()

// First, check to see if it's already deleted
vmopVM := vmoprv1.VirtualMachine{}
if err := v.Client.Get(ctx, types.NamespacedName{Namespace: supervisorMachineCtx.Machine.Namespace, Name: supervisorMachineCtx.Machine.Name}, &vmopVM); err != nil {
// If debug logging is enabled, report the number of vms in the cluster before and after the reconcile
if apierrors.IsNotFound(err) {
supervisorMachineCtx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStateNotFound
return err
Expand All @@ -116,7 +114,6 @@ func (v *VmopMachineService) ReconcileDelete(ctx context.Context, machineCtx cap
supervisorMachineCtx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStateError
return err
}

supervisorMachineCtx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStateDeleting
return nil
}
Expand All @@ -143,14 +140,12 @@ func (v *VmopMachineService) ReconcileNormal(ctx context.Context, machineCtx cap
log.V(2).Info("Reconciling VM")

// If debug logging is enabled, report the number of vms in the cluster before and after the reconcile
if log.V(5).Enabled() {
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.Info("Trace ReconcileVM PRE: VirtualMachines", "vmcount", len(vms), "error", err)
defer func() {
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.Info("Trace ReconcileVM POST: VirtualMachines", "vmcount", len(vms), "error", err)
}()
}
vms, err := v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.V(5).Info("Trace ReconcileVM PRE: VirtualMachines", "vmcount", len(vms), "error", err)
defer func() {
vms, err = v.getVirtualMachinesInCluster(ctx, supervisorMachineCtx)
log.V(5).Info("Trace ReconcileVM POST: VirtualMachines", "vmcount", len(vms), "error", err)
}()

// Set the VM state. Will get reset throughout the reconcile
supervisorMachineCtx.VSphereMachine.Status.VMStatus = vmwarev1.VirtualMachineStatePending
Expand Down Expand Up @@ -373,6 +368,9 @@ func (v *VmopMachineService) reconcileProviderID(ctx context.Context, supervisor
// getVirtualMachinesInCluster returns all VMOperator VirtualMachine objects in the current cluster.
// First filter by clusterSelectorKey. If the result is empty, they fall back to legacyClusterSelectorKey.
func (v *VmopMachineService) getVirtualMachinesInCluster(ctx context.Context, supervisorMachineCtx *vmware.SupervisorMachineContext) ([]*vmoprv1.VirtualMachine, error) {
if supervisorMachineCtx.Cluster == nil {
return []*vmoprv1.VirtualMachine{}, errors.Errorf("No cluster is set for machine %s in namespace %s", supervisorMachineCtx.GetVSphereMachine().GetName(), supervisorMachineCtx.GetVSphereMachine().GetNamespace())
}
labels := map[string]string{clusterSelectorKey: supervisorMachineCtx.Cluster.Name}
vmList := &vmoprv1.VirtualMachineList{}

Expand Down

0 comments on commit 6923f2b

Please sign in to comment.