diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java
index d4f670367f5..b7d0f04c17d 100644
--- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java
+++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/Config.java
@@ -799,10 +799,12 @@ public static Config fromKubeconfig(String context, String kubeconfigContents, S
if (Utils.isNullOrEmpty(kubeconfigContents)) {
throw new KubernetesClientException("Could not create Config from kubeconfig");
}
- final var kubeconfig = KubeConfigUtils.parseConfigFromString(kubeconfigContents);
+ final io.fabric8.kubernetes.api.model.Config kubeconfig;
if (kubeconfigPath != null) {
// TODO: temp workaround until the method is removed (marked for removal in 7.0.0)
- kubeconfig.setAdditionalProperty("KUBERNETES_CONFIG_FILE_KEY", new File(kubeconfigPath));
+ kubeconfig = KubeConfigUtils.parseConfig(new File(kubeconfigPath));
+ } else {
+ kubeconfig = KubeConfigUtils.parseConfigFromString(kubeconfigContents);
}
KubeConfigUtils.merge(config, context, kubeconfig);
if (!disableAutoConfig()) {
@@ -1463,7 +1465,29 @@ public void setCurrentContext(NamedContext context) {
* @return the path to the kubeconfig file.
*/
public File getFile() {
- return KubeConfigUtils.getFileFromContext(getCurrentContext());
+ return KubeConfigUtils.getFileWithNamedContext(getCurrentContext());
+ }
+
+ /**
+ * Returns the path to the file that contains the cluster information from which this configuration was loaded from.
+ *
+ * Returns {@code null} if no file was used.
+ *
+ * @return the path to the kubeconfig file.
+ */
+ public File getFileWithCluster() {
+ return KubeConfigUtils.getFileWithNamedCluster(getCurrentContext());
+ }
+
+ /**
+ * Returns the path to the file that contains the user information from which this configuration was loaded from.
+ *
+ * Returns {@code null} if no file was used.
+ *
+ * @return the path to the kubeconfig file.
+ */
+ public File getFileWithAuthInfo() {
+ return KubeConfigUtils.getFileWithNamedAuthInfo(getCurrentContext());
}
@JsonIgnore
diff --git a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java
index 3cb58d5f635..50810e6c2f7 100644
--- a/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java
+++ b/kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/internal/KubeConfigUtils.java
@@ -39,6 +39,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.function.Supplier;
import java.util.stream.Collectors;
import static io.fabric8.kubernetes.client.Config.HTTPS_PROTOCOL_PREFIX;
@@ -55,7 +56,9 @@ public class KubeConfigUtils {
private static final Logger logger = LoggerFactory.getLogger(io.fabric8.kubernetes.client.Config.class);
- private static final String KUBERNETES_CONFIG_FILE_KEY = "KUBERNETES_CONFIG_FILE_KEY";
+ private static final String KUBERNETES_CONFIG_CONTEXT_FILE_KEY = "KUBERNETES_CONFIG_CONTEXT_FILE_KEY";
+ private static final String KUBERNETES_CONFIG_CLUSTER_FILE_KEY = "KUBERNETES_CONFIG_CLUSTER_FILE_KEY";
+ private static final String KUBERNETES_CONFIG_AUTH_INFO_FILE_KEY = "KUBERNETES_CONFIG_AUTH_INFO_FILE_KEY";
private static final String ACCESS_TOKEN = "access-token";
private static final String ID_TOKEN = "id-token";
@@ -68,7 +71,16 @@ public static Config parseConfig(File kubeconfig) {
}
try (var fis = Files.newInputStream(kubeconfig.toPath())) {
final var ret = Serialization.unmarshal(fis, Config.class);
- ret.setAdditionalProperty(KUBERNETES_CONFIG_FILE_KEY, kubeconfig);
+ if (ret.getContexts() != null) {
+ ret.getContexts().forEach(ctx -> ctx.getAdditionalProperties().put(KUBERNETES_CONFIG_CONTEXT_FILE_KEY, kubeconfig));
+ }
+ if (ret.getClusters() != null) {
+ ret.getClusters()
+ .forEach(cluster -> cluster.getAdditionalProperties().put(KUBERNETES_CONFIG_CLUSTER_FILE_KEY, kubeconfig));
+ }
+ if (ret.getUsers() != null) {
+ ret.getUsers().forEach(user -> user.getAdditionalProperties().put(KUBERNETES_CONFIG_AUTH_INFO_FILE_KEY, kubeconfig));
+ }
return ret;
} catch (Exception e) {
throw KubernetesClientException.launderThrowable(kubeconfig + " (File) is not a parseable Kubernetes Config", e);
@@ -87,34 +99,60 @@ public static Config parseConfigFromString(String contents) {
* @throws IOException in case of failure while writing to file.
*/
public static void persistKubeConfigIntoFile(Config kubeconfig, File kubeConfigPath) throws IOException {
- if (kubeconfig.getAdditionalProperties() != null) {
- kubeconfig.getAdditionalProperties().remove(KUBERNETES_CONFIG_FILE_KEY);
- }
if (kubeconfig.getContexts() != null) {
- kubeconfig.getContexts().stream()
- .filter(ctx -> ctx.getAdditionalProperties() != null)
- .forEach(ctx -> ctx.getAdditionalProperties().remove(KUBERNETES_CONFIG_FILE_KEY));
+ kubeconfig.getContexts().forEach(c -> removeAdditionalProperties(c::getAdditionalProperties));
+ }
+ if (kubeconfig.getClusters() != null) {
+ kubeconfig.getClusters().forEach(c -> removeAdditionalProperties(c::getAdditionalProperties));
}
if (kubeconfig.getUsers() != null) {
- kubeconfig.getUsers().stream()
- .filter(u -> u.getAdditionalProperties() != null)
- .forEach(u -> u.getAdditionalProperties().remove(KUBERNETES_CONFIG_FILE_KEY));
+ kubeconfig.getUsers().forEach(c -> removeAdditionalProperties(c::getAdditionalProperties));
}
Files.writeString(kubeConfigPath.toPath(), Serialization.asYaml(kubeconfig));
}
- public static File getFileFromContext(NamedContext namedContext) {
- return namedContext != null && namedContext.getAdditionalProperties() != null
- && namedContext.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY) instanceof File
- ? (File) namedContext.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY)
- : null;
+ /**
+ * Returns the file containing the context information if it was loaded using KubeConfigUtils#parseConfig.
+ *
+ * @param namedContext the context to get the file from.
+ * @return the file containing the context information if it was loaded using KubeConfigUtils#parseConfig or null.
+ */
+ public static File getFileWithNamedContext(NamedContext namedContext) {
+ return getFile(namedContext != null ? namedContext::getAdditionalProperties : null, KUBERNETES_CONFIG_CONTEXT_FILE_KEY);
+ }
+
+ /**
+ * Returns the file containing the cluster information if it was loaded using KubeConfigUtils#parseConfig.
+ *
+ * @param namedContext the context to get the file from.
+ * @return the file containing the cluster information if it was loaded using KubeConfigUtils#parseConfig or null.
+ */
+ public static File getFileWithNamedCluster(NamedContext namedContext) {
+ return getFile(namedContext != null ? namedContext::getAdditionalProperties : null, KUBERNETES_CONFIG_CLUSTER_FILE_KEY);
+ }
+
+ /**
+ * Returns the file containing the auth info information if it was loaded using KubeConfigUtils#parseConfig.
+ *
+ * @param namedContext the context to get the file from.
+ * @return the file containing the auth info information if it was loaded using KubeConfigUtils#parseConfig or null.
+ */
+ public static File getFileWithNamedAuthInfo(NamedContext namedContext) {
+ return getFile(namedContext != null ? namedContext::getAdditionalProperties : null, KUBERNETES_CONFIG_AUTH_INFO_FILE_KEY);
+ }
+
+ private static File getFileWithNamedCluster(NamedCluster namedCluster) {
+ return getFile(namedCluster != null ? namedCluster::getAdditionalProperties : null, KUBERNETES_CONFIG_CLUSTER_FILE_KEY);
}
- public static File getFileFromAuthInfo(NamedAuthInfo namedAuthInfo) {
- return namedAuthInfo != null && namedAuthInfo.getAdditionalProperties() != null
- && namedAuthInfo.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY) instanceof File
- ? (File) namedAuthInfo.getAdditionalProperties().get(KUBERNETES_CONFIG_FILE_KEY)
- : null;
+ private static File getFileWithNamedAuthInfo(NamedAuthInfo namedAuthInfo) {
+ return getFile(namedAuthInfo != null ? namedAuthInfo::getAdditionalProperties : null, KUBERNETES_CONFIG_AUTH_INFO_FILE_KEY);
+ }
+
+ private static File getFile(Supplier