我正在 kubernetes python 客户端中创建一个程序,它在 pod 内卷曲一个 url 并期望输出json
为str
. 以下是我的程序:
from kubernetes import client, config, watch
from kubernetes.stream import stream
config.load_kube_config()
k8s_corev1 = client.CoreV1Api()
def run_command_inside_pod(api_instance, pod, ns, exec_command):
api_response = stream(api_instance.connect_get_namespaced_pod_exec, pod, ns,
command=exec_command,
stderr=True, stdin=False,
stdout=True, tty=False)
return api_response
def metrics_datastore_check():
"""
:param name:
:return:
"""
name = "metrics-datastore-0"
print("Waiting for pod %s to get ready." % name)
exec_command = ['curl', '--silent', '-i', 'localhost:9200/_cluster/health?pretty']
es_status = run_command_inside_pod(k8s_corev1,name,"default",exec_command)
print (es_status)
print (type(es_status))
metrics_datastore_check()
上述程序返回以下输出:
(env) [root@ip-10-0-1-48 k8s_Client]# python2 es_pod.py
Waiting for pod metrics-datastore-0 to get ready.
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Content-Length: 470
{
"cluster_name" : "metrics-datastore",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 1,
"active_primary_shards" : 0,
"active_shards" : 0,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
<type 'str'>
正如您可以清楚地看到返回值的类型,str
因此我无法使用 json 工具正确解析它。dict
任何人都可以帮助我str
以dict
.