Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch support #385

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RenokiCo\PhpK8s;

use Closure;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Str;
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
use RenokiCo\PhpK8s\Kinds\K8sResource;
Expand Down Expand Up @@ -146,6 +147,8 @@ class KubernetesCluster
self::GET_OP => 'GET',
self::CREATE_OP => 'POST',
self::REPLACE_OP => 'PUT',
self::JSON_PATCH_OP => 'PATCH',
self::MERGE_PATCH_OP => 'PATCH',
self::DELETE_OP => 'DELETE',
self::LOG_OP => 'GET',
self::WATCH_OP => 'GET',
Expand All @@ -158,6 +161,8 @@ class KubernetesCluster
const CREATE_OP = 'create';
const REPLACE_OP = 'replace';
const DELETE_OP = 'delete';
const MERGE_PATCH_OP = 'merge_patch';
const JSON_PATCH_OP = 'json_patch';
const LOG_OP = 'logs';
const WATCH_OP = 'watch';
const WATCH_LOGS_OP = 'watch_logs';
Expand Down Expand Up @@ -195,23 +200,44 @@ public function setResourceClass(string $resourceClass)
* @param string $path
* @param string|null|Closure $payload
* @param array $query
* @param array $options
* @return mixed
*
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
public function runOperation(string $operation, string $path, $payload = '', array $query = ['pretty' => 1])
{
public function runOperation(
string $operation,
string $path,
string $payload = '',
array $query = ['pretty' => 1],
array $options = []
): mixed {
switch ($operation) {
case static::WATCH_OP: return $this->watchPath($path, $payload, $query); break;
case static::WATCH_LOGS_OP: return $this->watchLogsPath($path, $payload, $query); break;
case static::EXEC_OP: return $this->execPath($path, $query); break;
case static::ATTACH_OP: return $this->attachPath($path, $payload, $query); break;
default: break;
case static::WATCH_OP:
return $this->watchPath($path, $payload, $query);
break;
case static::WATCH_LOGS_OP:
return $this->watchLogsPath($path, $payload, $query);
break;
case static::EXEC_OP:
return $this->execPath($path, $query);
break;
case static::ATTACH_OP:
return $this->attachPath($path, $payload, $query);
break;
case static::MERGE_PATCH_OP:
$options[RequestOptions::HEADERS]['Content-Type'] = 'application/merge-patch+json';
break;
case static::JSON_PATCH_OP:
$options[RequestOptions::HEADERS]['Content-Type'] = 'application/json-patch+json';
break;
default:
break;
}

$method = static::$operations[$operation] ?? static::$operations[static::GET_OP];

return $this->makeRequest($method, $path, $payload, $query);
return $this->makeRequest($method, $path, $payload, $query, $options);
}

/**
Expand Down
29 changes: 21 additions & 8 deletions src/Traits/Cluster/MakesHttpCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,19 @@ public function getClient()
*
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
public function call(string $method, string $path, string $payload = '', array $query = ['pretty' => 1])
{
public function call(
string $method,
string $path,
string $payload = '',
array $query = ['pretty' => 1],
array $options = []
): \Psr\Http\Message\ResponseInterface {
if ($payload) {
$options[RequestOptions::BODY] = $payload;
}

try {
$response = $this->getClient()->request($method, $this->getCallableUrl($path, $query), [
RequestOptions::BODY => $payload,
]);
$response = $this->getClient()->request($method, $this->getCallableUrl($path, $query), $options);
} catch (ClientException $e) {
$errorPayload = json_decode((string) $e->getResponse()->getBody(), true);

Expand All @@ -103,15 +110,21 @@ public function call(string $method, string $path, string $payload = '', array $
* @param string $path
* @param string $payload
* @param array $query
* @param array $options
* @return mixed
*
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
protected function makeRequest(string $method, string $path, string $payload = '', array $query = ['pretty' => 1])
{
protected function makeRequest(
string $method,
string $path,
string $payload = '',
array $query = ['pretty' => 1],
array $options = []
): mixed {
$resourceClass = $this->resourceClass;

$response = $this->call($method, $path, $payload, $query);
$response = $this->call($method, $path, $payload, $query, $options);

$json = @json_decode($response->getBody(), true);

Expand Down
55 changes: 53 additions & 2 deletions src/Traits/RunsClusterOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,57 @@ public function update(array $query = ['pretty' => 1]): bool
return true;
}

/**
* Issue a patch operation towards a cluster.
*
* @param array $payload JSON
* @param array $query
* @return bool
*
* @throws KubernetesAPIException
**/
public function patchMergeType(array $payload, array $query = ['pretty' => 1]): bool
{
$this->refreshResourceVersion();

$instance = $this->cluster
->setResourceClass(get_class($this))
->runOperation(
KubernetesCluster::MERGE_PATCH_OP,
$this->resourcePath(),
json_encode($payload),
$query
);

$this->syncWith($instance->toArray());

return true;
}

public function patchJSONType(
string $operation,
string $path,
?string $value = null,
array $query = ['pretty' => 1]
): bool {
$this->refreshResourceVersion();

$payload = [['op' => $operation, 'path' => $path, 'value' => $value]];

$instance = $this->cluster
->setResourceClass(get_class($this))
->runOperation(
KubernetesCluster::JSON_PATCH_OP,
$this->resourcePath(),
json_encode($payload),
$query
);

$this->syncWith($instance->toArray());

return true;
}

/**
* Delete the resource.
*
Expand Down Expand Up @@ -445,7 +496,7 @@ public function scaler(): K8sScale
/**
* Exec a command on the current resource.
*
* @param string|array $command
* @param array|string $command
* @param string|null $container
* @param array $query
* @return string
Expand All @@ -454,7 +505,7 @@ public function scaler(): K8sScale
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
public function exec(
$command,
array|string $command,
string $container = null,
array $query = ['pretty' => 1, 'stdin' => 1, 'stdout' => 1, 'stderr' => 1, 'tty' => 1]
) {
Expand Down
18 changes: 18 additions & 0 deletions tests/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function test_deployment_api_interaction()
$this->runUpdateTests();
$this->runWatchAllTests();
$this->runWatchTests();
$this->runPatchTests();
$this->runDeletionTests();
}

Expand Down Expand Up @@ -239,6 +240,23 @@ public function runUpdateTests()
$this->assertInstanceOf(K8sPod::class, $dep->getTemplate());
}

/**
* @throws KubernetesAPIException
*/
public function runPatchTests()
{
$dep = $this->cluster->getDeploymentByName('mysql');

$this->assertTrue($dep->isSynced());

$dep->patchMergeType(['metadata' => ['annotations' => ['foo' => 'bar']]]);
$dep->pachJSONType('add', '/metadata/annotations/foo2', 'bar2');

$this->assertTrue($dep->isSynced());
$this->assertTrue(($dep->getAnnotations()['foo'] ?? false) == 'bar');
$this->assertTrue(($dep->getAnnotations()['foo2'] ?? false) == 'bar2');
}

public function runDeletionTests()
{
$dep = $this->cluster->getDeploymentByName('mysql');
Expand Down