From 34dacc46d717ffe50f091a73933001446fa56d52 Mon Sep 17 00:00:00 2001 From: Wu Lei Date: Tue, 15 Mar 2022 17:01:12 +0800 Subject: [PATCH] ResizeWindow support virt console multiplexing remove ID param for ExecResize interface --- cluster/calcium/execute.go | 6 +++--- cluster/calcium/helper.go | 4 ++-- cluster/calcium/remove_test.go | 2 ++ engine/docker/exec.go | 2 +- engine/engine.go | 6 +++--- engine/fake/fake.go | 4 ++-- engine/mocks/API.go | 20 ++++++++++---------- engine/systemd/exec.go | 4 ++-- engine/virt/virt.go | 19 +++++++++---------- 9 files changed, 34 insertions(+), 33 deletions(-) diff --git a/cluster/calcium/execute.go b/cluster/calcium/execute.go index a07d80b70..8fc78b584 100644 --- a/cluster/calcium/execute.go +++ b/cluster/calcium/execute.go @@ -43,7 +43,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo Detach: false, } - result, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig) + execID, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig) if err != nil { logger.Errorf(ctx, "[ExecuteWorkload] Failed to attach execID: %+v", err) return @@ -52,7 +52,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo splitFunc, split := bufio.ScanLines, byte('\n') if opts.OpenStdin { processVirtualizationInStream(ctx, inStream, inCh, func(height, width uint) error { - return workload.Engine.ExecResize(ctx, opts.WorkloadID, result, height, width) + return workload.Engine.ExecResize(ctx, execID, height, width) }) splitFunc, split = bufio.ScanBytes, byte(0) } @@ -61,7 +61,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo ch <- &types.AttachWorkloadMessage{WorkloadID: opts.WorkloadID, Data: m.Data, StdStreamType: m.StdStreamType} } - execCode, err := workload.Engine.ExecExitCode(ctx, opts.WorkloadID, result) + execCode, err := workload.Engine.ExecExitCode(ctx, opts.WorkloadID, execID) if err != nil { logger.Errorf(ctx, "[ExecuteWorkload] Failed to get exitcode: %+v", err) return diff --git a/cluster/calcium/helper.go b/cluster/calcium/helper.go index 1b4044a2e..5655527fd 100644 --- a/cluster/calcium/helper.go +++ b/cluster/calcium/helper.go @@ -37,7 +37,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string AttachStdout: true, } b := []byte{} - result, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig) + execID, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig) if err != nil { return nil, errors.WithStack(err) } @@ -46,7 +46,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string b = append(b, m.Data...) } - exitCode, err := client.ExecExitCode(ctx, ID, result) + exitCode, err := client.ExecExitCode(ctx, ID, execID) if err != nil { return b, errors.WithStack(err) } diff --git a/cluster/calcium/remove_test.go b/cluster/calcium/remove_test.go index 9156c6075..ceece2358 100644 --- a/cluster/calcium/remove_test.go +++ b/cluster/calcium/remove_test.go @@ -3,6 +3,7 @@ package calcium import ( "context" "testing" + "time" enginemocks "github.com/projecteru2/core/engine/mocks" lockmocks "github.com/projecteru2/core/lock/mocks" @@ -60,6 +61,7 @@ func TestRemoveWorkload(t *testing.T) { assert.False(t, r.Success) } assert.NoError(t, c.doRemoveWorkloadSync(ctx, []string{"xx"})) + time.Sleep(time.Second) store.AssertExpectations(t) // success diff --git a/engine/docker/exec.go b/engine/docker/exec.go index c595697f6..3d90cf092 100644 --- a/engine/docker/exec.go +++ b/engine/docker/exec.go @@ -88,7 +88,7 @@ func (e *Engine) ExecExitCode(ctx context.Context, ID, execID string) (int, erro } // ExecResize resize exec tty -func (e *Engine) ExecResize(ctx context.Context, ID, execID string, height, width uint) error { +func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) error { opts := dockertypes.ResizeOptions{ Height: height, Width: width, diff --git a/engine/engine.go b/engine/engine.go index 2d55c4de3..a034fc6c0 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -15,9 +15,9 @@ type API interface { Ping(ctx context.Context) error CloseConn() error - Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) - ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) - ExecExitCode(ctx context.Context, ID, result string) (int, error) + Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) + ExecResize(ctx context.Context, execID string, height, width uint) (err error) + ExecExitCode(ctx context.Context, ID, execID string) (int, error) NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error) NetworkDisconnect(ctx context.Context, network, target string, force bool) error diff --git a/engine/fake/fake.go b/engine/fake/fake.go index b26e453e9..bc63d2ee4 100644 --- a/engine/fake/fake.go +++ b/engine/fake/fake.go @@ -30,12 +30,12 @@ func (f *Engine) CloseConn() error { } // Execute . -func (f *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { +func (f *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { return "", nil, nil, nil, f.DefaultErr } // ExecResize . -func (f *Engine) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) { +func (f *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) { return f.DefaultErr } diff --git a/engine/mocks/API.go b/engine/mocks/API.go index 9ab5f2d6f..908b18692 100644 --- a/engine/mocks/API.go +++ b/engine/mocks/API.go @@ -81,20 +81,20 @@ func (_m *API) CloseConn() error { return r0 } -// ExecExitCode provides a mock function with given fields: ctx, ID, result -func (_m *API) ExecExitCode(ctx context.Context, ID string, result string) (int, error) { - ret := _m.Called(ctx, ID, result) +// ExecExitCode provides a mock function with given fields: ctx, ID, execID +func (_m *API) ExecExitCode(ctx context.Context, ID string, execID string) (int, error) { + ret := _m.Called(ctx, ID, execID) var r0 int if rf, ok := ret.Get(0).(func(context.Context, string, string) int); ok { - r0 = rf(ctx, ID, result) + r0 = rf(ctx, ID, execID) } else { r0 = ret.Get(0).(int) } var r1 error if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok { - r1 = rf(ctx, ID, result) + r1 = rf(ctx, ID, execID) } else { r1 = ret.Error(1) } @@ -102,13 +102,13 @@ func (_m *API) ExecExitCode(ctx context.Context, ID string, result string) (int, return r0, r1 } -// ExecResize provides a mock function with given fields: ctx, ID, result, height, width -func (_m *API) ExecResize(ctx context.Context, ID string, result string, height uint, width uint) error { - ret := _m.Called(ctx, ID, result, height, width) +// ExecResize provides a mock function with given fields: ctx, execID, height, width +func (_m *API) ExecResize(ctx context.Context, execID string, height uint, width uint) error { + ret := _m.Called(ctx, execID, height, width) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, string, string, uint, uint) error); ok { - r0 = rf(ctx, ID, result, height, width) + if rf, ok := ret.Get(0).(func(context.Context, string, uint, uint) error); ok { + r0 = rf(ctx, execID, height, width) } else { r0 = ret.Error(0) } diff --git a/engine/systemd/exec.go b/engine/systemd/exec.go index d85e8c40a..1ec4d04bd 100644 --- a/engine/systemd/exec.go +++ b/engine/systemd/exec.go @@ -15,13 +15,13 @@ func (e *Engine) Execute(ctx context.Context, target string, config *enginetypes } // ExecResize resize the terminal size -func (e *Engine) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) { +func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) { err = types.ErrEngineNotImplemented return } // ExecExitCode fetches exceuction exit code -func (e *Engine) ExecExitCode(ctx context.Context, ID, pid string) (execCode int, err error) { +func (e *Engine) ExecExitCode(ctx context.Context, ID, execID string) (execCode int, err error) { err = types.ErrEngineNotImplemented return } diff --git a/engine/virt/virt.go b/engine/virt/virt.go index 5dc8eed84..3fcfb39b8 100644 --- a/engine/virt/virt.go +++ b/engine/virt/virt.go @@ -32,8 +32,6 @@ const ( ImageUserKey = "ImageUser" // Type indicate type Type = "virt" - - ttyFlag = "tty" ) // Virt implements the core engine.API interface. @@ -89,26 +87,27 @@ func (v *Virt) CloseConn() error { } // Execute executes a command in vm -func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (pid string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { +// in tty mode, 'execID' return value indicates the execID which has the pattern '%s_%s', at other times it indicates the pid +func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) { if config.Tty { flags := virttypes.AttachGuestFlags{Safe: true, Force: true} - _, stream, err := v.client.AttachGuest(ctx, ID, config.Cmd, flags) + execID, stream, err := v.client.AttachGuest(ctx, ID, config.Cmd, flags) if err != nil { return "", nil, nil, nil, err } - return ttyFlag, ioutil.NopCloser(stream), nil, stream, nil + return execID, ioutil.NopCloser(stream), nil, stream, nil } msg, err := v.client.ExecuteGuest(ctx, ID, config.Cmd) return strconv.Itoa(msg.Pid), ioutil.NopCloser(bytes.NewReader(msg.Data)), nil, nil, err } // ExecExitCode get return code of a specific execution. -func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err error) { - if pid == ttyFlag { +func (v *Virt) ExecExitCode(ctx context.Context, ID, execID string) (code int, err error) { + if strings.Contains(execID, "_") { return 0, nil } - intPid, err := strconv.Atoi(pid) + intPid, err := strconv.Atoi(execID) if err != nil { return -1, err } @@ -120,8 +119,8 @@ func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err } // ExecResize resize exec tty -func (v *Virt) ExecResize(ctx context.Context, ID, pid string, height, width uint) (err error) { - return v.client.ResizeConsoleWindow(ctx, ID, height, width) +func (v *Virt) ExecResize(ctx context.Context, execID string, height, width uint) (err error) { + return v.client.ResizeConsoleWindow(ctx, execID, height, width) } // NetworkConnect connects to a network.