Skip to content

Commit

Permalink
expose Config#getFileWithCurrentContext & #getFileWithContext to cons…
Browse files Browse the repository at this point in the history
…umers

Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Sep 17, 2024
1 parent a8b5f48 commit 818ee41
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<io.fabric8.kubernetes.api.model.Config> 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);
}
Expand Down Expand Up @@ -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<File> files) {
public void setKubeConfigFiles(List<KubeConfigFile> 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<KubeConfigFile> 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<KubeConfigFile> 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<File> 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()));
}
}

Expand All @@ -1809,7 +1841,7 @@ public void setKubeConfigFiles(List<KubeConfigFile> 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<File> getFiles() {
if (this.kubeConfigFiles == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamedContext> 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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ static Stream<Arguments> 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<Boolean>) (hasIt -> assertThat(hasIt).isTrue()))),
named("given config with authInfo", getTestKubeConfig()),
named("given existing name", "test/api-test-com:443"),
named("then return true", (Consumer<Boolean>) (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<Boolean>) (hasIt -> assertThat(hasIt).isFalse()))),
named("given config with authInfo", getTestKubeConfig()),
named("given missing authInfo name", "bogus"),
named("then return false", (Consumer<Boolean>) (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<Boolean>) (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<Boolean>) (hasIt -> assertThat(hasIt).isFalse()))));
}

private static Config getTestKubeConfig() {
Expand Down

0 comments on commit 818ee41

Please sign in to comment.