NovinkaZverejnili sme 50+ Laravel balíkov ako open source
Custom AI apps, agents and automation — Roundly ConsultingRoundly
Všetky balíky
Kubernetes API for Laravel

Vlastné zdroje a makrá

Pre akýkoľvek kind — vrátane vlastných CRD — definujte triedu zdroja a zaregistrujte ju v config/kubernetes.php alebo za behu:

use RoundlyConsulting\KubernetesApi\Facades\Kubernetes;
use RoundlyConsulting\KubernetesApi\Resources\Resource;

class Application extends Resource
{
    protected string $version = 'example.com/v2';

    protected string $kind = 'Application';

    protected bool $usesNamespaces = true;
}

Kubernetes::registerResource('apps', Application::class);

Kubernetes::cluster('production')->apps()->get(); // all Application resources
Kubernetes::apps()->get();                        // on the default cluster
  • $version — apiVersion. v1 (predvolená) smeruje na /api/v1, všetko ostatné na /apis/<verzia>.
  • $kind — kind zapisovaný do každého payloadu.
  • $usesNamespaces — true pre zdroje v namespace; predvolené false robí zdroj klastrovým.
  • $plural — REST plurál. Ak ostane null, použije sa kind malými písmenami v množnom čísle; nastavte ho, keď sa spec.names.plural vo vašom CRD líši.
  • Pridajte traity, ktoré potrebujete — HasSpec, HasStatus, HasStatusPhase, HasReplicas… z RoundlyConsulting\KubernetesApi\Traits\Resource.
namespace App\Kubernetes;

use RoundlyConsulting\KubernetesApi\Facades\Kubernetes;
use RoundlyConsulting\KubernetesApi\Resources\Resource;
use RoundlyConsulting\KubernetesApi\Traits\Resource\HasSpec;
use RoundlyConsulting\KubernetesApi\Traits\Resource\HasStatus;

class DatabaseMigration extends Resource
{
    use HasSpec;
    use HasStatus;

    protected string $version = 'ops.example.com/v1';

    protected string $kind = 'DatabaseMigration';

    protected ?string $plural = 'dbmigrations';   // the CRD's spec.names.plural

    protected bool $usesNamespaces = true;
}

// Registered or not, any class resolves through resource():
Kubernetes::cluster('production')->resource(DatabaseMigration::class)
    ->setNamespace('shop')
    ->setName('add-orders-index')
    ->setSpec('database', 'orders')
    ->create();

Registrácia a výmena v konfigurácii

Mapa resources sa číta pri každom volaní a Kubernetes::registerResource() zapisuje do tej istej mapy. Ak vstavané meno nasmerujete na vlastnú podtriedu, vymení sa na každom klastri vrátane fasády — triedy zdrojov zámerne nie sú final. Meno, na ktoré už odpovedá metóda triedy Cluster (url, namespace, …), sa odmietne s InvalidResourceException:

// config/kubernetes.php
'resources' => [
    // …keep the built-in entries from the published file…
    'dbMigrations' => App\Kubernetes\DatabaseMigration::class,

    // Swap a built-in: every $cluster->pods() now returns your subclass
    'pods' => App\Kubernetes\Pod::class,
],
namespace App\Kubernetes;

use RoundlyConsulting\KubernetesApi\Resources\Pod as BasePod;

class Pod extends BasePod
{
    public function isServing(): bool
    {
        return $this->isRunning() && $this->containersReady();
    }
}

Makrá a podmienené volania

Cluster aj každý zdroj používajú traity Macroable a Conditionable z Laravelu, takže ich môžete fluentne rozširovať aj vetviť. Makro triedy Cluster zavoláte aj na fasáde, pre predvolený klaster:

use RoundlyConsulting\KubernetesApi\Cluster;
use RoundlyConsulting\KubernetesApi\Facades\Kubernetes;
use RoundlyConsulting\KubernetesApi\Resources\Deployment;

// A resource macro — $this is the resource
Deployment::macro('isFullyAvailable', function (): bool {
    /** @var Deployment $this */
    return $this->getAvailableReplicasCount() >= $this->getReplicas();
});

// A client macro — $this is the cluster client
Cluster::macro('tenantNamespace', function (string $tenant) {
    /** @var Cluster $this */
    return $this->namespaces()->setName("tenant-{$tenant}");
});

$cluster = Kubernetes::cluster('production');

$cluster->deployments()->setNamespace('shop')->withName('checkout')->find()->isFullyAvailable();
$cluster->tenantNamespace('acme')->create();
Kubernetes::tenantNamespace('acme');   // a Cluster macro also works on the facade (default cluster)

// Conditionable — branch without breaking the chain
$cluster->deployments()->setNamespace('shop')->withName('checkout')->find()
    ->when($peakTraffic, fn (Deployment $deployment) => $deployment->setReplicas(12))
    ->unless($peakTraffic, fn (Deployment $deployment) => $deployment->setReplicas(3))
    ->update();

Makrá zdrojov, ktorých názov začína na set, get, with, addTo alebo remove, zachytia magické metódy atribútov — zvoľte inú predponu, napríklad is…, has… či find….

Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

Ďalšie spôsoby podpory vrátane kryptomien

Odoslaním daru súhlasíte s našimi podmienkami prijímania darov.

Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.