2

当我直接在我的 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) 环境中工作,我正在为我的 http 客户端使用 Guzzle。

这是我的代码:

        $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

4

1 回答 1

1

您是否尝试过从该流中实际读取?私有字段可能会令人困惑,所以我建议尝试公共界面,看看你会得到什么:

$body = $response->getBody();
while (!$body->eof()) {
    echo $body->read(1024);
}
于 2020-03-02T10:37:34.393 回答