Skip to content

Commit

Permalink
Merge pull request #2 from renoki-co/update/in-cluster
Browse files Browse the repository at this point in the history
[2.x] In-Cluster Configuration
  • Loading branch information
rennokki committed Mar 22, 2021
2 parents 101db88 + 33fd2d8 commit fa0ed36
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ jobs:

- name: Install dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "orchestra/database:${{ matrix.testbench }}" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --${{ matrix.prefer }} --prefer-dist --no-interaction --no-suggest
- name: Setup in-cluster config
run: |
sudo mkdir -p /var/run/secrets/kubernetes.io/serviceaccount
echo "some-token" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/token
echo "c29tZS1jZXJ0Cg==" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
echo "some-namespace" | sudo tee /var/run/secrets/kubernetes.io/serviceaccount/namespace
sudo chmod -R 777 /var/run/secrets/kubernetes.io/serviceaccount/
- name: Run tests
run: |
vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"renoki-co/php-k8s": "^2.0"
"renoki-co/php-k8s": "^2.2"
},
"autoload": {
"psr-4": {
Expand All @@ -28,10 +28,8 @@
"test": "vendor/bin/phpunit"
},
"require-dev": {
"laravel/legacy-factories": "^1.1",
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.0",
"orchestra/database": "^5.0|^6.0"
"orchestra/testbench": "^5.0|^6.0"
},
"config": {
"sort-packages": true
Expand Down
15 changes: 15 additions & 0 deletions config/k8s.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@
'token' => env('KUBE_BEARER_TOKEN', null),
],

/*
|--------------------------------------------------------------------------
| In-Cluster Driver
|--------------------------------------------------------------------------
|
| In-Cluster Driver works only if the written PHP app runs
| inside a Kubernetes Pod, within a Cluster. The configuration
| is being loaded automatically.
|
*/

'cluster' => [
'driver' => 'cluster',
],

],

];
11 changes: 11 additions & 0 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function loadFromConfig(array $config)
case 'kubeconfig': $this->configureWithKubeConfigFile($config); break;
case 'http': $this->configureWithHttpAuth($config); break;
case 'token': $this->configureWithToken($config); break;
case 'cluster': $this->configureInCluster(); break;
default: break;
}
}
Expand Down Expand Up @@ -131,6 +132,16 @@ protected function configureWithToken(array $config)
$this->cluster->withToken($config['token']);
}

/**
* Load the In-Cluster configuration.
*
* @return void
*/
protected function configureInCluster()
{
$this->cluster->inClusterConfiguration();
}

/**
* Get the initialized cluster.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RenokiCo\LaravelK8s\Test;

use RenokiCo\LaravelK8s\LaravelK8sFacade;
use RenokiCo\PhpK8s\Kinds\K8sResource;

class ConfigurationTest extends TestCase
{
Expand Down Expand Up @@ -108,4 +109,18 @@ public function test_token_authentication()

$this->assertEquals('Bearer some-token', $token);
}

public function test_in_cluster_config()
{
$cluster = LaravelK8sFacade::connection('cluster')->getCluster();

[
'headers' => ['authorization' => $token],
'verify' => $caPath,
] = $cluster->getClient()->getConfig();

$this->assertEquals('Bearer some-token', $token);
$this->assertEquals('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt', $caPath);
$this->assertEquals('some-namespace', K8sResource::$defaultNamespace);
}
}

0 comments on commit fa0ed36

Please sign in to comment.