7
7
use Illuminate \Contracts \Support \Jsonable ;
8
8
use RenokiCo \PhpK8s \Contracts \Loggable ;
9
9
use RenokiCo \PhpK8s \Contracts \Watchable ;
10
+ use RenokiCo \PhpK8s \Exceptions \KubernetesAPIException ;
10
11
use RenokiCo \PhpK8s \Exceptions \KubernetesWatchException ;
11
12
use RenokiCo \PhpK8s \KubernetesCluster ;
12
13
use RenokiCo \PhpK8s \Traits \HasAttributes ;
@@ -123,6 +124,22 @@ public function syncWith(array $attributes = [])
123
124
return $ this ;
124
125
}
125
126
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
+
126
143
/**
127
144
* Check if the resource changed from
128
145
* its initial state.
@@ -138,6 +155,23 @@ public function hasChanged(): bool
138
155
return $ this ->attributes !== $ this ->original ;
139
156
}
140
157
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
+
141
175
/**
142
176
* Get the API version of the resource.
143
177
* This function can be overwritten at the resource
@@ -361,8 +395,7 @@ public function toJsonPayload(string $kind = null)
361
395
*/
362
396
public function all (array $ query = ['pretty ' => 1 ])
363
397
{
364
- return $ this
365
- ->cluster
398
+ return $ this ->cluster
366
399
->setResourceClass (get_class ($ this ))
367
400
->runOperation (
368
401
KubernetesCluster::GET_OP ,
@@ -380,8 +413,7 @@ public function all(array $query = ['pretty' => 1])
380
413
*/
381
414
public function get (array $ query = ['pretty ' => 1 ])
382
415
{
383
- return $ this
384
- ->cluster
416
+ return $ this ->cluster
385
417
->setResourceClass (get_class ($ this ))
386
418
->runOperation (
387
419
KubernetesCluster::GET_OP ,
@@ -399,8 +431,7 @@ public function get(array $query = ['pretty' => 1])
399
431
*/
400
432
public function create (array $ query = ['pretty ' => 1 ])
401
433
{
402
- return $ this
403
- ->cluster
434
+ return $ this ->cluster
404
435
->setResourceClass (get_class ($ this ))
405
436
->runOperation (
406
437
KubernetesCluster::CREATE_OP ,
@@ -512,8 +543,7 @@ public function watchAll(Closure $callback, array $query = ['pretty' => 1])
512
543
);
513
544
}
514
545
515
- return $ this
516
- ->cluster
546
+ return $ this ->cluster
517
547
->setResourceClass (get_class ($ this ))
518
548
->runOperation (
519
549
KubernetesCluster::WATCH_OP ,
@@ -539,8 +569,7 @@ public function watch(Closure $callback, array $query = ['pretty' => 1])
539
569
);
540
570
}
541
571
542
- return $ this
543
- ->cluster
572
+ return $ this ->cluster
544
573
->setResourceClass (get_class ($ this ))
545
574
->runOperation (
546
575
KubernetesCluster::WATCH_OP ,
@@ -577,8 +606,7 @@ public function logs(array $query = ['pretty' => 1])
577
606
);
578
607
}
579
608
580
- return $ this
581
- ->cluster
609
+ return $ this ->cluster
582
610
->setResourceClass (get_class ($ this ))
583
611
->runOperation (
584
612
KubernetesCluster::LOG_OP ,
@@ -619,8 +647,7 @@ public function watchLogs(Closure $callback, array $query = ['pretty' => 1])
619
647
// Ensure the ?follow=1 query exists to trigger the watch.
620
648
$ query = array_merge ($ query , ['follow ' => 1 ]);
621
649
622
- return $ this
623
- ->cluster
650
+ return $ this ->cluster
624
651
->setResourceClass (get_class ($ this ))
625
652
->runOperation (
626
653
KubernetesCluster::WATCH_LOGS_OP ,
0 commit comments