From 9576378e58f15aa9b77b4e53c83bfc1de14c0e24 Mon Sep 17 00:00:00 2001 From: Declum Date: Tue, 3 Aug 2021 03:03:58 +0530 Subject: [PATCH 1/7] Respect "namespace" defined in "context" --- src/Traits/Cluster/LoadsFromKubeConfig.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Traits/Cluster/LoadsFromKubeConfig.php b/src/Traits/Cluster/LoadsFromKubeConfig.php index a51ffdb4..10349ec9 100644 --- a/src/Traits/Cluster/LoadsFromKubeConfig.php +++ b/src/Traits/Cluster/LoadsFromKubeConfig.php @@ -77,6 +77,10 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context): } ['context' => ['cluster' => $cluster, 'user' => $user]] = $contextConfig; + + if (isset($contextConfig['context']['namespace'])) { + K8sResource::$defaultNamespace = $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."); From 6afb31efb34f9647e3364906c4ae754b680192e7 Mon Sep 17 00:00:00 2001 From: Declum Date: Tue, 3 Aug 2021 03:06:17 +0530 Subject: [PATCH 2/7] Remove whitespaces --- src/Traits/Cluster/LoadsFromKubeConfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Cluster/LoadsFromKubeConfig.php b/src/Traits/Cluster/LoadsFromKubeConfig.php index 10349ec9..4e7cce58 100644 --- a/src/Traits/Cluster/LoadsFromKubeConfig.php +++ b/src/Traits/Cluster/LoadsFromKubeConfig.php @@ -77,7 +77,7 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context): } ['context' => ['cluster' => $cluster, 'user' => $user]] = $contextConfig; - + if (isset($contextConfig['context']['namespace'])) { K8sResource::$defaultNamespace = $contextConfig['context']['namespace']; } From 5c93e4bba95a29bd9bcbc71bf85cd7fdeac6fe8e Mon Sep 17 00:00:00 2001 From: Declum Date: Tue, 3 Aug 2021 03:26:47 +0530 Subject: [PATCH 3/7] Update LoadsFromKubeConfig.php --- src/Traits/Cluster/LoadsFromKubeConfig.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Traits/Cluster/LoadsFromKubeConfig.php b/src/Traits/Cluster/LoadsFromKubeConfig.php index 4e7cce58..0b1a9adb 100644 --- a/src/Traits/Cluster/LoadsFromKubeConfig.php +++ b/src/Traits/Cluster/LoadsFromKubeConfig.php @@ -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 { From 95083b9a0168fa6832529f85a16e3533d339a449 Mon Sep 17 00:00:00 2001 From: rennokki Date: Tue, 3 Aug 2021 13:38:54 +0300 Subject: [PATCH 4/7] replacing direct attribution with setDefaultNamespace() --- src/Traits/Cluster/LoadsFromKubeConfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Cluster/LoadsFromKubeConfig.php b/src/Traits/Cluster/LoadsFromKubeConfig.php index 0b1a9adb..64e9da97 100644 --- a/src/Traits/Cluster/LoadsFromKubeConfig.php +++ b/src/Traits/Cluster/LoadsFromKubeConfig.php @@ -80,7 +80,7 @@ protected function loadKubeConfigFromArray(array $kubeconfig, string $context): ['context' => ['cluster' => $cluster, 'user' => $user]] = $contextConfig; if (isset($contextConfig['context']['namespace'])) { - K8sResource::$defaultNamespace = $contextConfig['context']['namespace']; + K8sResource::setDefaultNamespace($contextConfig['context']['namespace']); } if (! $clusterConfig = collect($kubeconfig['clusters'] ?? [])->where('name', $cluster)->first()) { From a69c809e08740dcf7fdc1df6bb1d67d1a60875e2 Mon Sep 17 00:00:00 2001 From: rennokki Date: Tue, 3 Aug 2021 13:41:17 +0300 Subject: [PATCH 5/7] Fixed ->where(...)->first() with ->firstWhere() --- src/Traits/Cluster/LoadsFromKubeConfig.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/Cluster/LoadsFromKubeConfig.php b/src/Traits/Cluster/LoadsFromKubeConfig.php index 64e9da97..27d8f12b 100644 --- a/src/Traits/Cluster/LoadsFromKubeConfig.php +++ b/src/Traits/Cluster/LoadsFromKubeConfig.php @@ -71,7 +71,7 @@ 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."); From f721e0b4e41207e84ac23d37548571f98b11f87a Mon Sep 17 00:00:00 2001 From: rennokki Date: Tue, 3 Aug 2021 13:41:51 +0300 Subject: [PATCH 6/7] Added namespace for testing --- tests/cluster/kubeconfig.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/cluster/kubeconfig.yaml b/tests/cluster/kubeconfig.yaml index 39a2314b..8d14387d 100644 --- a/tests/cluster/kubeconfig.yaml +++ b/tests/cluster/kubeconfig.yaml @@ -13,6 +13,7 @@ contexts: cluster: minikube user: minikube name: minikube + namespace: some-namespace - context: cluster: minikube-2 user: minikube-2 From 03933a7fb6c89b5e5dee0e4830eb2cfae62df8cb Mon Sep 17 00:00:00 2001 From: rennokki Date: Tue, 3 Aug 2021 14:02:46 +0300 Subject: [PATCH 7/7] Update kubeconfig.yaml --- tests/cluster/kubeconfig.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/cluster/kubeconfig.yaml b/tests/cluster/kubeconfig.yaml index 8d14387d..df90f1e0 100644 --- a/tests/cluster/kubeconfig.yaml +++ b/tests/cluster/kubeconfig.yaml @@ -18,14 +18,17 @@ contexts: 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: {}