Skip to content

Commit

Permalink
Merge pull request #121 from renoki-co/feature/get-from-all-namespaces
Browse files Browse the repository at this point in the history
[2.x] Added ->get[Resource]FromAllNamespaces
  • Loading branch information
rennokki authored Sep 3, 2021
2 parents 3c2b5d4 + 18d1b0c commit 46e981e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/Contracts/InteractsWithK8sCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ interface InteractsWithK8sCluster
/**
* Get the path, prefixed by '/', that points to the resources list.
*
* @param bool $withNamespace
* @return string
*/
public function allResourcesPath(): string;
public function allResourcesPath(bool $withNamespace = true): string;

/**
* Get the path, prefixed by '/', that points to the specific resource.
Expand Down
12 changes: 12 additions & 0 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,18 @@ public function __call($method, $parameters)
}
}

// Proxy the ->getAll[Resources]FromAllNamespaces($query = [...])
// For example, ->getAllIngressesFromAllNamespaces()
if (preg_match('/getAll(.+)FromAllNamespaces/', $method, $matches)) {
[$method, $resourcePlural] = $matches;

$resource = Str::singular($resourcePlural);

if (method_exists(K8s::class, $resource)) {
return $this->{$resource}()->allNamespaces($parameters[0] ?? ['pretty' => 1]);
}
}

// Proxy the ->getAll[Resources]($namespace = 'default', $query = [...])
// For example, ->getAllServices('staging')
if (preg_match('/getAll(.+)/', $method, $matches)) {
Expand Down
25 changes: 22 additions & 3 deletions src/Traits/RunsClusterOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function refreshOriginal(array $query = ['pretty' => 1])
*
* @param array $query
* @return $this
* @deprecated Deprecated in 1.9.0, will be removed in 2.0
*/
public function syncWithCluster(array $query = ['pretty' => 1])
{
Expand Down Expand Up @@ -148,6 +147,25 @@ public function all(array $query = ['pretty' => 1])
);
}

/**
* Get a list with all resources from all namespaces.
*
* @param array $query
* @return \RenokiCo\PhpK8s\ResourcesList
* @throws \RenokiCo\PhpK8s\Exceptions\KubernetesAPIException
*/
public function allNamespaces(array $query = ['pretty' => 1])
{
return $this->cluster
->setResourceClass(get_class($this))
->runOperation(
KubernetesCluster::GET_OP,
$this->allResourcesPath(false),
$this->toJsonPayload(),
$query
);
}

/**
* Get a specific resource.
*
Expand Down Expand Up @@ -462,11 +480,12 @@ public function attach(
/**
* Get the path, prefixed by '/', that points to the resources list.
*
* @param bool $withNamespace
* @return string
*/
public function allResourcesPath(): string
public function allResourcesPath(bool $withNamespace = true): string
{
return "{$this->getApiPathPrefix()}/".static::getPlural();
return "{$this->getApiPathPrefix($withNamespace)}/".static::getPlural();
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/IngressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function test_ingress_api_interaction()
{
$this->runCreationTests();
$this->runGetAllTests();
$this->runGetAllFromAllNamespacesTests();
$this->runGetTests();
$this->runUpdateTests();
$this->runWatchAllTests();
Expand Down Expand Up @@ -123,6 +124,19 @@ public function runGetAllTests()
}
}

public function runGetAllFromAllNamespacesTests()
{
$ingresss = $this->cluster->getAllIngressesFromAllNamespaces();

$this->assertInstanceOf(ResourcesList::class, $ingresss);

foreach ($ingresss as $ing) {
$this->assertInstanceOf(K8sIngress::class, $ing);

$this->assertNotNull($ing->getName());
}
}

public function runGetTests()
{
$ing = $this->cluster->getIngressByName('nginx');
Expand Down

0 comments on commit 46e981e

Please sign in to comment.