Skip to content

Commit 38a45ae

Browse files
committed
Remove Gorilla WebSocket Intercept
1 parent f46c96b commit 38a45ae

11 files changed

Lines changed: 1128 additions & 403 deletions

File tree

docs/openapi/gateway.swagger.json

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -996,29 +996,6 @@
996996
}
997997
}
998998
},
999-
"gatewayAttachToContainerRequest": {
1000-
"type": "object",
1001-
"properties": {
1002-
"containerId": {
1003-
"type": "string"
1004-
}
1005-
}
1006-
},
1007-
"gatewayAttachToContainerResponse": {
1008-
"type": "object",
1009-
"properties": {
1010-
"output": {
1011-
"type": "string"
1012-
},
1013-
"done": {
1014-
"type": "boolean"
1015-
},
1016-
"exitCode": {
1017-
"type": "integer",
1018-
"format": "int32"
1019-
}
1020-
}
1021-
},
1022999
"gatewayAuthorizeResponse": {
10231000
"type": "object",
10241001
"properties": {
@@ -1056,20 +1033,6 @@
10561033
}
10571034
}
10581035
},
1059-
"gatewayCheckpointContainerResponse": {
1060-
"type": "object",
1061-
"properties": {
1062-
"ok": {
1063-
"type": "boolean"
1064-
},
1065-
"checkpointId": {
1066-
"type": "string"
1067-
},
1068-
"errorMsg": {
1069-
"type": "string"
1070-
}
1071-
}
1072-
},
10731036
"gatewayCordonWorkerResponse": {
10741037
"type": "object",
10751038
"properties": {
@@ -1837,20 +1800,6 @@
18371800
}
18381801
}
18391802
},
1840-
"gatewayPutObjectResponse": {
1841-
"type": "object",
1842-
"properties": {
1843-
"ok": {
1844-
"type": "boolean"
1845-
},
1846-
"objectId": {
1847-
"type": "string"
1848-
},
1849-
"errorMsg": {
1850-
"type": "string"
1851-
}
1852-
}
1853-
},
18541803
"gatewayScaleDeploymentResponse": {
18551804
"type": "object",
18561805
"properties": {
@@ -2005,39 +1954,6 @@
20051954
}
20061955
}
20071956
},
2008-
"gatewaySyncContainerWorkspaceOperation": {
2009-
"type": "string",
2010-
"enum": [
2011-
"WRITE",
2012-
"DELETE",
2013-
"MOVED"
2014-
],
2015-
"default": "WRITE"
2016-
},
2017-
"gatewaySyncContainerWorkspaceRequest": {
2018-
"type": "object",
2019-
"properties": {
2020-
"containerId": {
2021-
"type": "string"
2022-
},
2023-
"path": {
2024-
"type": "string"
2025-
},
2026-
"newPath": {
2027-
"type": "string"
2028-
},
2029-
"isDir": {
2030-
"type": "boolean"
2031-
},
2032-
"data": {
2033-
"type": "string",
2034-
"format": "byte"
2035-
},
2036-
"op": {
2037-
"$ref": "#/definitions/gatewaySyncContainerWorkspaceOperation"
2038-
}
2039-
}
2040-
},
20411957
"gatewayTask": {
20421958
"type": "object",
20431959
"properties": {

docs/openapi/pod.swagger.json

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,46 @@
857857
"PodService"
858858
]
859859
}
860+
},
861+
"/pods/{stubId}/events": {
862+
"get": {
863+
"operationId": "PodService_SandboxListEvents",
864+
"responses": {
865+
"200": {
866+
"description": "A successful response.",
867+
"schema": {
868+
"$ref": "#/definitions/podPodSandboxListEventsResponse"
869+
}
870+
},
871+
"default": {
872+
"description": "An unexpected error response.",
873+
"schema": {
874+
"$ref": "#/definitions/rpcStatus"
875+
}
876+
}
877+
},
878+
"parameters": [
879+
{
880+
"name": "stubId",
881+
"in": "path",
882+
"required": true,
883+
"type": "string"
884+
},
885+
{
886+
"name": "searchAfter",
887+
"in": "query",
888+
"required": false,
889+
"type": "array",
890+
"items": {
891+
"type": "string"
892+
},
893+
"collectionFormat": "multi"
894+
}
895+
],
896+
"tags": [
897+
"PodService"
898+
]
899+
}
860900
}
861901
},
862902
"definitions": {
@@ -1103,6 +1143,36 @@
11031143
}
11041144
}
11051145
},
1146+
"podPodSandboxEvent": {
1147+
"type": "object",
1148+
"properties": {
1149+
"id": {
1150+
"type": "string"
1151+
},
1152+
"time": {
1153+
"type": "string"
1154+
},
1155+
"type": {
1156+
"type": "string"
1157+
},
1158+
"status": {
1159+
"type": "string"
1160+
},
1161+
"containerId": {
1162+
"type": "string"
1163+
},
1164+
"workerId": {
1165+
"type": "string"
1166+
},
1167+
"stubId": {
1168+
"type": "string"
1169+
},
1170+
"exitCode": {
1171+
"type": "integer",
1172+
"format": "int32"
1173+
}
1174+
}
1175+
},
11061176
"podPodSandboxExecResponse": {
11071177
"type": "object",
11081178
"properties": {
@@ -1194,6 +1264,30 @@
11941264
}
11951265
}
11961266
},
1267+
"podPodSandboxListEventsResponse": {
1268+
"type": "object",
1269+
"properties": {
1270+
"ok": {
1271+
"type": "boolean"
1272+
},
1273+
"errorMsg": {
1274+
"type": "string"
1275+
},
1276+
"events": {
1277+
"type": "array",
1278+
"items": {
1279+
"type": "object",
1280+
"$ref": "#/definitions/podPodSandboxEvent"
1281+
}
1282+
},
1283+
"nextCursor": {
1284+
"type": "array",
1285+
"items": {
1286+
"type": "string"
1287+
}
1288+
}
1289+
}
1290+
},
11971291
"podPodSandboxListFilesResponse": {
11981292
"type": "object",
11991293
"properties": {

manifests/kustomize/components/monitoring/victoria-metrics.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ valuesInline:
4646
regex: true
4747
- source_labels: [__meta_kubernetes_pod_container_port_number]
4848
action: keep
49-
regex: 9090
49+
regex: (9090|8112)
5050
---
5151
apiVersion: builtin
5252
kind: HelmChartInflationGenerator

pkg/abstractions/pod/pod.proto

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ service PodService {
157157
get : "/pods/{container_id}/urls"
158158
};
159159
}
160+
rpc SandboxListEvents(PodSandboxListEventsRequest)
161+
returns (PodSandboxListEventsResponse) {
162+
option (google.api.http) = {
163+
get : "/pods/{stub_id}/events"
164+
};
165+
}
160166
}
161167

