From b1dd6ea4a8b580198cc954c88ed26b9e8101d777 Mon Sep 17 00:00:00 2001 From: Simon Leeb <52261246+sliemeobn@users.noreply.github.com> Date: Sat, 7 Jun 2025 17:58:06 +0200 Subject: [PATCH] added support for KUBECONFIG env variable --- README.md | 1 + .../SwiftkubeClient/Client/KubernetesClient.swift | 1 + .../Config/KubernetesClientConfig.swift | 13 ++++++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e7af69c..5890ab7 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ client.shutdown(queue: queue) { (error: Error?) in The client tries to resolve a `kube config` automatically from different sources in the following order: +- If set, kube config file at path of environment variable `KUBECONFIG` - Kube config file in the user's `$HOME/.kube/config` directory - `ServiceAccount` token located at `/var/run/secrets/kubernetes.io/serviceaccount/token` and a mounted CA certificate, - if it's running in Kubernetes. diff --git a/Sources/SwiftkubeClient/Client/KubernetesClient.swift b/Sources/SwiftkubeClient/Client/KubernetesClient.swift index 4f4ccfa..d2bcc90 100644 --- a/Sources/SwiftkubeClient/Client/KubernetesClient.swift +++ b/Sources/SwiftkubeClient/Client/KubernetesClient.swift @@ -108,6 +108,7 @@ public actor KubernetesClient { /// /// The client tries to resolve a `kube config` automatically from different sources in the following order: /// + /// - A Kube config file at path of environment variable `KUBECONFIG` (if set) /// - A Kube config file in the user's `$HOME/.kube/config` directory /// - `ServiceAccount` token located at `/var/run/secrets/kubernetes.io/serviceaccount/token` and a mounted CA certificate, if it's running in Kubernetes. /// diff --git a/Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift b/Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift index ad07d01..596aee3 100644 --- a/Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift +++ b/Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift @@ -286,12 +286,19 @@ internal struct LocalKubeConfigLoader: KubernetesClientConfigLoader { redirectConfiguration: HTTPClient.Configuration.RedirectConfiguration, logger: Logger? ) throws -> KubernetesClientConfig? { - guard let homePath = ProcessInfo.processInfo.environment["HOME"] else { - logger?.info("Skipping kubeconfig in $HOME/.kube/config because HOME env variable is not set.") + var kubeConfigURL: URL? + + if let kubeConfigPath = ProcessInfo.processInfo.environment["KUBECONFIG"] { + kubeConfigURL = URL(fileURLWithPath: kubeConfigPath) + } else if let homePath = ProcessInfo.processInfo.environment["HOME"] { + kubeConfigURL = URL(fileURLWithPath: homePath + "/.kube/config") + } + + guard let kubeConfigURL else { + logger?.info("Skipping local kubeconfig detection, neither environment variable KUBECONFIG nor HOME are set.") return nil } - let kubeConfigURL = URL(fileURLWithPath: homePath + "/.kube/config") return try? URLConfigLoader(url: kubeConfigURL) .load(timeout: timeout, redirectConfiguration: redirectConfiguration, logger: logger) }