Skip to content

Commit 85bcb15

Browse files
committed
Support websocket transport for exec and attach
This allows end users to choose the transport to be used as well as runtime developers to experiment on feature development. Supersedes: #1305 Signed-off-by: Sascha Grunert <[email protected]>
1 parent f5e5b37 commit 85bcb15

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

Diff for: cmd/crictl/attach.go

+7-1
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: transportFlag,
48+
Aliases: []string{"r"},
49+
Value: transportSpdy,
50+
Usage: fmt.Sprintf("Transport protocol to be used, must be one of: %s, %s", transportSpdy, transportWebsocket),
51+
},
4652
},
4753
Action: func(c *cli.Context) error {
4854
id := c.Args().First()
@@ -109,5 +115,5 @@ func Attach(ctx context.Context, client internalapi.RuntimeService, opts attachO
109115
}
110116

111117
logrus.Debugf("Attach URL: %v", URL)
112-
return stream(ctx, opts.stdin, opts.tty, URL)
118+
return stream(ctx, opts.stdin, opts.tty, opts.transport, URL)
113119
}

Diff for: cmd/crictl/exec.go

+37-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
mobyterm "github.com/moby/term"
2727
"github.com/sirupsen/logrus"
2828
"github.com/urfave/cli/v2"
29-
restclient "k8s.io/client-go/rest"
29+
"k8s.io/client-go/rest"
3030
remoteclient "k8s.io/client-go/tools/remotecommand"
3131
internalapi "k8s.io/cri-api/pkg/apis"
3232
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -37,6 +37,10 @@ const (
3737
// TODO: make this configurable in kubelet.
3838
kubeletURLSchema = "http"
3939
kubeletURLHost = "http://127.0.0.1:10250"
40+
41+
transportFlag = "transport"
42+
transportWebsocket = "websocket"
43+
transportSpdy = "spdy"
4044
)
4145

4246
const detachSequence = "ctrl-p,ctrl-q"
@@ -67,6 +71,12 @@ var runtimeExecCommand = &cli.Command{
6771
Aliases: []string{"i"},
6872
Usage: "Keep STDIN open",
6973
},
74+
&cli.StringFlag{
75+
Name: transportFlag,
76+
Aliases: []string{"r"},
77+
Value: transportSpdy,
78+
Usage: fmt.Sprintf("Transport protocol to be used, must be one of: %s, %s", transportSpdy, transportWebsocket),
79+
},
7080
},
7181
Action: func(c *cli.Context) error {
7282
if c.NArg() < 2 {
@@ -79,11 +89,12 @@ var runtimeExecCommand = &cli.Command{
7989
}
8090

8191
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:],
92+
id: c.Args().First(),
93+
timeout: c.Int64("timeout"),
94+
tty: c.Bool("tty"),
95+
stdin: c.Bool("interactive"),
96+
cmd: c.Args().Slice()[1:],
97+
transport: c.String(transportFlag),
8798
}
8899
if c.Bool("sync") {
89100
exitCode, err := ExecSync(runtimeClient, opts)
@@ -160,13 +171,13 @@ func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptio
160171
}
161172

162173
logrus.Debugf("Exec URL: %v", URL)
163-
return stream(ctx, opts.stdin, opts.tty, URL)
174+
return stream(ctx, opts.stdin, opts.tty, opts.transport, URL)
164175
}
165176

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)
177+
func stream(ctx context.Context, in, tty bool, transport string, url *url.URL) error {
178+
executor, err := getExecutor(transport, url)
168179
if err != nil {
169-
return err
180+
return fmt.Errorf("get executor: %w", err)
170181
}
171182

172183
stdin, stdout, stderr := mobyterm.StdStreams()
@@ -204,3 +215,19 @@ func stream(ctx context.Context, in, tty bool, url *url.URL) error {
204215
streamOptions.TerminalSizeQueue = t.MonitorSize(t.GetSize())
205216
return t.Safe(func() error { return executor.StreamWithContext(ctx, streamOptions) })
206217
}
218+
219+
func getExecutor(transport string, url *url.URL) (exec remoteclient.Executor, err error) {
220+
config := &rest.Config{TLSClientConfig: rest.TLSClientConfig{Insecure: true}}
221+
222+
switch transport {
223+
case transportSpdy:
224+
return remoteclient.NewSPDYExecutor(config, "POST", url)
225+
226+
case transportWebsocket:
227+
return remoteclient.NewWebSocketExecutor(config, "GET", url.String())
228+
229+
default:
230+
return nil, fmt.Errorf("unknown transport: %s", transport)
231+
232+
}
233+
}

Diff for: cmd/crictl/util.go

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ type execOptions struct {
114114
stdin bool
115115
// Command to exec
116116
cmd []string
117+
// transport to be used
118+
transport string
117119
}
118120
type attachOptions struct {
119121
// id of container
@@ -122,6 +124,8 @@ type attachOptions struct {
122124
tty bool
123125
// Whether pass Stdin to container
124126
stdin bool
127+
// transport to be used
128+
transport string
125129
}
126130

127131
type portforwardOptions struct {

0 commit comments

Comments
 (0)