Skip to content

Commit

Permalink
Some improvement with last version of Kubernetes API (#105)
Browse files Browse the repository at this point in the history
* Set content type to "application/json" for post request
Allow illuminate/support 9+

* Remove beta in api
add role,rolebinding and service account
  • Loading branch information
frenchcomp authored Mar 28, 2022
1 parent 1eaff45 commit b726e64
Show file tree
Hide file tree
Showing 32 changed files with 457 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php" : "^7.4|^8.0",
"php-http/client-common": "^2.0",
"php-http/discovery" : "^1.0",
"illuminate/support" : "^5.0|^6.0|^7.0|^8.0",
"illuminate/support" : "^5.0|^6.0|^7.0|^8.0|^9.0",
"ratchet/pawl" : "^0.3",
"symfony/yaml" : "^4.0|^5.0|^6.0",
"softcreatr/jsonpath" : "^0.6"
Expand Down
10 changes: 9 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
use InvalidArgumentException;
use BadMethodCallException;
use Maclof\Kubernetes\Exceptions\ApiServerException;
use Maclof\Kubernetes\Repositories\RoleBindingRepository;
use Maclof\Kubernetes\Repositories\RoleRepository;
use Maclof\Kubernetes\Repositories\ServiceAccountRepository;
use Psr\Http\Client\ClientInterface;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException as YamlParseException;

use Http\Client\HttpClient;
use Http\Client\Common\HttpMethodsClient;
use Http\Client\Common\HttpMethodsClientInterface;
use Http\Client\Exception\TransferException as HttpTransferException;
Expand Down Expand Up @@ -64,6 +66,9 @@
* @method HorizontalPodAutoscalerRepository horizontalPodAutoscalers()
* @method CertificateRepository certificates()
* @method IssuersRepository issuers()
* @method ServiceAccountRepository serviceAccounts()
* @method RoleRepository roles()
* @method RoleBindingRepository roleBindings()
*/
class Client
{
Expand Down Expand Up @@ -381,6 +386,9 @@ public function sendRequest(string $method, string $uri, array $query = [], $bod

try {
$headers = $method === 'PATCH' ? $this->patchHeaders : [];
if ('POST' === $method) {
$headers['Content-Type'] = 'application/json';
}

if ($this->token) {
$token = $this->token;
Expand Down
41 changes: 41 additions & 0 deletions src/Collections/RoleBindingCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Collections;

use Maclof\Kubernetes\Models\RoleBinding;

/**
* @author Richard Déloge <[email protected]>
*/
class RoleBindingCollection extends Collection
{
/**
* @param array<int, array<mixed>|RoleBinding> $items
*/
public function __construct(array $items)
{
parent::__construct($this->getServiceAccounts($items));
}

/**
* Get an array of serviceAccounts.
*
* @param array<int, array<mixed>|RoleBinding> $items
* @return array<RoleBinding>
*/
protected function getServiceAccounts(array $items)
{
$final = [];
foreach ($items as &$item) {
if (!$item instanceof RoleBinding) {
$final[] = new RoleBinding($item);
} else {
$final[] = $item;
}
}

return $final;
}
}
41 changes: 41 additions & 0 deletions src/Collections/RoleCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Collections;

use Maclof\Kubernetes\Models\Role;

/**
* @author Richard Déloge <[email protected]>
*/
class RoleCollection extends Collection
{
/**
* @param array<int, array<mixed>|Role> $items
*/
public function __construct(array $items)
{
parent::__construct($this->getServiceAccounts($items));
}

/**
* Get an array of serviceAccounts.
*
* @param array<int, array<mixed>|Role> $items
* @return array<Role>
*/
protected function getServiceAccounts(array $items)
{
$final = [];
foreach ($items as &$item) {
if (!$item instanceof Role) {
$final[] = new Role($item);
} else {
$final[] = $item;
}
}

return $final;
}
}
41 changes: 41 additions & 0 deletions src/Collections/ServiceAccountCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Collections;

use Maclof\Kubernetes\Models\ServiceAccount;

/**
* @author Richard Déloge <[email protected]>
*/
class ServiceAccountCollection extends Collection
{
/**
* @param array<int, array<mixed>|ServiceAccount> $items
*/
public function __construct(array $items)
{
parent::__construct($this->getServiceAccounts($items));
}

/**
* Get an array of serviceAccounts.
*
* @param array<int, array<mixed>|ServiceAccount> $items
* @return array<ServiceAccount>
*/
protected function getServiceAccounts(array $items)
{
$final = [];
foreach ($items as &$item) {
if (!$item instanceof ServiceAccount) {
$final[] = new ServiceAccount($item);
} else {
$final[] = $item;
}
}

return $final;
}
}
2 changes: 1 addition & 1 deletion src/Models/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class CronJob extends Model
/**
* The api version.
*/
protected string $apiVersion = 'batch/v1beta1';
protected string $apiVersion = 'batch/v1';
}
2 changes: 1 addition & 1 deletion src/Models/DaemonSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class DaemonSet extends Model
/**
* The api version.
*/
protected string $apiVersion = 'extensions/v1beta1';
protected string $apiVersion = 'apps/v1';
}
2 changes: 1 addition & 1 deletion src/Models/HorizontalPodAutoscaler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class HorizontalPodAutoscaler extends \Maclof\Kubernetes\Models\Model
/**
* The api version.
*/
protected string $apiVersion = 'autoscaling/v2beta1';
protected string $apiVersion = 'autoscaling/v2';
}
2 changes: 1 addition & 1 deletion src/Models/Ingress.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class Ingress extends Model
/**
* The api version.
*/
protected string $apiVersion = 'networking.k8s.io/v1beta1';
protected string $apiVersion = 'networking.k8s.io/v1';
}
2 changes: 1 addition & 1 deletion src/Models/ReplicaSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ class ReplicaSet extends Model
/**
* The api version.
*/
protected string $apiVersion = 'extensions/v1beta1';
protected string $apiVersion = 'apps/v1';
}
16 changes: 16 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Models;