162168
message CreatePodRequest {
@@ -418,4 +424,27 @@ message PodSandboxListUrlsResponse {
418424
bool ok = 1;
419425
map<int32, string> urls = 2;
420426
string error_msg = 3;
427+
}
428+
429+
message PodSandboxListEventsRequest {
430+
string stub_id = 1;
431+
repeated string search_after = 2;
432+
}
433+
434+
message PodSandboxListEventsResponse {
435+
bool ok = 1;
436+
string error_msg = 2;
437+
repeated PodSandboxEvent events = 3;
438+
repeated string next_cursor = 4;
439+
}
440+
441+
message PodSandboxEvent {
442+
string id = 1;
443+
string time = 2;
444+
string type = 3;
445+
string status = 4;
446+
string container_id = 5;
447+
string worker_id = 6;
448+
string stub_id = 7;
449+
int32 exit_code = 8;
421450
}

pkg/abstractions/pod/proxy.go

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -298,90 +298,40 @@ func (pb *PodProxyBuffer) handleConnection(conn *connection) {
298298
}
299299
defer pb.decrementContainerConnections(container.id)
300300

301-
// If it's a websocket request, upgrade the connection
302-
if websocket.IsWebSocketUpgrade(request) {
303-
pb.proxyWebSocket(conn, container, targetHost, subPath)
304-
return
305-
}
306-
307-
// Otherwise, use regular HTTP proxying
308301
targetURL, err := url.Parse("http://" + targetHost)
309302
if err != nil {
310303
conn.ctx.String(http.StatusInternalServerError, "Invalid target URL")
311304
return
312305
}
313306

307+
isWebSocket := websocket.IsWebSocketUpgrade(request)
308+
314309
proxy := httputil.NewSingleHostReverseProxy(targetURL)
315310
proxy.Transport = &http.Transport{
316311
DialContext: func(ctx context.Context, networkType, addr string) (net.Conn, error) {
317312
conn, err := network.ConnectToHost(ctx, addr, containerDialTimeoutDurationS, pb.tailscale, pb.tsConfig)
318313
if err == nil {
319-
abstractions.SetConnOptions(conn, true, connectionKeepAliveInterval, connectionReadTimeout)
314+
readTimeout := connectionReadTimeout
315+
if isWebSocket {
316+
readTimeout = -1 // No read deadline for WebSocket connections
317+
}
318+
abstractions.SetConnOptions(conn, true, connectionKeepAliveInterval, readTimeout)
320319
}
321320
return conn, err
322321
},
323322
}
323+
proxy.FlushInterval = -1 // Flush immediately for streaming/SSE
324324

325325
defer func() {
326326
if r := recover(); r != nil {
327327
log.Error().Err(err).Str("stubId", pb.stubId).Str("workspace", pb.workspace.Name).Msg("handled abort in pod proxy")
328328
}
329329
}()
330330

331-
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {}
332-
proxy.ServeHTTP(response, request)
333-
}
334-
335-
func (pb *PodProxyBuffer) proxyWebSocket(conn *connection, container container, addr string, path string) error {
336-
subprotocols := websocket.Subprotocols(conn.ctx.Request())
337-
338-
upgrader := websocket.Upgrader{
339-
CheckOrigin: func(r *http.Request) bool {
340-
return true // Allow all origins
341-
},
342-
Subprotocols: subprotocols,
343-
}
344-
345-
clientConn, err := upgrader.Upgrade(conn.ctx.Response().Writer, conn.ctx.Request(), nil)
346-
if err != nil {
347-
return err
331+
proxy.ErrorHandler = func(rw http.ResponseWriter, req *http.Request, err error) {
332+
log.Error().Err(err).Str("stubId", pb.stubId).Msg("pod proxy error")
348333
}
349-
defer clientConn.Close()
350-
351-
wsURL := url.URL{Scheme: "ws", Host: addr, Path: path, RawQuery: conn.ctx.Request().URL.RawQuery}
352-
dstDialer := websocket.Dialer{
353-
NetDialContext: network.GetDialer(addr, pb.tailscale, pb.tsConfig),
354-
Subprotocols: subprotocols,
355-
}
356-
357-
serverConn, _, err := dstDialer.Dial(wsURL.String(), nil)
358-
if err != nil {
359-
return err
360-
}
361-
defer serverConn.Close()
362-
363-
wg := sync.WaitGroup{}
364-
wg.Add(2)
365-
366-
proxyMessages := func(src, dst *websocket.Conn) {
367-
defer wg.Done()
368-
369-
for {
370-
messageType, message, err := src.ReadMessage()
371-
if err != nil {
372-
break
373-
}
374-
if err := dst.WriteMessage(messageType, message); err != nil {
375-
break
376-
}
377-
}
378-
}
379-
380-
go proxyMessages(clientConn, serverConn)
381-
go proxyMessages(serverConn, clientConn)
382-
383-
wg.Wait()
384-
return nil
334+
proxy.ServeHTTP(response, request)
385335
}
386336

387337
func (pb *PodProxyBuffer) discoverContainers() {

0 commit comments

Comments
 (0)