Skip to content

Commit 2fd6398

Browse files
authored
Merge pull request #110 from declum/master
Respect "namespace" defined in "context"
2 parents d0f198b + 03933a7 commit 2fd6398

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Traits/Cluster/LoadsFromKubeConfig.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
77
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
88
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
9+
use RenokiCo\PhpK8s\Kinds\K8sResource;
910

1011
trait LoadsFromKubeConfig
1112
{
@@ -70,14 +71,18 @@ public function fromKubeConfigYamlFile(string $path = '/.kube/config', string $c
7071
*/
7172
protected function loadKubeConfigFromArray(array $kubeconfig, string $context): void
7273
{
73-
$contextConfig = collect($kubeconfig['contexts'] ?? [])->where('name', $context)->first();
74+
$contextConfig = collect($kubeconfig['contexts'] ?? [])->firstWhere('name', $context);
7475

7576
if (! $contextConfig) {
7677
throw new KubeConfigContextNotFound("The context {$context} does not exist in the provided Kube Config file.");
7778
}
7879

7980
['context' => ['cluster' => $cluster, 'user' => $user]] = $contextConfig;
8081

82+
if (isset($contextConfig['context']['namespace'])) {
83+
K8sResource::setDefaultNamespace($contextConfig['context']['namespace']);
84+
}
85+
8186
if (! $clusterConfig = collect($kubeconfig['clusters'] ?? [])->where('name', $cluster)->first()) {
8287
throw new KubeConfigClusterNotFound("The cluster {$cluster} does not exist in the provided Kube Config file.");
8388
}

tests/cluster/kubeconfig.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@ contexts:
1313
cluster: minikube
1414
user: minikube
1515
name: minikube
16+
namespace: some-namespace
1617
- context:
1718
cluster: minikube-2
1819
user: minikube-2
1920
name: minikube-2
21+
namespace: some-namespace
2022
- context:
2123
cluster: no-cluster
2224
user: minikube
2325
name: minikube-without-cluster
26+
namespace: some-namespace
2427
- context:
2528
cluster: minikube
2629
user: no-user
2730
name: minikube-without-user
31+
namespace: some-namespace
2832
current-context: minikube
2933
kind: Config
3034
preferences: {}

0 commit comments

Comments
 (0)