Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
clh: add vmInfo method
Browse files Browse the repository at this point in the history
API VMInfo call is done more than one time. This leads to have
similar code in multiple times, create context, defer, do call.

Move the logic to one function.

Signed-off-by: Jose Carlos Venegas Munoz <[email protected]>
  • Loading branch information
jcvenegas committed Mar 25, 2020
1 parent ebb8fd5 commit 5e7d253
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions virtcontainers/clh.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,11 @@ func (clh *cloudHypervisor) resizeVCPUs(reqVCPUs uint32) (currentVCPUs uint32, n
cl := clh.client()

// Retrieve the number of current vCPUs via HTTP API
ctx, cancel := context.WithTimeout(context.Background(), clhAPITimeout*time.Second)
info, _, err := cl.VmInfoGet(ctx)
info, err := clh.vmInfo()
if err != nil {
clh.Logger().WithField("function", "resizeVCPUs").WithError(openAPIClientError(err)).Info("[clh] VmInfoGet failed")
clh.Logger().WithField("function", "resizeVCPUs").WithError(err).Info("[clh] vmInfo failed")
return 0, 0, openAPIClientError(err)
}
// Reset the timer after the first HTTP API call
cancel()

currentVCPUs = uint32(info.Config.Cpus.BootVcpus)
newVCPUs = currentVCPUs
Expand All @@ -461,7 +458,7 @@ func (clh *cloudHypervisor) resizeVCPUs(reqVCPUs uint32) (currentVCPUs uint32, n
}

// Resize (hot-plug) vCPUs via HTTP API
ctx, cancel = context.WithTimeout(context.Background(), clhAPITimeout*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), clhAPITimeout*time.Second)
defer cancel()
if _, err = cl.VmResizePut(ctx, chclient.VmResize{DesiredVcpus: int32(reqVCPUs)}); err != nil {
return currentVCPUs, newVCPUs, errors.Wrap(err, "[clh] VmResizePut failed")
Expand Down Expand Up @@ -960,10 +957,10 @@ func (clh *cloudHypervisor) bootVM(ctx context.Context) error {
return openAPIClientError(err)
}

info, _, err := cl.VmInfoGet(ctx)
info, err := clh.vmInfo()

if err != nil {
return openAPIClientError(err)
return err
}

clh.Logger().Debugf("VM state after create: %#v", info)
Expand All @@ -979,10 +976,10 @@ func (clh *cloudHypervisor) bootVM(ctx context.Context) error {
return openAPIClientError(err)
}

info, _, err = cl.VmInfoGet(ctx)
info, err = clh.vmInfo()

if err != nil {
return openAPIClientError(err)
return err
}

clh.Logger().Debugf("VM state after boot: %#v", info)
Expand Down Expand Up @@ -1120,3 +1117,17 @@ func (clh *cloudHypervisor) cleanupVM(force bool) error {

return nil
}

// vmInfo ask to hypervisor for current VM status
func (clh *cloudHypervisor) vmInfo() (chclient.VmInfo, error) {
cl := clh.client()
ctx, cancelInfo := context.WithTimeout(context.Background(), clhAPITimeout*time.Second)
defer cancelInfo()

info, _, err := cl.VmInfoGet(ctx)
if err != nil {
clh.Logger().WithError(openAPIClientError(err)).Warn("VmInfoGet failed")
}
return info, openAPIClientError(err)

}

0 comments on commit 5e7d253

Please sign in to comment.