keboola / k8s-client
Keboola K8S client library
Installs: 10 027
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 7
Forks: 0
pkg:composer/keboola/k8s-client
Requires
- php: ^8.2
- keboola/retry: ^0.5.1
- kubernetes/php-client: 1.26.10-patch.1
- kubernetes/php-runtime: ^1.0.13
Requires (Dev)
- keboola/coding-standard: ^15.0
- monolog/monolog: ^3.9
- phpstan/phpstan: ^1.9
- phpstan/phpstan-phpunit: ^1.1
- phpunit/phpunit: ^9.5
- sempro/phpunit-pretty-print: ^1.4
- symfony/dotenv: ^6.2
- symfony/filesystem: ^6.1
- dev-main
- 2.5.0
- 2.4.1
- 2.4.0
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-roman-pat-1123-crd-conventions
- dev-roman-pat-1030-add-app-apprun-support
- dev-AJDA-1928-ondra
- dev-devin/1760948558-query-api-client-pr-comments
- dev-devin/AJDA-1993-1764056265-remove-duplicate-toxiproxy-methods
- dev-erik-AJDA-1087-single-job-v3
- dev-erik-AJDA-594-part2
- dev-miro-AJDA-1167
- dev-zajca-event-grid
- dev-pepa_PAT-64_podWatch
- dev-ujovlado-snowflake-size
- dev-PST-2442-ondra
- dev-pepa_k8s_deployment
- dev-roman-pst-1710
- dev-pepa_azClientNamedArgs
This package is auto-updated.
Last update: 2025-11-26 13:05:25 UTC
README
High-level K8S client library. It is based on kubernetes/php-client library, but enhances
it in many ways:
- support connection to multiple clusters
- automatic handling of result type (you don't need to check if the result is what you expect,
Statusor something else) - integrated retries in case of networking problems
- high-level operations like
createmultiple resources at once,waitWhileExiststo wait while given resource exists etc.
Usage
To create a client, use one of provided client factories:
GenericClientFacadeFactoryif you have cluster credentialsInClusterClientFacadeFactoryif you run inside a Pod which has access to K8S API
<?php use Keboola\K8sClient\ClientFacadeFactory\GenericClientFacadeFactory; use Kubernetes\Model\Io\K8s\Api\Core\V1\Container; use Kubernetes\Model\Io\K8s\Api\Core\V1\Pod; $clientFactory = new GenericClientFacadeFactory($retryProxy, $logger); $client = $clientFactory->createClusterClient( 'https://api.k8s-cluster.example.com', 'secret-token', 'var/k8s/caCert.pem', 'default' ); $pod = new Pod([ 'metadata' => [ 'name' => 'my-pod', ], 'spec' => [ 'restartPolicy' => 'Never', 'containers' => [ new Container([ 'name' => 'app', 'image' => 'alpine', 'command' => ['sh', '-c', 'echo hello; sleep 3; echo bye'], ]), ], ], ]); // create the pod $client->createModels([ $pod, ]); // wait for pod to finish do { $pod = $client->pods()->getStatus($pod->metadata->name); if (in_array($pod->status->phase, ['Succeeded', 'Failed'], true)) { break; } sleep(1); } while (true); // check pod logs $client->pods()->readLog($pod->metadata->name); // delete the pod $client->deleteModels([ $pod, ]);
Development
Prerequisites:
- configured
azandawsCLI tools (runaz loginandaws configure --profile keboola-dev-platform-services) - installed
terraform(https://www.terraform.io) andjq(https://stedolan.github.io/jq) to setup local env - installed
dockerto run & develop the library
TL;DR:
export NAME_PREFIX= # your name/nickname to make your resource unique & recognizable cat <<EOF > ./provisioning/local/terraform.tfvars name_prefix = "${NAME_PREFIX}" EOF terraform -chdir=./provisioning/local init -backend-config="key=k8s-client/${NAME_PREFIX}.tfstate" terraform -chdir=./provisioning/local apply ./provisioning/local/update-env.sh azure # or aws docker compose run --rm dev composer install docker compose run --rm dev composer ci
Installing AppRun CRD on Dev/CI Clusters
The functional tests require App and AppRun Custom Resource Definitions (CRDs) to be installed on the Kubernetes cluster. For manually maintained CI clusters, install CRDs manually on all three clusters (GCP, AWS, Azure):
# Download App and AppRun CRD definitions from keboola-operator repository: # - https://github.com/keboola/keboola-operator/blob/canary-operator/config/crd/bases/apps.keboola.com_apps.yaml # - https://github.com/keboola/keboola-operator/blob/canary-operator/config/crd/bases/apps.keboola.com_appruns.yaml # Save files locally as apps.keboola.com_appruns.yaml # Install on GCP CI cluster kubectl --context="gke_kbc-ci-platform-services_us-central1_gcp-ci-ps" apply -f apps.keboola.com_apps.yaml kubectl --context="gke_kbc-ci-platform-services_us-central1_gcp-ci-ps" apply -f apps.keboola.com_appruns.yaml # Install on AWS CI cluster kubectl --context="arn:aws:eks:eu-central-1:480319613404:cluster/ci-ps-eu-central-1" apply -f apps.keboola.com_apps.yaml kubectl --context="arn:aws:eks:eu-central-1:480319613404:cluster/ci-ps-eu-central-1" apply -f apps.keboola.com_appruns.yaml # Install on Azure CI cluster kubectl --context="sandboxes-ci-2021-aks" apply -f apps.keboola.com_apps.yaml kubectl --context="sandboxes-ci-2021-aks" apply -f apps.keboola.com_appruns.yaml
This is a one-time setup per cluster. The CRD defines the schema for AppRun resources used for billing and cost tracking.
Implementing new API
Only few K8S APIs we needed are implement so far. To implement new API, do following:
- create API client wrapper in
Keboola\K8sClient\ApiClient- this is a wrapper around
kubernetes/php-clientAPI class, takes care of handling results
- this is a wrapper around
- add the wrapper to
KubernetesApiClientFacade- inject the
kubernetes/php-clientclient through constructor - add support for the new resource to methods signatures
- inject the
- update
GenericClientFacadeFactoryto provide new API class toKubernetesApiClientFacade
License
MIT licensed, see LICENSE file.