Skip to content

Commit e7ecf7d

Browse files
authored
Merge pull request #59 from renoki-co/feature/k8s-macro
[feature] K8s Macro
2 parents 8c040c3 + ff25132 commit e7ecf7d

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

docs/kinds/Resource.md

+12
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

+39
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,15 @@
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+
__callStatic as macroCallStatic;
12+
}
13+
714
/**
815
* Create a new Node kind.
916
*
@@ -395,4 +402,36 @@ public static function fromYamlFile($cluster, string $path)
395402
{
396403
return static::fromYaml($cluster, file_get_contents($path));
397404
}
405+
406+
/**
407+
* Proxy the K8s call to cluster object.
408+
*
409+
* @param string $method
410+
* @param array $parameters
411+
* @return mixed
412+
*/
413+
public function __call($method, $parameters)
414+
{
415+
if (static::hasMacro($method)) {
416+
return $this->macroCall($method, $parameters);
417+
}
418+
419+
return $this->cluster->{$method}(...$parameters);
420+
}
421+
422+
/**
423+
* Proxy the K8s static call to cluster object.
424+
*
425+
* @param string $method
426+
* @param array $parameters
427+
* @return mixed
428+
*/
429+
public static function __callStatic($method, $parameters)
430+
{
431+
if (static::hasMacro($method)) {
432+
return static::macroCallStatic($method, $parameters);
433+
}
434+
435+
return new static;
436+
}
398437
}

src/Traits/HasAttributes.php

+1-1
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/Kinds/NewResource.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace RenokiCo\PhpK8s\Test\Kinds;
4+
5+
use RenokiCo\PhpK8s\Kinds\K8sPod;
6+
7+
class NewResource extends K8sPod
8+
{
9+
//
10+
}

tests/MacroTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,15 @@ 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 Kinds\NewResource($cluster, $attributes);
45+
});
46+
47+
$this->assertInstanceOf(Kinds\NewResource::class, K8s::newResource());
48+
$this->assertInstanceOf(Kinds\NewResource::class, (new K8s)->newResource());
49+
$this->assertInstanceOf(Kinds\NewResource::class, $this->cluster->newResource());
50+
}
4051
}

0 commit comments

Comments
 (0)