From 818ee41d911331f889e478bb8af369486f4c4e5f Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Fri, 6 Sep 2024 21:31:34 +0200 Subject: [PATCH] expose Config#getFileWithCurrentContext & #getFileWithContext to consumers Signed-off-by: Andre Dietisheim --- .../io/fabric8/kubernetes/client/Config.java | 62 ++++++++++++++----- .../client/internal/KubeConfigUtils.java | 27 ++++++-- .../client/internal/KubeConfigUtilsTest.java | 18 +++--- 3 files changed, 77 insertions(+), 30 deletions(-) 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 1f9dcf4ca60..0b3cddf53f5 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 @@ -1748,13 +1748,30 @@ public File getFile() { } public KubeConfigFile getFileWithAuthInfo(String name) { - if (Utils.isNullOrEmpty(name) - || Utils.isNullOrEmpty(getFiles())) { + if (Utils.isNullOrEmpty(name)) { + return null; + } + return getFirstKubeConfigFileMatching(config -> KubeConfigUtils.hasAuthInfoNamed(config, name)); + } + + public KubeConfigFile getFileWithContext(String name) { + if (Utils.isNullOrEmpty(name)) { + return null; + } + return getFirstKubeConfigFileMatching(config -> KubeConfigUtils.getContext(config, name) != null); + } + + public KubeConfigFile getFileWithCurrentContext() { + return getFirstKubeConfigFileMatching(config -> Utils.isNotNullOrEmpty(config.getCurrentContext())); + } + + private KubeConfigFile getFirstKubeConfigFileMatching(Predicate predicate) { + if (Utils.isNullOrEmpty(kubeConfigFiles)) { return null; } return kubeConfigFiles.stream() .filter(KubeConfigFile::isReadable) - .filter(entry -> KubeConfigUtils.hasAuthInfoNamed(entry.getConfig(), name)) + .filter(entry -> predicate.test(entry.getConfig())) .findFirst() .orElse(null); } @@ -1782,25 +1799,40 @@ public void setAdditionalProperty(String name, Object value) { this.additionalProperties.put(name, value); } - public void setFile(File file) { - setFiles(Collections.singletonList(file)); - } - - public void setFiles(List files) { + public void setKubeConfigFiles(List files) { if (Utils.isNullOrEmpty(files)) { this.kubeConfigFiles = Collections.emptyList(); } else { - this.kubeConfigFiles = files.stream() - .map(KubeConfigFile::new) - .collect(Collectors.toList()); + this.kubeConfigFiles = files; } } - public void setKubeConfigFiles(List files) { + /** + * Returns the kube config files that are used to configure this client. + * Returns the files that are listed in the KUBERNETES_KUBECONFIG_FILES env or system variables. + * Returns the default kube config file if it's not set'. + * + * @return the files and Configs that configure this client + */ + public List getKubeConfigFiles() { + if (this.kubeConfigFiles == null) { + return Collections.emptyList(); + } else { + return new ArrayList<>(kubeConfigFiles); + } + } + + public void setFile(File file) { + setFiles(Collections.singletonList(file)); + } + + public void setFiles(List files) { if (Utils.isNullOrEmpty(files)) { - this.kubeConfigFiles = Collections.emptyList(); + setKubeConfigFiles(Collections.emptyList()); } else { - this.kubeConfigFiles = files; + setKubeConfigFiles(files.stream() + .map(KubeConfigFile::new) + .collect(Collectors.toList())); } } @@ -1809,7 +1841,7 @@ public void setKubeConfigFiles(List files) { * Returns the files that are listed in the KUBERNETES_KUBECONFIG_FILES env or system variables. * Returns the default kube config file if it's not set'. * - * @return + * @return the files that configure this client */ public List getFiles() { if (this.kubeConfigFiles == null) { 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 315ad355159..db06894fd07 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 @@ -60,16 +60,31 @@ public static Config parseConfigFromString(String contents) { public static NamedContext getCurrentContext(Config config) { String contextName = config.getCurrentContext(); if (contextName != null) { + return getContext(config, contextName); + } + return null; + } + + /** + * Returns the {@link NamedContext} with the given name. + * Returns {@code null} otherwise + * + * @param config the config to search + * @param name the context name to match + * @return the context with the the given name + */ + public static NamedContext getContext(Config config, String name) { + NamedContext context = null; + if (config != null && name != null) { List contexts = config.getContexts(); if (contexts != null) { - for (NamedContext context : contexts) { - if (contextName.equals(context.getName())) { - return context; - } - } + context = contexts.stream() + .filter(toInspect -> name.equals(toInspect.getName())) + .findAny() + .orElse(null); } } - return null; + return context; } /** diff --git a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/KubeConfigUtilsTest.java b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/KubeConfigUtilsTest.java index 2748e63fc02..109480d0c38 100644 --- a/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/KubeConfigUtilsTest.java +++ b/kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/internal/KubeConfigUtilsTest.java @@ -157,19 +157,19 @@ static Stream hasAuthInfoNamed_arguments() { return Stream.of( // given config with authInfo, when getAuthInfoName with existing name, then should return true Arguments.of( - named("config with authInfo", getTestKubeConfig()), - named("existing name", "test/api-test-com:443"), - named("should return true", (Consumer) (hasIt -> assertThat(hasIt).isTrue()))), + named("given config with authInfo", getTestKubeConfig()), + named("given existing name", "test/api-test-com:443"), + named("then return true", (Consumer) (hasIt -> assertThat(hasIt).isTrue()))), // given config with authInfo, when getAuthInfoName with missing name, then should return false Arguments.of( - named("config with authInfo", getTestKubeConfig()), - named("missing authInfo name", "bogus"), - named("should return false", (Consumer) (hasIt -> assertThat(hasIt).isFalse()))), + named("given config with authInfo", getTestKubeConfig()), + named("given missing authInfo name", "bogus"), + named("then return false", (Consumer) (hasIt -> assertThat(hasIt).isFalse()))), // given config without authInfo, when getAuthInfoName with missing name, then should return false Arguments.of( - named("config without authInfo", new ConfigBuilder().build()), - named("missing authInfo name", "test/api-test-com:443"), - named("should return false", (Consumer) (hasIt -> assertThat(hasIt).isFalse())))); + named("given config without authInfo", new ConfigBuilder().build()), + named("given missing authInfo name", "test/api-test-com:443"), + named("then return false", (Consumer) (hasIt -> assertThat(hasIt).isFalse())))); } private static Config getTestKubeConfig() {