Skip to content

Commit f6064e2

Browse files
authored
Merge pull request #981 from yue9944882/revise-crd-generation-sh
Revise CRD generation guides: uses generator script instead of running images
2 parents 6811dc3 + 97eb1ab commit f6064e2

File tree

1 file changed

+37
-15
lines changed

1 file changed

+37
-15
lines changed

docs/generate-model-from-third-party-resources.md

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ and try to program java to operate the extended APIs. The generation process req
88

99
### Steps
1010

11+
##### Download and checkout generator scripts
12+
13+
Download the scripts for code generation then checkout to the correct directory:
14+
15+
```
16+
git clone https://github.com/kubernetes-client/gen
17+
cd gen/openapi/
18+
```
19+
20+
And also the code generation requires accessing docker service on the host:
21+
22+
```
23+
docker ps
24+
```
1125

1226
##### Fetch openapi spec from the cluster
1327

@@ -23,26 +37,31 @@ don't forget to deploy your CRDs into the local-run kubernetes cluster.
2337

2438
##### Generate Java model from the downloaded openapi spec
2539

26-
Run the following command and there will be a java sources generated under `/tmp/java`.
40+
Run the following commands and there will be a java sources generated under `/tmp/java`.
2741

2842
```
29-
docker run -i --rm yue9944882/java-model-gen < /tmp/swagger | tar -xzf - -C /tmp/
43+
bash java-crd-cmd.sh -o /tmp/java < /tmp/swagger
3044
```
3145

32-
By default, the package-name of generated codes will be `io.kubernetes.client`. If you want to override the package-name, consider
46+
By default, the package name of generated java classes will be `io.kubernetes.client`. If you want to override the package name, consider
3347
use `-p <package_name>` flag. Additionally, you can use `-l <length>` option to make the generator to trim the length of generated
3448
class name, the lower number generates shorter names. e.g. for a CRD named `crontabs.v1.example.io`, with `-l 1` the generated
35-
name will be `CronTab`, otherwise with `-l3` it will be `ExampleV1Crontab`.
49+
name will be `CronTab`, otherwise with `-l3` it will be `ExampleV1Crontab`. And also if you feel like only generating classes for a few CRDs
50+
instead of the whole lump of downloaded openapi specs, please use `-n` to filter out your target CRDs. For all supported flags, use `-h`
51+
for help info as is shown in the following:
3652

3753
```
38-
docker run -i --rm yue9944882/java-model-gen -p com.example < /tmp/swagger | tar -xzf - -C /tmp/
54+
Usage: generate a java project using input openapi spec from stdin
55+
-n: the prefix of the target CRD's api group to generate.
56+
-p: the base package name of the generated java project.
57+
-o: output directory of the generated java project.
58+
-l: keep the n last segments for the generated class name.
3959
```
4060

41-
4261
##### Best practice to manipulate Java models?
4362

4463
It's recommended to use [CustomObjectApi](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/apis/CustomObjectsApi.java)
45-
to read/write the extended resources from/to the cluster.
64+
or [GenericKubernetesApi](https://github.com/kubernetes-client/java/blob/master/extended/src/main/java/io/kubernetes/client/extended/generic/GenericKubernetesApi.java) to read/write the extended resources from/to the cluster.
4665

4766
### Hand-on exercise
4867

@@ -106,23 +125,23 @@ kubectl get --raw="/openapi/v2" > /tmp/swagger
106125
3. Generate models under `/tmp/java` directory
107126

108127
```bash
109-
docker run -i --rm yue9944882/java-model-gen -p com.example < /tmp/swagger | tar -xzf - -C /tmp/
128+
bash java-crd-cmd.sh -n com.example -p com.example -l 2 -o /tmp/java < /tmp/swagger
110129
```
111130

112131
after generation you will see bunch of generated model sources under the `/tmp/java`:
113132

114133
```bash
115-
ls -R /tmp/java/ | grep ComExample
116-
ComExampleStableV1CronTab.java
117-
ComExampleStableV1CronTabList.java
118-
ComExampleStableV1CronTabSpec.java
134+
ls -R /tmp/java/ | grep V1CronTab
135+
V1CronTab.java
136+
V1CronTabList.java
137+
V1CronTabSpec.java
119138
```
120139

121140
you're now free to request the models from master apiserver now:
122141

123142
```java
124143
// request extended models
125-
ComExampleStableV1CronTab crontab = new ComExampleStableV1CronTab();
144+
V1CronTab crontab = new V1CronTab();
126145
CustomObjectsApi customObjectsApi = new CustomObjectsApi(apiClient);
127146
customObjectsApi.createNamespacedCustomObject(
128147
"com.example.stable",
@@ -132,10 +151,13 @@ customObjectsApi.createNamespacedCustomObject(
132151
crontab,
133152
"true"
134153
);
154+
155+
// alternatively use generic kubernetes api
156+
GenericKubernetesApi<V1CronTab, V1CronTabList> crontabClient =
157+
new GenericKubernetesApi<>(V1CronTab.class, V1CronTabList.class, "com.example.stable", "v1", "crontabs", apiClient);
158+
KubernetesApiResponse<V1CronTab> createResponse = crontabClient.create(crontab);
135159
```
136160

137161

138162

139163

140-
141-

0 commit comments

Comments
 (0)