mirror of
https://github.com/kata-containers/kata-containers.git
synced 2025-06-22 05:28:25 +00:00
clh: add vmInfo method
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 <jose.carlos.venegas.munoz@intel.com>
This commit is contained in:
parent
ebb8fd576b
commit
5e7d253859
@ -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
|
||||
@ -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")
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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)
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user