16
16
Shows the functionality of portforward streaming using an nginx container.
17
17
"""
18
18
19
+ import select
19
20
import socket
20
21
import time
21
22
import urllib .request
@@ -53,8 +54,8 @@ def portforward_commands(api_instance):
53
54
}]
54
55
}
55
56
}
56
- resp = api_instance .create_namespaced_pod (body = pod_manifest ,
57
- namespace = 'default' )
57
+ api_instance .create_namespaced_pod (body = pod_manifest ,
58
+ namespace = 'default' )
58
59
while True :
59
60
resp = api_instance .read_namespaced_pod (name = name ,
60
61
namespace = 'default' )
@@ -65,40 +66,40 @@ def portforward_commands(api_instance):
65
66
66
67
pf = portforward (api_instance .connect_get_namespaced_pod_portforward ,
67
68
name , 'default' ,
68
- ports = '80,8080:80' )
69
- for port in (80 , 8080 ):
70
- http = pf .socket (port )
71
- http .settimeout (1 )
72
- http .sendall (b'GET / HTTP/1.1\r \n ' )
73
- http .sendall (b'Host: 127.0.0.1\r \n ' )
74
- http .sendall (b'Accept: */*\r \n ' )
75
- http .sendall (b'\r \n ' )
76
- response = b''
77
- while True :
78
- try :
79
- response += http .recv (1024 )
80
- except socket .timeout :
81
- break
82
- print (response .decode ('utf-8' ))
83
- http .close ()
69
+ ports = '80' )
70
+ http = pf .socket (80 )
71
+ http .setblocking (True )
72
+ http .sendall (b'GET / HTTP/1.1\r \n ' )
73
+ http .sendall (b'Host: 127.0.0.1\r \n ' )
74
+ http .sendall (b'Accept: */*\r \n ' )
75
+ http .sendall (b'Connection: close\r \n ' )
76
+ http .sendall (b'\r \n ' )
77
+ response = b''
78
+ while True :
79
+ select .select ([http ], [], [])
80
+ data = http .recv (1024 )
81
+ if not data :
82
+ break
83
+ response += data
84
+ http .close ()
85
+ print (response .decode ('utf-8' ))
84
86
85
87
# Monkey patch socket.create_connection which is used by http.client and
86
88
# urllib.request. The same can be done with urllib3.util.connection.create_connection
87
89
# if the "requests" package is used.
90
+ socket_create_connection = socket .create_connection
88
91
def kubernetes_create_connection (address , * args , ** kwargs ):
89
92
dns_name = address [0 ]
90
93
if isinstance (dns_name , bytes ):
91
94
dns_name = dns_name .decode ()
92
95
# Look for "<pod-name>.<namspace>.kubernetes" dns names and if found
93
- # provide a socket that is port forwarded to the kuberntest pod.
96
+ # provide a socket that is port forwarded to the kubernetes pod.
94
97
dns_name = dns_name .split ("." )
95
98
if len (dns_name ) != 3 or dns_name [2 ] != "kubernetes" :
96
99
return socket_create_connection (address , * args , ** kwargs )
97
100
pf = portforward (api_instance .connect_get_namespaced_pod_portforward ,
98
101
dns_name [0 ], dns_name [1 ], ports = str (address [1 ]))
99
102
return pf .socket (address [1 ])
100
-
101
- socket_create_connection = socket .create_connection
102
103
socket .create_connection = kubernetes_create_connection
103
104
104
105
# Access the nginx http server using the "<pod-name>.<namespace>.kubernetes" dns name.
@@ -111,9 +112,9 @@ def kubernetes_create_connection(address, *args, **kwargs):
111
112
112
113
def main ():
113
114
config .load_kube_config ()
114
- c = Configuration ()
115
+ c = Configuration . get_default_copy ()
115
116
c .assert_hostname = False
116
- # Configuration.set_default(c)
117
+ Configuration .set_default (c )
117
118
core_v1 = core_v1_api .CoreV1Api ()
118
119
119
120
portforward_commands (core_v1 )
0 commit comments