Skip to content

Commit 34dacc4

Browse files
author
Wu Lei
committed
ResizeWindow support virt console multiplexing
remove ID param for ExecResize interface
1 parent 7183711 commit 34dacc4

File tree

9 files changed

+34
-33
lines changed

9 files changed

+34
-33
lines changed

cluster/calcium/execute.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
4343
Detach: false,
4444
}
4545

46-
result, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig)
46+
execID, stdout, stderr, inStream, err := workload.Engine.Execute(ctx, opts.WorkloadID, execConfig)
4747
if err != nil {
4848
logger.Errorf(ctx, "[ExecuteWorkload] Failed to attach execID: %+v", err)
4949
return
@@ -52,7 +52,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
5252
splitFunc, split := bufio.ScanLines, byte('\n')
5353
if opts.OpenStdin {
5454
processVirtualizationInStream(ctx, inStream, inCh, func(height, width uint) error {
55-
return workload.Engine.ExecResize(ctx, opts.WorkloadID, result, height, width)
55+
return workload.Engine.ExecResize(ctx, execID, height, width)
5656
})
5757
splitFunc, split = bufio.ScanBytes, byte(0)
5858
}
@@ -61,7 +61,7 @@ func (c *Calcium) ExecuteWorkload(ctx context.Context, opts *types.ExecuteWorklo
6161
ch <- &types.AttachWorkloadMessage{WorkloadID: opts.WorkloadID, Data: m.Data, StdStreamType: m.StdStreamType}
6262
}
6363

64-
execCode, err := workload.Engine.ExecExitCode(ctx, opts.WorkloadID, result)
64+
execCode, err := workload.Engine.ExecExitCode(ctx, opts.WorkloadID, execID)
6565
if err != nil {
6666
logger.Errorf(ctx, "[ExecuteWorkload] Failed to get exitcode: %+v", err)
6767
return

cluster/calcium/helper.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string
3737
AttachStdout: true,
3838
}
3939
b := []byte{}
40-
result, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig)
40+
execID, stdout, stderr, _, err := client.Execute(ctx, ID, execConfig)
4141
if err != nil {
4242
return nil, errors.WithStack(err)
4343
}
@@ -46,7 +46,7 @@ func execuateInside(ctx context.Context, client engine.API, ID, cmd, user string
4646
b = append(b, m.Data...)
4747
}
4848

49-
exitCode, err := client.ExecExitCode(ctx, ID, result)
49+
exitCode, err := client.ExecExitCode(ctx, ID, execID)
5050
if err != nil {
5151
return b, errors.WithStack(err)
5252
}

cluster/calcium/remove_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package calcium
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
enginemocks "github.com/projecteru2/core/engine/mocks"
89
lockmocks "github.com/projecteru2/core/lock/mocks"
@@ -60,6 +61,7 @@ func TestRemoveWorkload(t *testing.T) {
6061
assert.False(t, r.Success)
6162
}
6263
assert.NoError(t, c.doRemoveWorkloadSync(ctx, []string{"xx"}))
64+
time.Sleep(time.Second)
6365
store.AssertExpectations(t)
6466

6567
// success

engine/docker/exec.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (e *Engine) ExecExitCode(ctx context.Context, ID, execID string) (int, erro
8888
}
8989

