Skip to content

Commit 49402a2

Browse files
authored
Merge pull request #31 from renoki-co/feature/exists
[feature] Cluster syncs
2 parents d4f8d64 + a9817aa commit 49402a2

13 files changed

+79
-29
lines changed

src/Kinds/K8sResource.php

+41-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Support\Jsonable;
88
use RenokiCo\PhpK8s\Contracts\Loggable;
99
use RenokiCo\PhpK8s\Contracts\Watchable;
10+
use RenokiCo\PhpK8s\Exceptions\KubernetesAPIException;
1011
use RenokiCo\PhpK8s\Exceptions\KubernetesWatchException;
1112
use RenokiCo\PhpK8s\KubernetesCluster;
1213
use RenokiCo\PhpK8s\Traits\HasAttributes;
@@ -123,6 +124,22 @@ public function syncWith(array $attributes = [])
123124
return $this;
124125
}
125126

127+
/**
128+
* Create or update the resource according
129+
* to the cluster availability.
130+
*
131+
* @param array $query
132+
* @return $this
133+
*/
134+
public function syncWithCluster(array $query = ['pretty' => 1])
135+
{
136+
try {
137+
return $this->get($query);
138+
} catch (KubernetesAPIException $e) {
139+
return $this->create($query);
140+
}
141+
}
142+
126143
/**
127144
* Check if the resource changed from
128145
* its initial state.
@@ -138,6 +155,23 @@ public function hasChanged(): bool
138155
return $this->attributes !== $this->original;
139156
}
140157

158+
/**
159+
* Check if the current resource exists.
160+
*
161+
* @param array $query
162+
* @return bool
163+
*/
164+
public function exists(array $query = ['pretty' => 1]): bool
165+
{
166+
try {
167+
$this->get($query);
168+
} catch (KubernetesAPIException $e) {
169+
return false;
170+
}
171+
172+
return true;
173+
}
174+
141175
/**
142176
* Get the API version of the resource.
143177
* This function can be overwritten at the resource
@@ -361,8 +395,7 @@ public function toJsonPayload(string $kind = null)
361395
*/
362396
public function all(array $query = ['pretty' => 1])
363397
{
364-
return $this
365-
->cluster
398+
return $this->cluster
366399
->setResourceClass(get_class($this))
367400
->runOperation(
368401
KubernetesCluster::GET_OP,
@@ -380,8 +413,7 @@ public function all(array $query = ['pretty' => 1])
380413
*/
381414
public function get(array $query = ['pretty' => 1])
382415
{
383-
return $this
384-
->cluster
416+
return $this->cluster
385417
->setResourceClass(get_class($this))
386418
->runOperation(
387419
KubernetesCluster::GET_OP,
@@ -399,8 +431,7 @@ public function get(array $query = ['pretty' => 1])
399431
*/
400432
public function create(array $query = ['pretty' => 1])
401433
{
402-
return $this
403-
->cluster
434+
return $this->cluster
404435
->setResourceClass(get_class($this))
405436
->runOperation(
406437
KubernetesCluster::CREATE_OP,
@@ -512,8 +543,7 @@ public function watchAll(Closure $callback, array $query = ['pretty' => 1])
512543
);
513544
}
514545

515-
return $this
516-
->cluster
546+
return $this->cluster
517547
->setResourceClass(get_class($this))
518548
->runOperation(
519549
KubernetesCluster::WATCH_OP,
@@ -539,8 +569,7 @@ public function watch(Closure $callback, array $query = ['pretty' => 1])
539569
);
540570
}
541571

542-
return $this
543-
->cluster
572+
return $this->cluster
544573
->setResourceClass(get_class($this))
545574
->runOperation(
546575
KubernetesCluster::WATCH_OP,
@@ -577,8 +606,7 @@ public function logs(array $query = ['pretty' => 1])
577606
);
578607
}
579608

580-
return $this
581-
->cluster
609+
return $this->cluster
582610
->setResourceClass(get_class($this))
583611
->runOperation(
584612
KubernetesCluster::LOG_OP,
@@ -619,8 +647,7 @@ public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
619647
// Ensure the ?follow=1 query exists to trigger the watch.
620648
$query = array_merge($query, ['follow' => 1]);
621649

622-
return $this
623-
->cluster
650+
return $this->cluster
624651
->setResourceClass(get_class($this))
625652
->runOperation(
626653
KubernetesCluster::WATCH_LOGS_OP,

tests/ConfigMapTest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ public function runCreationTests()
5050
->removeData('somekey');
5151

5252
$this->assertFalse($cm->isSynced());
53+
$this->assertFalse($cm->exists());
5354

54-
$cm = $cm->create();
55+
$cm = $cm->syncWithCluster();
5556

5657
$this->assertTrue($cm->isSynced());
58+
$this->assertTrue($cm->exists());
5759

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

@@ -96,8 +98,7 @@ public function runUpdateTests()
9698

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

99-
$cm
100-
->removeData('key2')
101+
$cm->removeData('key2')
101102
->addData('newkey', 'newval');
102103

103104
$this->assertTrue($cm->update());

tests/DeploymentTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ public function runCreationTests()
105105
->setTemplate($pod);
106106

107107
$this->assertFalse($dep->isSynced());
108+
$this->assertFalse($dep->exists());
108109

109-
$dep = $dep->create();
110+
$dep = $dep->syncWithCluster();
110111

111112
$this->assertTrue($dep->isSynced());
113+
$this->assertTrue($dep->exists());
112114

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

tests/IngressTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ public function runCreationTests()
9595
->setRules(self::$rules);
9696

9797
$this->assertFalse($ing->isSynced());
98+
$this->assertFalse($ing->exists());
9899

99-
$ing = $ing->create();
100+
$ing = $ing->syncWithCluster();
100101

101102
$this->assertTrue($ing->isSynced());
103+
$this->assertTrue($ing->exists());
102104

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

tests/JobTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,12 @@ public function runCreationTests()
9696
->setTemplate($pod);
9797

9898
$this->assertFalse($job->isSynced());
99+
$this->assertFalse($job->exists());
99100

100-
$job = $job->create();
101+
$job = $job->syncWithCluster();
101102

102103
$this->assertTrue($job->isSynced());
104+
$this->assertTrue($job->exists());
103105

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

tests/NamespaceTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ public function runCreationTests()
6666
->setName('production');
6767

6868
$this->assertFalse($ns->isSynced());
69+
$this->assertFalse($ns->exists());
6970

70-
$ns = $ns->create();
71+
$ns = $ns->syncWithCluster();
7172

7273
$this->assertTrue($ns->isSynced());
74+
$this->assertTrue($ns->exists());
7375

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

tests/PersistentVolumeClaimTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ public function runCreationTests()
6565
->setStorageClass($gp2);
6666

6767
$this->assertFalse($pvc->isSynced());
68+
$this->assertFalse($pvc->exists());
6869

69-
$pvc = $pvc->create();
70+
$pvc = $pvc->syncWithCluster();
7071

7172
$this->assertTrue($pvc->isSynced());
73+
$this->assertTrue($pvc->exists());
7274

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

tests/PersistentVolumeTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ public function runCreationTests()
7474
->setStorageClass($sc);
7575

7676
$this->assertFalse($pv->isSynced());
77+
$this->assertFalse($pv->exists());
7778

78-
$pv = $pv->create();
79+
$pv = $pv->syncWithCluster();
7980

8081
$this->assertTrue($pv->isSynced());
82+
$this->assertTrue($pv->exists());
8183

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

tests/PodTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ public function runCreationTests()
130130
->setContainers([$mysql]);
131131

132132
$this->assertFalse($pod->isSynced());
133+
$this->assertFalse($pod->exists());
133134

134-
$pod = $pod->create();
135+
$pod = $pod->syncWithCluster();
135136

136137
$this->assertTrue($pod->isSynced());
138+
$this->assertTrue($pod->exists());
137139

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

tests/SecretTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ public function runCreationTests()
5252
->removeData('root');
5353

5454
$this->assertFalse($secret->isSynced());
55+
$this->assertFalse($secret->exists());
5556

56-
$secret = $secret->create();
57+
$secret = $secret->syncWithCluster();
5758

5859
$this->assertTrue($secret->isSynced());
60+
$this->assertTrue($secret->exists());
5961

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

tests/ServiceTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ public function runCreationTests()
6262
]);
6363

6464
$this->assertFalse($svc->isSynced());
65+
$this->assertFalse($svc->exists());
6566

66-
$svc = $svc->create();
67+
$svc = $svc->syncWithCluster();
6768

6869
$this->assertTrue($svc->isSynced());
70+
$this->assertTrue($svc->exists());
6971

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

tests/StatefulSetTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function runCreationTests()
134134
->setPorts([
135135
['protocol' => 'TCP', 'port' => 3306, 'targetPort' => 3306],
136136
])
137-
->create();
137+
->syncWithCluster();
138138

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

155155
$this->assertFalse($sts->isSynced());
156+
$this->assertFalse($sts->exists());
156157

157-
$sts = $sts->create();
158+
$sts = $sts->syncWithCluster();
158159

159160
$this->assertTrue($sts->isSynced());
161+
$this->assertTrue($sts->exists());
160162

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

tests/StorageClassTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,12 @@ public function runCreationTests()
5454
->setMountOptions(['debug']);
5555

5656
$this->assertFalse($sc->isSynced());
57+
$this->assertFalse($sc->exists());
5758

58-
$sc = $sc->create();
59+
$sc = $sc->syncWithCluster();
5960

6061
$this->assertTrue($sc->isSynced());
62+
$this->assertTrue($sc->exists());
6163

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

0 commit comments

Comments
 (0)