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

Commit a876a55

Browse files
authored
feat: able to specify api server flags (#46)
1 parent ed306d4 commit a876a55

File tree

5 files changed

+75
-20
lines changed

5 files changed

+75
-20
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.javaoperatorsdk.jenvtest;
22

3+
import java.util.List;
34
import java.util.Optional;
45

56
public class KubeAPIServerConfig {
@@ -20,10 +21,19 @@ public class KubeAPIServerConfig {
2021
*/
2122
private final boolean offlineMode;
2223

23-
KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean offlineMode) {
24+
/**
25+
* Flags to pass to Kube API Server on startup. Key and value are two separated items, like
26+
* specifying min-request-timeout needs to add in order two values: "--min-request-timeout" and
27+
* "300" for the actual desired value.
28+
*/
29+
private final List<String> apiServerFlags;
30+
31+
KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean offlineMode,
32+
List<String> apiServerFlags) {
2433
this.jenvtestDir = jenvtestDir;
2534
this.apiServerVersion = apiServerVersion;
2635
this.offlineMode = offlineMode;
36+
this.apiServerFlags = apiServerFlags;
2737
}
2838

2939
public String getJenvtestDir() {
@@ -37,4 +47,8 @@ public Optional<String> getApiServerVersion() {
3747
public boolean isOfflineMode() {
3848
return offlineMode;
3949
}
50+
51+
public List<String> getApiServerFlags() {
52+
return apiServerFlags;
53+
}
4054
}

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

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

33
import java.io.File;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
public final class KubeAPIServerConfigBuilder {
68

@@ -13,6 +15,7 @@ public final class KubeAPIServerConfigBuilder {
1315
private String jenvtestDir;
1416
private String apiServerVersion;
1517
private Boolean offlineMode;
18+
private final List<String> apiServerFlags = new ArrayList<>(0);
1619

1720
public KubeAPIServerConfigBuilder() {}
1821

@@ -58,6 +61,28 @@ public KubeAPIServerConfig build() {
5861
this.apiServerVersion = apiServerVersionEnvVar;
5962
}
6063
}
61-
return new KubeAPIServerConfig(jenvtestDir, apiServerVersion, offlineMode);
64+
return new KubeAPIServerConfig(jenvtestDir, apiServerVersion, offlineMode, apiServerFlags);
65+
}
66+
67+
public void withApiServerFlags(List<String> flags) {
68+
apiServerFlags.addAll(flags);
69+
}
70+
71+
public void withApiServerFlag(String key, String value) {
72+
checkKeyPrefix(key);
73+
apiServerFlags.add(key);
74+
apiServerFlags.add(value);
75+
}
76+
77+
public void withApiServerFlag(String key) {
78+
checkKeyPrefix(key);
79+
apiServerFlags.add(key);
80+
}
81+
82+
private void checkKeyPrefix(String key) {
83+
if (!key.startsWith("--")) {
84+
throw new JenvtestException(
85+
"Kube API Server flag needs to start with double dash: '--'; Instead found key: " + key);
86+
}
6287
}
6388
}

core/src/main/java/io/javaoperatorsdk/jenvtest/junit/EnableKubeAPIServer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
*/
2121
String kubeAPIVersion() default NOT_SET;
2222

23+
String[] apiServerFlags() default {};
2324
}

core/src/main/java/io/javaoperatorsdk/jenvtest/junit/KubeAPIServerExtension.java

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

33
import java.lang.reflect.AnnotatedElement;
44
import java.util.Arrays;
5+
import java.util.List;
56
import java.util.Optional;
67
import java.util.stream.Collectors;
78

@@ -46,11 +47,11 @@ public void afterEach(ExtensionContext extensionContext) {
4647
private void startIfAnnotationPresent(ExtensionContext extensionContext) {
4748
extensionContext.getElement().ifPresent(ae -> {
4849
var annotation = getExtensionAnnotationInstance(ae);
49-
annotation.ifPresent(a -> startApiServer(extensionContext, a));
50+
annotation.ifPresent(this::startApiServer);
5051
});
5152
}
5253

53-
private void startApiServer(ExtensionContext context, EnableKubeAPIServer annotation) {
54+
private void startApiServer(EnableKubeAPIServer annotation) {
5455
kubeApiServer = new KubeAPIServer(annotationToConfig(annotation));
5556
kubeApiServer.start();
5657
}
@@ -69,6 +70,9 @@ private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation) {
6970
if (!NOT_SET.equals(version)) {
7071
builder.withApiServerVersion(version);
7172
}
73+
if (annotation.apiServerFlags().length > 0) {
74+
builder.withApiServerFlags(List.of(annotation.apiServerFlags()));
75+
}
7276
return builder.build();
7377
}
7478

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

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

3+
import java.io.File;
34
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.List;
47
import java.util.Scanner;
58
import java.util.concurrent.atomic.AtomicBoolean;
69

@@ -39,21 +42,9 @@ public int startApiServer(int etcdPort) {
3942
throw new JenvtestException(
4043
"Missing binary for API Server on path: " + apiServerBinary.getAbsolutePath());
4144
}
42-
var port = Utils.findFreePort();
43-
apiServerProcess = new ProcessBuilder(apiServerBinary.getAbsolutePath(),
44-
"--cert-dir", config.getJenvtestDir(),
45-
"--secure-port", "" + port,
46-
"--etcd-servers", "http://0.0.0.0:" + etcdPort,
47-
"--authorization-mode", "RBAC",
48-
"--service-account-issuer", "https://localhost",
49-
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
50-
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
51-
"--service-account-key-file", certManager.getAPIServerKeyPath(),
52-
"--service-account-issuer", certManager.getAPIServerCertPath(),
53-
"--disable-admission-plugins", "ServiceAccount",
54-
"--client-ca-file", certManager.getClientCertPath(),
55-
"--service-cluster-ip-range", "10.0.0.0/24",
56-
"--allow-privileged")
45+
var apiServerPort = Utils.findFreePort();
46+
var command = createCommand(apiServerBinary, apiServerPort, etcdPort);
47+
apiServerProcess = new ProcessBuilder(command)
5748
.start();
5849
Utils.redirectProcessOutputToLogger(apiServerProcess.getInputStream(), apiLog);
5950
Utils.redirectProcessOutputToLogger(apiServerProcess.getErrorStream(), apiLog);
@@ -66,12 +57,32 @@ public int startApiServer(int etcdPort) {
6657
return null;
6758
});
6859
log.debug("API Server started");
69-
return port;
60+
return apiServerPort;
7061
} catch (IOException e) {
7162
throw new JenvtestException(e);
7263
}
7364
}
7465

66+
private List<String> createCommand(File apiServerBinary, int apiServerPort, int etcdPort) {
67+
var command = new ArrayList<String>();
68+
command.add(apiServerBinary.getAbsolutePath());
69+
command.addAll(config.getApiServerFlags());
70+
command.addAll(List.of("--cert-dir", config.getJenvtestDir(),
71+
"--secure-port", "" + apiServerPort,
72+
"--etcd-servers", "http://0.0.0.0:" + etcdPort,
73+
"--authorization-mode", "RBAC",
74+
"--service-account-issuer", "https://localhost",
75+
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
76+
"--service-account-signing-key-file", certManager.getAPIServerKeyPath(),
77+
"--service-account-key-file", certManager.getAPIServerKeyPath(),
78+
"--service-account-issuer", certManager.getAPIServerCertPath(),
79+
"--disable-admission-plugins", "ServiceAccount",
80+
"--client-ca-file", certManager.getClientCertPath(),
81+
"--service-cluster-ip-range", "10.0.0.0/24",
82+
"--allow-privileged"));
83+
return command;
84+
}
85+
7586
public void waitUntilDefaultNamespaceCreated() {
7687
try {
7788
AtomicBoolean started = new AtomicBoolean(false);

0 commit comments

Comments
 (0)