Skip to content

Commit c373b9d

Browse files
committed
Unittests and example for portforwarding ability added in python-base.
1 parent b5603d8 commit c373b9d

File tree

2 files changed

+224
-1
lines changed

2 files changed

+224
-1
lines changed

examples/pod_portforward.py

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Copyright 2020 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Shows the functionality of portforward streaming using an nginx container.
17+
"""
18+
19+
import socket
20+
import time
21+
import urllib.request
22+
23+
from kubernetes import config
24+
from kubernetes.client import Configuration
25+
from kubernetes.client.api import core_v1_api
26+
from kubernetes.client.rest import ApiException
27+
from kubernetes.stream import portforward
28+
29+
30+
def portforward_commands(api_instance):
31+
name = 'portforward-example'
32+
resp = None
33+
try:
34+
resp = api_instance.read_namespaced_pod(name=name,
35+
namespace='default')
36+
except ApiException as e:
37+
if e.status != 404:
38+
print("Unknown error: %s" % e)
39+
exit(1)
40+
41+
if not resp:
42+
print("Pod %s does not exist. Creating it..." % name)
43+
pod_manifest = {
44+
'apiVersion': 'v1',
45+
'kind': 'Pod',
46+
'metadata': {
47+
'name': name
48+
},
49+
'spec': {
50+
'containers': [{
51+
'image': 'nginx',
52+
'name': 'nginx',
53+
}]
54+
}
55+
}
56+
resp = api_instance.create_namespaced_pod(body=pod_manifest,
57+
namespace='default')
58+
while True:
59+
resp = api_instance.read_namespaced_pod(name=name,
60+
namespace='default')
61+
if resp.status.phase != 'Pending':
62+
break
63+
time.sleep(1)
64+
print("Done.")
65+
66+
pf = portforward(api_instance.connect_get_namespaced_pod_portforward,
67+
name, 'default',
68+
ports='80')
69+
http = pf.socket(80)
70+
http.settimeout(1)
71+
http.sendall(b'GET / HTTP/1.1\r\n')
72+
http.sendall(b'Host: 127.0.0.1\r\n')
73+
http.sendall(b'Accept: */*\r\n')
74+
http.sendall(b'\r\n')
75+
response = b''
76+
while True:
77+
try:
78+
response += http.recv(1024)
79+
except socket.timeout:
80+
break
81+
print(response.decode('utf-8'))
82+
http.close()
83+
84+
# Monkey patch socket.create_connection which is used by http.client and
85+
# urllib.request. The same can be done with urllib3.util.connection.create_connection
86+
# if the "requests" package is used.
87+
def kubernetes_create_connection(address, *args, **kwargs):
88+
dns_name = address[0]
89+
if isinstance(dns_name, bytes):
90+
dns_name = dns_name.decode()
91+
# Look for "<pod-name>.<namspace>.kubernetes" dns names and if found
92+
# provide a socket that is port forwarded to the kuberntest pod.
93+
dns_name = dns_name.split(".")
94+
if len(dns_name) != 3 or dns_name[2] != "kubernetes":
95+
return socket_create_connection(address, *args, **kwargs)
96+
pf = portforward(api_instance.connect_get_namespaced_pod_portforward,
97+
dns_name[0], dns_name[1], ports=str(address[1]))
98+
return pf.socket(address[1])
99+
100+
socket_create_connection = socket.create_connection
101+
socket.create_connection = kubernetes_create_connection
102+
103+
# Access the nginx http server using the "<pod-name>.<namespace>.kubernetes" dns name.
104+
response = urllib.request.urlopen('http://%s.default.kubernetes' % name)
105+
html = response.read().decode('utf-8')
106+
response.close()
107+
print('Status:', response.status)
108+
print(html)
109+
110+
111+
def main():
112+
config.load_kube_config()
113+
c = Configuration()
114+
c.assert_hostname = False
115+
Configuration.set_default(c)
116+
core_v1 = core_v1_api.CoreV1Api()
117+
118+
portforward_commands(core_v1)
119+
120+
121+
if __name__ == '__main__':
122+
main()

kubernetes/e2e_test/test_client.py

+102-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
# under the License.
1414

1515
import json
16+
import socket
1617
import time
1718
import unittest
19+
import urllib.request
1820
import uuid
1921

2022
from kubernetes.client import api_client
2123
from kubernetes.client.api import core_v1_api
2224
from kubernetes.e2e_test import base
23-
from kubernetes.stream import stream
25+
from kubernetes.stream import stream, portforward
2426
from kubernetes.stream.ws_client import ERROR_CHANNEL
2527

2628

@@ -119,6 +121,7 @@ def test_pod_apis(self):
119121

120122
resp = api.delete_namespaced_pod(name=name, body={},
121123
namespace='default')
124+
122125
def test_exit_code(self):
123126
client = api_client.ApiClient(configuration=self.config)
124127
api = core_v1_api.CoreV1Api(client)
@@ -159,6 +162,104 @@ def test_exit_code(self):
159162
resp = api.delete_namespaced_pod(name=name, body={},
160163
namespace='default')
161164

165+
def test_portforward_raw(self):
166+
client = api_client.ApiClient(configuration=self.config)
167+
api = core_v1_api.CoreV1Api(client)
168+
169+
name = 'portforward-raw-' + short_uuid()
170+
pod_manifest = manifest_with_command(name, "while true;do nc -l -p 1234 -e /bin/cat; done")
171+
resp = api.create_namespaced_pod(body=pod_manifest,
172+
namespace='default')
173+
self.assertEqual(name, resp.metadata.name)
174+
self.assertTrue(resp.status.phase)
175+
176+
while True:
177+
resp = api.read_namespaced_pod(name=name,
178+
namespace='default')
179+
self.assertEqual(name, resp.metadata.name)
180+
self.assertTrue(resp.status.phase)
181+
if resp.status.phase != 'Pending':
182+
break
183+
time.sleep(1)
184+
185+
pf = portforward(api.connect_get_namespaced_pod_portforward,
186+
name, 'default',
187+
ports='1234')
188+
sock = pf.socket(1234)
189+
sock.settimeout(1)
190+
sent = b'Test port forwarding...'
191+
sock.sendall(sent)
192+
reply = b''
193+
while True:
194+
try:
195+
reply += sock.recv(1024)
196+
except socket.timeout:
197+
break
198+
self.assertEqual(sent, reply)
199+
sock.close()
200+
self.assertIsNone(pf.error(1234))
201+
202+
resp = api.delete_namespaced_pod(name=name, body={},
203+
namespace='default')
204+
205+
def test_portforward_http(self):
206+
client = api_client.ApiClient(configuration=self.config)
207+
api = core_v1_api.CoreV1Api(client)
208+
209+
name = 'portforward-http-' + short_uuid()
210+
pod_manifest = {
211+
'apiVersion': 'v1',
212+
'kind': 'Pod',
213+
'metadata': {
214+
'name': name
215+
},
216+
'spec': {
217+
'containers': [{
218+
'name': 'nginx',
219+
'image': 'nginx',
220+
}]
221+
}
222+
}
223+
224+
resp = api.create_namespaced_pod(body=pod_manifest,
225+
namespace='default')
226+
self.assertEqual(name, resp.metadata.name)
227+
self.assertTrue(resp.status.phase)
228+
229+
while True:
230+
resp = api.read_namespaced_pod(name=name,
231+
namespace='default')
232+
self.assertEqual(name, resp.metadata.name)
233+
self.assertTrue(resp.status.phase)
234+
if resp.status.phase != 'Pending':
235+
break
236+
time.sleep(1)
237+
238+
def kubernetes_create_connection(address, *args, **kwargs):
239+
dns_name = address[0]
240+
if isinstance(dns_name, bytes):
241+
dns_name = dns_name.decode()
242+
dns_name = dns_name.split(".")
243+
if len(dns_name) != 3 or dns_name[2] != "kubernetes":
244+
return socket_create_connection(address, *args, **kwargs)
245+
pf = portforward(api.connect_get_namespaced_pod_portforward,
246+
dns_name[0], dns_name[1], ports=str(address[1]))
247+
return pf.socket(address[1])
248+
249+
socket_create_connection = socket.create_connection
250+
try:
251+
socket.create_connection = kubernetes_create_connection
252+
response = urllib.request.urlopen('http://%s.default.kubernetes/' % name)
253+
html = response.read().decode('utf-8')
254+
finally:
255+
socket.create_connection = socket_create_connection
256+
257+
self.assertEqual(response.status, 200)
258+
self.assertTrue('<h1>Welcome to nginx!</h1>' in html)
259+
260+
resp = api.delete_namespaced_pod(name=name, body={},
261+
namespace='default')
262+
162263
def test_service_apis(self):
163264
client = api_client.ApiClient(configuration=self.config)
164265
api = core_v1_api.CoreV1Api(client)

0 commit comments

Comments
 (0)