From 544730b4b12f878ce3679a2d2f91f87d6aba000a Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Fri, 29 Nov 2019 05:58:03 -0800 Subject: [PATCH] vc: Drop Sandbox#Pause and Sandbox#Resume Fixes #2276 Signed-off-by: Ted Yu --- virtcontainers/api.go | 18 ---- virtcontainers/api_test.go | 58 ------------- virtcontainers/implementation.go | 10 --- virtcontainers/interfaces.go | 4 - virtcontainers/pkg/vcmock/mock.go | 18 ---- virtcontainers/pkg/vcmock/mock_test.go | 54 ------------ virtcontainers/pkg/vcmock/types.go | 2 - virtcontainers/sandbox.go | 111 ------------------------- 8 files changed, 275 deletions(-) diff --git a/virtcontainers/api.go b/virtcontainers/api.go index 3af197302b..8bf3db5b4f 100644 --- a/virtcontainers/api.go +++ b/virtcontainers/api.go @@ -659,24 +659,6 @@ func KillContainer(ctx context.Context, sandboxID, containerID string, signal sy return s.KillContainer(containerID, signal, all) } -// PauseSandbox is the virtcontainers pausing entry point which pauses an -// already running sandbox. -func PauseSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) { - span, ctx := trace(ctx, "PauseSandbox") - defer span.Finish() - - return togglePauseSandbox(ctx, sandboxID, true) -} - -// ResumeSandbox is the virtcontainers resuming entry point which resumes -// (or unpauses) and already paused sandbox. -func ResumeSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) { - span, ctx := trace(ctx, "ResumeSandbox") - defer span.Finish() - - return togglePauseSandbox(ctx, sandboxID, false) -} - // ProcessListContainer is the virtcontainers entry point to list // processes running inside a container func ProcessListContainer(ctx context.Context, sandboxID, containerID string, options ProcessListOptions) (ProcessList, error) { diff --git a/virtcontainers/api_test.go b/virtcontainers/api_test.go index 3e883f0f86..c6bb1a27ca 100644 --- a/virtcontainers/api_test.go +++ b/virtcontainers/api_test.go @@ -354,64 +354,6 @@ func TestStopSandboxNoopAgentSuccessful(t *testing.T) { assert.NotNil(vp) } -func TestPauseThenResumeSandboxNoopAgentSuccessful(t *testing.T) { - defer cleanUp() - assert := assert.New(t) - - config := newTestSandboxConfigNoop() - - ctx := context.Background() - - p, _, err := createAndStartSandbox(ctx, config) - assert.NoError(err) - assert.NotNil(p) - - contID := "100" - contConfig := newTestContainerConfigNoop(contID) - - _, c, err := CreateContainer(ctx, p.ID(), contConfig) - assert.NoError(err) - assert.NotNil(c) - - p, err = PauseSandbox(ctx, p.ID()) - assert.NoError(err) - assert.NotNil(p) - - pImpl, ok := p.(*Sandbox) - assert.True(ok) - - expectedState := types.StatePaused - - assert.Equal(pImpl.state.State, expectedState, "unexpected paused sandbox state") - - for i, c := range p.GetAllContainers() { - cImpl, ok := c.(*Container) - assert.True(ok) - - assert.Equal(expectedState, cImpl.state.State, - fmt.Sprintf("paused container %d has unexpected state", i)) - } - - p, err = ResumeSandbox(ctx, p.ID()) - assert.NoError(err) - assert.NotNil(p) - - pImpl, ok = p.(*Sandbox) - assert.True(ok) - - expectedState = types.StateRunning - - assert.Equal(pImpl.state.State, expectedState, "unexpected resumed sandbox state") - - for i, c := range p.GetAllContainers() { - cImpl, ok := c.(*Container) - assert.True(ok) - - assert.Equal(cImpl.state.State, expectedState, - fmt.Sprintf("resumed container %d has unexpected state", i)) - } -} - func TestStopSandboxKataAgentSuccessful(t *testing.T) { assert := assert.New(t) if tc.NotValid(ktu.NeedRoot()) { diff --git a/virtcontainers/implementation.go b/virtcontainers/implementation.go index 4bc2dbead6..079afa0a23 100644 --- a/virtcontainers/implementation.go +++ b/virtcontainers/implementation.go @@ -77,16 +77,6 @@ func (impl *VCImpl) StatusSandbox(ctx context.Context, sandboxID string) (Sandbo return StatusSandbox(ctx, sandboxID) } -// PauseSandbox implements the VC function of the same name. -func (impl *VCImpl) PauseSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) { - return PauseSandbox(ctx, sandboxID) -} - -// ResumeSandbox implements the VC function of the same name. -func (impl *VCImpl) ResumeSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) { - return ResumeSandbox(ctx, sandboxID) -} - // CreateContainer implements the VC function of the same name. func (impl *VCImpl) CreateContainer(ctx context.Context, sandboxID string, containerConfig ContainerConfig) (VCSandbox, VCContainer, error) { return CreateContainer(ctx, sandboxID, containerConfig) diff --git a/virtcontainers/interfaces.go b/virtcontainers/interfaces.go index 84ee0cb0a9..fa6b584e8b 100644 --- a/virtcontainers/interfaces.go +++ b/virtcontainers/interfaces.go @@ -27,8 +27,6 @@ type VC interface { DeleteSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) FetchSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) ListSandbox(ctx context.Context) ([]SandboxStatus, error) - PauseSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) - ResumeSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) RunSandbox(ctx context.Context, sandboxConfig SandboxConfig) (VCSandbox, error) StartSandbox(ctx context.Context, sandboxID string) (VCSandbox, error) StatusSandbox(ctx context.Context, sandboxID string) (SandboxStatus, error) @@ -72,8 +70,6 @@ type VCSandbox interface { Start() error Stop(force bool) error - Pause() error - Resume() error Release() error Monitor() (chan error, error) Delete() error diff --git a/virtcontainers/pkg/vcmock/mock.go b/virtcontainers/pkg/vcmock/mock.go index b09e5bd78f..51a2c2163c 100644 --- a/virtcontainers/pkg/vcmock/mock.go +++ b/virtcontainers/pkg/vcmock/mock.go @@ -119,24 +119,6 @@ func (m *VCMock) StatusSandbox(ctx context.Context, sandboxID string) (vc.Sandbo return vc.SandboxStatus{}, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) } -// PauseSandbox implements the VC function of the same name. -func (m *VCMock) PauseSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { - if m.PauseSandboxFunc != nil { - return m.PauseSandboxFunc(ctx, sandboxID) - } - - return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) -} - -// ResumeSandbox implements the VC function of the same name. -func (m *VCMock) ResumeSandbox(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { - if m.ResumeSandboxFunc != nil { - return m.ResumeSandboxFunc(ctx, sandboxID) - } - - return nil, fmt.Errorf("%s: %s (%+v): sandboxID: %v", mockErrorPrefix, getSelf(), m, sandboxID) -} - // CreateContainer implements the VC function of the same name. func (m *VCMock) CreateContainer(ctx context.Context, sandboxID string, containerConfig vc.ContainerConfig) (vc.VCSandbox, vc.VCContainer, error) { if m.CreateContainerFunc != nil { diff --git a/virtcontainers/pkg/vcmock/mock_test.go b/virtcontainers/pkg/vcmock/mock_test.go index dc94d740a3..0ded182e84 100644 --- a/virtcontainers/pkg/vcmock/mock_test.go +++ b/virtcontainers/pkg/vcmock/mock_test.go @@ -197,60 +197,6 @@ func TestVCMockListSandbox(t *testing.T) { assert.True(IsMockError(err)) } -func TestVCMockPauseSandbox(t *testing.T) { - assert := assert.New(t) - - m := &VCMock{} - assert.Nil(m.PauseSandboxFunc) - - ctx := context.Background() - _, err := m.PauseSandbox(ctx, testSandboxID) - assert.Error(err) - assert.True(IsMockError(err)) - - m.PauseSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { - return &Sandbox{}, nil - } - - sandbox, err := m.PauseSandbox(ctx, testSandboxID) - assert.NoError(err) - assert.Equal(sandbox, &Sandbox{}) - - // reset - m.PauseSandboxFunc = nil - - _, err = m.PauseSandbox(ctx, testSandboxID) - assert.Error(err) - assert.True(IsMockError(err)) -} - -func TestVCMockResumeSandbox(t *testing.T) { - assert := assert.New(t) - - m := &VCMock{} - assert.Nil(m.ResumeSandboxFunc) - - ctx := context.Background() - _, err := m.ResumeSandbox(ctx, testSandboxID) - assert.Error(err) - assert.True(IsMockError(err)) - - m.ResumeSandboxFunc = func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) { - return &Sandbox{}, nil - } - - sandbox, err := m.ResumeSandbox(ctx, testSandboxID) - assert.NoError(err) - assert.Equal(sandbox, &Sandbox{}) - - // reset - m.ResumeSandboxFunc = nil - - _, err = m.ResumeSandbox(ctx, testSandboxID) - assert.Error(err) - assert.True(IsMockError(err)) -} - func TestVCMockRunSandbox(t *testing.T) { assert := assert.New(t) diff --git a/virtcontainers/pkg/vcmock/types.go b/virtcontainers/pkg/vcmock/types.go index 51d7111012..8cf0e0b7d2 100644 --- a/virtcontainers/pkg/vcmock/types.go +++ b/virtcontainers/pkg/vcmock/types.go @@ -48,8 +48,6 @@ type VCMock struct { DeleteSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) ListSandboxFunc func(ctx context.Context) ([]vc.SandboxStatus, error) FetchSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) - PauseSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) - ResumeSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) RunSandboxFunc func(ctx context.Context, sandboxConfig vc.SandboxConfig) (vc.VCSandbox, error) StartSandboxFunc func(ctx context.Context, sandboxID string) (vc.VCSandbox, error) StatusSandboxFunc func(ctx context.Context, sandboxID string) (vc.SandboxStatus, error) diff --git a/virtcontainers/sandbox.go b/virtcontainers/sandbox.go index 1d91057a46..fb0c710377 100644 --- a/virtcontainers/sandbox.go +++ b/virtcontainers/sandbox.go @@ -1599,48 +1599,6 @@ func (s *Sandbox) Stop(force bool) error { return nil } -// Pause pauses the sandbox -func (s *Sandbox) Pause() error { - if err := s.hypervisor.pauseSandbox(); err != nil { - return err - } - - //After the sandbox is paused, it's needed to stop its monitor, - //Otherwise, its monitors will receive timeout errors if it is - //paused for a long time, thus its monitor will not tell it's a - //crash caused timeout or just a paused timeout. - if s.monitor != nil { - s.monitor.stop() - } - - if err := s.pauseSetStates(); err != nil { - return err - } - - if err := s.storeSandbox(); err != nil { - return err - } - - return nil -} - -// Resume resumes the sandbox -func (s *Sandbox) Resume() error { - if err := s.hypervisor.resumeSandbox(); err != nil { - return err - } - - if err := s.resumeSetStates(); err != nil { - return err - } - - if err := s.storeSandbox(); err != nil { - return err - } - - return nil -} - // list lists all sandbox running on the host. func (s *Sandbox) list() ([]Sandbox, error) { return nil, nil @@ -1668,26 +1626,6 @@ func (s *Sandbox) setSandboxState(state types.StateString) error { return nil } -func (s *Sandbox) pauseSetStates() error { - // XXX: When a sandbox is paused, all its containers are forcibly - // paused too. - if err := s.setContainersState(types.StatePaused); err != nil { - return err - } - - return s.setSandboxState(types.StatePaused) -} - -func (s *Sandbox) resumeSetStates() error { - // XXX: Resuming a paused sandbox puts all containers back into the - // running state. - if err := s.setContainersState(types.StateRunning); err != nil { - return err - } - - return s.setSandboxState(types.StateRunning) -} - // getAndSetSandboxBlockIndex retrieves sandbox block index and increments it for // subsequent accesses. This index is used to maintain the index at which a // block device is assigned to a container in the sandbox. @@ -1737,55 +1675,6 @@ func (s *Sandbox) decrementSandboxBlockIndex() error { return nil } -func (s *Sandbox) setContainersState(state types.StateString) error { - if state == "" { - return vcTypes.ErrNeedState - } - - for _, c := range s.containers { - if err := c.setContainerState(state); err != nil { - return err - } - } - - return nil -} - -// togglePauseSandbox pauses a sandbox if pause is set to true, else it resumes it. -func togglePauseSandbox(ctx context.Context, sandboxID string, pause bool) (*Sandbox, error) { - span, ctx := trace(ctx, "togglePauseSandbox") - defer span.Finish() - - if sandboxID == "" { - return nil, vcTypes.ErrNeedSandbox - } - - lockFile, err := rwLockSandbox(ctx, sandboxID) - if err != nil { - return nil, err - } - defer unlockSandbox(ctx, sandboxID, lockFile) - - // Fetch the sandbox from storage and create it. - s, err := fetchSandbox(ctx, sandboxID) - if err != nil { - return nil, err - } - defer s.releaseStatelessSandbox() - - if pause { - err = s.Pause() - } else { - err = s.Resume() - } - - if err != nil { - return nil, err - } - - return s, nil -} - // HotplugAddDevice is used for add a device to sandbox // Sandbox implement DeviceReceiver interface from device/api/interface.go func (s *Sandbox) HotplugAddDevice(device api.Device, devType config.DeviceType) error {