From 5e7d25385917061f502d213b39f54ee1603d874f Mon Sep 17 00:00:00 2001 From: Jose Carlos Venegas Munoz Date: Tue, 24 Mar 2020 20:43:07 +0000 Subject: [PATCH] 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 --- virtcontainers/clh.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/virtcontainers/clh.go b/virtcontainers/clh.go index 10a6048a0c..4e411bb9e7 100644 --- a/virtcontainers/clh.go +++ b/virtcontainers/clh.go @@ -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) + +}