From acf833cb4a6772a087ac5529c81e05374ea6b41a Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Sun, 9 Dec 2018 19:24:33 +0100 Subject: [PATCH] virtcontainers: Call agent startSandbox from startVM We always ask the agent to start the sandbox when we start the VM, so we should simply call agent.startSandbox from startVM instead of open coding those. This slightly simplifies the complex createSandboxFromConfig routine. Fixes: #1011 Signed-off-by: Samuel Ortiz Signed-off-by: Eric Ernst --- virtcontainers/api.go | 16 +--------------- virtcontainers/sandbox.go | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/virtcontainers/api.go b/virtcontainers/api.go index d0de395bea..d83eee9cb7 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -105,21 +105,7 @@ func createSandboxFromConfig(ctx context.Context, sandboxConfig SandboxConfig, f } }() - // Once startVM is done, we want to guarantee - // that the sandbox is manageable. For that we need - // to start the sandbox inside the VM. - if err = s.agent.startSandbox(s); err != nil { - return nil, err - } - - // rollback to stop sandbox in VM - defer func() { - if err != nil { - s.agent.stopSandbox(s) - } - }() - - if err = s.getAndStoreGuestDetails(); err != nil { + if err := s.getAndStoreGuestDetails(); err != nil { return nil, err } diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 589ef578e1..27343bbcae 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1234,9 +1234,18 @@ func (s *Sandbox) startVM() error { } } - // Store the network s.Logger().Info("VM started") + // Once the hypervisor is done starting the sandbox, + // we want to guarantee that it is manageable. + // For that we need to ask the agent to start the + // sandbox inside the VM. + if err := s.agent.startSandbox(s); err != nil { + return err + } + + s.Logger().Info("Agent started in the sandbox") + return nil } @@ -1245,6 +1254,10 @@ func (s *Sandbox) stopVM() error { span, _ := s.trace("stopVM") defer span.Finish() + if err := s.agent.stopSandbox(s); err != nil { + s.Logger().WithError(err).WithField("sandboxid", s.id).Warning("Agent did not stop sandbox") + } + return s.hypervisor.stopSandbox() }