Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Commit 92a333a

Browse files
authored
feat: restore previous current context (#63)
When API Server stops will restore the previous context to be active.
1 parent 87e00ed commit 92a333a

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void stop() {
5555
log.debug("Stopping");
5656
kubeApiServerProcess.stopApiServer();
5757
etcdProcess.stopEtcd();
58-
kubeConfig.cleanupFromKubeConfig();
58+
kubeConfig.restoreKubeConfig();
5959
etcdProcess.cleanEtcdData();
6060
log.debug("Stopped");
6161
}

core/src/main/java/io/javaoperatorsdk/jenvtest/kubeconfig/KubeConfig.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.IOException;
44
import java.io.InputStream;
5+
import java.nio.charset.Charset;
56
import java.nio.charset.StandardCharsets;
67
import java.text.MessageFormat;
78
import java.util.ArrayList;
@@ -22,6 +23,7 @@ public class KubeConfig {
2223

2324
private final CertManager certManager;
2425
private final BinaryManager binaryManager;
26+
private String previousCurrentContext;
2527

2628
public KubeConfig(CertManager certManager, BinaryManager binaryManager) {
2729
this.certManager = certManager;
@@ -30,6 +32,7 @@ public KubeConfig(CertManager certManager, BinaryManager binaryManager) {
3032

3133
public void updateKubeConfig(int apiServerPort) {
3234
log.debug("Updating kubeconfig");
35+
previousCurrentContext = execWithKubectlConfigAndWait("current-context").trim();
3336
execWithKubectlConfigAndWait("set-cluster", JENVTEST,
3437
"--server=https://127.0.0.1:" + apiServerPort,
3538
"--certificate-authority=" + certManager.getAPIServerCertPath());
@@ -41,12 +44,15 @@ public void updateKubeConfig(int apiServerPort) {
4144
execWithKubectlConfigAndWait("use-context", JENVTEST);
4245
}
4346

44-
public void cleanupFromKubeConfig() {
47+
public void restoreKubeConfig() {
4548
log.debug("Cleanig up kubeconfig");
4649
unset("contexts." + JENVTEST);
4750
unset("clusters." + JENVTEST);
4851
unset("users." + JENVTEST);
4952
unset("current-context");
53+
if (previousCurrentContext != null && !previousCurrentContext.isEmpty()) {
54+
execWithKubectlConfigAndWait("use-context", previousCurrentContext);
55+
}
5056
}
5157

5258
private void unset(String target) {
@@ -65,14 +71,19 @@ public String generateKubeConfigYaml(int apiServerPort) {
6571
}
6672
}
6773

68-
private void execWithKubectlConfigAndWait(String... arguments) {
74+
private String execWithKubectlConfigAndWait(String... arguments) {
6975
try {
7076
List<String> args = new ArrayList<>(arguments.length + 2);
7177
args.add(binaryManager.binaries().getKubectl().getPath());
7278
args.add("config");
7379
args.addAll(List.of(arguments));
7480
var process = new ProcessBuilder(args).start();
81+
String stdout;
82+
try (InputStream is = process.getInputStream()) {
83+
stdout = IOUtils.toString(is, Charset.defaultCharset());
84+
}
7585
process.waitFor();
86+
return stdout;
7687
} catch (IOException e) {
7788
throw new JenvtestException(e);
7889
} catch (InterruptedException e) {

core/src/test/java/io/javaoperatorsdk/jenvtest/kubeconfig/KubeConfigTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class KubeConfigTest {
1414
public static final String CLIENT_KEY_PATH = "~/.jenvtest/clientcertkey";
1515
public static final String CLIENT_CERT_PATH = "~/.jenvtest/clientcertpath";
1616
public static final int API_SERVER_PORT = 32101;
17+
1718
CertManager certManagerMock = mock(CertManager.class);
1819
KubeConfig kubeConfig = new KubeConfig(certManagerMock, null);
1920

0 commit comments

Comments
 (0)