diff --git a/config/k8s.php b/config/k8s.php index ca7e8b8..b15ede1 100644 --- a/config/k8s.php +++ b/config/k8s.php @@ -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'), + ], + ], ]; diff --git a/src/KubernetesCluster.php b/src/KubernetesCluster.php index 83f4059..6584624 100644 --- a/src/KubernetesCluster.php +++ b/src/KubernetesCluster.php @@ -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; } } @@ -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. * diff --git a/tests/ConfigurationTest.php b/tests/ConfigurationTest.php index 9a75295..4fe7878 100644 --- a/tests/ConfigurationTest.php +++ b/tests/ConfigurationTest.php @@ -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']; + } } diff --git a/tests/TestCase.php b/tests/TestCase.php index adca6d3..9cf2cbf 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,6 +6,16 @@ abstract class TestCase extends Orchestra { + /** + * {@inheritDoc} + */ + public function tearDown(): void + { + parent::tearDown(); + + unset($_SERVER['KUBECONFIG']); + } + /** * {@inheritdoc} */ diff --git a/tests/cluster/kubeconfig-2.yaml b/tests/cluster/kubeconfig-2.yaml new file mode 100644 index 0000000..a3f2a77 --- /dev/null +++ b/tests/cluster/kubeconfig-2.yaml @@ -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