From 096fc36d0bb74481a5b469eedc4b1a2b4a0334eb Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Fri, 31 Jan 2025 17:19:40 +0100 Subject: [PATCH] fix: copy KUBECONFIG as defined in shell to system properties (#826) Signed-off-by: Andre Dietisheim --- .../kubernetes/model/client/ClientAdapter.kt | 1 + .../model/client/KubeConfigEnvVar.kt | 43 +++++++++++++++++++ .../model/client/ClientAdapterTest.kt | 1 - 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/KubeConfigEnvVar.kt diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapter.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapter.kt index 1561d9dbe..92e7e2ca0 100644 --- a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapter.kt +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapter.kt @@ -64,6 +64,7 @@ abstract class ClientAdapter(private val fabric8Client: C) createConfig: (context: String?) -> Config = { context -> Config.autoConfigure(context) }, externalTrustManagerProvider: ((toIntegrate: List) -> X509TrustManager)? = null ): ClientAdapter { + KubeConfigEnvVar.copyToSystemProperties() val config = createConfig.invoke(context) setNamespace(namespace, config) val builder = clientBuilder ?: KubernetesClientBuilder() diff --git a/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/KubeConfigEnvVar.kt b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/KubeConfigEnvVar.kt new file mode 100644 index 000000000..d93274e0d --- /dev/null +++ b/src/main/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/KubeConfigEnvVar.kt @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2025 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.kubernetes.model.client + +import com.intellij.openapi.util.SystemInfo +import com.intellij.util.EnvironmentUtil + +object KubeConfigEnvVar { + + private const val KUBECONFIG_ENV_VAR = "KUBECONFIG" + + /** + * Copies the "KUBECONFIG" env variable and it's value to the system properties. + * This env variable is used to list multiple config files and is supported by `kubectl`. + * + * example: + * ``` + * export KUBECONFIG=${HOME}/.kube/config:${HOME}/.kube/minikube.yaml + * ``` + * On MacOS env variables present in the shell are not present in IDEA + * because applications that are launched from the dock don't get + * env variables that are exported for the shell (`~/.zshrc`, `~/.bashrc`, `~/.zprofile`, etc.). + * Therefore they are not present in [System.getProperties]. + * This method inspects the shell env variables and copies them over to the System properties. + * + * **See Also:** [issue #826](https://github.com/redhat-developer/intellij-kubernetes/issues/826) + */ + fun copyToSystemProperties() { + if (SystemInfo.isMac) { + val kubeconfig = EnvironmentUtil.getValue(KUBECONFIG_ENV_VAR) ?: return + System.getProperties()[KUBECONFIG_ENV_VAR] = kubeconfig + System.getProperties()[KUBECONFIG_ENV_VAR.lowercase()] = kubeconfig + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapterTest.kt b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapterTest.kt index 8f5d39370..02ebcce6f 100644 --- a/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapterTest.kt +++ b/src/test/kotlin/com/redhat/devtools/intellij/kubernetes/model/client/ClientAdapterTest.kt @@ -33,7 +33,6 @@ import java.util.function.Consumer class ClientAdapterTest { - private val certificate: X509Certificate = mock { on { subjectX500Principal } doReturn mock() }