PHP code example of devstub / kubernetes-api-php-client

1. Go to this page and download the library: Download devstub/kubernetes-api-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devstub / kubernetes-api-php-client example snippets



$client = new \DevStub\KubernetesAPIClient\Client();
$client->config()
    ->setAPINodeUrl("https://kubernetesServerAPI.com/api/") //
    ->setAPIVersion('v1beta1')
    ->setAuthType(\DevStub\KubernetesAPIClient\Config::AUTH_TYPE_HTTP_BASIC) // if we are using http authentication
    ->setAuthOptions(['username' => 'username', 'password' => 'password']); // we set the auth credentials

$response = null;

// here we create a new service by passing a json text
// we can also create the request structure with objects and method chaining .. read below for instructions
$client->services()->create(
    '{
  "id": "framework-dev",
  "kind": "Service",
  "apiVersion": "v1beta1",
  "port": 80,
  "containerPort": 80,
  "selector": {
    "name": "app-instance"
  }
}
',$response
);

$statusObj = $response->getContentObject();
// ... process the response here... 

	$pod  = new \DevStub\KubernetesAPIClient\Entity\v1beta1\Pod();
	$pod->setId("app-instance-dev-test".time());
	$pod->setKind("Pod");
	$pod->setApiVersion("v1beta1");

	$desiredState = new \DevStub\KubernetesAPIClient\Entity\v1beta1\PodState();
	// we prepare the desired state object
	// ...
	// then we pass it to the pod 
	$pod->setDesiredState($desiredState);
	// ...
	// ... we continue the same process for all other options
	// ...

	

	$client->pods()->create(null,$response)
		->setId("app-instance-dev-test".time())
		->setKind("Pod")
		->setApiVersion("v1beta1")
		->setDesiredState() // new object is automatically instantiated here
			->setManifest() //new object is automatically instantiated here
				->setVolumes() new object is automatically instantiated here
					->append() // add array item
						->setName("data") // set property name
						->setSource() new object is automatically instantiated here
							->setEmptyDir()->end() new object is automatically instantiated here and closed
							->end() // we close the setSource
						->end() //we close the append
					->end() // we close the setVolumes
				->setVersion("v1beta1")
				->setId("app-instance-dev-test".time())
				->setContainers()
					->append()
						->setName("framework")
						->setImage("registry.domain/user/dev-framework:v0.1.304")
						->setImagePullPolicy("PullIfNotPresent")
						->setLifecycle()
							->setPostStart()
								->setExec()
									->setCommand()
										->append("/opt/conf/poststart.sh")
										->end()
									->end()
								->end()
							->end()
						->setVolumeMounts()
							->append()
								->setName("data")
								->setMountPath("/data")
								->end()
							->end()
						->setPorts()
							->append()
								->setContainerPort(80)
								->end()
							->end()
						->end()
					->end()
				->end()
			->end()
		->end();
	

	$response = '';

	$client->services()->create(
		'{
	  "id": "framework-dev",
	  "kind": "Service",
	  "apiVersion": "v1beta1",
	  "port": 80,
	  "containerPort": 80,
	  "selector": {
		"name": "app-instance"
	  }
	}
	',$response
	);
	var_dump($response);
	

->setAPINodeUrl("https://kubernetesServerAPI.com/api/") //

->setApiVersion("v1beta1");

->setAuthType(\DevStub\KubernetesAPIClient\Config::AUTH_TYPE_HTTP_BASIC)

->setAuthOptions(['username' => 'username', 'password' => 'password']); // we set the auth credentials

->setConnectionAdapter(\DevStub\KubernetesAPIClient\Config::CONNECTION_ADAPTER_GUZZLE)

$clientConfig = new \DevStub\KubernetesAPIClient\Config();

$clientConfig->setAPINodeUrl("https://162.242.254.164/api/");
$clientConfig->setAPIVersion('v1beta1');
$client->config($clientConfig)

// or you can use method chaining on config

$clientConfig->setAPINodeUrl("https://162.242.254.164/api/")
			 ->setAPIVersion('v1beta1');
$client->config($clientConfig)


$client->services()->create($serviceObject);

$response = $client->pods()->get();
if ($response->getStatusCode() == 500) { // we check against http status code 500
    throw \Exception("error retrieving the response");
}

$response = $client->pods()->get();
$responseContent = $response->getContentRaw(); // this returns a json string
var_dump($responseContent);

$response = $client->pods()->get();
/* @var \DevStub\KubernetesAPIClient\Entity\v1beta1\PodList $podList */
$podList = $response->getContentObject();
/* @var \DevStub\KubernetesAPIClient\Entity\v1beta1\Pod $pod */
foreach ($podList->getItems() as $pod) {
	print "\n".$pod->getId();
}


bash
# Install Composer
curl -sS https://getcomposer.org/installer | php
json
 {
   "stub/kubernetes-api-php-client": "*"
   }
}