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

Commit 88467d3

Browse files
authored
feat: process logs and stop handling (#13)
1 parent 2ce0f02 commit 88467d3

File tree

10 files changed

+83
-49
lines changed

10 files changed

+83
-49
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>com.csviri</groupId>
7-
<artifactId>k8s-api-wrapper</artifactId>
6+
<groupId>com.csviri.jenvtest</groupId>
7+
<artifactId>jenvtest</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
99
<packaging>jar</packaging>
1010

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7-
import java.io.*;
8-
9-
public class APIServer {
7+
public class APIServer implements UnexpectedProcessStopHandler {
108

119
private static final Logger log = LoggerFactory.getLogger(APIServer.class);
1210

@@ -27,39 +25,34 @@ public APIServer(APIServerConfig config) {
2725
this.config = config;
2826
this.binaryManager = new BinaryManager(config);
2927
this.certManager = new CertManager(config.getJenvtestDirectory());
30-
this.kubeConfigManager = new KubeConfigManager(certManager,binaryManager);
31-
this.etcdProcessManager = new EtcdProcessManager(binaryManager,config);
32-
this.apiServerProcessManager = new APIServerProcessManager(certManager,binaryManager,config);
28+
this.kubeConfigManager = new KubeConfigManager(certManager, binaryManager);
29+
this.etcdProcessManager = new EtcdProcessManager(binaryManager, this);
30+
this.apiServerProcessManager = new APIServerProcessManager(certManager, binaryManager, this, config);
3331
}
3432

3533
public void start() {
3634
log.debug("Stating API Server. Using jenvtest dir: {}", config.getJenvtestDirectory());
3735
binaryManager.initAndDownloadIfRequired();
3836
certManager.createCertificatesIfNeeded();
39-
prepareLogDirectory();
4037
etcdProcessManager.cleanEtcdData();
4138
etcdProcessManager.startEtcd();
4239
apiServerProcessManager.startApiServer();
4340
kubeConfigManager.updateKubeConfig();
4441
apiServerProcessManager.waitUntilDefaultNamespaceCreated();
45-
log.info("API Server ready to use");
42+
log.debug("API Server ready to use");
4643
}
4744

4845
public void stop() {
46+
log.debug("Stopping");
4947
apiServerProcessManager.stopApiServer();
5048
etcdProcessManager.stopEtcd();
5149
kubeConfigManager.cleanupFromKubeConfig();
5250
etcdProcessManager.cleanEtcdData();
53-
log.debug("Fully stopped.");
51+
log.debug("Stopped");
5452
}
5553

56-
private void prepareLogDirectory() {
57-
var logDir = new File(config.logDirectory());
58-
if (!logDir.exists()) {
59-
var res = logDir.mkdirs();
60-
if (!res) {
61-
log.warn("Problem with creating log dir: {}", logDir.getPath());
62-
}
63-
}
54+
@Override
55+
public void processStopped(Process process) {
56+
stop();
6457
}
6558
}

src/main/java/com/csviri/jenvtest/APIServerProcessManager.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@
1111
public class APIServerProcessManager {
1212

1313
private static final Logger log = LoggerFactory.getLogger(APIServerProcessManager.class);
14+
private static final Logger apiLog = LoggerFactory.getLogger(APIServerProcessManager.class
15+
.getName()+".apiServerProcess");
1416

1517
private final CertManager certManager;
1618
private final BinaryManager binaryManager;
1719
private final APIServerConfig config;
1820
private volatile Process apiServerProcess;
1921
private volatile boolean stopped = false;
22+
private final UnexpectedProcessStopHandler processStopHandler;
2023

21-
public APIServerProcessManager(CertManager certManager, BinaryManager binaryManager,APIServerConfig config) {
24+
public APIServerProcessManager(CertManager certManager, BinaryManager binaryManager,
25+
UnexpectedProcessStopHandler processStopHandler,
26+
APIServerConfig config) {
2227
this.certManager = certManager;
2328
this.binaryManager = binaryManager;
2429
this.config = config;
30+
this.processStopHandler = processStopHandler;
2531
}
2632

27-
2833
public void startApiServer() {
2934
var apiServerBinary = binaryManager.binaries().getApiServer();
3035
try {
@@ -47,15 +52,19 @@ public void startApiServer() {
4752
"--allow-privileged"
4853
)
4954
.start();
55+
Utils.redirectProcessOutputToLogger(apiServerProcess.getInputStream(), apiLog);
56+
Utils.redirectProcessOutputToLogger(apiServerProcess.getErrorStream(), apiLog);
5057
apiServerProcess.onExit().thenApply(p-> {
5158
if (!stopped) {
52-
throw new JenvtestException("APIServer stopped unexpectedly.");
59+
stopped = true;
60+
log.error("API Server process stopped unexpectedly");
61+
this.processStopHandler.processStopped(p);
5362
}
5463
return null;
5564
});
5665
log.debug("API Server started");
5766
} catch (IOException e) {
58-
throw new RuntimeException(e);
67+
throw new JenvtestException(e);
5968
}
6069
}
6170

@@ -88,6 +97,9 @@ public void waitUntilDefaultNamespaceCreated() {
8897
}
8998

9099
public void stopApiServer() {
100+
if (stopped) {
101+
return;
102+
}
91103
stopped = true;
92104
if (apiServerProcess != null) {
93105
apiServerProcess.destroyForcibly();

src/main/java/com/csviri/jenvtest/EtcdProcessManager.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
public class EtcdProcessManager {
1212

1313
private static final Logger log = LoggerFactory.getLogger(EtcdProcessManager.class);
14+
private static final Logger etcdLog = LoggerFactory.getLogger(EtcdProcessManager.class.getName() + ".etcdProcess");
1415

1516
private final BinaryManager binaryManager;
16-
private final APIServerConfig config;
17+
1718
private volatile Process etcdProcess;
1819
private volatile boolean stopped = false;
20+
private final UnexpectedProcessStopHandler processStopHandler;
1921

20-
public EtcdProcessManager(BinaryManager binaryManager, APIServerConfig config) {
22+
public EtcdProcessManager(BinaryManager binaryManager, UnexpectedProcessStopHandler processStopHandler) {
2123
this.binaryManager = binaryManager;
22-
this.config = config;
24+
this.processStopHandler = processStopHandler;
2325
}
2426

2527
public void startEtcd() {
@@ -28,19 +30,17 @@ public void startEtcd() {
2830
if (!etcdBinary.exists()) {
2931
throw new JenvtestException("Missing binary for etcd on path: " + etcdBinary.getAbsolutePath());
3032
}
31-
var logsFile = new File(config.logDirectory(), "etcd.logs");
32-
3333
etcdProcess = new ProcessBuilder(etcdBinary.getAbsolutePath(),
3434
"--listen-client-urls=http://0.0.0.0:2379",
3535
"--advertise-client-urls=http://0.0.0.0:2379")
36-
// todo log to a different logger on debug level
37-
.redirectOutput(logsFile)
38-
.redirectError(logsFile)
3936
.start();
40-
// todo better stop handling - we should stop the whole app this case
41-
etcdProcess.onExit().thenApply(p-> {
37+
Utils.redirectProcessOutputToLogger(etcdProcess.getInputStream(), etcdLog);
38+
Utils.redirectProcessOutputToLogger(etcdProcess.getErrorStream(), etcdLog);
39+
etcdProcess.onExit().thenApply(p -> {
4240
if (!stopped) {
43-
throw new JenvtestException("Etcd stopped unexpectedly");
41+
stopped = true;
42+
log.error("etcd process stopped unexpectedly");
43+
processStopHandler.processStopped(p);
4444
}
4545
return null;
4646
});
@@ -59,6 +59,9 @@ public void cleanEtcdData() {
5959
}
6060

6161
public void stopEtcd() {
62+
if (stopped) {
63+
return;
64+
}
6265
stopped = true;
6366
if (etcdProcess != null) {
6467
etcdProcess.destroy();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.csviri.jenvtest;
2+
3+
public interface UnexpectedProcessStopHandler {
4+
5+
void processStopped(Process process);
6+
7+
}

src/main/java/com/csviri/jenvtest/VersioningUtils.java renamed to src/main/java/com/csviri/jenvtest/Utils.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package com.csviri.jenvtest;
22

3+
import org.slf4j.Logger;
4+
5+
import java.io.InputStream;
6+
import java.io.OutputStream;
37
import java.util.Comparator;
48
import java.util.List;
9+
import java.util.Scanner;
10+
11+
public class Utils {
512

6-
public class VersioningUtils {
13+
private Utils() {
14+
}
715

816
public static final SemverComparator SEMVER_COMPARATOR = new SemverComparator();
917

@@ -45,4 +53,13 @@ public int compare(String v1, String v2) {
4553
}
4654
}
4755

56+
public static void redirectProcessOutputToLogger(InputStream outputStream, Logger logger) {
57+
new Thread(() -> {
58+
Scanner sc = new Scanner(outputStream);
59+
while (sc.hasNextLine()) {
60+
logger.trace( sc.nextLine());
61+
}
62+
}).start();
63+
}
64+
4865
}

src/main/java/com/csviri/jenvtest/binary/BinaryDownloader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.csviri.jenvtest.binary;
22

33
import com.csviri.jenvtest.JenvtestException;
4-
import com.csviri.jenvtest.VersioningUtils;
4+
import com.csviri.jenvtest.Utils;
55
import com.google.cloud.storage.Storage;
66
import com.google.cloud.storage.StorageOptions;
77
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
@@ -35,8 +35,9 @@ public BinaryDownloader(String jenvtestDir) {
3535

3636
public File download(String version) {
3737
try {
38+
log.info("Downloading binaries with version: {}", version);
3839
String url = "https://storage.googleapis.com/kubebuilder-tools/kubebuilder-tools-" + version +
39-
"-" + VersioningUtils.getOSName() + "-" + VersioningUtils.getOSArch() + ".tar.gz";
40+
"-" + Utils.getOSName() + "-" + Utils.getOSArch() + ".tar.gz";
4041

4142
File tempFile = File.createTempFile("kubebuilder-tools", ".tar.gz");
4243
log.debug("Downloading binary from url: {} to Temp file: {}", url, tempFile.getPath());
@@ -98,24 +99,23 @@ private File createDirForBinaries(String version) {
9899

99100
public File downloadLatest() {
100101
String latest = findLatestVersion();
101-
log.info("Downloading latest version binaries: {}", latest);
102102
return download(latest);
103103
}
104104

105105
private String findLatestVersion() {
106106
Storage storage = StorageOptions.getDefaultInstance().getService();
107107
var blobs = storage.get(BUCKET_NAME).list();
108108
var allRelevantVersions = StreamSupport.stream(blobs.iterateAll().spliterator(), false).filter(b ->
109-
b.asBlobInfo().getName().contains(VersioningUtils.getOSName())
110-
&& b.asBlobInfo().getName().contains(VersioningUtils.getOSArch()))
109+
b.asBlobInfo().getName().contains(Utils.getOSName())
110+
&& b.asBlobInfo().getName().contains(Utils.getOSArch()))
111111
.map(b -> {
112112
String stripped = b.asBlobInfo().getName().replace(TAR_PREFIX, "");
113113
String version = stripped.substring(0, stripped.indexOf("-"));
114114
if (version.startsWith("v")) {
115115
version = version.substring(1);
116116
}
117117
return version;
118-
}).sorted(VersioningUtils.SEMVER_COMPARATOR).collect(Collectors.toList());
118+
}).sorted(Utils.SEMVER_COMPARATOR).collect(Collectors.toList());
119119
if (allRelevantVersions.isEmpty()) {
120120
throw new JenvtestException("Cannot find relevant version to download");
121121
}

src/main/java/com/csviri/jenvtest/binary/BinaryManager.java

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

33
import com.csviri.jenvtest.APIServerConfig;
44
import com.csviri.jenvtest.JenvtestException;
5-
import com.csviri.jenvtest.VersioningUtils;
5+
import com.csviri.jenvtest.Utils;
66

77
import java.io.File;
88
import java.util.List;
@@ -14,13 +14,15 @@
1414
public class BinaryManager {
1515

1616
public static final String BINARY_LIST_DIR = "k8s";
17-
public static final String PLATFORM_SUFFIX = "-" + VersioningUtils.getOSName() + "-" + VersioningUtils.getOSArch();
17+
public static final String PLATFORM_SUFFIX = "-" + Utils.getOSName() + "-" + Utils.getOSArch();
1818

1919
private Binaries binaries;
2020
private final APIServerConfig config;
21+
BinaryDownloader downloader;
2122

2223
public BinaryManager(APIServerConfig config) {
2324
this.config = config;
25+
downloader = new BinaryDownloader(config.getJenvtestDirectory());
2426
}
2527

2628
public void initAndDownloadIfRequired() {
@@ -31,7 +33,7 @@ public void initAndDownloadIfRequired() {
3133
if (!config.getDownloadBinaries()) {
3234
throw new JenvtestException("Binaries cannot be found, and download is turned off");
3335
}
34-
BinaryDownloader downloader = new BinaryDownloader(config.getJenvtestDirectory());
36+
3537
binaryDir = config.getApiServerVersion().isEmpty() ?
3638
downloader.downloadLatest()
3739
: downloader.download(config.getApiServerVersion().get());
@@ -78,7 +80,7 @@ private Optional<File> findLatestBinariesAvailable() {
7880
if (dirVersionList.isEmpty()) {
7981
return Optional.empty();
8082
}
81-
String latest = VersioningUtils.getLatestVersion(dirVersionList) + PLATFORM_SUFFIX;
83+
String latest = Utils.getLatestVersion(dirVersionList) + PLATFORM_SUFFIX;
8284
return Optional.of(new File(binariesListDir, latest));
8385
}
8486

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class JUnitExtensionTest {
1919
void testCommunication() {
2020
var client = new KubernetesClientBuilder().build();
2121
client.resource(testConfigMap()).createOrReplace();
22-
var cm = client.resource(configMap()).get();
22+
var cm = client.resource(TestUtils.testConfigMap()).get();
2323

2424
assertThat(cm).isNotNull();
2525
}

src/test/java/com/csviri/jenvtest/VersioningUtilsTest.java renamed to src/test/java/com/csviri/jenvtest/UtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import static org.assertj.core.api.Assertions.assertThat;
99

10-
class VersioningUtilsTest {
10+
class UtilsTest {
1111

1212
@Test
1313
void getsLatestVersion() {
14-
assertThat(VersioningUtils.getLatestVersion(new ArrayList<>(List.of("1.22.4", "1.26.3", "1.26.1", "1.11.2"))))
14+
assertThat(Utils.getLatestVersion(new ArrayList<>(List.of("1.22.4", "1.26.3", "1.26.1", "1.11.2"))))
1515
.isEqualTo("1.26.3");
16-
assertThat(VersioningUtils.getLatestVersion(new ArrayList<>(List.of("1.22", "1.23.1", "1.24"))))
16+
assertThat(Utils.getLatestVersion(new ArrayList<>(List.of("1.22", "1.23.1", "1.24"))))
1717
.isEqualTo("1.24");
1818
}
1919
}

0 commit comments

Comments
 (0)