From 0fe39b8fc2064d9b369f67e31cc8adbfe3397653 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 2 Sep 2021 21:11:11 +0300 Subject: [PATCH 1/2] Added ->get[Resource]FromAllNamespaces --- src/Contracts/InteractsWithK8sCluster.php | 3 ++- src/KubernetesCluster.php | 12 ++++++++++++ src/Traits/RunsClusterOperations.php | 24 +++++++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/Contracts/InteractsWithK8sCluster.php b/src/Contracts/InteractsWithK8sCluster.php index ad593158..71a75ec4 100644 --- a/src/Contracts/InteractsWithK8sCluster.php +++ b/src/Contracts/InteractsWithK8sCluster.php @@ -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. diff --git a/src/KubernetesCluster.php b/src/KubernetesCluster.php index fdf5486e..6aee8bf3 100644 --- a/src/KubernetesCluster.php +++ b/src/KubernetesCluster.php @@ -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)) { diff --git a/src/Traits/RunsClusterOperations.php b/src/Traits/RunsClusterOperations.php index fc614d2a..3c85667b 100644 --- a/src/Traits/RunsClusterOperations.php +++ b/src/Traits/RunsClusterOperations.php @@ -148,6 +148,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. * @@ -462,11 +481,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(); } /** From 18d1b0c396fe726a0704d08643032c52f715506c Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Fri, 3 Sep 2021 15:16:55 +0300 Subject: [PATCH 2/2] Added basic testing Removed deprecation notice --- src/Traits/RunsClusterOperations.php | 1 - tests/IngressTest.php | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Traits/RunsClusterOperations.php b/src/Traits/RunsClusterOperations.php index 3c85667b..a53c2099 100644 --- a/src/Traits/RunsClusterOperations.php +++ b/src/Traits/RunsClusterOperations.php @@ -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]) { diff --git a/tests/IngressTest.php b/tests/IngressTest.php index d59dc95d..2ce0e4d2 100644 --- a/tests/IngressTest.php +++ b/tests/IngressTest.php @@ -76,6 +76,7 @@ public function test_ingress_api_interaction() { $this->runCreationTests(); $this->runGetAllTests(); + $this->runGetAllFromAllNamespacesTests(); $this->runGetTests(); $this->runUpdateTests(); $this->runWatchAllTests(); @@ -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');