/**
* @author Richard Déloge <[email protected]>
*/
class Role extends Model
{
/**
* @var string
*/
protected string $apiVersion = 'rbac.authorization.k8s.io/v1';
}
16 changes: 16 additions & 0 deletions src/Models/RoleBinding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Models;

/**
* @author Richard Déloge <[email protected]>
*/
class RoleBinding extends Model
{
/**
* @var string
*/
protected string $apiVersion = 'rbac.authorization.k8s.io/v1';
}
12 changes: 12 additions & 0 deletions src/Models/ServiceAccount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Models;

/**
* @author Richard Déloge <[email protected]>
*/
class ServiceAccount extends Model
{
}
25 changes: 25 additions & 0 deletions src/Repositories/RoleBindingRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Collections\RoleBindingCollection;
use Maclof\Kubernetes\Models\RoleBinding;
use Maclof\Kubernetes\Collections\Collection;

/**
* @author Richard Déloge <[email protected]>
*/
class RoleBindingRepository extends Repository
{
protected string $uri = 'rolebindings';

/**
* @param array{items: array<int, array<mixed>|RoleBinding>} $response
*/
protected function createCollection(array $response): Collection
{
return new RoleBindingCollection($response['items']);
}
}
25 changes: 25 additions & 0 deletions src/Repositories/RoleRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Collections\RoleCollection;
use Maclof\Kubernetes\Models\Role;
use Maclof\Kubernetes\Collections\Collection;

/**
* @author Richard Déloge <[email protected]>
*/
class RoleRepository extends Repository
{
protected string $uri = 'roles';

/**
* @param array{items: array<int, array<mixed>|Role>} $response
*/
protected function createCollection(array $response): Collection
{
return new RoleCollection($response['items']);
}
}
25 changes: 25 additions & 0 deletions src/Repositories/ServiceAccountRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Collections\ServiceAccountCollection;
use Maclof\Kubernetes\Models\ServiceAccount;
use Maclof\Kubernetes\Collections\Collection;

/**
* @author Richard Déloge <[email protected]>
*/
class ServiceAccountRepository extends Repository
{
protected string $uri = 'serviceaccounts';

/**
* @param array{items: array<int, array<mixed>|ServiceAccount>} $response
*/
protected function createCollection(array $response): Collection
{
return new ServiceAccountCollection($response['items']);
}
}
12 changes: 8 additions & 4 deletions src/RepositoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,34 @@ class RepositoryRegistry implements \ArrayAccess, \Countable
'persistentVolume' => Repositories\PersistentVolumeRepository::class,
'persistentVolumeClaims' => Repositories\PersistentVolumeClaimRepository::class,
'namespaces' => Repositories\NamespaceRepository::class,
'serviceAccounts' => Repositories\ServiceAccountRepository::class,

// batch/v1
'jobs' => Repositories\JobRepository::class,

// batch/v2alpha1
// batch/v2
'cronJobs' => Repositories\CronJobRepository::class,

// apps/v1
'deployments' => Repositories\DeploymentRepository::class,

// extensions/v1beta1
// extensions/v1
'daemonSets' => Repositories\DaemonSetRepository::class,
'ingresses' => Repositories\IngressRepository::class,

// autoscaling/v2beta1
// autoscaling/v2
'horizontalPodAutoscalers' => Repositories\HorizontalPodAutoscalerRepository::class,

// networking.k8s.io/v1
'networkPolicies' => Repositories\NetworkPolicyRepository::class,

// certmanager.k8s.io/v1alpha1
// certmanager.k8s.io/v1
'certificates' => Repositories\CertificateRepository::class,
'issuers' => Repositories\IssuerRepository::class,

//rbac.authorization.k8s.io/v1
'roles' => Repositories\RoleRepository::class,
'roleBindings' => Repositories\RoleBindingRepository::class,
];

public function __construct()
Expand Down
2 changes: 1 addition & 1 deletion tests/RepositoryRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function test_builtin_repositories(): void
{
$registry = new RepositoryRegistry();

$this->assertCount(22, $registry);
$this->assertCount(25, $registry);
}

public function test_add_repository(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ protected function getFixture(string $path): ?string
// Fix for windows encoded fixtures.
$contents = str_replace("\r\n", "\n", $contents);

return $contents;
return trim($contents, ' ' . PHP_EOL);
}
}
Loading

0 comments on commit b726e64

Please sign in to comment.