Skip to content

Commit 8255d39

Browse files
authored
Merge pull request #10 from renoki-co/feature/3.x
[upgrade] 3.x
2 parents 2c7d9cb + 2dc919f commit 8255d39

File tree

7 files changed

+185
-9
lines changed

7 files changed

+185
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
}
1313
],
1414
"require": {
15-
"renoki-co/php-k8s": "^2.2"
15+
"renoki-co/php-k8s": "^3.0"
1616
},
1717
"autoload": {
1818
"psr-4": {

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: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use RenokiCo\PhpK8s\KubernetesCluster as PhpK8sCluster;
66

7+
/**
8+
* @see \RenokiCo\PhpK8s\KubernetesCluster
9+
*/
710
class KubernetesCluster
811
{
912
/**
@@ -28,7 +31,7 @@ public function __construct(array $config)
2831
* Switch the connection.
2932
*
3033
* @param string $connection
31-
* @return $this
34+
* @return \RenokiCo\LaravelK8s\KubernetesCluster
3235
*/
3336
public function connection(string $connection)
3437
{
@@ -47,13 +50,12 @@ public function connection(string $connection)
4750
*/
4851
protected function loadFromConfig(array $config)
4952
{
50-
$this->cluster = new PhpK8sCluster('http://127.0.0.1:8080');
51-
5253
switch ($config['driver'] ?? null) {
5354
case 'kubeconfig': $this->configureWithKubeConfigFile($config); break;
5455
case 'http': $this->configureWithHttpAuth($config); break;
5556
case 'token': $this->configureWithToken($config); break;
5657
case 'cluster': $this->configureInCluster($config); break;
58+
case 'variable': $this->configureWithKubeConfigVariable($config); break;
5759
default: break;
5860
}
5961
}
@@ -66,7 +68,7 @@ protected function loadFromConfig(array $config)
6668
*/
6769
protected function configureWithKubeConfigFile(array $config)
6870
{
69-
$this->cluster->fromKubeConfigYamlFile(
71+
$this->cluster = PhpK8sCluster::fromKubeConfigYamlFile(
7072
$config['path'], $config['context']
7173
);
7274
}
@@ -79,7 +81,7 @@ protected function configureWithKubeConfigFile(array $config)
7981
*/
8082
protected function configureWithHttpAuth(array $config)
8183
{
82-
$this->cluster = new PhpK8sCluster($config['host']);
84+
$this->cluster = PhpK8sCluster::fromUrl($config['host']);
8385

8486
if ($config['ssl']['verify'] ?? true) {
8587
$this->cluster->withCertificate(
@@ -111,7 +113,7 @@ protected function configureWithHttpAuth(array $config)
111113
*/
112114
protected function configureWithToken(array $config)
113115
{
114-
$this->cluster = new PhpK8sCluster($config['host']);
116+
$this->cluster = PhpK8sCluster::fromUrl($config['host']);
115117

116118
if ($config['ssl']['verify'] ?? true) {
117119
$this->cluster->withCertificate(
@@ -140,9 +142,19 @@ protected function configureWithToken(array $config)
140142
*/
141143
protected function configureInCluster(array $config)
142144
{
143-
$this->cluster = new PhpK8sCluster($config['host'] ?? 'https://kubernetes.default.svc.cluster.local');
145+
$this->cluster = PhpK8sCluster::inClusterConfiguration();
146+
}
144147

145-
$this->cluster->inClusterConfiguration();
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']);
146158
}
147159

148160
/**

src/LaravelK8sFacade.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,105 @@
44

55
use Illuminate\Support\Facades\Facade;
66

7+
/**
8+
* @method static \RenokiCo\LaravelK8s\KubernetesCluster connection(string $connection)
9+
* @method static \RenokiCo\PhpK8s\KubernetesCluster getCluster()
10+
* @method static \RenokiCo\PhpK8s\Kinds\K8sNode node(array $attributes = [])
11+
* @method static \RenokiCo\PhpK8s\Kinds\K8sNode getNodeByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
12+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllNodes(string $namespace = 'default', array $query = ['pretty' => 1])
13+
* @method static \RenokiCo\PhpK8s\Kinds\K8sEvent event(array $attributes = [])
14+
* @method static \RenokiCo\PhpK8s\Kinds\K8sEvent getEventByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
15+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllEventsFromAllNamespaces(array $query = ['pretty' => 1])
16+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllEvents(string $namespace = 'default', array $query = ['pretty' => 1])
17+
* @method static \RenokiCo\PhpK8s\Kinds\K8sNamespace namespace(array $attributes = [])
18+
* @method static \RenokiCo\PhpK8s\Kinds\K8sNamespace getNamespaceByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
19+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllNamespaces(string $namespace = 'default', array $query = ['pretty' => 1])
20+
* @method static \RenokiCo\PhpK8s\Kinds\K8sConfigMap configmap(array $attributes = [])
21+
* @method static \RenokiCo\PhpK8s\Kinds\K8sConfigMap getConfigmapByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
22+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllConfigmapsFromAllNamespaces(array $query = ['pretty' => 1])
23+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllConfigmaps(string $namespace = 'default', array $query = ['pretty' => 1])
24+
* @method static \RenokiCo\PhpK8s\Kinds\K8sSecret secret(array $attributes = [])
25+
* @method static \RenokiCo\PhpK8s\Kinds\K8sSecret getSecretByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
26+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllSecretsFromAllNamespaces(array $query = ['pretty' => 1])
27+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllSecrets(string $namespace = 'default', array $query = ['pretty' => 1])
28+
* @method static \RenokiCo\PhpK8s\Kinds\K8sIngress ingress(array $attributes = [])
29+
* @method static \RenokiCo\PhpK8s\Kinds\K8sIngress getIngressByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
30+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllIngressesFromAllNamespaces(array $query = ['pretty' => 1])
31+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllIngresses(string $namespace = 'default', array $query = ['pretty' => 1])
32+
* @method static \RenokiCo\PhpK8s\Kinds\K8sService service(array $attributes = [])
33+
* @method static \RenokiCo\PhpK8s\Kinds\K8sService getServiceByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
34+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServicesFromAllNamespaces(array $query = ['pretty' => 1])
35+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServices(string $namespace = 'default', array $query = ['pretty' => 1])
36+
* @method static \RenokiCo\PhpK8s\Kinds\K8sStorageClass storageClass(array $attributes = [])
37+
* @method static \RenokiCo\PhpK8s\Kinds\K8sStorageClass getStorageClassByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
38+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStorageClassesFromAllNamespaces(array $query = ['pretty' => 1])
39+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStorageClasses(string $namespace = 'default', array $query = ['pretty' => 1])
40+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolume persistentVolume(array $attributes = [])
41+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolume getPersistentVolumeByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
42+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumesFromAllNamespaces(array $query = ['pretty' => 1])
43+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumes(string $namespace = 'default', array $query = ['pretty' => 1])
44+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolumeClaim persistentVolumeClaim(array $attributes = [])
45+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPersistentVolumeClaim getPersistentVolumeClaimByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
46+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumeClaimsFromAllNamespaces(array $query = ['pretty' => 1])
47+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPersistentVolumeClaims(string $namespace = 'default', array $query = ['pretty' => 1])
48+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPod pod(array $attributes = [])
49+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPod getPodByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
50+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodsFromAllNamespaces(array $query = ['pretty' => 1])
51+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPods(string $namespace = 'default', array $query = ['pretty' => 1])
52+
* @method static \RenokiCo\PhpK8s\Kinds\K8sStatefulSet statefulSet(array $attributes = [])
53+
* @method static \RenokiCo\PhpK8s\Kinds\K8sStatefulSet getStatefulSetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
54+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStatefulSetsFromAllNamespaces(array $query = ['pretty' => 1])
55+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllStatefulSets(string $namespace = 'default', array $query = ['pretty' => 1])
56+
* @method static \RenokiCo\PhpK8s\Kinds\K8sDeployment deployment(array $attributes = [])
57+
* @method static \RenokiCo\PhpK8s\Kinds\K8sDeployment getDeploymentByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
58+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDeploymentsFromAllNamespaces(array $query = ['pretty' => 1])
59+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDeployments(string $namespace = 'default', array $query = ['pretty' => 1])
60+
* @method static \RenokiCo\PhpK8s\Kinds\K8sJob job(array $attributes = [])
61+
* @method static \RenokiCo\PhpK8s\Kinds\K8sJob getJobByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
62+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllJobsFromAllNamespaces(array $query = ['pretty' => 1])
63+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllJobs(string $namespace = 'default', array $query = ['pretty' => 1])
64+
* @method static \RenokiCo\PhpK8s\Kinds\K8sCronJob cronjob(array $attributes = [])
65+
* @method static \RenokiCo\PhpK8s\Kinds\K8sCronJob getCronjobByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
66+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllCronjobsFromAllNamespaces(array $query = ['pretty' => 1])
67+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllCronjobs(string $namespace = 'default', array $query = ['pretty' => 1])
68+
* @method static \RenokiCo\PhpK8s\Kinds\K8sDaemonSet daemonSet(array $attributes = [])
69+
* @method static \RenokiCo\PhpK8s\Kinds\K8sDaemonSet getDaemonSetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
70+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDaemonSetsFromAllNamespaces(array $query = ['pretty' => 1])
71+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllDaemonSets(string $namespace = 'default', array $query = ['pretty' => 1])
72+
* @method static \RenokiCo\PhpK8s\Kinds\K8sHorizontalPodAutoscaler horizontalPodAutoscaler(array $attributes = [])
73+
* @method static \RenokiCo\PhpK8s\Kinds\K8sHorizontalPodAutoscaler getHorizontalPodAutoscalerByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
74+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllHorizontalPodAutoscalersFromAllNamespaces(array $query = ['pretty' => 1])
75+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllHorizontalPodAutoscalers(string $namespace = 'default', array $query = ['pretty' => 1])
76+
* @method static \RenokiCo\PhpK8s\Kinds\K8sServiceAccount serviceAccount(array $attributes = [])
77+
* @method static \RenokiCo\PhpK8s\Kinds\K8sServiceAccount getServiceAccountByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
78+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServiceAccountsFromAllNamespaces(array $query = ['pretty' => 1])
79+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllServiceAccounts(string $namespace = 'default', array $query = ['pretty' => 1])
80+
* @method static \RenokiCo\PhpK8s\Kinds\K8sRole role(array $attributes = [])
81+
* @method static \RenokiCo\PhpK8s\Kinds\K8sRole getRoleByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
82+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRolesFromAllNamespaces(array $query = ['pretty' => 1])
83+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoles(string $namespace = 'default', array $query = ['pretty' => 1])
84+
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRole clusterRole(array $attributes = [])
85+
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRole getClusterRoleByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
86+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRolesFromAllNamespaces(array $query = ['pretty' => 1])
87+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoles(string $namespace = 'default', array $query = ['pretty' => 1])
88+
* @method static \RenokiCo\PhpK8s\Kinds\K8sRoleBinding roleBinding(array $attributes = [])
89+
* @method static \RenokiCo\PhpK8s\Kinds\K8sRoleBinding getRoleBindingByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
90+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoleBindingsFromAllNamespaces(array $query = ['pretty' => 1])
91+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllRoleBindings(string $namespace = 'default', array $query = ['pretty' => 1])
92+
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRoleBinding clusterRoleBinding(array $attributes = [])
93+
* @method static \RenokiCo\PhpK8s\Kinds\K8sClusterRoleBinding getClusterRoleBindingByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
94+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoleBindingsFromAllNamespaces(array $query = ['pretty' => 1])
95+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllClusterRoleBindings(string $namespace = 'default', array $query = ['pretty' => 1])
96+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget podDisruptionBudget(array $attributes = [])
97+
* @method static \RenokiCo\PhpK8s\Kinds\K8sPodDisruptionBudget getPodDisruptionBudgetByName(string $name, string $namespace = 'default', array $query = ['pretty' => 1])
98+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgetsFromAllNamespaces(array $query = ['pretty' => 1])
99+
* @method static \RenokiCo\PhpK8s\ResourcesList getAllPodDisruptionBudgets(string $namespace = 'default', array $query = ['pretty' => 1])
100+
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYaml(string $yaml)
101+
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromYamlFile(string $path, \Closure $callback = null)
102+
* @method static \RenokiCo\PhpK8s\Kinds\K8sResource|array[\RenokiCo\PhpK8s\Kinds\K8sResource] fromTemplatedYamlFile(string $path, array $replace, \Closure $callback = null)
103+
*
104+
* @see \RenokiCo\LaravelK8s\KubernetesCluster
105+
*/
7106
class LaravelK8sFacade extends Facade
8107
{
9108
/**

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)