Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions kubernetes/aio/stream/ws_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
STDERR_CHANNEL = 2
ERROR_CHANNEL = 3
RESIZE_CHANNEL = 4
CLOSE_CHANNEL = 255

V4_CHANNEL_PROTOCOL = 'v4.channel.k8s.io'
V5_CHANNEL_PROTOCOL = 'v5.channel.k8s.io'


def get_websocket_url(url):
Expand Down Expand Up @@ -57,6 +61,12 @@ def getheader(self, name, default=None):


class WsApiClient(ApiClient):
"""Async exec transport.

Preloaded responses collect UTF-8 stdout and stderr and honor v5 channel
close frames. Without preloading, the raw aiohttp WebSocket is returned;
callers are responsible for channel framing and close messages.
"""

def __init__(self, configuration=None, header_name=None, header_value=None,
cookie=None, pool_threads=1, heartbeat=None):
Expand Down Expand Up @@ -113,8 +123,19 @@ async def request(self, method, url, query_params=None, headers=None,

if headers is None:
headers = {}
if 'sec-websocket-protocol' not in headers:
headers['sec-websocket-protocol'] = 'v4.channel.k8s.io'
protocol_header = next(
(key for key in headers if key.lower() == 'sec-websocket-protocol'),
None,
)
if protocol_header is None:
protocols = [V5_CHANNEL_PROTOCOL, V4_CHANNEL_PROTOCOL]
headers['sec-websocket-protocol'] = ','.join(protocols)
else:
protocols = [
protocol.strip()
for protocol in headers[protocol_header].split(',')
if protocol.strip()
]

if query_params:
url += '?' + urlencode(query_params)
Expand All @@ -128,22 +149,37 @@ async def request(self, method, url, query_params=None, headers=None,
if _preload_content:

resp_all = ''
closed_channels = set()
async with pool_manager.ws_connect(
url, headers=headers, heartbeat=self.heartbeat
url, headers=headers, protocols=protocols,
heartbeat=self.heartbeat,
) as ws:
async for msg in ws:
msg = msg.data.decode('utf-8')
if len(msg) > 1:
channel = ord(msg[0])
data = msg[1:]
if data:
if channel in [STDOUT_CHANNEL, STDERR_CHANNEL]:
resp_all += data
data = msg.data
if len(data) > 1:
channel = data[0] if isinstance(data, bytes) else ord(data[0])
if (
channel == CLOSE_CHANNEL
and ws.protocol == V5_CHANNEL_PROTOCOL
):
closed_channel = (
data[1] if isinstance(data, bytes) else ord(data[1])
)
closed_channels.add(closed_channel)
continue
if channel in closed_channels:
continue
data = data[1:]
if isinstance(data, bytes):
data = data.decode('utf-8')
if data and channel in [STDOUT_CHANNEL, STDERR_CHANNEL]:
resp_all += data

return WsResponse(200, resp_all.encode('utf-8'))

else:

return pool_manager.ws_connect(
url, headers=headers, heartbeat=self.heartbeat
url, headers=headers, protocols=protocols,
heartbeat=self.heartbeat,
)
62 changes: 59 additions & 3 deletions kubernetes/aio/stream/ws_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@


class WsMock:
def __init__(self):
def __init__(self, messages=None, protocol=None):
self.iter = 0
self.messages = messages
self.protocol = protocol

def __aiter__(self):
return self
Expand All @@ -35,6 +37,10 @@ async def __aenter__(self):

async def __anext__(self):
self.iter += 1
if self.messages is not None:
if self.iter > len(self.messages):
raise StopAsyncIteration
return self.messages[self.iter - 1]
if self.iter > 5:
raise StopAsyncIteration
return WsResponse(200, (chr(1) + 'mock').encode('utf-8'))
Expand Down Expand Up @@ -75,10 +81,13 @@ async def test_exec_ws(self):
'wss://localhost/api/v1/namespaces/namespace/pods/pod/exec?'
'command=mock-command&stderr=true&stdin=false&stdout=true&tty=false',
headers={
'sec-websocket-protocol': 'v4.channel.k8s.io',
'sec-websocket-protocol': (
'v5.channel.k8s.io,v4.channel.k8s.io'
),
'Accept': '*/*',
'User-Agent': api_client.user_agent
},
protocols=['v5.channel.k8s.io', 'v4.channel.k8s.io'],
heartbeat=None
)

Expand All @@ -102,13 +111,60 @@ async def test_exec_ws_with_heartbeat(self):
'wss://localhost/api/v1/namespaces/namespace/pods/pod/exec?'
'command=mock-command&stderr=true&stdin=false&stdout=true&tty=false',
headers={
'sec-websocket-protocol': 'v4.channel.k8s.io',
'sec-websocket-protocol': (
'v5.channel.k8s.io,v4.channel.k8s.io'
),
'Accept': '*/*',
'User-Agent': api_client.user_agent
},
protocols=['v5.channel.k8s.io', 'v4.channel.k8s.io'],
heartbeat=30
)

