Skip to content

Commit

Permalink
Merge pull request #10 from renoki-co/feature/3.x
Browse files Browse the repository at this point in the history
[upgrade] 3.x
  • Loading branch information
rennokki authored Sep 5, 2021
2 parents 2c7d9cb + 2dc919f commit 8255d39
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 9 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"renoki-co/php-k8s": "^2.2"
"renoki-co/php-k8s": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
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'),
],

],

];
28 changes: 20 additions & 8 deletions src/KubernetesCluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use RenokiCo\PhpK8s\KubernetesCluster as PhpK8sCluster;

/**
* @see \RenokiCo\PhpK8s\KubernetesCluster
*/
class KubernetesCluster
{
/**
Expand All @@ -28,7 +31,7 @@ public function __construct(array $config)
* Switch the connection.
*
* @param string $connection
* @return $this
* @return \RenokiCo\LaravelK8s\KubernetesCluster
*/
public function connection(string $connection)
{
Expand All @@ -47,13 +50,12 @@ public function connection(string $connection)
*/
protected function loadFromConfig(array $config)
{
$this->cluster = new PhpK8sCluster('http://127.0.0.1:8080');

switch ($config['driver'] ?? null) {
case 'kubeconfig': $this->configureWithKubeConfigFile($config); break;
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 All @@ -66,7 +68,7 @@ protected function loadFromConfig(array $config)
*/
protected function configureWithKubeConfigFile(array $config)
{
$this->cluster->fromKubeConfigYamlFile(
$this->cluster = PhpK8sCluster::fromKubeConfigYamlFile(
$config['path'], $config['context']
);
}
Expand All @@ -79,7 +81,7 @@ protected function configureWithKubeConfigFile(array $config)
*/
protected function configureWithHttpAuth(array $config)
{
$this->cluster = new PhpK8sCluster($config['host']);
$this->cluster = PhpK8sCluster::fromUrl($config['host']);

if ($config['ssl']['verify'] ?? true) {
$this->cluster->withCertificate(
Expand Down Expand Up @@ -111,7 +113,7 @@ protected function configureWithHttpAuth(array $config)
*/
protected function configureWithToken(array $config)
{
$this->cluster = new PhpK8sCluster($config['host']);
$this->cluster = PhpK8sCluster::fromUrl($config['host']);

if ($config['ssl']['verify'] ?? true) {
$this->cluster->withCertificate(
Expand Down Expand Up @@ -140,9 +142,19 @@ protected function configureWithToken(array $config)
*/
protected function configureInCluster(array $config)
{
$this->cluster = new PhpK8sCluster($config['host'] ?? 'https://kubernetes.default.svc.cluster.local');
$this->cluster = PhpK8sCluster::inClusterConfiguration();
}

$this->cluster->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']);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions src/LaravelK8sFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,105 @@

use Illuminate\Support\Facades\Facade;

/**
* @method static \RenokiCo\LaravelK8s\KubernetesCluster connection(string $connection)
* @method static \RenokiCo\PhpK8s\KubernetesCluster getCluster()
* @method static \RenokiCo\PhpK8s\Kinds\K8sNode node(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sNode getNodeByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllNodes(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sEvent event(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sEvent getEventByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllEventsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllEvents(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sNamespace namespace(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sNamespace getNamespaceByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllNamespaces(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sConfigMap configmap(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sConfigMap getConfigmapByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllConfigmapsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllConfigmaps(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sSecret secret(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sSecret getSecretByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllSecretsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllSecrets(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sIngress ingress(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sIngress getIngressByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllIngressesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllIngresses(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sService service(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sService getServiceByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServicesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServices(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sStorageClass storageClass(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sStorageClass getStorageClassByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStorageClassesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStorageClasses(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolume persistentVolume(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolume getPersistentVolumeByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumes(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolumeClaim persistentVolumeClaim(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolumeClaim getPersistentVolumeClaimByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumeClaimsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumeClaims(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPod pod(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPod getPodByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPods(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sStatefulSet statefulSet(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sStatefulSet getStatefulSetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStatefulSetsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStatefulSets(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sDeployment deployment(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sDeployment getDeploymentByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDeploymentsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDeployments(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sJob job(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sJob getJobByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllJobsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllJobs(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sCronJob cronjob(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sCronJob getCronjobByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllCronjobsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllCronjobs(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sDaemonSet daemonSet(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sDaemonSet getDaemonSetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDaemonSetsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDaemonSets(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sHorizontalPodAutoscaler horizontalPodAutoscaler(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sHorizontalPodAutoscaler getHorizontalPodAutoscalerByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllHorizontalPodAutoscalersFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllHorizontalPodAutoscalers(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sServiceAccount serviceAccount(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sServiceAccount getServiceAccountByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServiceAccountsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServiceAccounts(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sRole role(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sRole getRoleByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRolesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoles(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRole clusterRole(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRole getClusterRoleByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRolesFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoles(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sRoleBinding roleBinding(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sRoleBinding getRoleBindingByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoleBindingsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoleBindings(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRoleBinding clusterRoleBinding(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRoleBinding getClusterRoleBindingByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoleBindingsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoleBindings(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget podDisruptionBudget(array $attributes = [])
* @method static \RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget getPodDisruptionBudgetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgetsFromAllNamespaces(array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgets(string $namespace = 'default', array $query = ['pretty' => 1])
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYaml(string $yaml)
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYamlFile(string $path, \Closure $callback = null)
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromTemplatedYamlFile(string $path, array $replace, \Closure $callback = null)
*
* @see \RenokiCo\LaravelK8s\KubernetesCluster
*/
class LaravelK8sFacade extends Facade
{
/**
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 8255d39

Please sign in to comment.