Skip to content

Commit

Permalink
Merge pull request #113 from renoki-co/fix/apply-2.x-fixes
Browse files Browse the repository at this point in the history
[1.x] Respect namespace from context
  • Loading branch information
rennokki authored Aug 3, 2021
2 parents f4a7798 + d84807c commit 33d7b18
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Traits/Cluster/LoadsFromKubeConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use RenokiCo\PhpK8s\Exceptions\KubeConfigClusterNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigContextNotFound;
use RenokiCo\PhpK8s\Exceptions\KubeConfigUserNotFound;
use RenokiCo\PhpK8s\Kinds\K8sResource;

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

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

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

if (isset($contextConfig['context']['namespace'])) {
K8sResource::setDefaultNamespace($contextConfig['context']['namespace']);
}

if (! $clusterConfig = collect($kubeconfig['clusters'] ?? [])->where('name', $cluster)->first()) {
throw new KubeConfigClusterNotFound("The cluster {$cluster} does not exist in the provided Kube Config file.");
}
Expand Down
4 changes: 4 additions & 0 deletions tests/cluster/kubeconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ contexts:
cluster: minikube
user: minikube
name: minikube
namespace: some-namespace
- context:
cluster: minikube-2
user: minikube-2
name: minikube-2
namespace: some-namespace
- context:
cluster: no-cluster
user: minikube
name: minikube-without-cluster
namespace: some-namespace
- context:
cluster: minikube
user: no-user
name: minikube-without-user
namespace: some-namespace
current-context: minikube
kind: Config
preferences: {}
Expand Down

0 comments on commit 33d7b18

Please sign in to comment.