Skip to content

[2.x] In-Cluster Configuration #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}