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

Commit b30df6c

Browse files
authored
feat: kube config not updated by default, flag to turn it on in annotation (#75)
1 parent 95e67f5 commit b30df6c

File tree

8 files changed

+70
-32
lines changed

8 files changed

+70
-32
lines changed

README.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,20 +128,3 @@ In general, it is a best practice to use additional standard frameworks to imple
128128
like [kubernetes-webooks-framework](https://github.com/java-operator-sdk/kubernetes-webooks-framework)
129129
with Quarkus or Spring. However, we demonstrate how it works
130130
in [this test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubernetesMutationHookHandlingTest.java#L53-L53)
131-
132-
### How does it work
133-
134-
In the background Kubernetes and etcd (and kubectl) binaries are downloaded if not found locally.
135-
136-
All the certificates for the Kube API Server and for the client is generated. The client config file
137-
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.
138-
139-
#### Downloading binaries
140-
141-
Binaries are downloaded automatically under ~/.jenvtest/k8s/[target-platform-and-version] if no binary found locally.
142-
If there are multiple binaries found, the latest if selected (unless a target version is not specified).
143-
144-
Also [`setup-envtest`](https://pkg.go.dev/sigs.k8s.io/controller-runtime/tools/setup-envtest#section-readme) can be used
145-
to download binaries manually. By executing `setup-envtest use --bin-dir ~/.jenvtest` will download the latest required
146-
binaries to the default directory. This is useful if always running the tests in offline mode.
147-

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class KubeAPIServerConfig {
3333
*/
3434
private final List<String> apiServerFlags;
3535

36+
/**
37+
* If kube config (in ~/kube/config ) file should be updated.
38+
*/
3639
private final boolean updateKubeConfig;
3740

3841
KubeAPIServerConfig(String jenvtestDir, String apiServerVersion, boolean offlineMode,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public final class KubeAPIServerConfigBuilder {
1515
private String jenvtestDir;
1616
private String apiServerVersion;
1717
private Boolean offlineMode;
18-
private boolean updateKubeConfig = true;
18+
private boolean updateKubeConfig = false;
1919
private final List<String> apiServerFlags = new ArrayList<>(0);
2020

2121
public KubeAPIServerConfigBuilder() {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@
2323
String kubeAPIVersion() default NOT_SET;
2424

2525
String[] apiServerFlags() default {};
26+
27+
boolean updateKubeConfigFile() default false;
2628
}

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,30 @@ private void initialize(ExtensionContext extensionContext, boolean staticContext
5858
.filter(h -> h.isTargetFieldAvailable(extensionContext, staticContext))
5959
.collect(Collectors.toList());
6060

61-
startIfAnnotationPresent(extensionContext, targetInjectors.isEmpty());
61+
startIfAnnotationPresent(extensionContext, !targetInjectors.isEmpty());
6262

6363
targetInjectors.forEach(i -> i.inject(extensionContext, staticContext, kubeApiServer));
6464
}
6565

6666
private void startIfAnnotationPresent(ExtensionContext extensionContext,
67-
boolean updateKubeConfig) {
67+
boolean willInjectClient) {
6868
extensionContext.getElement().ifPresent(ae -> {
6969
var annotation = getExtensionAnnotationInstance(ae);
70-
annotation.ifPresent(a -> startApiServer(a, updateKubeConfig));
70+
71+
annotation.ifPresent(a -> {
72+
if (!willInjectClient && !a.updateKubeConfigFile()) {
73+
log.warn(
74+
"Neither kube config file will be updated or client info will be injected into the test. "
75+
+
76+
"This is probably a miss configuration since server won't be easily accessible.");
77+
}
78+
startApiServer(a);
79+
});
7180
});
7281
}
7382

74-
private void startApiServer(EnableKubeAPIServer annotation, boolean updateKubeConfig) {
75-
kubeApiServer = new KubeAPIServer(annotationToConfig(annotation, updateKubeConfig));
83+
private void startApiServer(EnableKubeAPIServer annotation) {
84+
kubeApiServer = new KubeAPIServer(annotationToConfig(annotation));
7685
kubeApiServer.start();
7786
}
7887

@@ -84,8 +93,7 @@ private void stopIfAnnotationPresent(ExtensionContext extensionContext) {
8493
});
8594
}
8695

87-
private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation,
88-
boolean updateKubeConfig) {
96+
private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation) {
8997
var builder = KubeAPIServerConfigBuilder.anAPIServerConfig();
9098
var version = annotation.kubeAPIVersion();
9199
if (!NOT_SET.equals(version)) {
@@ -94,7 +102,8 @@ private KubeAPIServerConfig annotationToConfig(EnableKubeAPIServer annotation,
94102
if (annotation.apiServerFlags().length > 0) {
95103
builder.withApiServerFlags(List.of(annotation.apiServerFlags()));
96104
}
97-
builder.withUpdateKubeConfig(updateKubeConfig);
105+
106+
builder.withUpdateKubeConfig(annotation.updateKubeConfigFile());
98107
return builder.build();
99108
}
100109

core/src/test/java/io/javaoperatorsdk/jenvtest/sample/JUnitExtensionKubeConfigUpdateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import static io.javaoperatorsdk.jenvtest.sample.TestUtils.simpleTest;
88

9-
@EnableKubeAPIServer
9+
@EnableKubeAPIServer(updateKubeConfigFile = true)
1010
class JUnitExtensionKubeConfigUpdateTest {
1111

1212
@Test

core/src/test/java/io/javaoperatorsdk/jenvtest/sample/KubeApiServerTest.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.Test;
44

55
import io.fabric8.kubernetes.client.Config;
6+
import io.fabric8.kubernetes.client.KubernetesClient;
67
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
78
import io.javaoperatorsdk.jenvtest.KubeAPIServer;
89
import io.javaoperatorsdk.jenvtest.KubeAPIServerConfigBuilder;
@@ -25,14 +26,27 @@ void apiServerWithSpecificVersion() {
2526
.build()));
2627
}
2728

29+
@Test
30+
void usingKubeConfigFileToInitClient() {
31+
var kubeApi = new KubeAPIServer(KubeAPIServerConfigBuilder.anAPIServerConfig()
32+
.withUpdateKubeConfig(true)
33+
.build());
34+
kubeApi.start();
35+
36+
var client = new KubernetesClientBuilder().build();
37+
38+
TestUtils.simpleTest(client);
39+
}
40+
41+
2842
@Test
2943
void usingWildcardVersion() {
3044
var kubeApi = new KubeAPIServer(KubeAPIServerConfigBuilder.anAPIServerConfig()
3145
.withApiServerVersion("1.26.*")
3246
.build());
3347
kubeApi.start();
3448

35-
var client = new KubernetesClientBuilder().build();
49+
var client = createClient(kubeApi.getKubeConfigYaml());
3650
TestUtils.simpleTest(client);
3751
assertThat(client.getKubernetesVersion().getMinor()).isEqualTo("26");
3852

@@ -46,17 +60,21 @@ void creatingClientFromConfigString() {
4660
.build());
4761
kubeApi.start();
4862

49-
var client =
50-
new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeApi.getKubeConfigYaml()))
51-
.build();
63+
var client = createClient(kubeApi.getKubeConfigYaml());
5264
TestUtils.simpleTest(client);
5365

5466
kubeApi.stop();
5567
}
5668

5769
void testWithAPIServer(KubeAPIServer kubeApi) {
5870
kubeApi.start();
59-
simpleTest();
71+
var client = createClient(kubeApi.getKubeConfigYaml());
72+
simpleTest(client);
6073
kubeApi.stop();
6174
}
75+
76+
KubernetesClient createClient(String yaml) {
77+
return new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(yaml)).build();
78+
}
79+
6280
}

docs/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
## Configuration Options
4+
5+
6+
7+
8+
9+
## How does it work
10+
11+
In the background Kubernetes and etcd (and kubectl) binaries are downloaded if not found locally.
12+
13+
All the certificates for the Kube API Server and for the client is generated. The client config file
14+
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.
15+
16+
## Downloading binaries
17+
18+
Binaries are downloaded automatically under ~/.jenvtest/k8s/[target-platform-and-version] if no binary found locally.
19+
If there are multiple binaries found, the latest if selected (unless a target version is not specified).
20+
21+
Also [`setup-envtest`](https://pkg.go.dev/sigs.k8s.io/controller-runtime/tools/setup-envtest#section-readme) can be used
22+
to download binaries manually. By executing `setup-envtest use --bin-dir ~/.jenvtest` will download the latest required
23+
binaries to the default directory. This is useful if always running the tests in offline mode.

0 commit comments

Comments
 (0)