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

Commit 68623c1

Browse files
authored
feat: automatic binary download (#10)
1 parent d5b6b5d commit 68623c1

21 files changed

+388
-172
lines changed

devnotes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
API Server bins:
3+
https://console.cloud.google.com/storage/browser/kubebuilder-tools
4+
25
bins:
36
https://github.com/etcd-io/etcd/releases/download/v3.4.24/etcd-v3.4.24-linux-amd64.tar.gz
47
https://dl.k8s.io/v1.26.1/bin/linux/amd64/kube-apiserver

pom.xml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,39 @@
2424
<maven.compiler.source>11</maven.compiler.source>
2525
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
2626
<maven-surefire-plugin.version>3.0.0-M9</maven-surefire-plugin.version>
27+
<commons-compress.version>1.22</commons-compress.version>
28+
<google.cloud.libraries.version>26.9.0</google.cloud.libraries.version>
29+
<commons-io.version>2.11.0</commons-io.version>
2730
</properties>
2831

32+
<dependencyManagement>
33+
<dependencies>
34+
<dependency>
35+
<groupId>com.google.cloud</groupId>
36+
<artifactId>libraries-bom</artifactId>
37+
<version>${google.cloud.libraries.version}</version>
38+
<type>pom</type>
39+
<scope>import</scope>
40+
</dependency>
41+
</dependencies>
42+
</dependencyManagement>
43+
2944
<dependencies>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-storage</artifactId>
48+
</dependency>
49+
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
50+
<dependency>
51+
<groupId>org.apache.commons</groupId>
52+
<artifactId>commons-compress</artifactId>
53+
<version>${commons-compress.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>commons-io</groupId>
57+
<artifactId>commons-io</artifactId>
58+
<version>${commons-io.version}</version>
59+
</dependency>
3060
<dependency>
3161
<groupId>org.awaitility</groupId>
3262
<artifactId>awaitility</artifactId>
@@ -72,14 +102,10 @@
72102
<version>${assertj.version}</version>
73103
<scope>test</scope>
74104
</dependency>
75-
<dependency>
76-
<groupId>commons-io</groupId>
77-
<artifactId>commons-io</artifactId>
78-
<version>2.11.0</version>
79-
</dependency>
80105
</dependencies>
81106

82107

108+
83109
<build>
84110
<plugins>
85111
<plugin>

src/main/java/com/csviri/kubeapi/APIServer.java renamed to src/main/java/com/csviri/jenvtest/APIServer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.csviri.kubeapi;
1+
package com.csviri.jenvtest;
22

3+
import com.csviri.jenvtest.binary.BinaryManager;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -24,7 +25,7 @@ public APIServer() {
2425

2526
public APIServer(APIServerConfig config) {
2627
this.config = config;
27-
this.binaryManager = new BinaryManager(config.getJenvtestDirectory(),config.getApiServerVersion());
28+
this.binaryManager = new BinaryManager(config);
2829
this.certManager = new CertManager(config.getJenvtestDirectory());
2930
this.kubeConfigManager = new KubeConfigManager(certManager,binaryManager);
3031
this.etcdProcessManager = new EtcdProcessManager(binaryManager,config);
@@ -33,6 +34,7 @@ public APIServer(APIServerConfig config) {
3334

3435
public void start() {
3536
log.debug("Stating API Server. Using jenvtest dir: {}", config.getJenvtestDirectory());
37+
binaryManager.initAndDownloadIfRequired();
3638
prepareLogDirectory();
3739
etcdProcessManager.cleanEtcdData();
3840
etcdProcessManager.startEtcd();

src/main/java/com/csviri/kubeapi/APIServerConfig.java renamed to src/main/java/com/csviri/jenvtest/APIServerConfig.java

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

33
import java.io.File;
4+
import java.util.Optional;
45

56
public class APIServerConfig {
67

@@ -13,10 +14,18 @@ public class APIServerConfig {
1314
private String jenvtestDir;
1415

1516
/**
16-
* Sample: 1.26.1, 1.25.0
17+
* If not set the latest binary will be selected automatically
18+
* Sample: 1.26.1, 1.25.0.
1719
*/
1820
private String apiServerVersion;
1921

22+
/**
23+
* If true, tries to download binaries. If the apiServerVersion is not set and some local binaries found
24+
* won't try to download them again.
25+
* */
26+
// todo config with env var
27+
private boolean downloadBinaries = true;
28+
2029
public APIServerConfig() {
2130
var jenvtestDirFromEnvVar = System.getenv(CONFIG_ROOT_ENV_VAR);
2231
if (jenvtestDirFromEnvVar != null) {
@@ -35,8 +44,8 @@ public APIServerConfig setJenvtestDir(String jenvtestDir) {
3544
return this;
3645
}
3746

38-
public String getApiServerVersion() {
39-
return apiServerVersion;
47+
public Optional<String> getApiServerVersion() {
48+
return Optional.ofNullable(apiServerVersion);
4049
}
4150

4251
public APIServerConfig setApiServerVersion(String apiServerVersion) {
@@ -47,4 +56,13 @@ public APIServerConfig setApiServerVersion(String apiServerVersion) {
4756
public String logDirectory() {
4857
return new File(jenvtestDir, "logs").getPath();
4958
}
59+
60+
public boolean getDownloadBinaries() {
61+
return downloadBinaries;
62+
}
63+
64+
public APIServerConfig setDownloadBinaries(boolean downloadBinaries) {
65+
this.downloadBinaries = downloadBinaries;
66+
return this;
67+
}
5068
}

src/main/java/com/csviri/kubeapi/APIServerProcessManager.java renamed to src/main/java/com/csviri/jenvtest/APIServerProcessManager.java

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

3+
import com.csviri.jenvtest.binary.BinaryManager;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -29,7 +30,7 @@ public void startApiServer() {
2930
var apiServerBinary = binaryManager.binaries().getApiServer();
3031
try {
3132
if (!apiServerBinary.exists()) {
32-
throw new KubeApiException("Missing binary for API Server on path: " + apiServerBinary.getAbsolutePath());
33+
throw new JenvtestException("Missing binary for API Server on path: " + apiServerBinary.getAbsolutePath());
3334
}
3435

3536
apiServerProcess = new ProcessBuilder(apiServerBinary.getAbsolutePath(),
@@ -49,7 +50,7 @@ public void startApiServer() {
4950
.start();
5051
apiServerProcess.onExit().thenApply(p-> {
5152
if (!stopped) {
52-
throw new KubeApiException("APIServer stopped unexpectedly.");
53+
throw new JenvtestException("APIServer stopped unexpectedly.");
5354
}
5455
return null;
5556
});
@@ -77,13 +78,13 @@ public void waitUntilDefaultNamespaceCreated() {
7778
procWaiter.start();
7879
procWaiter.join(APIServer.STARTUP_TIMEOUT);
7980
if (!started.get()) {
80-
throw new KubeApiException("API Server did not start properly. Check the log files.");
81+
throw new JenvtestException("API Server did not start properly. Check the log files.");
8182
}
8283
} catch (IOException e) {
83-
throw new KubeApiException(e);
84+
throw new JenvtestException(e);
8485
} catch (InterruptedException e) {
8586
Thread.currentThread().interrupt();
86-
throw new KubeApiException(e);
87+
throw new JenvtestException(e);
8788
}
8889
}
8990

src/main/java/com/csviri/kubeapi/CertManager.java renamed to src/main/java/com/csviri/jenvtest/CertManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.csviri.kubeapi;
1+
package com.csviri.jenvtest;
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
@@ -28,7 +28,7 @@ public void ensureAPIServerCertificates() {
2828
generateAPIServerCertificates();
2929
generateUserCertificates();
3030
} catch (IOException | InterruptedException e) {
31-
throw new KubeApiException(e);
31+
throw new JenvtestException(e);
3232
}
3333
}
3434

src/main/java/com/csviri/kubeapi/EtcdProcessManager.java renamed to src/main/java/com/csviri/jenvtest/EtcdProcessManager.java

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

3+
import com.csviri.jenvtest.binary.BinaryManager;
34
import org.apache.commons.io.FileUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -25,7 +26,7 @@ public void startEtcd() {
2526
var etcdBinary = binaryManager.binaries().getEtcd();
2627
try {
2728
if (!etcdBinary.exists()) {
28-
throw new KubeApiException("Missing binary for etcd on path: " + etcdBinary.getAbsolutePath());
29+
throw new JenvtestException("Missing binary for etcd on path: " + etcdBinary.getAbsolutePath());
2930
}
3031
var logsFile = new File(config.logDirectory(), "etcd.logs");
3132

@@ -36,23 +37,24 @@ public void startEtcd() {
3637
.redirectOutput(logsFile)
3738
.redirectError(logsFile)
3839
.start();
40+
// todo better stop handling - we should stop the whole app this case
3941
etcdProcess.onExit().thenApply(p-> {
4042
if (!stopped) {
41-
throw new KubeApiException("Etcd stopped unexpectedly");
43+
throw new JenvtestException("Etcd stopped unexpectedly");
4244
}
4345
return null;
4446
});
4547
log.debug("etcd started");
4648
} catch (IOException e) {
47-
throw new KubeApiException(e);
49+
throw new JenvtestException(e);
4850
}
4951
}
5052

5153
public void cleanEtcdData() {
5254
try {
5355
FileUtils.deleteDirectory(new File("default.etcd"));
5456
} catch (IOException e) {
55-
throw new KubeApiException(e);
57+
throw new JenvtestException(e);
5658
}
5759
}
5860

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.csviri.jenvtest;
2+
3+
public class JenvtestException extends RuntimeException{
4+
5+
public JenvtestException() {
6+
}
7+
8+
public JenvtestException(String message) {
9+
super(message);
10+
}
11+
12+
public JenvtestException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
public JenvtestException(Throwable cause) {
17+
super(cause);
18+
}
19+
20+
public JenvtestException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
21+
super(message, cause, enableSuppression, writableStackTrace);
22+
}
23+
}

src/main/java/com/csviri/kubeapi/KubeConfigManager.java renamed to src/main/java/com/csviri/jenvtest/KubeConfigManager.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
package com.csviri.kubeapi;
1+
package com.csviri.jenvtest;
22

3+
import com.csviri.jenvtest.binary.BinaryManager;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -51,10 +52,10 @@ private void execWithKubectlConfigAndWait(String... arguments) {
5152
var process = new ProcessBuilder(args).start();
5253
process.waitFor();
5354
} catch (IOException e) {
54-
throw new KubeApiException(e);
55+
throw new JenvtestException(e);
5556
} catch (InterruptedException e) {
5657
Thread.currentThread().interrupt();
57-
throw new KubeApiException(e);
58+
throw new JenvtestException(e);
5859
}
5960
}
6061
}

src/main/java/com/csviri/kubeapi/SemverUtil.java renamed to src/main/java/com/csviri/jenvtest/VersioningUtils.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
package com.csviri.kubeapi;
1+
package com.csviri.jenvtest;
22

33
import java.util.Comparator;
44
import java.util.List;
55

6-
public class SemverUtil {
6+
public class VersioningUtils {
77

8-
private static SemverComparator semverComparator = new SemverComparator();
8+
public static final SemverComparator SEMVER_COMPARATOR = new SemverComparator();
99

10+
public static String getOSName() {
11+
String os = System.getProperty("os.name").toLowerCase();
12+
if (os.contains("win")) {
13+
return "windows";
14+
} else {
15+
return os;
16+
}
17+
}
18+
19+
public static String getOSArch() {
20+
return System.getProperty("os.arch").toLowerCase();
21+
}
1022

11-
public static final String getLatestVersion(List<String> versions) {
12-
versions.sort(semverComparator);
23+
public static String getLatestVersion(List<String> versions) {
24+
versions.sort(SEMVER_COMPARATOR);
1325
return versions.get(versions.size()-1);
1426
}
1527

0 commit comments

Comments
 (0)