Skip to content

Commit

Permalink
support websocket for exec and attach
Browse files Browse the repository at this point in the history
Signed-off-by: haozi007 <[email protected]>
  • Loading branch information
duguhaotian committed Dec 5, 2023
1 parent 046e620 commit e047447
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
14 changes: 10 additions & 4 deletions cmd/crictl/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ var runtimeAttachCommand = &cli.Command{
Aliases: []string{"i"},
Usage: "Keep STDIN open",
},
&cli.BoolFlag{
Name: "websocket",
Aliases: []string{"w"},
Usage: "Use websocket protocol",
},
},
Action: func(c *cli.Context) error {
id := c.Args().First()
Expand All @@ -63,9 +68,10 @@ var runtimeAttachCommand = &cli.Command{
defer cancel()

var opts = attachOptions{
id: id,
tty: c.Bool("tty"),
stdin: c.Bool("stdin"),
id: id,
tty: c.Bool("tty"),
stdin: c.Bool("stdin"),
websocket: c.Bool("websocket"),
}
if err = Attach(ctx, runtimeClient, opts); err != nil {
return fmt.Errorf("attaching running container failed: %w", err)
Expand Down Expand Up @@ -109,5 +115,5 @@ func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachO
}

logrus.Debugf("Attach URL: %v", URL)
return stream(ctx, opts.stdin, opts.tty, URL)
return stream(ctx, opts.stdin, opts.tty, opts.websocket, URL)
}
35 changes: 27 additions & 8 deletions cmd/crictl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
mobyterm "github.com/moby/term"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"k8s.io/apimachinery/pkg/util/httpstream"
restclient "k8s.io/client-go/rest"
remoteclient "k8s.io/client-go/tools/remotecommand"
internalapi "k8s.io/cri-api/pkg/apis"
Expand Down Expand Up @@ -67,6 +68,11 @@ var runtimeExecCommand = &cli.Command{
Aliases: []string{"i"},
Usage: "Keep STDIN open",
},
&cli.BoolFlag{
Name: "websocket",
Aliases: []string{"w"},
Usage: "Use websocket protocol",
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
Expand All @@ -79,11 +85,12 @@ var runtimeExecCommand = &cli.Command{
}

var opts = execOptions{
id: c.Args().First(),
timeout: c.Int64("timeout"),
tty: c.Bool("tty"),
stdin: c.Bool("interactive"),
cmd: c.Args().Slice()[1:],
id: c.Args().First(),
timeout: c.Int64("timeout"),
tty: c.Bool("tty"),
stdin: c.Bool("interactive"),
websocket: c.Bool("websocket"),
cmd: c.Args().Slice()[1:],
}
if c.Bool("sync") {
exitCode, err := ExecSync(runtimeClient, opts)
Expand Down Expand Up @@ -160,15 +167,27 @@ func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptio
}

logrus.Debugf("Exec URL: %v", URL)
return stream(ctx, opts.stdin, opts.tty, URL)
return stream(ctx, opts.stdin, opts.tty, opts.websocket, URL)
}

func stream(ctx context.Context, in, tty bool, url *url.URL) error {
executor, err := remoteclient.NewSPDYExecutor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}, "POST", url)
func stream(ctx context.Context, in, tty, websocket bool, url *url.URL) error {
config := &restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}
executor, err := remoteclient.NewSPDYExecutor(config, "POST", url)
if err != nil {
return err
}

if websocket {
websocketExecutor, err := remoteclient.NewWebSocketExecutor(config, "GET", url.String())
if err != nil {
return err
}
executor, err = remoteclient.NewFallbackExecutor(websocketExecutor, executor, httpstream.IsUpgradeFailure)
if err != nil {
return err
}
}

stdin, stdout, stderr := mobyterm.StdStreams()
streamOptions := remoteclient.StreamOptions{
Stdout: stdout,
Expand Down
4 changes: 4 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ type execOptions struct {
tty bool
// Whether to stream stdin
stdin bool
// Whether to use websocket
websocket bool
// Command to exec
cmd []string
}
Expand All @@ -122,6 +124,8 @@ type attachOptions struct {
tty bool
// Whether pass Stdin to container
stdin bool
// Whether to use websocket
websocket bool
}

type portforwardOptions struct {
Expand Down

0 comments on commit e047447

Please sign in to comment.