9090
// ExecResize resize exec tty
91-
func (e *Engine) ExecResize(ctx context.Context, ID, execID string, height, width uint) error {
91+
func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) error {
9292
opts := dockertypes.ResizeOptions{
9393
Height: height,
9494
Width: width,

engine/engine.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ type API interface {
1515
Ping(ctx context.Context) error
1616
CloseConn() error
1717

18-
Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error)
19-
ExecResize(ctx context.Context, ID, result string, height, width uint) (err error)
20-
ExecExitCode(ctx context.Context, ID, result string) (int, error)
18+
Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error)
19+
ExecResize(ctx context.Context, execID string, height, width uint) (err error)
20+
ExecExitCode(ctx context.Context, ID, execID string) (int, error)
2121

2222
NetworkConnect(ctx context.Context, network, target, ipv4, ipv6 string) ([]string, error)
2323
NetworkDisconnect(ctx context.Context, network, target string, force bool) error

engine/fake/fake.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ func (f *Engine) CloseConn() error {
3030
}
3131

3232
// Execute .
33-
func (f *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (result string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
33+
func (f *Engine) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
3434
return "", nil, nil, nil, f.DefaultErr
3535
}
3636

3737
// ExecResize .
38-
func (f *Engine) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) {
38+
func (f *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
3939
return f.DefaultErr
4040
}
4141

engine/mocks/API.go

+10-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

engine/systemd/exec.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ func (e *Engine) Execute(ctx context.Context, target string, config *enginetypes
1515
}
1616

1717
// ExecResize resize the terminal size
18-
func (e *Engine) ExecResize(ctx context.Context, ID, result string, height, width uint) (err error) {
18+
func (e *Engine) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
1919
err = types.ErrEngineNotImplemented
2020
return
2121
}
2222

2323
// ExecExitCode fetches exceuction exit code
24-
func (e *Engine) ExecExitCode(ctx context.Context, ID, pid string) (execCode int, err error) {
24+
func (e *Engine) ExecExitCode(ctx context.Context, ID, execID string) (execCode int, err error) {
2525
err = types.ErrEngineNotImplemented
2626
return
2727
}

engine/virt/virt.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ const (
3232
ImageUserKey = "ImageUser"
3333
// Type indicate type
3434
Type = "virt"
35-
36-
ttyFlag = "tty"
3735
)
3836

3937
// Virt implements the core engine.API interface.
@@ -89,26 +87,27 @@ func (v *Virt) CloseConn() error {
8987
}
9088

9189
// Execute executes a command in vm
92-
func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (pid string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
90+
// in tty mode, 'execID' return value indicates the execID which has the pattern '%s_%s', at other times it indicates the pid
91+
func (v *Virt) Execute(ctx context.Context, ID string, config *enginetypes.ExecConfig) (execID string, stdout, stderr io.ReadCloser, stdin io.WriteCloser, err error) {
9392
if config.Tty {
9493
flags := virttypes.AttachGuestFlags{Safe: true, Force: true}
95-
_, stream, err := v.client.AttachGuest(ctx, ID, config.Cmd, flags)
94+
execID, stream, err := v.client.AttachGuest(ctx, ID, config.Cmd, flags)
9695
if err != nil {
9796
return "", nil, nil, nil, err
9897
}
99-
return ttyFlag, ioutil.NopCloser(stream), nil, stream, nil
98+
return execID, ioutil.NopCloser(stream), nil, stream, nil
10099
}
101100
msg, err := v.client.ExecuteGuest(ctx, ID, config.Cmd)
102101
return strconv.Itoa(msg.Pid), ioutil.NopCloser(bytes.NewReader(msg.Data)), nil, nil, err
103102
}
104103

105104
// ExecExitCode get return code of a specific execution.
106-
func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err error) {
107-
if pid == ttyFlag {
105+
func (v *Virt) ExecExitCode(ctx context.Context, ID, execID string) (code int, err error) {
106+
if strings.Contains(execID, "_") {
108107
return 0, nil
109108
}
110109

111-
intPid, err := strconv.Atoi(pid)
110+
intPid, err := strconv.Atoi(execID)
112111
if err != nil {
113112
return -1, err
114113
}
@@ -120,8 +119,8 @@ func (v *Virt) ExecExitCode(ctx context.Context, ID, pid string) (code int, err
120119
}
121120

122121
// ExecResize resize exec tty
123-
func (v *Virt) ExecResize(ctx context.Context, ID, pid string, height, width uint) (err error) {
124-
return v.client.ResizeConsoleWindow(ctx, ID, height, width)
122+
func (v *Virt) ExecResize(ctx context.Context, execID string, height, width uint) (err error) {
123+
return v.client.ResizeConsoleWindow(ctx, execID, height, width)
125124
}
126125

127126
// NetworkConnect connects to a network.

0 commit comments

Comments
 (0)