dealroadshow/k8s-resources
Laravel package for defining and managing Kubernetes resource manifests (e.g., Deployments, Services, Ingress) in PHP. Helps generate, organize, and deploy K8s YAML from your app with reusable resource classes and configuration.
Installation
composer require dealroadshow/k8s-resources
Add to composer.json if using a monorepo or custom setup.
Basic Usage
use DealRoadshow\K8sResources\Resource\Pod;
$pod = new Pod();
$pod->setMetadata(['name' => 'my-pod']);
$pod->setSpec(['containers' => [['name' => 'nginx', 'image' => 'nginx:latest']]]);
$json = $pod->toArray(); // Convert to Kubernetes API-compatible array
Where to Look First
src/Resource/ for pre-defined Kubernetes resources (e.g., Pod, Deployment, Service).$resource->validate() to ensure compliance with Kubernetes schema before deployment.Dynamic Resource Creation
use DealRoadshow\K8sResources\Factory;
$factory = new Factory();
$deployment = $factory->create('Deployment');
$deployment->setMetadata(['namespace' => 'default']);
$deployment->setSpec([
'replicas' => 3,
'template' => [
'spec' => ['containers' => [['image' => 'nginx']]]
]
]);
Patch/Update Operations
$patch = new Pod();
$patch->setMetadata(['name' => 'existing-pod']);
$patch->setSpec(['containers' => [['env' => [['name' => 'ENV_VAR', 'value' => 'new-value']]]]]);
// Use with a Kubernetes client (e.g., `kubernetes-client-php`) to apply patches.
Integration with Kubernetes Clients
use DealRoadshow\K8sResources\Resource\ConfigMap;
use KubeClient\Client;
$client = new Client();
$configMap = new ConfigMap();
$configMap->setData(['config.ini' => '[settings]']);
$client->resource('ConfigMap')->create($configMap->toArray());
Custom Resource Definitions (CRDs)
Extend the base Resource class to support custom APIs:
use DealRoadshow\K8sResources\Resource;
class MyCustomResource extends Resource {
protected $apiVersion = 'mygroup.example/v1';
protected $kind = 'MyResource';
}
metadata.namespace for cluster-scoped resources (e.g., Deployment, Service).setLabels() and setAnnotations() for resource organization and tooling hooks.validate() before serialization to catch schema errors early.Schema Validation Strictness
metadata.name) will throw exceptions.Nested Resource Complexity
Deployment.spec.template.spec.containers) can be error-prone to construct manually.$podSpec = (new PodSpec())
->addContainer('nginx', 'nginx:latest')
->setRestartPolicy('Always');
API Version Mismatches
apps/v1 vs. apps/v1beta1).$apiVersion in custom resources to avoid ambiguity.Performance with Large Manifests
StatefulSet with many volumes) can be slow.validate() with true to get detailed error messages:
$errors = $resource->validate(true);
print_r($errors);
toArray() fails, check for circular references or unsupported PHP types (e.g., DateTime objects). Use json_encode($resource->toArray(), JSON_PRETTY_PRINT) to inspect.Custom Validators
Override validate() to add domain-specific rules:
class SecurePod extends Pod {
public function validate(bool $strict = false): array {
$errors = parent::validate($strict);
if (empty($this->getSpec()['securityContext'])) {
$errors[] = 'securityContext is required for secure pods.';
}
return $errors;
}
}
Serialization Hooks
Extend toArray() to transform data before serialization:
class EncryptedSecret extends Secret {
public function toArray(): array {
$data = parent::toArray();
$data['data'] = array_map('base64_encode', $data['data']);
return $data;
}
}
Kubernetes Client Integration
Create a wrapper for your preferred client (e.g., kubernetes-client-php, kubectl):
class K8sClient {
public function apply(Resource $resource): void {
$client = new Client();
$client->resource($resource->getKind())
->inNamespace($resource->getMetadata()['namespace'] ?? 'default')
->createOrUpdate($resource->toArray());
}
}
Testing Use the package to generate test manifests:
$testDeployment = (new Deployment())
->setMetadata(['name' => 'test-deployment'])
->setSpec(['replicas' => 1, 'template' => [...]]);
$this->assertEquals(['apiVersion' => 'apps/v1', ...], $testDeployment->toArray());
How can I help you explore Laravel packages today?