Skip to content

Commit 5a40aaa

Browse files
authored
Add support for KUBECONFIG env variable (#44)
1 parent b049bbc commit 5a40aaa

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ client.shutdown(queue: queue) { (error: Error?) in
120120

121121
The client tries to resolve a `kube config` automatically from different sources in the following order:
122122

123+
- If set, kube config file at path of environment variable `KUBECONFIG`
123124
- Kube config file in the user's `$HOME/.kube/config` directory
124125
- `ServiceAccount` token located at `/var/run/secrets/kubernetes.io/serviceaccount/token` and a mounted CA certificate,
125126
- if it's running in Kubernetes.

Sources/SwiftkubeClient/Client/KubernetesClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public actor KubernetesClient {
108108
///
109109
/// The client tries to resolve a `kube config` automatically from different sources in the following order:
110110
///
111+
/// - A Kube config file at path of environment variable `KUBECONFIG` (if set)
111112
/// - A Kube config file in the user's `$HOME/.kube/config` directory
112113
/// - `ServiceAccount` token located at `/var/run/secrets/kubernetes.io/serviceaccount/token` and a mounted CA certificate, if it's running in Kubernetes.
113114
///

Sources/SwiftkubeClient/Config/KubernetesClientConfig.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,19 @@ internal struct LocalKubeConfigLoader: KubernetesClientConfigLoader {
286286
redirectConfiguration: HTTPClient.Configuration.RedirectConfiguration,
287287
logger: Logger?
288288
) throws -> KubernetesClientConfig? {
289-
guard let homePath = ProcessInfo.processInfo.environment["HOME"] else {
290-
logger?.info("Skipping kubeconfig in $HOME/.kube/config because HOME env variable is not set.")
289+
var kubeConfigURL: URL?
290+
291+
if let kubeConfigPath = ProcessInfo.processInfo.environment["KUBECONFIG"] {
292+
kubeConfigURL = URL(fileURLWithPath: kubeConfigPath)
293+
} else if let homePath = ProcessInfo.processInfo.environment["HOME"] {
294+
kubeConfigURL = URL(fileURLWithPath: homePath + "/.kube/config")
295+
}
296+
297+
guard let kubeConfigURL else {
298+
logger?.info("Skipping local kubeconfig detection, neither environment variable KUBECONFIG nor HOME are set.")
291299
return nil
292300
}
293301

294-
let kubeConfigURL = URL(fileURLWithPath: homePath + "/.kube/config")
295302
return try? URLConfigLoader(url: kubeConfigURL)
296303
.load(timeout: timeout, redirectConfiguration: redirectConfiguration, logger: logger)
297304
}

0 commit comments

Comments
 (0)