From c027a68297d4b2e22a0d023ce66351fc4cad8aa1 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 29 Oct 2020 22:26:55 +0200 Subject: [PATCH 1/3] Added ->exists() --- src/Kinds/K8sResource.php | 18 ++++++++++++++++++ tests/ConfigMapTest.php | 2 ++ tests/DeploymentTest.php | 2 ++ tests/IngressTest.php | 2 ++ tests/JobTest.php | 2 ++ tests/NamespaceTest.php | 2 ++ tests/PersistentVolumeClaimTest.php | 2 ++ tests/PersistentVolumeTest.php | 2 ++ tests/PodTest.php | 2 ++ tests/SecretTest.php | 2 ++ tests/ServiceTest.php | 2 ++ tests/StatefulSetTest.php | 2 ++ tests/StorageClassTest.php | 2 ++ 13 files changed, 42 insertions(+) diff --git a/src/Kinds/K8sResource.php b/src/Kinds/K8sResource.php index 0763bd0b..17bdb834 100644 --- a/src/Kinds/K8sResource.php +++ b/src/Kinds/K8sResource.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Support\Jsonable; use RenokiCo\PhpK8s\Contracts\Loggable; use RenokiCo\PhpK8s\Contracts\Watchable; +use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException; use RenokiCo\PhpK8s\Exceptions\KubernetesWatchException; use RenokiCo\PhpK8s\KubernetesCluster; use RenokiCo\PhpK8s\Traits\HasAttributes; @@ -138,6 +139,23 @@ public function hasChanged(): bool return $this->attributes !== $this->original; } + /** + * Check if the current resource exists. + * + * @param array $query + * @return bool + */ + public function exists(array $query = ['pretty' => 1]): bool + { + try { + $this->get($query); + } catch (KubernetesAPIException $e) { + return false; + } + + return true; + } + /** * Get the API version of the resource. * This function can be overwritten at the resource diff --git a/tests/ConfigMapTest.php b/tests/ConfigMapTest.php index 313b407f..a03e7416 100644 --- a/tests/ConfigMapTest.php +++ b/tests/ConfigMapTest.php @@ -50,10 +50,12 @@ public function runCreationTests() ->removeData('somekey'); $this->assertFalse($cm->isSynced()); + $this->assertFalse($cm->exists()); $cm = $cm->create(); $this->assertTrue($cm->isSynced()); + $this->assertTrue($cm->exists()); $this->assertInstanceOf(K8sConfigMap::class, $cm); diff --git a/tests/DeploymentTest.php b/tests/DeploymentTest.php index 4b2fe275..32497e06 100644 --- a/tests/DeploymentTest.php +++ b/tests/DeploymentTest.php @@ -105,10 +105,12 @@ public function runCreationTests() ->setTemplate($pod); $this->assertFalse($dep->isSynced()); + $this->assertFalse($dep->exists()); $dep = $dep->create(); $this->assertTrue($dep->isSynced()); + $this->assertTrue($dep->exists()); $this->assertInstanceOf(K8sDeployment::class, $dep); diff --git a/tests/IngressTest.php b/tests/IngressTest.php index 3aad85b2..17400864 100644 --- a/tests/IngressTest.php +++ b/tests/IngressTest.php @@ -95,10 +95,12 @@ public function runCreationTests() ->setRules(self::$rules); $this->assertFalse($ing->isSynced()); + $this->assertFalse($ing->exists()); $ing = $ing->create(); $this->assertTrue($ing->isSynced()); + $this->assertTrue($ing->exists()); $this->assertInstanceOf(K8sIngress::class, $ing); diff --git a/tests/JobTest.php b/tests/JobTest.php index c51dee43..5d7536cf 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -96,10 +96,12 @@ public function runCreationTests() ->setTemplate($pod); $this->assertFalse($job->isSynced()); + $this->assertFalse($job->exists()); $job = $job->create(); $this->assertTrue($job->isSynced()); + $this->assertTrue($job->exists()); $this->assertInstanceOf(K8sJob::class, $job); diff --git a/tests/NamespaceTest.php b/tests/NamespaceTest.php index 9fd22e42..6def2f19 100644 --- a/tests/NamespaceTest.php +++ b/tests/NamespaceTest.php @@ -66,10 +66,12 @@ public function runCreationTests() ->setName('production'); $this->assertFalse($ns->isSynced()); + $this->assertFalse($ns->exists()); $ns = $ns->create(); $this->assertTrue($ns->isSynced()); + $this->assertTrue($ns->exists()); $this->assertInstanceOf(K8sNamespace::class, $ns); diff --git a/tests/PersistentVolumeClaimTest.php b/tests/PersistentVolumeClaimTest.php index 4cbc91bb..a6dfa7e6 100644 --- a/tests/PersistentVolumeClaimTest.php +++ b/tests/PersistentVolumeClaimTest.php @@ -65,10 +65,12 @@ public function runCreationTests() ->setStorageClass($gp2); $this->assertFalse($pvc->isSynced()); + $this->assertFalse($pvc->exists()); $pvc = $pvc->create(); $this->assertTrue($pvc->isSynced()); + $this->assertTrue($pvc->exists()); $this->assertInstanceOf(K8sPersistentVolumeClaim::class, $pvc); diff --git a/tests/PersistentVolumeTest.php b/tests/PersistentVolumeTest.php index ba400367..5548b1bd 100644 --- a/tests/PersistentVolumeTest.php +++ b/tests/PersistentVolumeTest.php @@ -74,10 +74,12 @@ public function runCreationTests() ->setStorageClass($sc); $this->assertFalse($pv->isSynced()); + $this->assertFalse($pv->exists()); $pv = $pv->create(); $this->assertTrue($pv->isSynced()); + $this->assertTrue($pv->exists()); $this->assertInstanceOf(K8sPersistentVolume::class, $pv); diff --git a/tests/PodTest.php b/tests/PodTest.php index ff28c6e4..d801f990 100644 --- a/tests/PodTest.php +++ b/tests/PodTest.php @@ -130,10 +130,12 @@ public function runCreationTests() ->setContainers([$mysql]); $this->assertFalse($pod->isSynced()); + $this->assertFalse($pod->exists()); $pod = $pod->create(); $this->assertTrue($pod->isSynced()); + $this->assertTrue($pod->exists()); $this->assertInstanceOf(K8sPod::class, $pod); diff --git a/tests/SecretTest.php b/tests/SecretTest.php index 28fb752c..c4c7dc74 100644 --- a/tests/SecretTest.php +++ b/tests/SecretTest.php @@ -52,10 +52,12 @@ public function runCreationTests() ->removeData('root'); $this->assertFalse($secret->isSynced()); + $this->assertFalse($secret->exists()); $secret = $secret->create(); $this->assertTrue($secret->isSynced()); + $this->assertTrue($secret->exists()); $this->assertInstanceOf(K8sSecret::class, $secret); diff --git a/tests/ServiceTest.php b/tests/ServiceTest.php index 66af12ce..60b57f19 100644 --- a/tests/ServiceTest.php +++ b/tests/ServiceTest.php @@ -62,10 +62,12 @@ public function runCreationTests() ]); $this->assertFalse($svc->isSynced()); + $this->assertFalse($svc->exists()); $svc = $svc->create(); $this->assertTrue($svc->isSynced()); + $this->assertTrue($svc->exists()); $this->assertInstanceOf(K8sService::class, $svc); diff --git a/tests/StatefulSetTest.php b/tests/StatefulSetTest.php index 6383cd77..708ff81c 100644 --- a/tests/StatefulSetTest.php +++ b/tests/StatefulSetTest.php @@ -153,10 +153,12 @@ public function runCreationTests() ->setVolumeClaims([$pvc]); $this->assertFalse($sts->isSynced()); + $this->assertFalse($sts->exists()); $sts = $sts->create(); $this->assertTrue($sts->isSynced()); + $this->assertTrue($sts->exists()); $this->assertInstanceOf(K8sStatefulSet::class, $sts); diff --git a/tests/StorageClassTest.php b/tests/StorageClassTest.php index fc41bce6..fe14d7ec 100644 --- a/tests/StorageClassTest.php +++ b/tests/StorageClassTest.php @@ -54,10 +54,12 @@ public function runCreationTests() ->setMountOptions(['debug']); $this->assertFalse($sc->isSynced()); + $this->assertFalse($sc->exists()); $sc = $sc->create(); $this->assertTrue($sc->isSynced()); + $this->assertTrue($sc->exists()); $this->assertInstanceOf(K8sStorageClass::class, $sc); From ac82ff17e75008ebe4ff731d0733bc7638a6676a Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 29 Oct 2020 22:33:40 +0200 Subject: [PATCH 2/3] Added ->syncWithCluster() --- src/Kinds/K8sResource.php | 16 ++++++++++++++++ tests/ConfigMapTest.php | 2 +- tests/DeploymentTest.php | 2 +- tests/IngressTest.php | 2 +- tests/JobTest.php | 2 +- tests/NamespaceTest.php | 2 +- tests/PersistentVolumeClaimTest.php | 2 +- tests/PersistentVolumeTest.php | 2 +- tests/PodTest.php | 2 +- tests/SecretTest.php | 2 +- tests/ServiceTest.php | 2 +- tests/StatefulSetTest.php | 4 ++-- tests/StorageClassTest.php | 2 +- 13 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/Kinds/K8sResource.php b/src/Kinds/K8sResource.php index 17bdb834..1a5fb612 100644 --- a/src/Kinds/K8sResource.php +++ b/src/Kinds/K8sResource.php @@ -124,6 +124,22 @@ public function syncWith(array $attributes = []) return $this; } + /** + * Create or update the resource according + * to the cluster availability. + * + * @param array $query + * @return $this + */ + public function syncWithCluster(array $query = ['pretty' => 1]) + { + try { + return $this->get($query); + } catch (KubernetesAPIException $e) { + return $this->create($query); + } + } + /** * Check if the resource changed from * its initial state. diff --git a/tests/ConfigMapTest.php b/tests/ConfigMapTest.php index a03e7416..42f543a3 100644 --- a/tests/ConfigMapTest.php +++ b/tests/ConfigMapTest.php @@ -52,7 +52,7 @@ public function runCreationTests() $this->assertFalse($cm->isSynced()); $this->assertFalse($cm->exists()); - $cm = $cm->create(); + $cm = $cm->syncWithCluster(); $this->assertTrue($cm->isSynced()); $this->assertTrue($cm->exists()); diff --git a/tests/DeploymentTest.php b/tests/DeploymentTest.php index 32497e06..1f749b82 100644 --- a/tests/DeploymentTest.php +++ b/tests/DeploymentTest.php @@ -107,7 +107,7 @@ public function runCreationTests() $this->assertFalse($dep->isSynced()); $this->assertFalse($dep->exists()); - $dep = $dep->create(); + $dep = $dep->syncWithCluster(); $this->assertTrue($dep->isSynced()); $this->assertTrue($dep->exists()); diff --git a/tests/IngressTest.php b/tests/IngressTest.php index 17400864..5ff1a0da 100644 --- a/tests/IngressTest.php +++ b/tests/IngressTest.php @@ -97,7 +97,7 @@ public function runCreationTests() $this->assertFalse($ing->isSynced()); $this->assertFalse($ing->exists()); - $ing = $ing->create(); + $ing = $ing->syncWithCluster(); $this->assertTrue($ing->isSynced()); $this->assertTrue($ing->exists()); diff --git a/tests/JobTest.php b/tests/JobTest.php index 5d7536cf..40c313d7 100644 --- a/tests/JobTest.php +++ b/tests/JobTest.php @@ -98,7 +98,7 @@ public function runCreationTests() $this->assertFalse($job->isSynced()); $this->assertFalse($job->exists()); - $job = $job->create(); + $job = $job->syncWithCluster(); $this->assertTrue($job->isSynced()); $this->assertTrue($job->exists()); diff --git a/tests/NamespaceTest.php b/tests/NamespaceTest.php index 6def2f19..14cbdc5c 100644 --- a/tests/NamespaceTest.php +++ b/tests/NamespaceTest.php @@ -68,7 +68,7 @@ public function runCreationTests() $this->assertFalse($ns->isSynced()); $this->assertFalse($ns->exists()); - $ns = $ns->create(); + $ns = $ns->syncWithCluster(); $this->assertTrue($ns->isSynced()); $this->assertTrue($ns->exists()); diff --git a/tests/PersistentVolumeClaimTest.php b/tests/PersistentVolumeClaimTest.php index a6dfa7e6..79664ab3 100644 --- a/tests/PersistentVolumeClaimTest.php +++ b/tests/PersistentVolumeClaimTest.php @@ -67,7 +67,7 @@ public function runCreationTests() $this->assertFalse($pvc->isSynced()); $this->assertFalse($pvc->exists()); - $pvc = $pvc->create(); + $pvc = $pvc->syncWithCluster(); $this->assertTrue($pvc->isSynced()); $this->assertTrue($pvc->exists()); diff --git a/tests/PersistentVolumeTest.php b/tests/PersistentVolumeTest.php index 5548b1bd..9e473583 100644 --- a/tests/PersistentVolumeTest.php +++ b/tests/PersistentVolumeTest.php @@ -76,7 +76,7 @@ public function runCreationTests() $this->assertFalse($pv->isSynced()); $this->assertFalse($pv->exists()); - $pv = $pv->create(); + $pv = $pv->syncWithCluster(); $this->assertTrue($pv->isSynced()); $this->assertTrue($pv->exists()); diff --git a/tests/PodTest.php b/tests/PodTest.php index d801f990..a52036d5 100644 --- a/tests/PodTest.php +++ b/tests/PodTest.php @@ -132,7 +132,7 @@ public function runCreationTests() $this->assertFalse($pod->isSynced()); $this->assertFalse($pod->exists()); - $pod = $pod->create(); + $pod = $pod->syncWithCluster(); $this->assertTrue($pod->isSynced()); $this->assertTrue($pod->exists()); diff --git a/tests/SecretTest.php b/tests/SecretTest.php index c4c7dc74..03b1d8c3 100644 --- a/tests/SecretTest.php +++ b/tests/SecretTest.php @@ -54,7 +54,7 @@ public function runCreationTests() $this->assertFalse($secret->isSynced()); $this->assertFalse($secret->exists()); - $secret = $secret->create(); + $secret = $secret->syncWithCluster(); $this->assertTrue($secret->isSynced()); $this->assertTrue($secret->exists()); diff --git a/tests/ServiceTest.php b/tests/ServiceTest.php index 60b57f19..b1556e6e 100644 --- a/tests/ServiceTest.php +++ b/tests/ServiceTest.php @@ -64,7 +64,7 @@ public function runCreationTests() $this->assertFalse($svc->isSynced()); $this->assertFalse($svc->exists()); - $svc = $svc->create(); + $svc = $svc->syncWithCluster(); $this->assertTrue($svc->isSynced()); $this->assertTrue($svc->exists()); diff --git a/tests/StatefulSetTest.php b/tests/StatefulSetTest.php index 708ff81c..685aa0fe 100644 --- a/tests/StatefulSetTest.php +++ b/tests/StatefulSetTest.php @@ -134,7 +134,7 @@ public function runCreationTests() ->setPorts([ ['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306], ]) - ->create(); + ->syncWithCluster(); $pvc = $this->cluster->persistentVolumeClaim() ->setName('mysql-pvc') @@ -155,7 +155,7 @@ public function runCreationTests() $this->assertFalse($sts->isSynced()); $this->assertFalse($sts->exists()); - $sts = $sts->create(); + $sts = $sts->syncWithCluster(); $this->assertTrue($sts->isSynced()); $this->assertTrue($sts->exists()); diff --git a/tests/StorageClassTest.php b/tests/StorageClassTest.php index fe14d7ec..19a73cf2 100644 --- a/tests/StorageClassTest.php +++ b/tests/StorageClassTest.php @@ -56,7 +56,7 @@ public function runCreationTests() $this->assertFalse($sc->isSynced()); $this->assertFalse($sc->exists()); - $sc = $sc->create(); + $sc = $sc->syncWithCluster(); $this->assertTrue($sc->isSynced()); $this->assertTrue($sc->exists()); From a9817aa33af980512b334c923c49e27e0b7a3f88 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Thu, 29 Oct 2020 22:33:43 +0200 Subject: [PATCH 3/3] Identation --- src/Kinds/K8sResource.php | 21 +++++++-------------- tests/ConfigMapTest.php | 3 +-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Kinds/K8sResource.php b/src/Kinds/K8sResource.php index 1a5fb612..bb785c50 100644 --- a/src/Kinds/K8sResource.php +++ b/src/Kinds/K8sResource.php @@ -395,8 +395,7 @@ public function toJsonPayload(string $kind = null) */ public function all(array $query = ['pretty' => 1]) { - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::GET_OP, @@ -414,8 +413,7 @@ public function all(array $query = ['pretty' => 1]) */ public function get(array $query = ['pretty' => 1]) { - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::GET_OP, @@ -433,8 +431,7 @@ public function get(array $query = ['pretty' => 1]) */ public function create(array $query = ['pretty' => 1]) { - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::CREATE_OP, @@ -546,8 +543,7 @@ public function watchAll(Closure $callback, array $query = ['pretty' => 1]) ); } - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::WATCH_OP, @@ -573,8 +569,7 @@ public function watch(Closure $callback, array $query = ['pretty' => 1]) ); } - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::WATCH_OP, @@ -611,8 +606,7 @@ public function logs(array $query = ['pretty' => 1]) ); } - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::LOG_OP, @@ -653,8 +647,7 @@ public function watchLogs(Closure $callback, array $query = ['pretty' => 1]) // Ensure the ?follow=1 query exists to trigger the watch. $query = array_merge($query, ['follow' => 1]); - return $this - ->cluster + return $this->cluster ->setResourceClass(get_class($this)) ->runOperation( KubernetesCluster::WATCH_LOGS_OP, diff --git a/tests/ConfigMapTest.php b/tests/ConfigMapTest.php index 42f543a3..d8232be8 100644 --- a/tests/ConfigMapTest.php +++ b/tests/ConfigMapTest.php @@ -98,8 +98,7 @@ public function runUpdateTests() $this->assertTrue($cm->isSynced()); - $cm - ->removeData('key2') + $cm->removeData('key2') ->addData('newkey', 'newval'); $this->assertTrue($cm->update());