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 15, 2023
1 parent a3c6835 commit 81d8f2f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
17 changes: 16 additions & 1 deletion cmd/crictl/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ var runtimeAttachCommand = &cli.Command{
Aliases: []string{"i"},
Usage: "Keep STDIN open",
},
&cli.StringFlag{
Name: "transport-protocol",
Aliases: []string{"p"},
Usage: "protocol of transport, One of: spdy|websocket",
Value: "spdy",
},
&cli.BoolFlag{
Name: "transport-vesion5",
Usage: "Use v5.channel.k8s.io to transport",
Value: true,
},
},
Action: func(c *cli.Context) error {
id := c.Args().First()
Expand All @@ -66,6 +77,10 @@ var runtimeAttachCommand = &cli.Command{
id: id,
tty: c.Bool("tty"),
stdin: c.Bool("stdin"),
transport: transport{
protocol: c.String("transport-protocol"),
isV5: c.Bool("transport-vesion5"),
},
}
if err = Attach(ctx, runtimeClient, opts); err != nil {
return fmt.Errorf("attaching running container failed: %w", err)
Expand Down Expand Up @@ -109,5 +124,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.transport, URL)
}
42 changes: 38 additions & 4 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/remotecommand"
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,17 @@ var runtimeExecCommand = &cli.Command{
Aliases: []string{"i"},
Usage: "Keep STDIN open",
},
&cli.StringFlag{
Name: "transport-protocol",
Aliases: []string{"p"},
Usage: "Protocol of transport, One of: spdy|websocket",
Value: "spdy",
},
&cli.BoolFlag{
Name: "transport-vesion5",
Usage: "Use v5.channel.k8s.io to transport",
Value: true,
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 2 {
Expand All @@ -83,7 +95,11 @@ var runtimeExecCommand = &cli.Command{
timeout: c.Int64("timeout"),
tty: c.Bool("tty"),
stdin: c.Bool("interactive"),
cmd: c.Args().Slice()[1:],
transport: transport{
protocol: c.String("transport-protocol"),
isV5: c.Bool("transport-version"),
},
cmd: c.Args().Slice()[1:],
}
if c.Bool("sync") {
exitCode, err := ExecSync(runtimeClient, opts)
Expand Down Expand Up @@ -160,11 +176,29 @@ 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.transport, URL)
}

func getExecutor(ts transport, url *url.URL) (exec remoteclient.Executor, err error) {
config := &restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}

switch ts.protocol {
case "websocket":
if ts.isV5 {
exec, err = remoteclient.NewWebSocketExecutor(config, "GET", url.String())
} else {
// support old version of websocket
exec, err = remoteclient.NewWebSocketExecutorForProtocols(config, "GET", url.String(), remotecommand.SupportedStreamingProtocols...)
}
default:
exec, err = remoteclient.NewSPDYExecutor(config, "POST", url)
}

return
}

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 bool, ts transport, url *url.URL) error {
executor, err := getExecutor(ts, url)
if err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ type listOptions struct {
resolveImagePath bool
}

type transport struct {
protocol string
isV5 bool
}

type execOptions struct {
// id of container
id string
Expand All @@ -112,6 +117,8 @@ type execOptions struct {
tty bool
// Whether to stream stdin
stdin bool
// protocol of transport
transport
// Command to exec
cmd []string
}
Expand All @@ -122,6 +129,8 @@ type attachOptions struct {
tty bool
// Whether pass Stdin to container
stdin bool
// protocol of transport
transport
}

type portforwardOptions struct {
Expand Down

0 comments on commit 81d8f2f

Please sign in to comment.