[当我直接在自己的Pod中ssh并在命令行上运行以下命令时:curl -v --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" https://kubernetes.default.svc.cluster.local/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12
它会工作并以流的形式返回指定作业(api-job-12
)的所有更新。
但是,当我进入应用程序级代码时,无法获得API来传输响应(请求超时,根本没有响应)。我在PHP(Laravel)环境中工作,并且将Guzzle用于我的http客户端。
这是我的代码:
$token = file_get_contents('/var/run/secrets/kubernetes.io/serviceaccount/token');
$client = new Client([
'headers' => [
'Authorization' => "Bearer {$token}"
]
]);
$response = $client->get(
'https://kubernetes.default.svc/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12',
[
'verify' => '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt',
'stream' => true
]
);
dd($response->getBody()->getContents());
当我转储$response->getBody()
时,我得到以下信息:
^ GuzzleHttp\Psr7\Stream {#338
-stream: stream resource @21
crypto: array:4 [
"protocol" => "TLSv1.2"
"cipher_name" => "ECDHE-RSA-AES128-GCM-SHA256"
"cipher_bits" => 128
"cipher_version" => "TLSv1.2"
]
timed_out: false
blocked: true
eof: false
wrapper_data: array:4 [
0 => "HTTP/1.1 200 OK"
1 => "Content-Type: application/json"
2 => "Date: Sun, 01 Mar 2020 20:15:33 GMT"
3 => "Connection: close"
]
wrapper_type: "http"
stream_type: "tcp_socket/ssl"
mode: "r"
unread_bytes: 0
seekable: false
uri: "https://kubernetes.default.svc/apis/batch/v1/watch/namespaces/kludge/jobs/api-job-12"
options: array:2 [
"http" => array:5 [
"method" => "GET"
"header" => """
Authorization: Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrbHVkZ2UiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlY3JldC5uYW1lIjoiZGVmYXVsdC10b2tlbi14ZjhsciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiZDA5ZjZiZTAtM2UyOS00NWU3LWI3ZjgtOGE1YWI0OGZjNDJiIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50OmtsdWRnZTpkZWZhdWx0In0.eAtABzj-SimmTGNCQXkmtVHFPYoxiZWV7ET5tYK3OIa6-Ea6WA3cy7cMmObRILTc26cLU4YX8YovhzoNAV8RkteKAVGv2HNvaeOXD_AKkYilX618SUMEfat-zsnXYUego24gNLPtPFRefRyEwAnxf6E61DDwSWWlyptKiggcnl8GHrlY_14oumOsFpsjsRTc807DsuZGn1jCU1Dw2DPhSz457a-afXb0jggzorYNzDtfG6rBTKYctPI4wfh30y9iwjPLTU5L5B-8mYqWn9lgOs2Z9XkFu1GRUD19j6bgAnzoyfVCY8uJp9FGi1Ega84n_MsC6cXmS7K7_QiyBtFR-Q
User-Agent: GuzzleHttp/6.5.1 curl/7.64.0 PHP/7.2.28
Host: kubernetes.default.svc
Content-Length: 0
Connection: close
"""
"protocol_version" => "1.1"
"ignore_errors" => true
"follow_location" => 0
]
"ssl" => array:4 [
"cafile" => "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
它似乎确实是一个流对象,但是该对象说它没有要读取的字节,并且它是不可搜索的。是否有人对我如何在此处使用PHP获得流响应有任何想法?
这是我正在调用的Kubernetes API端点:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#watch-47
您是否尝试过实际阅读此信息流?私有字段可能会造成混淆,因此我建议尝试the public interface并查看获得的结果:
$body = $response->getBody();
while (!$body->eof()) {
echo $body->read(1024);
}