Skip to content

Commit 7fa9598

Browse files
authored
Merge pull request #131 from renoki-co/feature/crd-definition
[feature] CRD register
2 parents 96b60f1 + e130a29 commit 7fa9598

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818

1919
strategy:
20+
fail-fast: false
2021
matrix:
2122
php:
2223
- '7.4'

src/K8s.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace RenokiCo\PhpK8s;
44

55
use Closure;
6+
use Illuminate\Support\Str;
67
use Illuminate\Support\Traits\Macroable;
78
use RenokiCo\PhpK8s\Traits\InitializesInstances;
89
use RenokiCo\PhpK8s\Traits\InitializesResources;
@@ -81,6 +82,23 @@ public static function fromTemplatedYamlFile($cluster, string $path, array $repl
8182
});
8283
}
8384

85+
/**
86+
* Register a CRD inside the package.
87+
*
88+
* @param string $class
89+
* @param string|null $name
90+
* @return void
91+
*/
92+
public static function registerCrd(string $class, string $name = null): void
93+
{
94+
static::macro(
95+
Str::camel($name ?: substr($class, strrpos($class, '\\') + 1)),
96+
function ($cluster = null, array $attributes = []) use ($class) {
97+
return new $class($cluster, $attributes);
98+
}
99+
);
100+
}
101+
84102
/**
85103
* Proxy the K8s call to cluster object.
86104
*

tests/MacroTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ public function test_resource_macro()
4040

4141
public function test_k8s_macro()
4242
{
43-
K8s::macro('newResource', function ($cluster = null, $attributes = []) {
44-
return new Kinds\NewResource($cluster, $attributes);
45-
});
43+
K8s::registerCrd(Kinds\NewResource::class);
44+
K8s::registerCrd(Kinds\NewResource::class, 'nr');
4645

4746
$this->assertInstanceOf(Kinds\NewResource::class, K8s::newResource());
4847
$this->assertInstanceOf(Kinds\NewResource::class, (new K8s)->newResource());
4948
$this->assertInstanceOf(Kinds\NewResource::class, $this->cluster->newResource());
49+
50+
$this->assertInstanceOf(Kinds\NewResource::class, K8s::nr());
51+
$this->assertInstanceOf(Kinds\NewResource::class, (new K8s)->nr());
52+
$this->assertInstanceOf(Kinds\NewResource::class, $this->cluster->nr());
5053
}
5154
}

0 commit comments

Comments
 (0)