Skip to content

Commit 4eb240d

Browse files
committed
Added macro for K8s
1 parent 8c040c3 commit 4eb240d

4 files changed

Lines changed: 45 additions & 1 deletion

File tree

docs/kinds/Resource.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,15 @@ K8sPod::macro('changeMetadata', function (array $metadata) {
228228
return $this->setMetadata($metadata);
229229
});
230230
```
231+
232+
Usually, it's initialize the resources from `K8s` class, so that they automatically gets redirected to cluster calls that actually make the API requests, so if you have new resources to initialize, you can use the macro on it, for example, like an [Agones Fleet](https://agones.dev/site/docs/reference/fleet):
233+
234+
```php
235+
K8s::macro('agonesFleet', function ($cluster = null, $attributes = []) {
236+
return new Kinds\MyAgonesFleet($cluster, $attributes);
237+
});
238+
239+
foreach (K8s::agonesFleet()->all() as $fleet) {
240+
//
241+
}
242+
```

src/K8s.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
namespace RenokiCo\PhpK8s;
44

5+
use Illuminate\Support\Traits\Macroable;
6+
57
class K8s
68
{
9+
use Macroable {
10+
__call as macroCall;
11+
}
12+
713
/**
814
* Create a new Node kind.
915
*
@@ -395,4 +401,20 @@ public static function fromYamlFile($cluster, string $path)
395401
{
396402
return static::fromYaml($cluster, file_get_contents($path));
397403
}
404+
405+
/**
406+
* Proxy the K8s call to cluster object.
407+
*
408+
* @param string $method
409+
* @param array $parameters
410+
* @return mixed
411+
*/
412+
public function __call($method, $parameters)
413+
{
414+
if (static::hasMacro($method)) {
415+
return $this->macroCall($method, $parameters);
416+
}
417+
418+
return $this->cluster->{$method}(...$parameters);
419+
}
398420
}

src/Traits/HasAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function getAttribute(string $name, $default = null)
8181
*
8282
* @param string $method
8383
* @param array $parameters
84-
* @return $this
84+
* @return mixed
8585
*/
8686
public function __call(string $method, array $parameters)
8787
{

tests/MacroTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,14 @@ public function test_resource_macro()
3737

3838
$this->assertEquals(['val1', 'val2'], $pod->getMacroTest());
3939
}
40+
41+
public function test_k8s_macro()
42+
{
43+
K8s::macro('newResource', function ($cluster = null, $attributes = []) {
44+
return new K8sPod($cluster, $attributes);
45+
});
46+
47+
$this->assertInstanceOf(K8sPod::class, K8s::newResource());
48+
$this->assertInstanceOf(K8sPod::class, $this->cluster->newResource());
49+
}
4050
}

0 commit comments

Comments
 (0)