Skip to content

Commit 047e1db

Browse files
committed
add stream to http request and solve config bugs in kubernetesclient
1 parent 734c9fd commit 047e1db

File tree

2 files changed

+40
-53
lines changed

2 files changed

+40
-53
lines changed

kubesys/client.py

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ def createResource(self, jsonStr, **kwargs) -> Union[dict, bool, str]:
8686
url = self.analyzer.FullKindToApiPrefixDict[kind] + "/"
8787
url += self.getNamespace(self.analyzer.FullKindToNamespaceDict[kind], namespace)
8888
url += self.analyzer.FullKindToNameDict[kind]
89-
90-
return createRequest(url=url, token=self.token, method="POST", body=jsonStr, keep_json=False, **kwargs)
89+
return createRequest(url=url, token=self.token, method="POST", body=jsonStr,keep_json=False, config=self.config, **kwargs)
9190

9291
def updateResource(self, jsonStr, **kwargs) -> Union[dict, bool, str]:
9392
jsonObj = jsonStr
@@ -104,7 +103,7 @@ def updateResource(self, jsonStr, **kwargs) -> Union[dict, bool, str]:
104103
url += self.getNamespace(self.analyzer.FullKindToNamespaceDict[kind], namespace)
105104
url += self.analyzer.FullKindToNameDict[kind] + "/" + jsonObj["metadata"]["name"]
106105

107-
return createRequest(url=url, token=self.token, method="PUT", body=jsonStr, keep_json=False, **kwargs)
106+
return createRequest(url=url, token=self.token, method="PUT", body=jsonStr, keep_json=False,config=self.config, **kwargs)
108107

