Skip to content

Macros are not working using fromYaml() with custom CRDs #12

Closed
@palcamora

Description

@palcamora

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:

K8s::connection(1)
                       ->fromYaml($content)
                       ->create();

It throws the following error:

[2021-09-13 10:54:54] local.ERROR: Undefined property: RenokiCo\PhpK8s\K8s::$cluster {"exception":"[object] (ErrorException(code: 0): Undefined property: RenokiCo\PhpK8s\K8s::$cluster at /home/vagrant/backend/vendor/renoki-co/php-k8s/src/K8s.php:427)
[stacktrace]
#0 /home/vagrant/backend/vendor/renoki-co/php-k8s/src/K8s.php(427): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#1 /home/vagrant/backend/app/Observers/ConfigurationObserver.php(24): RenokiCo\PhpK8s\K8s->__call()
#2 /home/vagrant/backend/vendor/illuminate/events/Dispatcher.php(404): App\Models\Configuration::App\Observers\{closure}()
#3 /home/vagrant/backend/vendor/illuminate/events/Dispatcher.php(249): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}()
#4 /home/vagrant/backend/vendor/illuminate/database/Eloquent/Concerns/HasEvents.php(189): Illuminate\Events\Dispatcher->dispatch()
#5 /home/vagrant/backend/vendor/illuminate/database/Eloquent/Model.php(1166): Illuminate\Database\Eloquent\Model->fireModelEvent()
#6 /home/vagrant/backend/vendor/illuminate/database/Eloquent/Model.php(986): Illuminate\Database\Eloquent\Model->performInsert()
#7 /home/vagrant/backend/app/Http/Controllers/Kubernetes/DeploymentController.php(236): Illuminate\Database\Eloquent\Model->save()
#8 /home/vagrant/backend/app/Http/Controllers/Kubernetes/DeploymentController.php(55): App\Http\Controllers\Kubernetes\DeploymentController->createConfigurations()
#9 /home/vagrant/backend/vendor/illuminate/container/BoundMethod.php(36): App\Http\Controllers\Kubernetes\DeploymentController->store()
#10 /home/vagrant/backend/vendor/illuminate/container/Util.php(40): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#11 /home/vagrant/backend/vendor/illuminate/container/BoundMethod.php(93): Illuminate\Container\Util::unwrapIfClosure()
#12 /home/vagrant/backend/vendor/illuminate/container/BoundMethod.php(37): Illuminate\Container\BoundMethod::callBoundMethod()
#13 /home/vagrant/backend/vendor/illuminate/container/Container.php(651): Illuminate\Container\BoundMethod::call()
#14 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(389): Illuminate\Container\Container->call()
#15 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(355): Laravel\Lumen\Application->callControllerCallable()
#16 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(329): Laravel\Lumen\Application->callLumenController()
#17 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(282): Laravel\Lumen\Application->callControllerAction()
#18 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(267): Laravel\Lumen\Application->callActionOnArrayBasedRoute()
#19 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(169): Laravel\Lumen\Application->handleFoundRoute()
#20 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(429): Laravel\Lumen\Application->Laravel\Lumen\Concerns\{closure}()
#21 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(175): Laravel\Lumen\Application->sendThroughPipeline()
#22 /home/vagrant/backend/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(112): Laravel\Lumen\Application->dispatch()
#23 /home/vagrant/backend/public/index.php(28): Laravel\Lumen\Application->run()
#24 {main}
"}

Looking inside K8s file, I realised thath the problem it with macros.

/**
     * Proxy the K8s call to cluster object.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        if (static::hasMacro($method)) {
            return $this->macroCall($method, $parameters);
        }

        return $this->cluster->{$method}(...$parameters);
    }

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.

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions