diff --git a/cmd/crictl/attach.go b/cmd/crictl/attach.go index 093241300e..04cb8241d0 100644 --- a/cmd/crictl/attach.go +++ b/cmd/crictl/attach.go @@ -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() @@ -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) @@ -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) } diff --git a/cmd/crictl/exec.go b/cmd/crictl/exec.go index 58b3068b32..3aebfbd787 100644 --- a/cmd/crictl/exec.go +++ b/cmd/crictl/exec.go @@ -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" @@ -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 { @@ -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) @@ -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 } diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index d9be5444cb..07cddebbcd 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -103,6 +103,11 @@ type listOptions struct { resolveImagePath bool } +type transport struct { + protocol string + isV5 bool +} + type execOptions struct { // id of container id string @@ -112,6 +117,8 @@ type execOptions struct { tty bool // Whether to stream stdin stdin bool + // protocol of transport + transport // Command to exec cmd []string } @@ -122,6 +129,8 @@ type attachOptions struct { tty bool // Whether pass Stdin to container stdin bool + // protocol of transport + transport } type portforwardOptions struct {