Skip to content

Commit d4b9408

Browse files
committed
support websocket for exec and attach
Signed-off-by: haozi007 <duguhaotian@gmail.com>
1 parent a3c6835 commit d4b9408

File tree

3 files changed

+55
-12
lines changed

3 files changed

+55
-12
lines changed

cmd/crictl/attach.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ var runtimeAttachCommand = &cli.Command{
4343
Aliases: []string{"i"},
4444
Usage: "Keep STDIN open",
4545
},
46+
&cli.StringFlag{
47+
Name: "transport",
48+
Aliases: []string{"p"},
49+
Usage: "transport protocol, One of: spdy|websocket",
50+
Value: "spdy",
51+
},
4652
},
4753
Action: func(c *cli.Context) error {
4854
id := c.Args().First()
@@ -63,9 +69,10 @@ var runtimeAttachCommand = &cli.Command{
6369
defer cancel()
6470

6571
var opts = attachOptions{
66-
id: id,
67-
tty: c.Bool("tty"),
68-
stdin: c.Bool("stdin"),
72+
id: id,
73+
tty: c.Bool("tty"),
74+
stdin: c.Bool("stdin"),
75+
transport: c.String("transport"),
6976
}
7077
if err = Attach(ctx, runtimeClient, opts); err != nil {
7178
return fmt.Errorf("attaching running container failed: %w", err)
@@ -109,5 +116,5 @@ func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachO
109116
}
110117

111118
logrus.Debugf("Attach URL: %v", URL)
112-
return stream(ctx, opts.stdin, opts.tty, URL)
119+
return stream(ctx, opts.stdin, opts.tty, opts.transport, URL)
113120
}

cmd/crictl/exec.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
mobyterm "github.com/moby/term"
2727
"github.com/sirupsen/logrus"
2828
"github.com/urfave/cli/v2"
29+
"k8s.io/apimachinery/pkg/util/httpstream"
2930
restclient "k8s.io/client-go/rest"
3031
remoteclient "k8s.io/client-go/tools/remotecommand"
3132
internalapi "k8s.io/cri-api/pkg/apis"
@@ -67,6 +68,12 @@ var runtimeExecCommand = &cli.Command{
6768
Aliases: []string{"i"},
6869
Usage: "Keep STDIN open",
6970
},
71+
&cli.StringFlag{
72+
Name: "transport",
73+
Aliases: []string{"p"},
74+
Usage: "transport protocol, One of: spdy|websocket",
75+
Value: "spdy",
76+
},
7077
},
7178
Action: func(c *cli.Context) error {
7279
if c.NArg() < 2 {
@@ -79,11 +86,12 @@ var runtimeExecCommand = &cli.Command{
7986
}
8087

8188
var opts = execOptions{
82-
id: c.Args().First(),
83-
timeout: c.Int64("timeout"),
84-
tty: c.Bool("tty"),
85-
stdin: c.Bool("interactive"),
86-
cmd: c.Args().Slice()[1:],
89+
id: c.Args().First(),
90+
timeout: c.Int64("timeout"),
91+
tty: c.Bool("tty"),
92+
stdin: c.Bool("interactive"),
93+
transport: c.String("transport"),
94+
cmd: c.Args().Slice()[1:],
8795
}
8896
if c.Bool("sync") {
8997
exitCode, err := ExecSync(runtimeClient, opts)
@@ -160,11 +168,35 @@ func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptio
160168
}
161169

162170
logrus.Debugf("Exec URL: %v", URL)
163-
return stream(ctx, opts.stdin, opts.tty, URL)
171+
return stream(ctx, opts.stdin, opts.tty, opts.transport, URL)
172+
}
173+
174+
func getExecutor(protocol string, url *url.URL) (exec remoteclient.Executor, err error) {
175+
config := &restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}
176+
177+
switch protocol {
178+
case "spdy":
179+
exec, err = remoteclient.NewSPDYExecutor(config, "POST", url)
180+
case "websocket":
181+
exec, err = remoteclient.NewWebSocketExecutor(config, "GET", url.String())
182+
default:
183+
exec, err = remoteclient.NewSPDYExecutor(config, "POST", url)
184+
if err != nil {
185+
break
186+
}
187+
var wexec remoteclient.Executor
188+
wexec, err = remoteclient.NewWebSocketExecutor(config, "GET", url.String())
189+
if err != nil {
190+
break
191+
}
192+
exec, err = remoteclient.NewFallbackExecutor(wexec, exec, httpstream.IsUpgradeFailure)
193+
}
194+
195+
return
164196
}
165197

166-
func stream(ctx context.Context, in, tty bool, url *url.URL) error {
167-
executor, err := remoteclient.NewSPDYExecutor(&restclient.Config{TLSClientConfig: restclient.TLSClientConfig{Insecure: true}}, "POST", url)
198+
func stream(ctx context.Context, in, tty bool, protocol string, url *url.URL) error {
199+
executor, err := getExecutor(protocol, url)
168200
if err != nil {
169201
return err
170202
}

cmd/crictl/util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ type execOptions struct {
112112
tty bool
113113
// Whether to stream stdin
114114
stdin bool
115+
// protocol of transport
116+
transport string
115117
// Command to exec
116118
cmd []string
117119
}
@@ -122,6 +124,8 @@ type attachOptions struct {
122124
tty bool
123125
// Whether pass Stdin to container
124126
stdin bool
127+
// protocol of transport
128+
transport string
125129
}
126130

127131
type portforwardOptions struct {

0 commit comments

Comments
 (0)