Skip to content

Commit

Permalink
allow to retrieve name of current (kubeconfig) context
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 4, 2023
1 parent d23d5af commit 5387662
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,32 @@ public static ToolsConfig loadToolsConfig(URL url) throws IOException {
public static NamedContext getCurrentContext() {
try {
Config config = loadKubeConfig();
return KubeConfigUtils.getCurrentContext(config);
return getCurrentContext(config);
} catch (IOException e) {
return null;
}
}

public static NamedContext getCurrentContext(Config config) {
if (config == null) {
return null;
}
return KubeConfigUtils.getCurrentContext(config);
}

public static String getCurrentContextName(Config config) {
String current = null;
NamedContext currentContext = getCurrentContext(config);
if (currentContext != null
&& currentContext.getName() != null) {
current = currentContext.getName();
}
return current;
}

public static String getCurrentContextName() {
try {
return getCurrentContextName(loadKubeConfig());
} catch (IOException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package com.redhat.devtools.intellij.common.utils;

import io.fabric8.kubernetes.api.model.AuthInfo;
import io.fabric8.kubernetes.api.model.AuthInfoBuilder;
import io.fabric8.kubernetes.api.model.AuthProviderConfig;
import io.fabric8.kubernetes.api.model.Context;
import io.fabric8.kubernetes.api.model.NamedAuthInfo;
Expand Down Expand Up @@ -488,6 +487,60 @@ public void kubeConfig_and_clientConfig_are_NOT_equal_if_kubeConfig_has_no_curre
assertThat(equal).isFalse();
}

@Test
public void getCurrentContext_should_return_current_context() {
// given
NamedContext currentContext = ctx3;
io.fabric8.kubernetes.api.model.Config kubeConfig = kubeConfig(
currentContext,
allContexts,
allUsers);
// when
NamedContext currentFoundInFile = ConfigHelper.getCurrentContext(kubeConfig);
// then
assertThat(currentFoundInFile).isEqualTo(currentContext);
}

@Test
public void getCurrentContext_should_return_null_if_there_is_no_current_context() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig = kubeConfig(
null,
allContexts,
allUsers);
// when
NamedContext currentFoundInFile = ConfigHelper.getCurrentContext(kubeConfig);
// then
assertThat(currentFoundInFile).isNull();
}

@Test
public void getCurrentContextName_should_return_name_of_current_context() {
// given
NamedContext currentContext = ctx3;
io.fabric8.kubernetes.api.model.Config kubeConfig = kubeConfig(
currentContext,
allContexts,
allUsers);
// when
String currentContextName = ConfigHelper.getCurrentContextName(kubeConfig);
// then
assertThat(currentContextName).isEqualTo(currentContext.getName());
}

@Test
public void getCurrentContextName_should_return_null_if_there_is_no_current_context() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig = kubeConfig(
null,
allContexts,
allUsers);
// when
String currentContext = ConfigHelper.getCurrentContextName(kubeConfig);
// then
assertThat(currentContext).isNull();
}

private static AuthInfo authInfo(String token, AuthProviderConfig config) {
AuthInfo authInfo = new AuthInfo();
authInfo.setAuthProvider(config);
Expand Down

0 comments on commit 5387662

Please sign in to comment.