thatguyjono
thatguyjono

Reputation: 525

How to handle a stream response using the Kubernetes API?

When I ssh directly inside my pod, and run the following on the command line: 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 it works and returns all the updates for the specified job (api-job-12) in the form of a stream.

However, when I'm inside my application level code, I can't get the API to stream the response (the request times out with no response at all). I'm working inside a PHP(Laravel) environment and I'm using Guzzle for my http client.

Here's my code:

        $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());

When I dump $response->getBody() I get the following:

^ 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"

It does appear to be a stream object, but the object says it has no bytes to read and that it is not seekable. Does anyone have any ideas as to how I can get a stream response using PHP here?

Here's the Kubernetes API endpoint that I'm calling: https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.10/#watch-47

Upvotes: 2

Views: 853

Answers (1)

Alexey Shokov
Alexey Shokov

Reputation: 5010

Have you tried to actually read from this stream? Private fields can be confusing, so I would suggest to try the public interface and see what you get:

$body = $response->getBody();
while (!$body->eof()) {
    echo $body->read(1024);
}

Upvotes: 1

Related Questions