Skip to content

Commit

Permalink
Merge pull request #131 from renoki-co/feature/crd-definition
Browse files Browse the repository at this point in the history
[feature] CRD register
  • Loading branch information
rennokki authored Sep 15, 2021
2 parents 96b60f1 + e130a29 commit 7fa9598
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php:
- '7.4'
Expand Down
18 changes: 18 additions & 0 deletions src/K8s.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RenokiCo\PhpK8s;

use Closure;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use RenokiCo\PhpK8s\Traits\InitializesInstances;
use RenokiCo\PhpK8s\Traits\InitializesResources;
Expand Down Expand Up @@ -81,6 +82,23 @@ public static function fromTemplatedYamlFile($cluster, string $path, array $repl
});
}

/**
* Register a CRD inside the package.
*
* @param string $class
* @param string|null $name
* @return void
*/
public static function registerCrd(string $class, string $name = null): void
{
static::macro(
Str::camel($name ?: substr($class, strrpos($class, '\\') + 1)),
function ($cluster = null, array $attributes = []) use ($class) {
return new $class($cluster, $attributes);
}
);
}

/**
* Proxy the K8s call to cluster object.
*
Expand Down
9 changes: 6 additions & 3 deletions tests/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ public function test_resource_macro()

public function test_k8s_macro()
{
K8s::macro('newResource', function ($cluster = null, $attributes = []) {
return new Kinds\NewResource($cluster, $attributes);
});
K8s::registerCrd(Kinds\NewResource::class);
K8s::registerCrd(Kinds\NewResource::class, 'nr');

$this->assertInstanceOf(Kinds\NewResource::class, K8s::newResource());
$this->assertInstanceOf(Kinds\NewResource::class, (new K8s)->newResource());
$this->assertInstanceOf(Kinds\NewResource::class, $this->cluster->newResource());

$this->assertInstanceOf(Kinds\NewResource::class, K8s::nr());
$this->assertInstanceOf(Kinds\NewResource::class, (new K8s)->nr());
$this->assertInstanceOf(Kinds\NewResource::class, $this->cluster->nr());
}
}

0 comments on commit 7fa9598

Please sign in to comment.