Skip to content

Commit 3f84055

Browse files
authored
Merge pull request #22 from appwrite-labs/codex/fix-log-operation-raw-text
[codex] Preserve raw log output for log operations
2 parents 0ba9261 + 70cd171 commit 3f84055

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

src/KubernetesCluster.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ public function runOperation(Operation|string $operation, string $path, string|n
174174
return match ($operation) {
175175
Operation::WATCH => $this->watchPath($path, $payload, $query),
176176
Operation::WATCH_LOGS => $this->watchLogsPath($path, $payload, $query),
177+
Operation::LOG => (string) $this->call(
178+
$operation->httpMethod(),
179+
$path,
180+
is_string($payload) ? $payload : '',
181+
$query
182+
)->getBody(),
177183
Operation::EXEC => $this->execPath($path, $query),
178184
Operation::ATTACH => $this->attachPath($path, $payload, $query),
179185
Operation::APPLY => $this->applyPath($path, $payload, $query),

src/Traits/Cluster/MakesHttpCalls.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ protected function makeRequest(string $method, string $path, string $payload = '
156156
$json = @json_decode($response->getBody(), true);
157157

158158
// If the output is not JSONable, return the response itself.
159-
// This can be encountered in case of a pod log request, for example,
160-
// where the data returned are just console logs.
159+
// This can be encountered when an endpoint returns plain-text output.
161160

162161
if (! $json) {
163162
return (string) $response->getBody();

tests/KubernetesClusterTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Test;
4+
5+
use GuzzleHttp\Psr7\Response;
6+
use Psr\Http\Message\ResponseInterface;
7+
use RenokiCo\PhpK8s\Enums\Operation;
8+
use RenokiCo\PhpK8s\Kinds\K8sPod;
9+
use RenokiCo\PhpK8s\KubernetesCluster;
10+
11+
class KubernetesClusterTest extends TestCase
12+
{
13+
public function test_log_operations_return_raw_json_logs_as_strings(): void
14+
{
15+
$expectedLogs = '{"status":200,"message":"ok","container":{"size":123}}';
16+
17+
$cluster = new class('http://127.0.0.1:8080', new Response(200, [], $expectedLogs)) extends KubernetesCluster
18+
{
19+
public function __construct(?string $url, private readonly ResponseInterface $response)
20+
{
21+
parent::__construct($url);
22+
}
23+
24+
public function call(string $method, string $path, string $payload = '', array $query = ['pretty' => 1], array $options = []): ResponseInterface
25+
{
26+
return $this->response;
27+
}
28+
};
29+
30+
$result = $cluster
31+
->setResourceClass(K8sPod::class)
32+
->runOperation(Operation::LOG, '/api/v1/namespaces/default/pods/example/log', '');
33+
34+
$this->assertSame($expectedLogs, $result);
35+
}
36+
37+
public function test_non_log_operations_still_hydrate_resources_from_json(): void
38+
{
39+
$cluster = new class('http://127.0.0.1:8080', new Response(200, [], '{"kind":"Pod","metadata":{"name":"example"}}')) extends KubernetesCluster
40+
{
41+
public function __construct(?string $url, private readonly ResponseInterface $response)
42+
{
43+
parent::__construct($url);
44+
}
45+
46+
public function call(string $method, string $path, string $payload = '', array $query = ['pretty' => 1], array $options = []): ResponseInterface
47+
{
48+
return $this->response;
49+
}
50+
};
51+
52+
$result = $cluster
53+
->setResourceClass(K8sPod::class)
54+
->runOperation(Operation::GET, '/api/v1/namespaces/default/pods/example', '');
55+
56+
$this->assertInstanceOf(K8sPod::class, $result);
57+
$this->assertSame('example', $result->getName());
58+
}
59+
}

0 commit comments

Comments
 (0)