diff --git a/cluster/calcium/create.go b/cluster/calcium/create.go index 5236219c1..2b7d355c7 100644 --- a/cluster/calcium/create.go +++ b/cluster/calcium/create.go @@ -56,8 +56,8 @@ func (c *Calcium) doCreateWorkloads(ctx context.Context, opts *types.DeployOptio cctx, cancel := context.WithTimeout(utils.InheritTracingInfo(ctx, context.TODO()), c.config.GlobalTimeout) for nodename := range deployMap { processing := opts.GetProcessing(nodename) - if e := c.store.DeleteProcessing(cctx, processing); e != nil { - logger.Errorf(ctx, "[Calcium.doCreateWorkloads] delete processing failed for %s: %+v", nodename, e) + if err := c.store.DeleteProcessing(cctx, processing); err != nil { + logger.Errorf(ctx, "[Calcium.doCreateWorkloads] delete processing failed for %s: %+v", nodename, err) } } close(ch) diff --git a/cluster/calcium/create_test.go b/cluster/calcium/create_test.go index e1f5638f4..799cd8b57 100644 --- a/cluster/calcium/create_test.go +++ b/cluster/calcium/create_test.go @@ -233,7 +233,7 @@ func TestCreateWorkloadTxn(t *testing.T) { engine.On("ImageLocalDigests", mock.Anything, mock.Anything).Return([]string{""}, nil) engine.On("ImageRemoteDigest", mock.Anything, mock.Anything).Return("", nil) engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(nil, errors.Wrap(context.DeadlineExceeded, "VirtualizationCreate")).Twice() - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD) walCommitted = false ch, err = c.CreateWorkload(ctx, opts) diff --git a/cluster/calcium/lambda_test.go b/cluster/calcium/lambda_test.go index 72a8673c5..97467d43f 100644 --- a/cluster/calcium/lambda_test.go +++ b/cluster/calcium/lambda_test.go @@ -268,7 +268,7 @@ func newLambdaCluster(t *testing.T) (*Calcium, []*types.Node) { // doCreateAndStartWorkload fails: AddWorkload engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationCreated{ID: "workloadfortonictest"}, nil) engine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil) - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD) engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationInfo{}, nil) store.On("AddWorkload", mock.Anything, mock.Anything, mock.Anything).Return(nil) diff --git a/cluster/calcium/remove_test.go b/cluster/calcium/remove_test.go index 480ce7144..80893f06e 100644 --- a/cluster/calcium/remove_test.go +++ b/cluster/calcium/remove_test.go @@ -65,7 +65,7 @@ func TestRemoveWorkload(t *testing.T) { // success engine := &enginemocks.API{} workload.Engine = engine - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) store.On("GetWorkloads", mock.Anything, mock.Anything).Return([]*types.Workload{workload}, nil) store.On("RemoveWorkload", mock.Anything, mock.Anything).Return(nil) store.On("UpdateNodeResource", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) diff --git a/cluster/calcium/replace_test.go b/cluster/calcium/replace_test.go index a5b2ec44b..87ba2fa27 100644 --- a/cluster/calcium/replace_test.go +++ b/cluster/calcium/replace_test.go @@ -168,9 +168,6 @@ func TestReplaceWorkload(t *testing.T) { // failed by VirtualizationCreate engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(nil, types.ErrCannotGetEngine).Once() engine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(types.ErrCannotGetEngine).Once() - //store.On("UpdateNodeResource", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() - //engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil).Once() - //store.On("RemoveWorkload", mock.Anything, mock.Anything).Return(nil).Once() ch, err = c.ReplaceWorkload(ctx, opts) assert.NoError(t, err) for r := range ch { @@ -200,7 +197,7 @@ func TestReplaceWorkload(t *testing.T) { store.AssertExpectations(t) engine.AssertExpectations(t) - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) store.On("RemoveWorkload", mock.Anything, mock.Anything).Return(nil) store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD) // succ diff --git a/cluster/calcium/wal.go b/cluster/calcium/wal.go index 43c80ea9c..e97c5cab0 100644 --- a/cluster/calcium/wal.go +++ b/cluster/calcium/wal.go @@ -5,6 +5,7 @@ import ( "encoding/json" "time" + "github.com/pkg/errors" "github.com/projecteru2/core/log" "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" @@ -123,7 +124,7 @@ func (h *CreateWorkloadHandler) Handle(ctx context.Context, raw interface{}) (er if err != nil { return logger.Err(ctx, err) } - if _, err = node.Engine.VirtualizationRemove(ctx, wrk.ID, true, true); err != nil { + if err = node.Engine.VirtualizationRemove(ctx, wrk.ID, true, true); err != nil && !errors.Is(err, types.ErrWorkloadNotExists) { return logger.Err(ctx, err) } diff --git a/cluster/calcium/wal_test.go b/cluster/calcium/wal_test.go index 46cc92782..24f8ee952 100644 --- a/cluster/calcium/wal_test.go +++ b/cluster/calcium/wal_test.go @@ -72,14 +72,14 @@ func TestHandleCreateWorkloadError(t *testing.T) { store.On("GetWorkload", mock.Anything, mock.Anything).Return(wrk, err).Once() store.On("GetNode", mock.Anything, wrk.Nodename).Return(node, nil).Once() - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(0, err).Once() + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(err).Once() c.wal.Recover(context.TODO()) store.AssertExpectations(t) engine.AssertExpectations(t) store.On("GetWorkload", mock.Anything, wrkid).Return(wrk, fmt.Errorf("err")).Once() store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil).Once() - engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil).Once() + engine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(types.ErrWorkloadNotExists).Once() c.wal.Recover(context.TODO()) store.AssertExpectations(t) engine.AssertExpectations(t) @@ -117,7 +117,7 @@ func TestHandleCreateWorkloadHandled(t *testing.T) { eng, ok := node.Engine.(*enginemocks.API) require.True(t, ok) eng.On("VirtualizationRemove", mock.Anything, wrk.ID, true, true). - Return(1, nil). + Return(nil). Once() c.wal.Recover(context.TODO()) @@ -163,7 +163,7 @@ func TestHandleCreateLambda(t *testing.T) { eng := wrk.Engine.(*enginemocks.API) eng.On("VirtualizationWait", mock.Anything, wrk.ID, "").Return(&enginetypes.VirtualizationWaitResult{Code: 0}, nil).Once() eng.On("VirtualizationRemove", mock.Anything, wrk.ID, true, true). - Return(1, nil). + Return(nil). Once() eng.On("VirtualizationResourceRemap", mock.Anything, mock.Anything).Return(nil, nil).Once() store.On("GetWorkloads", mock.Anything, []string{wrk.ID}). diff --git a/engine/docker/container.go b/engine/docker/container.go index 132e9a154..346b018be 100644 --- a/engine/docker/container.go +++ b/engine/docker/container.go @@ -17,6 +17,7 @@ import ( corecluster "github.com/projecteru2/core/cluster" enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" + "github.com/projecteru2/core/types" coretypes "github.com/projecteru2/core/types" "github.com/projecteru2/core/utils" @@ -346,12 +347,12 @@ func (e *Engine) VirtualizationStop(ctx context.Context, ID string, gracefulTime } // VirtualizationRemove remove virtualization -func (e *Engine) VirtualizationRemove(ctx context.Context, ID string, removeVolumes, force bool) (removed int, err error) { +func (e *Engine) VirtualizationRemove(ctx context.Context, ID string, removeVolumes, force bool) (err error) { if err = e.client.ContainerRemove(ctx, ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: removeVolumes, Force: force}); err == nil { - return 1, nil + return nil } if strings.Contains(err.Error(), "No such container") { - return 0, nil + return types.ErrWorkloadNotExists } return } diff --git a/engine/engine.go b/engine/engine.go index 7491f6921..b2a117378 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -41,7 +41,7 @@ type API interface { VirtualizationCopyTo(ctx context.Context, ID, target string, content []byte, uid, gid int, mode int64) error VirtualizationStart(ctx context.Context, ID string) error VirtualizationStop(ctx context.Context, ID string, gracefulTimeout time.Duration) error - VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (int, error) + VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error VirtualizationInspect(ctx context.Context, ID string) (*enginetypes.VirtualizationInfo, error) VirtualizationLogs(ctx context.Context, opts *enginetypes.VirtualizationLogStreamOptions) (stdout, stderr io.ReadCloser, err error) VirtualizationAttach(ctx context.Context, ID string, stream, openStdin bool) (stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) diff --git a/engine/fake/fake.go b/engine/fake/fake.go index 5f769f7c6..f9b7992b5 100644 --- a/engine/fake/fake.go +++ b/engine/fake/fake.go @@ -139,8 +139,8 @@ func (f *Engine) VirtualizationStop(ctx context.Context, ID string, gracefulTime } // VirtualizationRemove . -func (f *Engine) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (int, error) { - return 0, types.ErrNilEngine +func (f *Engine) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error { + return types.ErrNilEngine } // VirtualizationInspect . diff --git a/engine/mocks/API.go b/engine/mocks/API.go index a20258314..cb994fb9c 100644 --- a/engine/mocks/API.go +++ b/engine/mocks/API.go @@ -654,24 +654,17 @@ func (_m *API) VirtualizationLogs(ctx context.Context, opts *types.Virtualizatio } // VirtualizationRemove provides a mock function with given fields: ctx, ID, volumes, force -func (_m *API) VirtualizationRemove(ctx context.Context, ID string, volumes bool, force bool) (int, error) { +func (_m *API) VirtualizationRemove(ctx context.Context, ID string, volumes bool, force bool) error { ret := _m.Called(ctx, ID, volumes, force) - var r0 int - if rf, ok := ret.Get(0).(func(context.Context, string, bool, bool) int); ok { + var r0 error + if rf, ok := ret.Get(0).(func(context.Context, string, bool, bool) error); ok { r0 = rf(ctx, ID, volumes, force) } else { - r0 = ret.Get(0).(int) - } - - var r1 error - if rf, ok := ret.Get(1).(func(context.Context, string, bool, bool) error); ok { - r1 = rf(ctx, ID, volumes, force) - } else { - r1 = ret.Error(1) + r0 = ret.Error(0) } - return r0, r1 + return r0 } // VirtualizationResize provides a mock function with given fields: ctx, ID, height, width diff --git a/engine/mocks/fakeengine/mock.go b/engine/mocks/fakeengine/mock.go index c0616e16a..37e5ae43b 100644 --- a/engine/mocks/fakeengine/mock.go +++ b/engine/mocks/fakeengine/mock.go @@ -97,7 +97,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint e.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) e.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil) e.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil) - e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) vcJSON := &enginetypes.VirtualizationInfo{ID: ID, Image: "mock-image", Running: true, Networks: map[string]string{"mock-network": "1.1.1.1"}} e.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(vcJSON, nil) logs := ioutil.NopCloser(bytes.NewBufferString("logs1...\nlogs2...\n")) diff --git a/engine/virt/virt.go b/engine/virt/virt.go index d01fab41a..e629cfe7e 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -19,6 +19,7 @@ import ( enginetypes "github.com/projecteru2/core/engine/types" "github.com/projecteru2/core/log" coresource "github.com/projecteru2/core/source" + "github.com/projecteru2/core/types" coretypes "github.com/projecteru2/core/types" ) @@ -228,12 +229,12 @@ func (v *Virt) VirtualizationStop(ctx context.Context, ID string, gracefulTimeou } // VirtualizationRemove removes a guest. -func (v *Virt) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (removed int, err error) { +func (v *Virt) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (err error) { if _, err = v.client.DestroyGuest(ctx, ID, force); err == nil { - return 1, nil + return nil } if strings.Contains(err.Error(), "key not exists") { - return 0, nil + return types.ErrWorkloadNotExists } return } diff --git a/types/workload.go b/types/workload.go index 4fc662302..1cc05b67d 100644 --- a/types/workload.go +++ b/types/workload.go @@ -81,11 +81,13 @@ func (c *Workload) Stop(ctx context.Context, force bool) error { } // Remove a workload -func (c *Workload) Remove(ctx context.Context, force bool) error { +func (c *Workload) Remove(ctx context.Context, force bool) (err error) { if c.Engine == nil { return errors.WithStack(ErrNilEngine) } - _, err := c.Engine.VirtualizationRemove(ctx, c.ID, true, force) + if err = c.Engine.VirtualizationRemove(ctx, c.ID, true, force); errors.Is(err, ErrWorkloadNotExists) { + err = nil + } return errors.WithStack(err) } diff --git a/types/workload_test.go b/types/workload_test.go index 6349c54b1..a8922730e 100644 --- a/types/workload_test.go +++ b/types/workload_test.go @@ -27,7 +27,7 @@ func TestWorkloadControl(t *testing.T) { mockEngine := &mocks.API{} mockEngine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil) mockEngine.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil) - mockEngine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(1, nil) + mockEngine.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) ctx := context.Background() c := Workload{}