@@ -26,6 +26,7 @@ import (
26
26
mobyterm "github.com/moby/term"
27
27
"github.com/sirupsen/logrus"
28
28
"github.com/urfave/cli/v2"
29
+ "k8s.io/apimachinery/pkg/util/httpstream"
29
30
restclient "k8s.io/client-go/rest"
30
31
remoteclient "k8s.io/client-go/tools/remotecommand"
31
32
internalapi "k8s.io/cri-api/pkg/apis"
@@ -67,6 +68,12 @@ var runtimeExecCommand = &cli.Command{
67
68
Aliases : []string {"i" },
68
69
Usage : "Keep STDIN open" ,
69
70
},
71
+ & cli.StringFlag {
72
+ Name : "transport" ,
73
+ Aliases : []string {"p" },
74
+ Usage : "transport protocol, One of: spdy|websocket" ,
75
+ Value : "spdy" ,
76
+ },
70
77
},
71
78
Action : func (c * cli.Context ) error {
72
79
if c .NArg () < 2 {
@@ -79,11 +86,12 @@ var runtimeExecCommand = &cli.Command{
79
86
}
80
87
81
88
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 :],
87
95
}
88
96
if c .Bool ("sync" ) {
89
97
exitCode , err := ExecSync (runtimeClient , opts )
@@ -160,11 +168,35 @@ func Exec(ctx context.Context, client internalapi.RuntimeService, opts execOptio
160
168
}
161
169
162
170
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
164
196
}
165
197
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 )
168
200
if err != nil {
169
201
return err
170
202
}
0 commit comments