async def test_exec_ws_preserves_explicit_subprotocol(self):
mock = Mock()
mock.RESTClientObject.return_value.pool_manager = mock
with patch('kubernetes.aio.client.api_client.rest', mock):
api_client = WsApiClient()
await api_client.request(
'GET',
'https://localhost/api',
headers={'Sec-WebSocket-Protocol': 'v4.channel.k8s.io'},
_preload_content=False,
)

mock.ws_connect.assert_called_once_with(
'wss://localhost/api',
headers={'Sec-WebSocket-Protocol': 'v4.channel.k8s.io'},
protocols=['v4.channel.k8s.io'],
heartbeat=None,
)

async def test_exec_ws_v5_channel_close_stops_channel_data(self):
mock = Mock()
mock.RESTClientObject.return_value.pool_manager = mock
mock.ws_connect.return_value = WsMock(
messages=[
WsResponse(200, b'\x01before'),
WsResponse(200, b'\xff\x01'),
WsResponse(200, b'\x01after'),
WsResponse(200, b'\x02stderr'),
],
protocol='v5.channel.k8s.io',
)
with patch('kubernetes.aio.client.api_client.rest', mock):
api_client = WsApiClient()
response = await api_client.request(
'GET',
'https://localhost/api',
)

self.assertEqual(await response.read(), b'beforestderr')
self.assertEqual(
['v5.channel.k8s.io', 'v4.channel.k8s.io'],
mock.ws_connect.call_args.kwargs['protocols'],
)

def test_parse_error_data_success(self):
error_data = '{"metadata":{},"status":"Success"}'
return_code = WsApiClient.parse_error_data(error_data)
Expand Down
36 changes: 33 additions & 3 deletions kubernetes/aio/test/test_generated_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ async def asyncSetUp(self):
self.response_status = 200
self.response_headers = {}
self.responses = None
self.websocket_protocol = 'v4.channel.k8s.io'
self.websocket_messages = (b'\x01remote output',)
self.negotiated_websocket_protocol = None
app = web.Application()
app.router.add_route('*', '/{path:.*}', self._handle_request)
self.runner = web.AppRunner(app)
Expand All @@ -69,10 +72,12 @@ async def _handle_request(self, request):
self.requests.append((request, body))
if request.path.endswith('/exec'):
response = web.WebSocketResponse(
protocols=('v4.channel.k8s.io',),
protocols=(self.websocket_protocol,),
)
await response.prepare(request)
await response.send_bytes(b'\x01remote output')
self.negotiated_websocket_protocol = response.ws_protocol
for message in self.websocket_messages:
await response.send_bytes(message)
await response.close()
return response
if request.query.get('watch') == 'true':
Expand Down Expand Up @@ -302,9 +307,34 @@ async def test_websocket_exec_returns_buffered_output(self):

self.assertEqual('remote output', output)
self.assertEqual(
'v4.channel.k8s.io',
'v5.channel.k8s.io,v4.channel.k8s.io',
self.requests[-1][0].headers['Sec-WebSocket-Protocol'],
)
self.assertEqual(
'v4.channel.k8s.io',
self.negotiated_websocket_protocol,
)

async def test_websocket_exec_v5_channel_close(self):
self.websocket_protocol = 'v5.channel.k8s.io'
self.websocket_messages = (
b'\x01before',
b'\xff\x01',
b'\x01after',
b'\x02stderr',
)
websocket_client = WsApiClient(self.configuration)
self.addAsyncCleanup(websocket_client.close)

output = await CoreV1Api(websocket_client).connect_get_namespaced_pod_exec(
'pod', 'default', command=['echo', 'hello'], stdout=True,
)

self.assertEqual('beforestderr', output)
self.assertEqual(
'v5.channel.k8s.io',
self.negotiated_websocket_protocol,
)

async def test_websocket_exec_preserves_interactive_raw_stream(self):
websocket_client = WsApiClient(self.configuration)
Expand Down