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

[k8s-client] Support for apps/deployment & replace method #245

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
8 changes: 8 additions & 0 deletions libs/k8s-client/provisioning/local/kubernetes/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ resource "kubernetes_role" "k8s_client" {
]
verbs = ["get", "list", "delete", "create", "patch", "deletecollection"]
}

rule {
api_groups = ["apps"]
resources = [
"deployments",
]
verbs = ["get", "list", "delete", "create", "patch", "deletecollection"]
}
}

resource "kubernetes_role_binding" "k8s_client" {
Expand Down
9 changes: 9 additions & 0 deletions libs/k8s-client/src/ApiClient/BaseNamespaceApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ public function patch(string $name, Patch $model, array $queries = []): Abstract
return $this->apiClient->request($this->baseApi, 'patch', $this->itemClass, $name, $model, $queries);
}

/**
* @param TItem $model
* @return TItem
*/
public function replace(string $name, AbstractModel $model, array $queries = []): AbstractModel
{
return $this->apiClient->request($this->baseApi, 'replace', $this->itemClass, $name, $model, $queries);
}

public function delete(string $name, ?DeleteOptions $options = null, array $queries = []): Status
{
$options ??= new DeleteOptions();
Expand Down
26 changes: 26 additions & 0 deletions libs/k8s-client/src/ApiClient/DeploymentsApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Keboola\K8sClient\ApiClient;

use Keboola\K8sClient\KubernetesApiClient;
use Kubernetes\API\Deployment as DeploymentsApi;
use Kubernetes\Model\Io\K8s\Api\Apps\V1\Deployment;
use Kubernetes\Model\Io\K8s\Api\Apps\V1\DeploymentList;

/**
* @template-extends BaseNamespaceApiClient<DeploymentsApi, DeploymentList, Deployment>
*/
class DeploymentsApiClient extends BaseNamespaceApiClient
{
public function __construct(KubernetesApiClient $apiClient)
{
parent::__construct(
$apiClient,
new DeploymentsApi(),
DeploymentList::class,
Deployment::class,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Keboola\K8sClient\ClientFacadeFactory;

use Keboola\K8sClient\ApiClient\ConfigMapsApiClient;
use Keboola\K8sClient\ApiClient\DeploymentsApiClient;
use Keboola\K8sClient\ApiClient\EventsApiClient;
use Keboola\K8sClient\ApiClient\IngressesApiClient;
use Keboola\K8sClient\ApiClient\PersistentVolumeClaimsApiClient;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function createClusterClient(
return new KubernetesApiClientFacade(
$this->logger,
new ConfigMapsApiClient($apiClient),
new DeploymentsApiClient($apiClient),
new EventsApiClient($apiClient),
new IngressesApiClient($apiClient),
new PersistentVolumeClaimsApiClient($apiClient),
Expand Down
32 changes: 21 additions & 11 deletions libs/k8s-client/src/KubernetesApiClientFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Keboola\K8sClient;

use Keboola\K8sClient\ApiClient\ConfigMapsApiClient;
use Keboola\K8sClient\ApiClient\DeploymentsApiClient;
use Keboola\K8sClient\ApiClient\EventsApiClient;
use Keboola\K8sClient\ApiClient\IngressesApiClient;
use Keboola\K8sClient\ApiClient\PersistentVolumeClaimsApiClient;
Expand All @@ -14,6 +15,7 @@
use Keboola\K8sClient\ApiClient\ServicesApiClient;
use Keboola\K8sClient\Exception\ResourceNotFoundException;
use Keboola\K8sClient\Exception\TimeoutException;
use Kubernetes\Model\Io\K8s\Api\Apps\V1\Deployment;
use Kubernetes\Model\Io\K8s\Api\Core\V1\ConfigMap;
use Kubernetes\Model\Io\K8s\Api\Core\V1\Event;
use Kubernetes\Model\Io\K8s\Api\Core\V1\PersistentVolume;
Expand All @@ -29,6 +31,7 @@
use RuntimeException;
use Throwable;

// phpcs:disable Generic.Files.LineLength.MaxExceeded
class KubernetesApiClientFacade
{
private const LIST_INTERNAL_PAGE_SIZE = 100;
Expand All @@ -38,6 +41,7 @@ class KubernetesApiClientFacade
public function __construct(
private readonly LoggerInterface $logger,
private readonly ConfigMapsApiClient $configMapApiClient,
private readonly DeploymentsApiClient $deploymentsApiClient,
private readonly EventsApiClient $eventsApiClient,
private readonly IngressesApiClient $ingressesApiClient,
private readonly PersistentVolumeClaimsApiClient $persistentVolumeClaimsApiClient,
Expand All @@ -48,6 +52,7 @@ public function __construct(
) {
$this->resourceTypeClientMap = [
ConfigMap::class => $this->configMapApiClient,
Deployment::class => $this->deploymentsApiClient,
Event::class => $this->eventsApiClient,
PersistentVolumeClaim::class => $this->persistentVolumeClaimsApiClient,
Pod::class => $this->podsApiClient,
Expand Down Expand Up @@ -78,6 +83,11 @@ public function events(): EventsApiClient
return $this->eventsApiClient;
}

public function deployments(): DeploymentsApiClient
{
return $this->deploymentsApiClient;
}

public function persistentVolumeClaims(): PersistentVolumeClaimsApiClient
{
return $this->persistentVolumeClaimsApiClient;
Expand All @@ -99,7 +109,7 @@ public function persistentVolumes(): PersistentVolumesApiClient
}

/**
* @phpstan-template T of ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @phpstan-template T of ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @phpstan-param class-string<T> $resourceType
* @phpstan-return T
*/
Expand All @@ -125,8 +135,8 @@ public function get(string $resourceType, string $name, array $queries = [])
* new Ingress(...),
* ])
*
* @param array<ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
* @return (ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume)[]
* @param array<ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
* @return (ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume)[]
*/
public function createModels(array $resources, array $queries = []): array
{
Expand All @@ -153,7 +163,7 @@ public function createModels(array $resources, array $queries = []): array
* new PersistentVolume(...),
* ])
*
* @param array<ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
* @param array<ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
* @return Status[]
*/
public function deleteModels(array $resources, ?DeleteOptions $deleteOptions = null, array $queries = []): array
Expand All @@ -169,7 +179,7 @@ public function deleteModels(array $resources, ?DeleteOptions $deleteOptions = n
}

/**
* @param array<ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
* @param array<ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume> $resources
*/
public function waitWhileExists(array $resources, float $timeout = INF): void
{
Expand Down Expand Up @@ -209,7 +219,7 @@ public function waitWhileExists(array $resources, float $timeout = INF): void
}

/**
* @template T of ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @template T of ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @param class-string<T> $resourceType
* @return iterable<T>
*/
Expand All @@ -235,7 +245,7 @@ public function listMatching(string $resourceType, array $queries = []): iterabl
* Resources are delete sequentially by API type. If some delete request fails, the error is logged and other APIs
* are still called. Finally, the last exception is re-thrown.
*
* @template T of ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @template T of ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @param array{
* resourceTypes?: class-string<T>[]
* } $queries
Expand Down Expand Up @@ -274,7 +284,7 @@ public function deleteAllMatching(?DeleteOptions $deleteOptions = null, array $q
}

/**
* @template T of ConfigMap|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @template T of ConfigMap|Deployment|Event|PersistentVolumeClaim|Pod|Secret|Service|Ingress|PersistentVolume
* @param class-string<T> $resourceType
*/
public function checkResourceExists(string $resourceType, string $resourceName): bool
Expand All @@ -291,17 +301,17 @@ public function checkResourceExists(string $resourceType, string $resourceName):
/**
* @param class-string<AbstractModel> $resourceType
* @return ($resourceType is class-string<ConfigMap> ? ConfigMapsApiClient :
* ($resourceType is class-string<Deployment> ? DeploymentsApiClient :
* ($resourceType is class-string<Event> ? EventsApiClient :
* ($resourceType is class-string<PersistentVolumeClaim> ? PersistentVolumeClaimsApiClient :
* ($resourceType is class-string<Pod> ? PodsApiClient :
* ($resourceType is class-string<Secret> ? SecretsApiClient :
* ($resourceType is class-string<Service> ? ServicesApiClient :
* ($resourceType is class-string<Ingress> ? IngressesApiClient :
* ($resourceType is class-string<PersistentVolume> ? PersistentVolumesApiClient :
* never))))))))
* never)))))))))
*/
// phpcs:ignore Generic.Files.LineLength.MaxExceeded
private function getApiForResource(string $resourceType): ConfigMapsApiClient|EventsApiClient|PersistentVolumeClaimsApiClient|PodsApiClient|SecretsApiClient|ServicesApiClient|IngressesApiClient|PersistentVolumesApiClient
private function getApiForResource(string $resourceType): ConfigMapsApiClient|DeploymentsApiClient|EventsApiClient|PersistentVolumeClaimsApiClient|PodsApiClient|SecretsApiClient|ServicesApiClient|IngressesApiClient|PersistentVolumesApiClient
{
if (!array_key_exists($resourceType, $this->resourceTypeClientMap)) {
throw new RuntimeException(sprintf(
Expand Down
Loading