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

Commit c31ed04

Browse files
committed
docs: code snippets
1 parent 859293a commit c31ed04

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

README.md

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# jenvtest
22

3-
jenvtest makes it easy to implement integration tests with Kubernetes API Server in Java.
3+
jenvtest makes it easy to implement integration tests with Kubernetes API Server in Java.
44
Inspired by [envtest](https://book.kubebuilder.io/reference/envtest.html) in go.
55

6-
It runs the API Server binaries directly (without nodes and other components). Thus, only etcd and Kubernetes API Server.
6+
It runs the API Server binaries directly (without nodes and other components). Thus, only etcd and Kubernetes API
7+
Server.
78
Linux, Windows, Mac is supported.
89

910
Project is in early phases, heading towards mvp release.
1011

11-
## Usage
12+
## Usage
1213

1314
Include dependency:
1415

1516
```xml
1617
<dependency>
17-
<groupId>io.javaoperatorsdk</groupId>
18-
<artifactId>jenvtest</artifactId>
19-
<version>[version]</version>
20-
<scope>test</scope>
18+
<groupId>io.javaoperatorsdk</groupId>
19+
<artifactId>jenvtest</artifactId>
20+
<version>[version]</version>
21+
<scope>test</scope>
2122
</dependency>
2223
```
2324

2425
### In Unit Tests
2526

26-
See sample unit test [here](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/JUnitExtensionTest.java#L10-L10)
27+
See sample unit
28+
test [here](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/JUnitExtensionTest.java#L10-L10)
2729

2830
```java
29-
31+
3032
@EnableKubeAPIServer // Start/Stop Kube API Server in the background
3133
class JUnitExtensionTest {
3234

@@ -46,7 +48,7 @@ class JUnitExtensionTest {
4648
.withName("test1")
4749
.withNamespace("default")
4850
.build())
49-
.withData(Map.of("key","data"))
51+
.withData(Map.of("key", "data"))
5052
.build();
5153
}
5254

@@ -55,26 +57,61 @@ class JUnitExtensionTest {
5557

5658
### API
5759

58-
The underlying API can be used directly. See [KubeApiServer](https://github.com/java-operator-sdk/jenvtest/blob/main/core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServer.java#L47-L47)
60+
The underlying API can be used directly.
61+
See [KubeApiServer](https://github.com/java-operator-sdk/jenvtest/blob/main/core/src/main/java/io/javaoperatorsdk/jenvtest/KubeAPIServer.java#L47-L47)
62+
63+
See
64+
it's [usage in a test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java#L12-L35).
65+
66+
```java
67+
class KubeApiServerTest {
68+
69+
@Test
70+
void trivialCase() {
71+
testWithAPIServer(new KubeAPIServer());
72+
}
73+
74+
@Test
75+
void apiServerWithSpecificVersion() {
76+
testWithAPIServer(new KubeAPIServer(
77+
KubeAPIServerConfigBuilder.anAPIServerConfig()
78+
.withApiServerVersion("1.26.0")
79+
.build()));
80+
}
81+
82+
83+
void testWithAPIServer(KubeAPIServer kubeApi) {
84+
kubeApi.start();
85+
86+
var client = new KubernetesClientBuilder().build();
87+
client.resource(TestUtils.testConfigMap()).create();
88+
var cm = client.resource(TestUtils.testConfigMap()).get();
5989

60-
https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java#L12-L35
90+
Assertions.assertThat(cm).isNotNull();
91+
92+
kubeApi.stop();
93+
}
94+
}
95+
```
6196

6297
### Testing Mutation and Validation Webhooks
6398

64-
An additional benefits os running K8S API Server this way, is that it makes easy to test
65-
[Conversion Hooks](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#webhook-conversion)
99+
An additional benefits os running K8S API Server this way, is that it makes easy to test
100+
[Conversion Hooks](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definition-versioning/#webhook-conversion)
66101
and/or
67102
[Dynamic Admission Controllers](https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/)
68103

69-
In general using additional standard frameworks to implement webhookhooks is adviced, like [kubernetes-webooks-framework](https://github.com/java-operator-sdk/kubernetes-webooks-framework)
70-
with Quarkus or Spring. However, we demonstrate how it works in [this test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubernetesMutationHookHandlingTest.java#L53-L53)
104+
In general using additional standard frameworks to implement webhookhooks is adviced,
105+
like [kubernetes-webooks-framework](https://github.com/java-operator-sdk/kubernetes-webooks-framework)
106+
with Quarkus or Spring. However, we demonstrate how it works
107+
in [this test](https://github.com/java-operator-sdk/jenvtest/blob/main/samples/src/test/java/io/javaoperatorsdk/jenvtest/KubernetesMutationHookHandlingTest.java#L53-L53)
71108

72109
### How does it work
73110

74111
In the background Kubernetes and etcd (and kubectl) binaries are downloaded if not found locally.
75112

76113
All the certificates for the Kube API Server and for the client is generated. The client config file
77-
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.
114+
(`~/kube/config`) file is updated, to any client can be used to talk to the API Server.
78115

79116
#### Downloading binaries
80117

samples/src/test/java/io/javaoperatorsdk/jenvtest/KubeApiServerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,4 @@ void testWithAPIServer(KubeAPIServer kubeApi) {
3333

3434
kubeApi.stop();
3535
}
36-
37-
3836
}

0 commit comments

Comments
 (0)