-
Notifications
You must be signed in to change notification settings - Fork 950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: support resize the tty of cri exec process #2063
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ import ( | |
// Runtime is the interface to execute the commands and provide the streams. | ||
type Runtime interface { | ||
// Exec executes the command in pod. | ||
Exec(ctx context.Context, containerID string, cmd []string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) | ||
Exec(ctx context.Context, containerID string, cmd []string, resizeChan <-chan apitypes.ResizeOptions, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) | ||
|
||
// Attach attaches to pod. | ||
Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error | ||
|
@@ -38,7 +38,7 @@ func NewStreamRuntime(ctrMgr mgr.ContainerMgr) Runtime { | |
} | ||
|
||
// Exec executes a command inside the container. | ||
func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) { | ||
func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []string, resizeChan <-chan apitypes.ResizeOptions, streamOpts *remotecommand.Options, streams *remotecommand.Streams) (uint32, error) { | ||
createConfig := &apitypes.ExecCreateConfig{ | ||
Cmd: cmd, | ||
AttachStdin: streamOpts.Stdin, | ||
|
@@ -63,6 +63,13 @@ func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []stri | |
return 0, fmt.Errorf("failed to start exec for container %q: %v", containerID, err) | ||
} | ||
|
||
handleResizing(containerID, execid, resizeChan, func(size apitypes.ResizeOptions) { | ||
err := s.containerMgr.ResizeExec(ctx, execid, size) | ||
if err != nil { | ||
logrus.Errorf("failed to resize process %q console for container %q: %v", execid, containerID, err) | ||
} | ||
}) | ||
|
||
// TODO Find a better way instead of the dead loop | ||
var ei *apitypes.ContainerExecInspect | ||
for { | ||
|
@@ -80,6 +87,27 @@ func (s *streamRuntime) Exec(ctx context.Context, containerID string, cmd []stri | |
return uint32(ei.ExitCode), nil | ||
} | ||
|
||
// handleResizing spawns a goroutine that processes the resize channel, calling resizeFunc for each | ||
// remotecommand.TerminalSize received from the channel. The resize channel must be closed elsewhere to stop the | ||
// goroutine. | ||
func handleResizing(containerID, execID string, resizeChan <-chan apitypes.ResizeOptions, resizeFunc func(size apitypes.ResizeOptions)) { | ||
if resizeChan == nil { | ||
return | ||
} | ||
go func() { | ||
for { | ||
size, ok := <-resizeChan | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the size is always valid? |
||
if !ok { | ||
return | ||
} | ||
if size.Height <= 0 || size.Width <= 0 { | ||
continue | ||
} | ||
resizeFunc(size) | ||
} | ||
}() | ||
} | ||
|
||
// Attach attaches to a running container. | ||
func (s *streamRuntime) Attach(ctx context.Context, containerID string, streamOpts *remotecommand.Options, streams *remotecommand.Streams) error { | ||
attachConfig := &mgr.AttachConfig{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we add the ctx or event id for log stuff?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems we can't provide enough useful information in this function
So I do the log in function
handleResizing
from which we could know the container and exec id