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

Commit 4181a29

Browse files
authored
feat: find available ports for processes (#42)
1 parent 7811585 commit 4181a29

File tree

5 files changed

+25
-11
lines changed

5 files changed

+25
-11
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public void start() {
4040
binaryManager.initAndDownloadIfRequired();
4141
certManager.createCertificatesIfNeeded();
4242
etcdProcess.cleanEtcdData();
43-
etcdProcess.startEtcd();
44-
kubeApiServerProcess.startApiServer();
45-
kubeConfig.updateKubeConfig();
43+
var etcdPort = etcdProcess.startEtcd();
44+
var apiServerPort = kubeApiServerProcess.startApiServer(etcdPort);
45+
kubeConfig.updateKubeConfig(apiServerPort);
4646
kubeApiServerProcess.waitUntilDefaultNamespaceCreated();
4747
log.debug("API Server ready to use");
4848
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public KubeConfig(CertManager certManager, BinaryManager binaryManager) {
2222
this.binaryManager = binaryManager;
2323
}
2424

25-
public void updateKubeConfig() {
25+
public void updateKubeConfig(int apiServerPort) {
2626
log.debug("Updating kubeconfig");
27-
execWithKubectlConfigAndWait("set-cluster", JENVTEST, "--server=https://127.0.0.1:6443",
27+
execWithKubectlConfigAndWait("set-cluster", JENVTEST,
28+
"--server=https://127.0.0.1:" + apiServerPort,
2829
"--certificate-authority=" + certManager.getAPIServerCertPath());
2930
execWithKubectlConfigAndWait("set-credentials", JENVTEST,
3031
"--client-certificate=" + certManager.getClientCertPath(),

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.javaoperatorsdk.jenvtest;
22

3+
import java.io.IOException;
34
import java.io.InputStream;
5+
import java.net.ServerSocket;
46
import java.util.Comparator;
57
import java.util.List;
68
import java.util.Scanner;
@@ -51,4 +53,11 @@ public static String platformSuffix(OSInfo osInfo) {
5153
return "-" + osInfo.getOSName() + "-" + osInfo.getOSArch();
5254
}
5355

56+
public static int findFreePort() {
57+
try (ServerSocket socket = new ServerSocket(0)) {
58+
return socket.getLocalPort();
59+
} catch (IOException e) {
60+
}
61+
return -1;
62+
}
5463
}

core/src/main/java/io/javaoperatorsdk/jenvtest/process/EtcdProcess.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,17 @@ public EtcdProcess(BinaryManager binaryManager,
2929
this.processStopHandler = processStopHandler;
3030
}
3131

32-
public void startEtcd() {
32+
public int startEtcd() {
3333
var etcdBinary = binaryManager.binaries().getEtcd();
34+
var port = Utils.findFreePort();
3435
try {
3536
if (!etcdBinary.exists()) {
3637
throw new JenvtestException(
3738
"Missing binary for etcd on path: " + etcdBinary.getAbsolutePath());
3839
}
3940
etcdProcess = new ProcessBuilder(etcdBinary.getAbsolutePath(),
40-
"--listen-client-urls=http://0.0.0.0:2379",
41-
"--advertise-client-urls=http://0.0.0.0:2379")
41+
"--listen-client-urls=http://0.0.0.0:" + port,
42+
"--advertise-client-urls=http://0.0.0.0:" + port)
4243
.start();
4344
Utils.redirectProcessOutputToLogger(etcdProcess.getInputStream(), etcdLog);
4445
Utils.redirectProcessOutputToLogger(etcdProcess.getErrorStream(), etcdLog);
@@ -51,6 +52,7 @@ public void startEtcd() {
5152
return null;
5253
});
5354
log.debug("etcd started");
55+
return port;
5456
} catch (IOException e) {
5557
throw new JenvtestException(e);
5658
}

core/src/main/java/io/javaoperatorsdk/jenvtest/process/KubeAPIServerProcess.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ public KubeAPIServerProcess(CertManager certManager, BinaryManager binaryManager
3232
this.processStopHandler = processStopHandler;
3333
}
3434

35-
public void startApiServer() {
35+
public int startApiServer(int etcdPort) {
3636
var apiServerBinary = binaryManager.binaries().getApiServer();
3737
try {
3838
if (!apiServerBinary.exists()) {
3939
throw new JenvtestException(
4040
"Missing binary for API Server on path: " + apiServerBinary.getAbsolutePath());
4141
}
42-
42+
var port = Utils.findFreePort();
4343
apiServerProcess = new ProcessBuilder(apiServerBinary.getAbsolutePath(),
4444
"--cert-dir", config.getJenvtestDir(),
45-
"--etcd-servers", "http://0.0.0.0:2379",
45+
"--secure-port", "" + port,
46+
"--etcd-servers", "http://0.0.0.0:" + etcdPort,
4647
"--authorization-mode", "RBAC",
4748
"--service-account-issuer", "https://localhost",
4849
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
@@ -65,6 +66,7 @@ public void startApiServer() {
6566
return null;
6667
});
6768
log.debug("API Server started");
69+
return port;
6870
} catch (IOException e) {
6971
throw new JenvtestException(e);
7072
}

0 commit comments

Comments
 (0)