-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros are not working using fromYaml() with custom CRDs #12
Comments
Please check the documentation. It is a small confusion. Please let me know if this way works. I have a small doubt that it might not work with macros since the initialization from YAML to resource might not be seen properly. |
Thanks for your answer. I call I tried to use the original KubernetesCluster class (
And this is still generating a If you prefer, I can create an empty laravel project and show you the completly code I use in order to reproduce the error. |
For some reason, I saw Before calling it a bug, make sure to call the macro on use RenokiCo\PhpK8s\K8s as PhpK8s;
PhpK8s::macro('istioGateway', function ($cluster = null, array $attributes = []) {
return new IstioGateway($cluster, $attributes);
}); |
This can be some good feature to better help to define CRDs 🤔 Just by passing the class name and having a macro set up instead of having to do it your own. 🤩 use RenokiCo\PhpK8s\K8s as PhpK8s;
PhpK8s::crd(IstioGateway::class); |
I still have the same issue. I hace created an empty lumen installation and I will show you all the relevant files that I have write to reproduce this issue. bootstrap/app.php $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(\RenokiCo\LaravelK8s\LaravelK8sServiceProvider::class); App\Providers\AppServiceProvider <?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use RenokiCo\PhpK8s\K8s;
use App\Kubernetes\Kinds\IstioGateway;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
K8s::macro('istioGateway', function ($cluster = null, array $attributes = []){
return new IstioGateway($cluster, $attributes);
});
}
} IstioGateway <?php
namespace App\Kubernetes\Kinds;
use RenokiCo\PhpK8s\Contracts\InteractsWithK8sCluster;
use RenokiCo\PhpK8s\Kinds\K8sResource;
class IstioGateway extends K8sResource implements InteractsWithK8sCluster
{
/**
* The resource Kind parameter.
*
* @var null|string
*/
protected static $kind = 'Gateway';
/**
* The default version for the resource.
*
* @var string
*/
protected static $defaultVersion = 'networking.istio.io/v1beta1';
/**
* Wether the resource has a namespace.
*
* @var bool
*/
protected static $namespaceable = true;
} ExampleController <?php
namespace App\Http\Controllers;
use RenokiCo\LaravelK8s\LaravelK8sFacade as K8sFacade;
class ExampleController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
//
}
public function setNamespace() {
return K8sFacade::namespace()
->setName('renoki-test')
->setLabels([
'istio-injection' => 'enabled'
])->create();
}
public function getNamespace() {
$yaml = $this->array2yaml(K8sFacade::namespace()
->setName('renoki-test2')
->setLabels([
'istio-injection' => 'enabled'
])->toArray());
dd(K8sFacade::fromYaml($yaml));
}
public function setGateway() {
return K8sFacade::istioGateway()
->setName('test-gateway')
->setNamespace('renoki-test')
->setSpec([
'selector' => [
'istio' => 'ingressgateway',
],
'servers' => [
[
'hosts' => 'test.gateway.io',
'port' => [
'name' => 'https',
'number' => 443,
'protocol' => 'HTTPS'
],
'tls' => [
'credentialName' => 'kcertificate',
'mode' => 'SIMPLE'
]
]
]
])->create();
}
public function getGateway() {
$yaml = $this->array2yaml(K8sFacade::istioGateway()
->setName('test-gateway2')
->setNamespace('renoki-test2')
->setSpec([
'selector' => [
'istio' => 'ingressgateway',
],
'servers' => [
[
'hosts' => 'test.gateway.io',
'port' => [
'name' => 'https',
'number' => 443,
'protocol' => 'HTTPS'
],
'tls' => [
'credentialName' => 'kdashboard-certificate',
'mode' => 'SIMPLE'
]
]
]
])->toArray());
dd(K8sFacade::fromYaml($yaml));
}
private function array2yaml(array $arr)
{
$yaml = yaml_emit($arr);
return str_replace("---\n", "", $yaml);
}
} Now I will explain the behaviour:
^ RenokiCo\PhpK8s\Kinds\K8sNamespace {#46 ▼
#attributes: array:1 [▼
"metadata" => array:2 [▼
"name" => "renoki-test"
"labels" => array:1 [▶]
]
]
#original: array:1 [▼
"metadata" => array:2 [▼
"name" => "renoki-test"
"labels" => array:1 [▶]
]
]
#synced: false
#cluster: RenokiCo\PhpK8s\KubernetesCluster {#18 ▶}
}
^ RenokiCo\PhpK8s\K8s {#46} And when I add public function getGateway() {
$yaml = $this->array2yaml(K8sFacade::istioGateway()
->setName('test-gateway2')
->setNamespace('renoki-test2')
->setSpec([
'selector' => [
'istio' => 'ingressgateway',
],
'servers' => [
[
'hosts' => 'test.gateway.io',
'port' => [
'name' => 'https',
'number' => 443,
'protocol' => 'HTTPS'
],
'tls' => [
'credentialName' => 'kdashboard-certificate',
'mode' => 'SIMPLE'
]
]
]
])->toArray());
// dd(K8sFacade::fromYaml($yaml));
return K8sFacade::fromYaml($yaml)->create();
Of course I have tried all the combinations that you told me before, but it still have this weird behaviour, and even checking the original code, I am not able to explain what is happening 😓. I expect this explanation become more helpfull. Thanks you |
It seems like the YAML gets declared with - PhpK8s::macro('istioGateway', function ($cluster = null, array $attributes = []) {
+ PhpK8s::macro('gateway', function ($cluster = null, array $attributes = []) {
return new IstioGateway($cluster, $attributes);
}); I think this behavior needs to be changed accordingly so that you can declare the naming as a convention for both YAML-imported and creating instances also based on namespaces. In this case, kind: Gateway
apiVersion: networking.istio.io/v1beta1
phpK8s:
macro: istioGateway PhpK8s::macro('istioGateway', function ($cluster = null, array $attributes = []) {
return new IstioGateway($cluster, $attributes);
}); |
Maybe having annotations would look better (and would fix the issues with the YAML validation) ? kind: Gateway
apiVersion: networking.istio.io/v1beta1
metadata:
name: my-gateway
annotations:
php-k8s.renoki.org/macro: istioGateway |
If you remember, I normally use this PhpK8s::macro('gateway', function ($cluster = null, array $attributes = []) {
return new IstioGateway($cluster, $attributes);
}); But with the same result. And yes, the way with annotations looks great for me 😁 |
I might pluck the namespace for the It's weird that I have just tested locally in this configuration and it worked. Let me put up a test for it and open a PR. |
Okay, thanks you |
Okey, I just realized what is the problem 😅 As PhpK8s::macro('Gateway', function ($cluster = null, array $attributes = []) {
return new IstioGateway($cluster, $attributes);
}); |
Yes, that's an issue actually 🤔 I think we can make the kind to be called as |
It should be fixed as of The class name or namespace doesn't matter anymore. You can call The test is here. When calling Delete the macro declaration and replace it with the register method. You can see in the test the pretty simple approach. |
I fixed it in PHP K8s 3.1.1: https://github.com/renoki-co/php-k8s/releases/tag/3.1.1 I have also updated the docs: https://php-k8s.renoki.org/advanced/create-classes-for-crds/macros |
Okay I am updating, thanks you! Another little thing. I use the ---
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: test-gateway2
namespace: renoki-test2
spec:
selector:
istio: ingressgateway
servers:
- hosts: test.gateway.io
port:
name: https
number: 443
protocol: HTTPS
tls:
credentialName: kcertificate
mode: SIMPLE
... And when I execute Thanks you again |
This is the behavior: https://www.php.net/manual/en/function.yaml-emit.php |
Just replaced the function with |
I have defined the Macros inside KubernetesProvider.
K8s::macro('gateway', function ($cluster = null, array $attributes = []){ return new IstioGateway($cluster, $attributes); });
And when I call K8s::connection(1)->gateway->whatever->create(); it works properly.
But if I try to recover a [kind: gateway] from yaml (using it like this:
It throws the following error:
Looking inside K8s file, I realised thath the problem it with macros.
It is calling $this->cluster, instead of $this->macroCall, because it does not recognize de macros calling in this way.
It is pretty strange and I dont know how to resolve this issue.
The text was updated successfully, but these errors were encountered: