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
111 changes: 111 additions & 0 deletions kubernetes/aio/e2e_test/test_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import asyncio
import uuid
from unittest import IsolatedAsyncioTestCase

from aiohttp import WSMsgType

from kubernetes.aio import client
from kubernetes.aio.e2e_test import base
from kubernetes.aio.stream import WsApiClient
from kubernetes.aio.stream.ws_client import (
CLOSE_CHANNEL, ERROR_CHANNEL, STDERR_CHANNEL, STDIN_CHANNEL,
STDOUT_CHANNEL, V5_CHANNEL_PROTOCOL,
)


class TestExec(IsolatedAsyncioTestCase):
@classmethod
def setUpClass(cls):
cls.config = base.get_e2e_configuration()

async def test_v5_exec(self):
name = 'async-exec-' + uuid.uuid4().hex
namespace = 'default'
pod = {
'apiVersion': 'v1',
'kind': 'Pod',
'metadata': {'name': name},
'spec': {
'containers': [{
'name': 'busybox',
'image': 'busybox:1.36',
'command': ['/bin/sh', '-c', 'sleep 3600'],
}],
},
}
async with client.ApiClient(configuration=self.config) as api_client:
core = client.CoreV1Api(api_client)
await core.create_namespaced_pod(namespace, pod)
try:
async def wait_ready():
while True:
current = await core.read_namespaced_pod(name, namespace)
if current.status.phase in ('Failed', 'Succeeded'):
self.fail('Pod terminated before becoming ready: %s'
% current.status)
if (current.status.container_statuses and
current.status.container_statuses[0].ready):
return
await asyncio.sleep(1)

await asyncio.wait_for(wait_ready(), timeout=180)

async with WsApiClient(configuration=self.config) as ws_client:
exec_api = client.CoreV1Api(ws_client)
result = await asyncio.wait_for(
exec_api.connect_get_namespaced_pod_exec(
name, namespace,
command=['/bin/sh', '-c',
'printf stdout; printf stderr >&2'],
stderr=True, stdin=False, stdout=True, tty=False,
),
timeout=30,
)
self.assertIn('stdout', result)
self.assertIn('stderr', result)

websocket = await exec_api.connect_get_namespaced_pod_exec_without_preload_content(
name, namespace,
command=['/bin/sh', '-c',
'value=$(cat); printf "received:%s" "$value"; '
'printf error-output >&2'],
stderr=True, stdin=True, stdout=True, tty=False,
)
async with websocket as ws:
self.assertEqual(V5_CHANNEL_PROTOCOL, ws.protocol)
await ws.send_bytes(bytes([STDIN_CHANNEL]) + b'hello')
await ws.send_bytes(bytes([CLOSE_CHANNEL, STDIN_CHANNEL]))

async def receive_output():
channels = {
STDOUT_CHANNEL: b'',
STDERR_CHANNEL: b'',
ERROR_CHANNEL: b'',
}
async for message in ws:
self.assertEqual(WSMsgType.BINARY, message.type)
channel, data = message.data[0], message.data[1:]
if channel in channels:
channels[channel] += data
return channels

channels = await asyncio.wait_for(receive_output(), timeout=30)
self.assertEqual(b'received:hello', channels[STDOUT_CHANNEL])
self.assertEqual(b'error-output', channels[STDERR_CHANNEL])
self.assertEqual(
0, ws_client.parse_error_data(channels[ERROR_CHANNEL])
)
finally:
await core.delete_namespaced_pod(name, namespace)
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