109108
def checkAndReturnRealKind(self, kind, mapper) -> Union[str, str]:
110109
index = kind.find(".")
@@ -133,7 +132,7 @@ def deleteResource(self, kind, namespace, name, **kwargs) -> Union[dict, bool, s
133132
url += self.getNamespace(self.analyzer.FullKindToNamespaceDict[fullKind], namespace)
134133
url += self.analyzer.FullKindToNameDict[fullKind] + "/" + name
135134

136-
return createRequest(url=url, token=self.token, method="DELETE", keep_json=False, **kwargs)
135+
return createRequest(url=url, token=self.token, method="DELETE", keep_json=False,config=self.config, **kwargs)
137136

138137
def getResource(self, kind, name, namespace="", **kwargs) -> Union[dict, bool, str]:
139138
fullKind, error_str = self.checkAndReturnRealKind(kind, self.analyzer.KindToFullKindDict)
@@ -144,7 +143,7 @@ def getResource(self, kind, name, namespace="", **kwargs) -> Union[dict, bool, s
144143
url += self.getNamespace(self.analyzer.FullKindToNamespaceDict[fullKind], namespace)
145144
url += self.analyzer.FullKindToNameDict[fullKind] + "/" + name
146145

147-
return createRequest(url=url, token=self.token, method="GET", keep_json=False, **kwargs)
146+
return createRequest(url=url, token=self.token, method="GET", keep_json=False, config=self.config,**kwargs)
148147

149148
def listResources(self, kind, namespace="", **kwargs) -> Union[dict, bool, str]:
150149
fullKind, error_str = self.checkAndReturnRealKind(kind, self.analyzer.KindToFullKindDict)
@@ -155,7 +154,7 @@ def listResources(self, kind, namespace="", **kwargs) -> Union[dict, bool, str]:
155154
url += self.getNamespace(self.analyzer.FullKindToNamespaceDict[fullKind], namespace)
156155
url += self.analyzer.FullKindToNameDict[fullKind]
157156

158-
return createRequest(url=url, token=self.token, method="GET", keep_json=False, **kwargs)
157+
return createRequest(url=url, token=self.token, method="GET", keep_json=False, config=self.config,**kwargs)
159158

160159
def bindResource(self, pod, host, **kwargs) -> Union[dict, bool, str]:
161160
jsonObj = {}
@@ -181,7 +180,7 @@ def bindResource(self, pod, host, **kwargs) -> Union[dict, bool, str]:
181180
url += self.analyzer.FullKindToNameDict[kind] + "/"
182181
url += pod["metadata"]["name"] + "/binding"
183182

184-
return createRequest(url=url, token=self.token, method="POST", data=jsonObj, keep_json=False, **kwargs)
183+
return createRequest(url=url, token=self.token, method="POST", data=jsonObj, keep_json=False, config=self.config,**kwargs)
185184

186185
def watchResource(self, kind, namespace, watcherhandler, name=None, thread_name=None, is_daemon=True,
187186
**kwargs) -> KubernetesWatcher:
@@ -199,8 +198,8 @@ def watchResource(self, kind, namespace, watcherhandler, name=None, thread_name=
199198
url += self.analyzer.FullKindToNameDict[fullKind] + "/" + name
200199
else:
201200
url += self.analyzer.FullKindToNameDict[fullKind]
202-
203-
thread_t = threading.Thread(target=KubernetesClient.watching, args=(url, self.token, watcherhandler, kwargs,),
201+
thread_t = threading.Thread(target=KubernetesClient.watching,
202+
args=(url, self.token, self.config, watcherhandler, kwargs,),
204203
name=thread_name, daemon=is_daemon)
205204

206205
watcher = KubernetesWatcher(thread_t=thread_t, kind=kind, namespace=namespace, watcher_handler=watcherhandler,
@@ -253,38 +252,25 @@ def watchResourcesBase(self, kind, namespace, handlerFunction, thread_name=None,
253252
isDaemon=is_daemon, **kwargs)
254253

255254
@staticmethod
256-
def watching(url, token, watchHandler, kwargs):
257-
if token is None:
258-
header = {
259-
"Accept": "*/*",
260-
"Accept-Encoding": "gzip, deflate, br",
261-
}
262-
else:
263-
header = {
264-
"Accept": "*/*",
265-
"Authorization": "Bearer " + token,
266-
"Accept-Encoding": "gzip, deflate, br",
267-
}
268-
269-
with requests.get(url=formatURL(url, getParams(kwargs)), headers=header, verify=False, stream=True) as response:
270-
for json_bytes in response.iter_lines():
271-
if len(json_bytes) < 1:
272-
continue
273-
274-
jsonObj = jsonBytesToDict(json_bytes)
275-
if "type" not in jsonObj.keys():
276-
print("type is not found in keys while watching, dict is: ", jsonObj)
277-
exit(-3)
278-
279-
if jsonObj["type"] == "ADDED":
280-
watchHandler.DoAdded(jsonObj["object"])
281-
elif jsonObj["type"] == "MODIFIED":
282-
watchHandler.DoModified(jsonObj["object"])
283-
elif jsonObj["type"] == "DELETED":
284-
watchHandler.DoDeleted(jsonObj["object"])
285-
else:
286-
print("unknow type while watching:", jsonObj["type"])
287-
255+
def watching(url, token, config, watchHandler, kwargs):
256+
response=createRequest(url=formatURL(url, getParams(kwargs)), token=token, method="GET", keep_json=False, config=config,stream=True)[0]
257+
for json_bytes in response.iter_lines():
258+
if len(json_bytes) < 1:
259+
continue
260+
261+
jsonObj = jsonBytesToDict(json_bytes)
262+
if "type" not in jsonObj.keys():
263+
print("type is not found in keys while watching, dict is: ", jsonObj)
264+
exit(-3)
265+
266+
if jsonObj["type"] == "ADDED":
267+
watchHandler.DoAdded(jsonObj["object"])
268+
elif jsonObj["type"] == "MODIFIED":
269+
watchHandler.DoModified(jsonObj["object"])
270+
elif jsonObj["type"] == "DELETED":
271+
watchHandler.DoDeleted(jsonObj["object"])
272+
else:
273+
print("unknow type while watching:", jsonObj["type"])
288274
KubernetesClient.removeWatcher(thread_name=threading.currentThread().getName())
289275

290276
@staticmethod
@@ -326,7 +312,7 @@ def updateResourceStatus(self, jsonStr, **kwargs) -> Union[dict, bool, str]:
326312
url += self.analyzer.FullKindToNameDict[kind] + "/" + jsonObj["metadata"]["name"]
327313
url += "/status"
328314

329-
return createRequest(url=url, token=self.token, method="PUT", body=jsonObj, keep_json=False, **kwargs)
315+
return createRequest(url=url, token=self.token, method="PUT", body=jsonObj, keep_json=False,config=self.config, **kwargs)
330316

331317
def listResourcesWithLabelSelector(self, kind, namespace, labels) -> Union[dict, bool, str]:
332318
fullKind, error_str = self.checkAndReturnRealKind(kind, self.analyzer.KindToFullKindDict)
@@ -341,7 +327,7 @@ def listResourcesWithLabelSelector(self, kind, namespace, labels) -> Union[dict,
341327
for key, value in labels.items():
342328
url += key + "%3D" + value + ","
343329
url = url[:len(url) - 1]
344-
return createRequest(url=url, token=self.token, method="GET", keep_json=False)
330+
return createRequest(url=url, token=self.token, method="GET", keep_json=False,config=self.config)
345331

346332
def listResourcesWithFieldSelector(self, kind, namespace, fields) -> Union[dict, bool, str]:
347333
fullKind, error_str = self.checkAndReturnRealKind(kind, self.analyzer.KindToFullKindDict)
@@ -357,7 +343,7 @@ def listResourcesWithFieldSelector(self, kind, namespace, fields) -> Union[dict,
357343
url += key + "%3D" + value + ","
358344

359345
url = url[:len(url) - 1]
360-
return createRequest(url=url, token=self.token, method="GET", keep_json=False)
346+
return createRequest(url=url, token=self.token, method="GET", keep_json=False,config=self.config)
361347

362348
def getKinds(self) -> list:
363349
return list(self.analyzer.KindToFullKindDict.keys())

kubesys/http_request.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import json
88

99
__author__ = ('Tian Yu <[email protected]>',
10+
'Jiexin Liu <[email protected]>',
1011
'Heng Wu <[email protected]>')
1112

1213
from kubesys.tls import tlsPaths
1314

1415

1516
def createRequest(url, token, method="GET", body=None, verify=False,
16-
keep_json=False, config=None, **kwargs) -> Union[object, bool, str]:
17+
keep_json=False, config=None,stream=False, **kwargs) -> Union[object, bool, str]:
1718
response, OK, status_code = doCreateRequest(
18-
formatURL(url, getParams(kwargs)), token, method, body, config)
19+
formatURL(url, getParams(kwargs)), token, method, body, config,stream)
1920

2021
result = response.json()
2122
if keep_json:
@@ -24,12 +25,12 @@ def createRequest(url, token, method="GET", body=None, verify=False,
2425
return result, OK, status_code
2526

2627

27-
def doCreateRequest(url, token, method="GET", body=None, config=None) \
28+
def doCreateRequest(url, token, method="GET", body=None, config=None,stream=False) \
2829
-> Union[object, bool, str]:
2930
if config is None:
30-
response = doCreateRequestWithToken(url, token, method, body)
31+
response = doCreateRequestWithToken(url, token, method,stream, body)
3132
else:
32-
response = doCreateRequestWithConfig(url, config, method, body)
33+
response = doCreateRequestWithConfig(url, config, method,stream, body)
3334

3435
if 200 <= response.status_code <= 299:
3536
return response, True, response.status_code
@@ -38,10 +39,10 @@ def doCreateRequest(url, token, method="GET", body=None, config=None) \
3839
return response, False, response.status_code
3940

4041

41-
def doCreateRequestWithToken(url, token, method, body=None):
42+
def doCreateRequestWithToken(url, token, method,stream, body=None):
4243
header, data = getHeaderAndBody(token, body)
4344
return requests.request(method, url=url,
44-
headers=header, data=data, verify=False)
45+
headers=header, data=data, verify=False,stream=stream)
4546

4647
# if method_upper == "GET":
4748
# return requests.get(url=formatURL(url, getParams(kwargs)), headers=header, verify=False)
@@ -56,12 +57,12 @@ def doCreateRequestWithToken(url, token, method, body=None):
5657
# exit(-1)
5758

5859

59-
def doCreateRequestWithConfig(url, config, method, body=None):
60+
def doCreateRequestWithConfig(url, config, method, stream,body=None):
6061

6162
header, data = getHeaderAndBody(None, body)
6263
pem, ca, key = tlsPaths(config)
6364
return requests.request(method, url=url, headers=header, data=data,
64-
verify=pem, cert=(ca, key))
65+
verify=pem, cert=(ca, key),stream=stream)
6566

6667

6768
def getHeaderAndBody(token, body):

0 commit comments

Comments
 (0)