Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public void start() {
binaryManager.initAndDownloadIfRequired();
certManager.createCertificatesIfNeeded();
etcdProcess.cleanEtcdData();
etcdProcess.startEtcd();
kubeApiServerProcess.startApiServer();
kubeConfig.updateKubeConfig();
var etcdPort = etcdProcess.startEtcd();
var apiServerPort = kubeApiServerProcess.startApiServer(etcdPort);
kubeConfig.updateKubeConfig(apiServerPort);
kubeApiServerProcess.waitUntilDefaultNamespaceCreated();
log.debug("API Server ready to use");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public KubeConfig(CertManager certManager, BinaryManager binaryManager) {
this.binaryManager = binaryManager;
}

public void updateKubeConfig() {
public void updateKubeConfig(int apiServerPort) {
log.debug("Updating kubeconfig");
execWithKubectlConfigAndWait("set-cluster", JENVTEST, "--server=https://127.0.0.1:6443",
execWithKubectlConfigAndWait("set-cluster", JENVTEST,
"--server=https://127.0.0.1:" + apiServerPort,
"--certificate-authority=" + certManager.getAPIServerCertPath());
execWithKubectlConfigAndWait("set-credentials", JENVTEST,
"--client-certificate=" + certManager.getClientCertPath(),
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/io/javaoperatorsdk/jenvtest/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.javaoperatorsdk.jenvtest;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
Expand Down Expand Up @@ -51,4 +53,11 @@ public static String platformSuffix(OSInfo osInfo) {
return "-" + osInfo.getOSName() + "-" + osInfo.getOSArch();
}

public static int findFreePort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
} catch (IOException e) {
}
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,17 @@ public EtcdProcess(BinaryManager binaryManager,
this.processStopHandler = processStopHandler;
}

public void startEtcd() {
public int startEtcd() {
var etcdBinary = binaryManager.binaries().getEtcd();
var port = Utils.findFreePort();
try {
if (!etcdBinary.exists()) {
throw new JenvtestException(
"Missing binary for etcd on path: " + etcdBinary.getAbsolutePath());
}
etcdProcess = new ProcessBuilder(etcdBinary.getAbsolutePath(),
"--listen-client-urls=http://0.0.0.0:2379",
"--advertise-client-urls=http://0.0.0.0:2379")
"--listen-client-urls=http://0.0.0.0:" + port,
"--advertise-client-urls=http://0.0.0.0:" + port)
.start();
Utils.redirectProcessOutputToLogger(etcdProcess.getInputStream(), etcdLog);
Utils.redirectProcessOutputToLogger(etcdProcess.getErrorStream(), etcdLog);
Expand All @@ -51,6 +52,7 @@ public void startEtcd() {
return null;
});
log.debug("etcd started");
return port;
} catch (IOException e) {
throw new JenvtestException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ public KubeAPIServerProcess(CertManager certManager, BinaryManager binaryManager
this.processStopHandler = processStopHandler;
}

public void startApiServer() {
public int startApiServer(int etcdPort) {
var apiServerBinary = binaryManager.binaries().getApiServer();
try {
if (!apiServerBinary.exists()) {
throw new JenvtestException(
"Missing binary for API Server on path: " + apiServerBinary.getAbsolutePath());
}

var port = Utils.findFreePort();
apiServerProcess = new ProcessBuilder(apiServerBinary.getAbsolutePath(),
"--cert-dir", config.getJenvtestDir(),
"--etcd-servers", "http://0.0.0.0:2379",
"--secure-port", "" + port,
"--etcd-servers", "http://0.0.0.0:" + etcdPort,
"--authorization-mode", "RBAC",
"--service-account-issuer", "https://localhost",
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
Expand All @@ -65,6 +66,7 @@ public void startApiServer() {
return null;
});
log.debug("API Server started");
return port;
} catch (IOException e) {
throw new JenvtestException(e);
}
Expand Down