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

Commit 9ef9f59

Browse files
committed
working test and waiting for startup with kubectl
1 parent 73eebbb commit 9ef9f59

File tree

2 files changed

+29
-35
lines changed

2 files changed

+29
-35
lines changed

src/main/java/com/csviri/kubeapi/APIServer.java

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,19 @@
66

77
import java.io.*;
88
import java.util.Scanner;
9+
import java.util.concurrent.atomic.AtomicBoolean;
910

1011
public class APIServer {
1112

1213
private static final Logger log = LoggerFactory.getLogger(APIServer.class);
1314
public static final int STARTUP_TIMEOUT = 10_000;
1415

1516
private final APIServerConfig config;
16-
private BinaryManager binaryManager;
17-
private CertManager certManager;
18-
private KubeConfigManager kubeConfigManager;
17+
private final BinaryManager binaryManager;
18+
private final CertManager certManager;
19+
private final KubeConfigManager kubeConfigManager;
1920
private Process etcdProcess;
2021
private volatile Process apiServerProcess;
21-
private Thread startupWaiter;
22-
private volatile boolean startedUpProperly;
2322

2423
public APIServer() {
2524
this(new APIServerConfig());
@@ -39,7 +38,7 @@ public void start() {
3938
startEtcd();
4039
startApiServer();
4140
kubeConfigManager.updateKubeConfig();
42-
waitUntilDefaultNamespaceCreated();
41+
waitUntilDefaultNamespaceCreatedWithK();
4342
log.info("API Server ready to use");
4443
}
4544

@@ -58,6 +57,7 @@ public void stop() {
5857
stopEtcd();
5958
kubeConfigManager.cleanupFromKubeConfig();
6059
cleanEtcdData();
60+
log.debug("Fully stopped.");
6161
}
6262

6363
private void stopApiServer() {
@@ -82,13 +82,30 @@ private void stopEtcd() {
8282
log.debug("etcd stopped");
8383
}
8484

85-
private void waitUntilDefaultNamespaceCreated() {
85+
private void waitUntilDefaultNamespaceCreatedWithK() {
8686
try {
87-
startupWaiter.join(STARTUP_TIMEOUT);
88-
if (!startedUpProperly) {
89-
throw new KubeApiException("Something went wrong starting up KubeApi server. Check the logs");
87+
AtomicBoolean started = new AtomicBoolean(false);
88+
var proc = new ProcessBuilder("kubectl","get","ns","--watch").start();
89+
var procWaiter = new Thread(() -> {
90+
try(Scanner sc = new Scanner(proc.getInputStream())){
91+
while (sc.hasNextLine()) {
92+
String line = sc.nextLine();
93+
if (line.contains("default")) {
94+
started.set(true);
95+
return;
96+
}
97+
}
9098
}
99+
});
100+
procWaiter.start();
101+
procWaiter.join(APIServer.STARTUP_TIMEOUT);
102+
if (!started.get()) {
103+
throw new KubeApiException("API Server did not start properly. Check the log files.");
104+
}
105+
} catch (IOException e) {
106+
throw new KubeApiException(e);
91107
} catch (InterruptedException e) {
108+
Thread.currentThread().interrupt();
92109
throw new KubeApiException(e);
93110
}
94111
}
@@ -139,32 +156,9 @@ private void startApiServer() {
139156
"--allow-privileged"
140157
)
141158
.start();
142-
143-
addStartupReadyHandler();
144-
// todo detect premature termination
145-
// apiServerProcess.onExit()
146159
log.debug("API Server started");
147160
} catch (IOException e) {
148161
throw new RuntimeException(e);
149162
}
150163
}
151-
152-
private void addStartupReadyHandler() {
153-
// alternative would be to use health checks? https://kubernetes.io/docs/reference/using-api/health-checks/
154-
// waits until fully started, otherwise default namespace might be missing
155-
this.startupWaiter = new Thread(() -> {
156-
// todo the scanner is not closed
157-
Scanner sc = new Scanner(apiServerProcess.getErrorStream());
158-
while (sc.hasNextLine()) {
159-
String line = sc.nextLine();
160-
// if (line.contains("Caches are synced")) {
161-
if (line.contains("all system priority classes are created successfully or already exist")) {
162-
startedUpProperly = true;
163-
return;
164-
}
165-
}
166-
});
167-
this.startupWaiter.start();
168-
}
169-
170164
}

src/test/java/com/csviri/kubeapi/JUnitExtensionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import static org.assertj.core.api.Assertions.assertThat;
1313

1414
@EnableAPIServer
15-
public class JUnitExtensionTest {
15+
class JUnitExtensionTest {
1616

1717
@Test
18-
public void testCommunication() {
18+
void testCommunication() {
1919
var client = new KubernetesClientBuilder().build();
2020
client.resource(configMap()).createOrReplace();
2121
var cm = client.resource(configMap()).get();

0 commit comments

Comments
 (0)