Skip to content

Commit

Permalink
Added environment variable provider
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Sep 5, 2021
1 parent 06a49b2 commit 2dc919f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
20 changes: 20 additions & 0 deletions config/k8s.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@
'host' => env('KUBE_HOST', 'https://kubernetes.default.svc.cluster.local'),
],

/*
|--------------------------------------------------------------------------
| Environment Variable Driver
|--------------------------------------------------------------------------
|
| The environment variable driver leverages your current (possibly set)
| KUBECONFIG environment variable. The variable contains a list of paths
| towards multiple kubeconfig files that will be read, merged and based
| on the selected context from the configuration, it will connect
| to the cluster, just like the "kubeconfig" driver.
|
| Read more: https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
|
*/

'environment' => [
'driver' => 'environment',
'context' => env('KUBECONFIG_CONTEXT', 'minikube'),
],

],

];
13 changes: 13 additions & 0 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function loadFromConfig(array $config)
case 'http': $this->configureWithHttpAuth($config); break;
case 'token': $this->configureWithToken($config); break;
case 'cluster': $this->configureInCluster($config); break;
case 'variable': $this->configureWithKubeConfigVariable($config); break;
default: break;
}
}
Expand Down Expand Up @@ -144,6 +145,18 @@ protected function configureInCluster(array $config)
$this->cluster = PhpK8sCluster::inClusterConfiguration();
}

/**
* Configure the cluster using the
* KUBECONFIG environment variable.
*
* @param array $config
* @return void
*/
protected function configureWithKubeConfigVariable(array $config)
{
$this->cluster = PhpK8sCluster::fromKubeConfigVariable($config['context']);
}

/**
* Get the initialized cluster.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,29 @@ public function test_in_cluster_config()
$this->assertEquals('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt', $caPath);
$this->assertEquals('some-namespace', K8sResource::$defaultNamespace);
}

/**
* @dataProvider environmentVariableContextProvider
*/
public function test_from_environment_variable(string $context = null, string $expectedDomain)
{
$_SERVER['KUBECONFIG'] = __DIR__.'/cluster/kubeconfig.yaml::'.__DIR__.'/cluster/kubeconfig-2.yaml';

$this->app['config']->set('k8s.default', 'variable');
$this->app['config']->set('k8s.connections.variable', [
'driver' => 'variable',
'context' => $context,
]);

$cluster = LaravelK8sFacade::connection('variable')->getCluster();

$this->assertSame("https://{$expectedDomain}:8443/?", $cluster->getCallableUrl('/', []));
}

public function environmentVariableContextProvider(): iterable
{
yield [null, 'minikube'];
yield ['minikube-2', 'minikube-2'];
yield ['minikube-3', 'minikube-3'];
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

abstract class TestCase extends Orchestra
{
/**
* {@inheritDoc}
*/
public function tearDown(): void
{
parent::tearDown();

unset($_SERVER['KUBECONFIG']);
}

/**
* {@inheritdoc}
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/cluster/kubeconfig-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
clusters:
- cluster:
certificate-authority-data: c29tZS1jYQo= # "some-ca"
server: https://minikube-3:8443
name: minikube-3
contexts:
- context:
cluster: minikube-3
user: minikube
name: minikube-3

0 comments on commit 2dc919f

Please sign in to comment.