Skip to content

Commit

Permalink
Cleanup + fixes + add support for daemon sets
Browse files Browse the repository at this point in the history
  • Loading branch information
maclof committed Sep 5, 2017
1 parent 88214a0 commit 6d2474e
Show file tree
Hide file tree
Showing 15 changed files with 139 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $ composer require maclof/kubernetes-client
* Cron Jobs

### extensions/v1beta1
* Daemon Sets
* Deployments
* Ingresses

Expand Down
9 changes: 5 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Client
*
* @var string
*/
protected $groupVersion = 'v1';
protected $apiVersion = 'v1';

/**
* The address of the master server.
Expand Down Expand Up @@ -118,6 +118,7 @@ class Client
'cronJobs' => 'Repositories\CronJobRepository',

// extensions/v1beta1
'daemonSets' => 'Repositories\DaemonSetRepository',
'deployments' => 'Repositories\DeploymentRepository',
'ingresses' => 'Repositories\IngressRepository',
];
Expand Down Expand Up @@ -253,12 +254,12 @@ public function getGuzzleClient()
* @param array $query
* @param mixed $body
* @param boolean $namespace
* @param string $groupVersion
* @param string $apiVersion
* @return array
*/
public function sendRequest($method, $uri, $query = [], $body = [], $namespace = true, $groupVersion = null)
public function sendRequest($method, $uri, $query = [], $body = [], $namespace = true, $apiVersion = null)
{
$baseUri = $groupVersion ? '/apis/' . $groupVersion : '/api/' . $this->groupVersion;
$baseUri = $apiVersion ? '/apis/' . $apiVersion : '/api/' . $this->apiVersion;
if ($namespace) {
$baseUri .= '/namespaces/' . $this->namespace;
}
Expand Down
31 changes: 31 additions & 0 deletions src/Collections/DaemonSetCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace Maclof\Kubernetes\Collections;

use Maclof\Kubernetes\Models\DaemonSet;

class DaemonSetCollection extends Collection
{
/**
* The constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
parent::__construct($this->getDaemonSets(isset($data['items']) ? $data['items'] : []));
}

/**
* Get an array of daemon sets.
*
* @param array $items
* @return array
*/
protected function getDaemonSets(array $items)
{
foreach ($items as &$item) {
$item = new DaemonSet($item);
}

return $items;
}
}
11 changes: 11 additions & 0 deletions src/Models/DaemonSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace Maclof\Kubernetes\Models;

class DaemonSet extends Model
{
/**
* The api version.
*
* @var string
*/
protected $apiVersion = 'extensions/v1beta1';
}
12 changes: 11 additions & 1 deletion src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Model

/**
* Whether or not the kind is plural.
*
*
* @var boolean
*/
protected $pluralKind = false;
Expand Down Expand Up @@ -80,6 +80,16 @@ public function getSchema()
return json_encode($schema, JSON_PRETTY_PRINT);
}

/**
* Get the api version.
*
* @return string
*/
public function getApiVersion()
{
return $this->apiVersion;
}

/**
* Get the model as a string.
*
Expand Down
2 changes: 0 additions & 2 deletions src/Repositories/CronJobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class CronJobRepository extends Repository
{
protected $uri = 'cronjobs';

protected $groupVersion = 'batch/v2alpha1';

protected function createCollection($response)
{
return new CronJobCollection($response);
Expand Down
13 changes: 13 additions & 0 deletions src/Repositories/DaemonSetRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Collections\DaemonSetCollection;

class DaemonSetRepository extends Repository
{
protected $uri = 'daemonsets';

protected function createCollection($response)
{
return new DaemonSetCollection($response);
}
}
2 changes: 0 additions & 2 deletions src/Repositories/DeploymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
class DeploymentRepository extends Repository
{
protected $uri = 'deployments';

protected $groupVersion = 'extensions/v1beta1';

protected function createCollection($response)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Repositories/IngressRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
class IngressRepository extends Repository
{
protected $uri = 'ingresses';

protected $groupVersion = 'extensions/v1beta1';

protected function createCollection($response)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Repositories/JobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
class JobRepository extends Repository
{
protected $uri = 'jobs';

protected $groupVersion = 'batch/v1';

protected function createCollection($response)
{
Expand Down
2 changes: 0 additions & 2 deletions src/Repositories/ReplicaSetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class ReplicaSetRepository extends Repository
{
protected $uri = 'replicasets';

protected $groupVersion = 'extensions/v1beta1';

protected function createCollection($response)
{
return new ReplicaSetCollection($response);
Expand Down
38 changes: 33 additions & 5 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Models\DeleteOptions;
use Maclof\Kubernetes\Models\Model;
use Maclof\Kubernetes\Models\DeleteOptions;

abstract class Repository
{
Expand All @@ -20,11 +20,11 @@ abstract class Repository
protected $namespace = true;

/**
* The group version to use for requests.
* The api version to use for requests.
*
* @var null
*/
protected $groupVersion = false;
protected $apiVersion;

/**
* The label selector.
Expand Down Expand Up @@ -60,9 +60,37 @@ public function __construct($client)
* @param boolean $namespace
* @return array
*/
public function sendRequest($method, $uri, $query = [], $body = [], $namespace = true)
protected function sendRequest($method, $uri, $query = [], $body = [], $namespace = true)
{
return $this->client->sendRequest($method, $uri, $query, $body, $namespace, $this->groupVersion);
$apiVersion = $this->getApiVersion();
if ($apiVersion == 'v1') {
$apiVersion = null;
}

return $this->client->sendRequest($method, $uri, $query, $body, $namespace, $apiVersion);
}

/**
* Get the api version from the model.
*
* @return string
*/
protected function getApiVersion()
{
if ($this->apiVersion) {
return $this->apiVersion;
}

$className = str_replace('Repository', '', class_basename($this));
$classPath = 'Maclof\Kubernetes\Models\\' . $className;

if (!class_exists($classPath)) {
return;
}

$this->apiVersion = (new $classPath)->getApiVersion();

return $this->apiVersion;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/cron-jobs/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kind": "CronJob",
"apiVersion": "batch\/v2alpha1"
}
2 changes: 1 addition & 1 deletion tests/fixtures/jobs/empty.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"kind": "Job",
"apiVersion": "extensions\/v1beta1"
"apiVersion": "batch\/v1"
}
29 changes: 29 additions & 0 deletions tests/models/CronJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Maclof\Kubernetes\Models\CronJob;

class CronJobTest extends TestCase
{
public function test_get_schema()
{
$job = new CronJob;

$schema = $job->getSchema();
$fixture = $this->getFixture('cron-jobs/empty.json');

$this->assertEquals($schema, $fixture);
}

public function test_get_metadata()
{
$job = new CronJob([
'metadata' => [
'name' => 'test',
],
]);

$metadata = $job->getMetadata('name');

$this->assertEquals($metadata, 'test');
}
}

0 comments on commit 6d2474e

Please sign in to comment.