77use Illuminate \Contracts \Support \Jsonable ;
88use RenokiCo \PhpK8s \Contracts \Loggable ;
99use RenokiCo \PhpK8s \Contracts \Watchable ;
10+ use RenokiCo \PhpK8s \Exceptions \KubernetesAPIException ;
1011use RenokiCo \PhpK8s \Exceptions \KubernetesWatchException ;
1112use RenokiCo \PhpK8s \KubernetesCluster ;
1213use 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 ,
0 commit comments