Skip to content

Commit

Permalink
ResizeWindow support virt console multiplexing
Browse files Browse the repository at this point in the history
remove ID param for ExecResize interface
  • Loading branch information
Wu Lei committed Apr 4, 2022
1 parent 7183711 commit 34dacc4
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 33 deletions.
6 changes: 3 additions & 3 deletions cluster/calcium/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions cluster/calcium/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package calcium
import (
"context"
"testing"
"time"

enginemocks "github.com/projecteru2/core/engine/mocks"
lockmocks "github.com/projecteru2/core/lock/mocks"
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion engine/docker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions engine/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
20 changes: 10 additions & 10 deletions engine/mocks/API.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions engine/systemd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
19 changes: 9 additions & 10 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ const (
ImageUserKey = "ImageUser"
// Type indicate type
Type = "virt"

ttyFlag = "tty"
)

// Virt implements the core engine.API interface.
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand Down

0 comments on commit 34dacc4

Please sign in to comment.