Skip to content

Commit 2dc919f

Browse files
committed
Added environment variable provider
1 parent 06a49b2 commit 2dc919f

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

config/k8s.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@
9696
'host' => env('KUBE_HOST', 'https://kubernetes.default.svc.cluster.local'),
9797
],
9898

99+
/*
100+
|--------------------------------------------------------------------------
101+
| Environment Variable Driver
102+
|--------------------------------------------------------------------------
103+
|
104+
| The environment variable driver leverages your current (possibly set)
105+
| KUBECONFIG environment variable. The variable contains a list of paths
106+
| towards multiple kubeconfig files that will be read, merged and based
107+
| on the selected context from the configuration, it will connect
108+
| to the cluster, just like the "kubeconfig" driver.
109+
|
110+
| Read more: https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/
111+
|
112+
*/
113+
114+
'environment' => [
115+
'driver' => 'environment',
116+
'context' => env('KUBECONFIG_CONTEXT', 'minikube'),
117+
],
118+
99119
],
100120

101121
];

src/KubernetesCluster.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected function loadFromConfig(array $config)
5555
case 'http': $this->configureWithHttpAuth($config); break;
5656
case 'token': $this->configureWithToken($config); break;
5757
case 'cluster': $this->configureInCluster($config); break;
58+
case 'variable': $this->configureWithKubeConfigVariable($config); break;
5859
default: break;
5960
}
6061
}
@@ -144,6 +145,18 @@ protected function configureInCluster(array $config)
144145
$this->cluster = PhpK8sCluster::inClusterConfiguration();
145146
}
146147

148+
/**
149+
* Configure the cluster using the
150+
* KUBECONFIG environment variable.
151+
*
152+
* @param array $config
153+
* @return void
154+
*/
155+
protected function configureWithKubeConfigVariable(array $config)
156+
{
157+
$this->cluster = PhpK8sCluster::fromKubeConfigVariable($config['context']);
158+
}
159+
147160
/**
148161
* Get the initialized cluster.
149162
*

tests/ConfigurationTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,29 @@ public function test_in_cluster_config()
130130
$this->assertEquals('/var/run/secrets/kubernetes.io/serviceaccount/ca.crt', $caPath);
131131
$this->assertEquals('some-namespace', K8sResource::$defaultNamespace);
132132
}
133+
134+
/**
135+
* @dataProvider environmentVariableContextProvider
136+
*/
137+
public function test_from_environment_variable(string $context = null, string $expectedDomain)
138+
{
139+
$_SERVER['KUBECONFIG'] = __DIR__.'/cluster/kubeconfig.yaml::'.__DIR__.'/cluster/kubeconfig-2.yaml';
140+
141+
$this->app['config']->set('k8s.default', 'variable');
142+
$this->app['config']->set('k8s.connections.variable', [
143+
'driver' => 'variable',
144+
'context' => $context,
145+
]);
146+
147+
$cluster = LaravelK8sFacade::connection('variable')->getCluster();
148+
149+
$this->assertSame("https://{$expectedDomain}:8443/?", $cluster->getCallableUrl('/', []));
150+
}
151+
152+
public function environmentVariableContextProvider(): iterable
153+
{
154+
yield [null, 'minikube'];
155+
yield ['minikube-2', 'minikube-2'];
156+
yield ['minikube-3', 'minikube-3'];
157+
}
133158
}

tests/TestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
abstract class TestCase extends Orchestra
88
{
9+
/**
10+
* {@inheritDoc}
11+
*/
12+
public function tearDown(): void
13+
{
14+
parent::tearDown();
15+
16+
unset($_SERVER['KUBECONFIG']);
17+
}
18+
919
/**
1020
* {@inheritdoc}
1121
*/

tests/cluster/kubeconfig-2.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
clusters:
2+
- cluster:
3+
certificate-authority-data: c29tZS1jYQo= # "some-ca"
4+
server: https://minikube-3:8443
5+
name: minikube-3
6+
contexts:
7+
- context:
8+
cluster: minikube-3
9+
user: minikube
10+
name: minikube-3

0 commit comments

Comments
 (0)