PHP K8s
Github Repo
3.x
3.x
  • ๐ŸšขIntroduction
  • ๐ŸŽ‰Support
  • โซUpgrading to 3.x
  • Getting Started
    • ๐Ÿš€Installation
    • ๐Ÿ™ŒShowcase
    • ๐Ÿ”’Authentication
    • โ™ป๏ธActive Development
      • ๐Ÿ“—Default Versions
      • ๐Ÿง™Supported Kubernetes versions
      • ๐Ÿ—ณ๏ธPackage versioning
  • Cluster Interaction
    • ๐ŸงญGetting Started
    • ๐ŸŽญCRUD Operations
    • ๐Ÿ“ฆImport from YAML
    • ๐Ÿ‘€Watching Resources
  • Resources
    • ๐Ÿ‘“Base Resource
      • Attributes Methods
      • Metadata Methods
      • Custom Callers
    • ๐Ÿง‘Namespace
    • ๐Ÿ–ฅ๏ธNode
    • ๐Ÿ“กEvent
    • ๐Ÿ“ฆWorkloads
      • Pod
      • Deployment
      • StatefulSet
      • DaemonSet
      • Job
      • CronJob
    • ๐ŸงตConfigurations
      • ConfigMap
      • Secret
    • ๐Ÿ“€Storage
      • StorageClass
      • PersistentVolume
      • PersistentVolumeClaim
    • ๐Ÿ“ถNetworking
      • Service
      • Ingress
    • โ†”๏ธScaling & Availability
      • HorizontalPodAutoscaler
      • PodDisruptionBudget
    • ๐Ÿ”‘RBAC
      • ClusterRole
      • ClusterRoleBinding
      • Role
      • RoleBinding
      • ServiceAccount
  • Instances
    • Affinity
    • Container
    • Container Probes
    • Expressions
    • Resource Metrics
    • RBAC Rules
    • Volumes
  • Advanced
    • ๐ŸฐMacros
    • โœจCreate classes for CRDs
      • ๐ŸŽ‡Getting started
      • ๐ŸฐMacros
      • ๐Ÿ‘€Watchable Resources
      • โ†”๏ธScalable Resources
      • ๐Ÿ’ŠPodable Resources
      • ๐Ÿ“„Loggable Resources
      • ๐Ÿš’Helper Traits
  • Frameworks
    • Laravel
    • PHP Helm
Powered by GitBook
On this page
  • Retrieving all resources
  • Retrieving all resources from all namespaces
  • Retrieving a specific resource
  • Creating resources
  • Updating resources
  • Deleting resources
  • Creating or updating resources

Was this helpful?

  1. Cluster Interaction

CRUD Operations

PreviousGetting StartedNextImport from YAML

Last updated 3 years ago

Was this helpful?

CRUD operations are available only if you have set up , as the methods are available only from a KubernetesCluster instance.

Retrieving all resources

Getting all resources can be done by calling ->all():

$namespaces = $cluster->namespace()->all();

// Or you can use a specific method to call it at once
$namespaces = $cluster->getAllNamespaces();

// For namespaced resources, you may pass the namespace
$stagingServices = $cluster->getAllServices('staging');

The result is an RenokiCo\PhpK8s\ResourcesList instance. The class is extending \Illuminate\Support\Collection, on which you can chain various methods as described here:

Retrieving all resources from all namespaces

Retrieving all resources from all namespaces can also be done using the allNamespaces() method:

$allPods = $cluster->pod()->allNamespaces();

// Or you can use the specific method:
$allPods = $cluster->getAllPodsFromAllNamespaces();

For the specific method, the format should be getAll[Resource]FromAllNamespaces()

Retrieving a specific resource

Getting only one resource is done by calling ->get():

$service = $cluster->service()->whereNamespace('staging')->whereName('nginx')->get();

// You can also shorten it like
$service = $cluster->service()->whereNamespace('staging')->getByName('nginx');

// Or you can use a specific method to call it in at once
$service = $cluster->getServiceByName('nginx', 'staging');

Filters can vary, depending if the resources are namespaceable or not. By default, the namespace is default and can be missed from the function call.

Creating resources

Calling the ->create() method after building your Kind will create the resource in your cluster. In fact, it will actually sync it to the Cluster as Kubernetes uses the resource names as identifiers.

$ns = $cluster->namespace()->setName('staging')->create();

$ns->isSynced(); // true

Updating resources

While Kubernetes has the ability to PATCH a resource or REPLACE it entirely, PHP K8s relies on REPLACE to update your resource since you have to retrieve it first (thus getting a synced class), edit it, then triggering the update.

$cm = $cluster->getConfigmapByName('env');

$cm->addData('API_KEY', '123')->update();

Deleting resources

You will have to simply call ->delete() on the resource, after you retrieve it.

$cm = $cluster->getConfigmapByName('settings');

if ($cm->delete()) {
    echo 'Configmap deleted! ๐ŸŽ‰';
}

Additionally, you can pass query parameters, grace period, and the propagation policy if needed:

public function delete(
    array $query = ['pretty' => 1],
    $gracePeriod = null,
    string $propagationPolicy = 'Foreground'
) {
    //
}

Creating or updating resources

Sometimes, you want to create a resource if it's not existent or update it with the current resource class info. You can do this in one chain call:

$cluster->configmap()
    ->addData('RAND', mt_rand(0, 999))
    ->createOrUpdate();

Each time the above code is running, it will create the ConfigMap if it's not existent, or it will update the existent one with a random number between 0 and 999.

๐ŸŽญ
authentication
https://laravel.com/docs/master/collections