Skip to content

Commit

Permalink
Merge pull request #31 from renoki-co/feature/exists
Browse files Browse the repository at this point in the history
[feature] Cluster syncs
  • Loading branch information
rennokki authored Oct 30, 2020
2 parents d4f8d64 + a9817aa commit 49402a2
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 29 deletions.
55 changes: 41 additions & 14 deletions src/Kinds/K8sResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,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.
Expand All @@ -138,6 +155,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
Expand Down Expand Up @@ -361,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,
Expand All @@ -380,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,
Expand All @@ -399,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,
Expand Down Expand Up @@ -512,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,
Expand All @@ -539,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,
Expand Down Expand Up @@ -577,8 +606,7 @@ public function logs(array $query = ['pretty' => 1])
);
}

return $this
->cluster
return $this->cluster
->setResourceClass(get_class($this))
->runOperation(
KubernetesCluster::LOG_OP,
Expand Down Expand Up @@ -619,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,
Expand Down
7 changes: 4 additions & 3 deletions tests/ConfigMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ public function runCreationTests()
->removeData('somekey');

$this->assertFalse($cm->isSynced());
$this->assertFalse($cm->exists());

$cm = $cm->create();
$cm = $cm->syncWithCluster();

$this->assertTrue($cm->isSynced());
$this->assertTrue($cm->exists());

$this->assertInstanceOf(K8sConfigMap::class, $cm);

Expand Down Expand Up @@ -96,8 +98,7 @@ public function runUpdateTests()

$this->assertTrue($cm->isSynced());

$cm
->removeData('key2')
$cm->removeData('key2')
->addData('newkey', 'newval');

$this->assertTrue($cm->update());
Expand Down
4 changes: 3 additions & 1 deletion tests/DeploymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ public function runCreationTests()
->setTemplate($pod);

$this->assertFalse($dep->isSynced());
$this->assertFalse($dep->exists());

$dep = $dep->create();
$dep = $dep->syncWithCluster();

$this->assertTrue($dep->isSynced());
$this->assertTrue($dep->exists());

$this->assertInstanceOf(K8sDeployment::class, $dep);

Expand Down
4 changes: 3 additions & 1 deletion tests/IngressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,12 @@ public function runCreationTests()
->setRules(self::$rules);

$this->assertFalse($ing->isSynced());
$this->assertFalse($ing->exists());

$ing = $ing->create();
$ing = $ing->syncWithCluster();

$this->assertTrue($ing->isSynced());
$this->assertTrue($ing->exists());

$this->assertInstanceOf(K8sIngress::class, $ing);

Expand Down
4 changes: 3 additions & 1 deletion tests/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,12 @@ public function runCreationTests()
->setTemplate($pod);

$this->assertFalse($job->isSynced());
$this->assertFalse($job->exists());

$job = $job->create();
$job = $job->syncWithCluster();

$this->assertTrue($job->isSynced());
$this->assertTrue($job->exists());

$this->assertInstanceOf(K8sJob::class, $job);

Expand Down
4 changes: 3 additions & 1 deletion tests/NamespaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public function runCreationTests()
->setName('production');

$this->assertFalse($ns->isSynced());
$this->assertFalse($ns->exists());

$ns = $ns->create();
$ns = $ns->syncWithCluster();

$this->assertTrue($ns->isSynced());
$this->assertTrue($ns->exists());

$this->assertInstanceOf(K8sNamespace::class, $ns);

Expand Down
4 changes: 3 additions & 1 deletion tests/PersistentVolumeClaimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ public function runCreationTests()
->setStorageClass($gp2);

$this->assertFalse($pvc->isSynced());
$this->assertFalse($pvc->exists());

$pvc = $pvc->create();
$pvc = $pvc->syncWithCluster();

$this->assertTrue($pvc->isSynced());
$this->assertTrue($pvc->exists());

$this->assertInstanceOf(K8sPersistentVolumeClaim::class, $pvc);

Expand Down
4 changes: 3 additions & 1 deletion tests/PersistentVolumeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public function runCreationTests()
->setStorageClass($sc);

$this->assertFalse($pv->isSynced());
$this->assertFalse($pv->exists());

$pv = $pv->create();
$pv = $pv->syncWithCluster();

$this->assertTrue($pv->isSynced());
$this->assertTrue($pv->exists());

$this->assertInstanceOf(K8sPersistentVolume::class, $pv);

Expand Down
4 changes: 3 additions & 1 deletion tests/PodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ public function runCreationTests()
->setContainers([$mysql]);

$this->assertFalse($pod->isSynced());
$this->assertFalse($pod->exists());

$pod = $pod->create();
$pod = $pod->syncWithCluster();

$this->assertTrue($pod->isSynced());
$this->assertTrue($pod->exists());

$this->assertInstanceOf(K8sPod::class, $pod);

Expand Down
4 changes: 3 additions & 1 deletion tests/SecretTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ public function runCreationTests()
->removeData('root');

$this->assertFalse($secret->isSynced());
$this->assertFalse($secret->exists());

$secret = $secret->create();
$secret = $secret->syncWithCluster();

$this->assertTrue($secret->isSynced());
$this->assertTrue($secret->exists());

$this->assertInstanceOf(K8sSecret::class, $secret);

Expand Down
4 changes: 3 additions & 1 deletion tests/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ 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());

$this->assertInstanceOf(K8sService::class, $svc);

Expand Down
6 changes: 4 additions & 2 deletions tests/StatefulSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function runCreationTests()
->setPorts([
['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306],
])
->create();
->syncWithCluster();

$pvc = $this->cluster->persistentVolumeClaim()
->setName('mysql-pvc')
Expand All @@ -153,10 +153,12 @@ public function runCreationTests()
->setVolumeClaims([$pvc]);

$this->assertFalse($sts->isSynced());
$this->assertFalse($sts->exists());

$sts = $sts->create();
$sts = $sts->syncWithCluster();

$this->assertTrue($sts->isSynced());
$this->assertTrue($sts->exists());

$this->assertInstanceOf(K8sStatefulSet::class, $sts);

Expand Down
4 changes: 3 additions & 1 deletion tests/StorageClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public function runCreationTests()
->setMountOptions(['debug']);

$this->assertFalse($sc->isSynced());
$this->assertFalse($sc->exists());

$sc = $sc->create();
$sc = $sc->syncWithCluster();

$this->assertTrue($sc->isSynced());
$this->assertTrue($sc->exists());

$this->assertInstanceOf(K8sStorageClass::class, $sc);

Expand Down

0 comments on commit 49402a2

Please sign in to comment.