Skip to content

Commit ffdbfef

Browse files
committed
publishes crd-model-gen images
1 parent fb27f2e commit ffdbfef

File tree

3 files changed

+60
-121
lines changed

3 files changed

+60
-121
lines changed

client-java-contrib/cert-manager/update.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
# This script generates the model classes from a released version of cert-manager CRDs
1717
# under src/main/java/io/cert/manager/models.
1818

19-
# build the crdgen image which includes all the toolchains needed for code generation
20-
docker build .. -t crdgen
21-
2219
# a crdgen container is run in a way that:
2320
# 1. it has access to the docker daemon on the host so that it is able to create sibling container on the host
2421
# 2. it runs on the host network so that it is able to communicate with the KinD cluster it launched on the host
@@ -28,7 +25,8 @@ docker run \
2825
-v "$(pwd)":"$(pwd)" \
2926
-ti \
3027
--network host \
31-
crdgen /generate.sh \
28+
docker.pkg.github.com/kubernetes-client/java/crd-model-gen:v1.0.0 \
29+
/generate.sh \
3230
-u https://github.com/jetstack/cert-manager/releases/download/v0.16.1/cert-manager.crds.yaml \
3331
-n io.cert-manager \
3432
-p io.cert.manager \

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

+48-117
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,66 @@
11
# Guide to generate Java codes from CustomResourceDefinition
22

3-
__TL;DR__: This document will be useful when you extend third-party resources into your kubernetes cluster e.g.
4-
[CustomResourceDefinition](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/)
5-
and try to program java to operate the extended APIs. The generation process requires your CRD to be defined with
6-
[structral-schema](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/#specifying-a-structural-schema).
7-
Alternatively, you can manually write your models for CRDs by implementing [KubernetesObject](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/common/KubernetesObject.java)
3+
__TL;DR__: This document shows you how to generate java class models from your CRD YAML manifests.
4+
Alternatively, without this automatic code-generation process, you can always manually write your
5+
models for CRDs by implementing [KubernetesObject](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/common/KubernetesObject.java)
86
and [KubernetesListObject](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/common/KubernetesListObject.java) interfaces.
97

8+
### Example Commands For remote CRD manifests
109

11-
### Steps
12-
13-
##### Download and checkout generator scripts
14-
15-
Download the scripts for code generation then checkout to the correct directory:
16-
17-
```
18-
git clone https://github.com/kubernetes-client/gen
19-
cd gen/openapi/
20-
```
21-
22-
And also the code generation requires accessing docker service on the host:
23-
24-
```
25-
docker ps
26-
```
27-
28-
##### Fetch openapi spec from the cluster
29-
30-
Run the following command to fetch openapi spec from your existing cluster above 1.15+ version (with CRD already deployed):
10+
Run the following commands and it will download and generate the java models for you. Note that
11+
a docker daemon service is required on the host.
3112

3213
```bash
33-
kubectl get --raw="/openapi/v2" > /tmp/swagger # or any other file-path
14+
mkdir -p /tmp/java && cd /tmp/java
15+
docker run \
16+
--rm \
17+
-v /var/run/docker.sock:/var/run/docker.sock \
18+
-v "$(pwd)":"$(pwd)" \
19+
-ti \
20+
--network host \
21+
docker.pkg.github.com/kubernetes-client/java/crd-model-gen:v1.0.0 \
22+
/generate.sh \
23+
-u https://gist.githubusercontent.com/yue9944882/266fee8e95c2f15a93778263633e72ed/raw/be12c13379eeed13d2532cb65da61fffb19ee3e7/crontab-crd.yaml \
24+
-n com.example.stable \
25+
-p com.example.stable \
26+
-o "$(pwd)"
3427
```
3528

36-
Note that if you can't find a 1.15+ cluster anywhere, consider run one locally by [kind](https://github.com/bsycorp/kind) then
37-
don't forget to deploy your CRDs into the local-run kubernetes cluster.
38-
3929

40-
##### Generate Java model from the downloaded openapi spec
41-
42-
Run the following commands and there will be a java sources generated under `/tmp/java`.
43-
44-
```
45-
bash java-crd-cmd.sh -o /tmp/java < /tmp/swagger
46-
```
30+
### Example Commands For local CRD manifests
4731

48-
By default, the package name of generated java classes will be `io.kubernetes.client`. If you want to override the package name, consider
49-
use `-p <package_name>` flag. Additionally, you can use `-l <length>` option to make the generator to trim the length of generated
50-
class name, the lower number generates shorter names. e.g. for a CRD named `crontabs.v1.example.io`, with `-l 1` the generated
51-
name will be `CronTab`, otherwise with `-l3` it will be `ExampleV1Crontab`. And also if you feel like only generating classes for a few CRDs
52-
instead of the whole lump of downloaded openapi specs, please use `-n` to filter out your target CRDs. For all supported flags, use `-h`
53-
for help info as is shown in the following:
54-
55-
```
56-
Usage: generate a java project using input openapi spec from stdin
57-
-n: the prefix of the target CRD's api group to generate.
58-
-p: the base package name of the generated java project.
59-
-o: output directory of the generated java project.
60-
-l: keep the n last segments for the generated class name.
61-
```
62-
63-
##### Best practice to manipulate Java models?
64-
65-
It's recommended to use [CustomObjectApi](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/apis/CustomObjectsApi.java)
66-
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.
67-
68-
### Hand-on exercise
69-
70-
71-
Now we make use of the example CRD provided by offical documents to generate a piece of java models:
72-
73-
1. Deploy CRD into the cluster
32+
First of all, you're required to download/place your CRD manifests into a single YAML file or under
33+
one directory. In the following example, we're manually downloading the manifests to directory
34+
`/tmp/crds/`. And similar to code-generation from the remote manifests, we need to mount the crd
35+
file/directory into the code-generation container to make it work.
7436

7537
```bash
76-
# Create the example CRD
77-
kubectl create -f -
78-
apiVersion: apiextensions.k8s.io/v1beta1
79-
kind: CustomResourceDefinition
80-
metadata:
81-
# name must match the spec fields below, and be in the form: <plural>.<group>
82-
name: crontabs.stable.example.com
83-
spec:
84-
# group name to use for REST API: /apis/<group>/<version>
85-
group: stable.example.com
86-
# list of versions supported by this CustomResourceDefinition
87-
versions:
88-
- name: v1
89-
# Each version can be enabled/disabled by Served flag.
90-
served: true
91-
# One and only one version must be marked as the storage version.
92-
storage: true
93-
# either Namespaced or Cluster
94-
scope: Namespaced
95-
names:
96-
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
97-
plural: crontabs
98-
# singular name to be used as an alias on the CLI and for display
99-
singular: crontab
100-
# kind is normally the CamelCased singular type. Your resource manifests use this.
101-
kind: CronTab
102-
# shortNames allow shorter string to match your resource on the CLI
103-
shortNames:
104-
- ct
105-
preserveUnknownFields: false
106-
validation:
107-
openAPIV3Schema:
108-
type: object
109-
properties:
110-
spec:
111-
type: object
112-
properties:
113-
cronSpec:
114-
type: string
115-
image:
116-
type: string
117-
replicas:
118-
type: integer
38+
# Downloading
39+
mkdir -p /tmp/crds && cd /tmp/crds
40+
wget https://gist.githubusercontent.com/yue9944882/266fee8e95c2f15a93778263633e72ed/raw/be12c13379eeed13d2532cb65da61fffb19ee3e7/crontab-crd.yaml
41+
42+
# Local generation
43+
LOCAL_MANIFEST_FILE=/tmp/crds/crontab-crd.yaml
44+
mkdir -p /tmp/java && cd /tmp/java
45+
docker run \
46+
--rm \
47+
-v "$LOCAL_MANIFEST_FILE":"$LOCAL_MANIFEST_FILE" \
48+
-v /var/run/docker.sock:/var/run/docker.sock \
49+
-v "$(pwd)":"$(pwd)" \
50+
-ti \
51+
--network host \
52+
docker.pkg.github.com/kubernetes-client/java/crd-model-gen:v1.0.0 \
53+
/generate.sh \
54+
-u $LOCAL_MANIFEST_FILE \
55+
-n com.example.stable \
56+
-p com.example.stable \
57+
-o "$(pwd)"
11958
```
12059

121-
2. Fetch openapi spec from server and dump it to /tmp/swagger
122-
123-
```bash
124-
kubectl get --raw="/openapi/v2" > /tmp/swagger
125-
```
12660

127-
3. Generate models under `/tmp/java` directory
128-
129-
```bash
130-
bash java-crd-cmd.sh -n com.example -p com.example -l 2 -o /tmp/java < /tmp/swagger
131-
```
61+
### Manipulate the generated models
13262

133-
after generation you will see bunch of generated model sources under the `/tmp/java`:
63+
After generation you will see bunch of generated model sources under the `/tmp/java`:
13464

13565
```bash
13666
ls -R /tmp/java/ | grep V1CronTab
@@ -154,7 +84,8 @@ customObjectsApi.createNamespacedCustomObject(
15484
"true"
15585
);
15686

157-
// alternatively use generic kubernetes api
87+
// alternatively use generic kubernetes api, the generic api is aimed to address the drawbacks
88+
// from the CustomObjectsApi.
15889
GenericKubernetesApi<V1CronTab, V1CronTabList> crontabClient =
15990
new GenericKubernetesApi<>(V1CronTab.class, V1CronTabList.class, "com.example.stable", "v1", "crontabs", apiClient);
16091
KubernetesApiResponse<V1CronTab> createResponse = crontabClient.create(crontab);
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
IMAGE_REPOSITORY=docker.pkg.github.com
4+
IMAGE_NAME=kubernetes-client/java/crd-model-gen
5+
IMAGE_VERSION=v1.0.0
6+
IMAGE=${IMAGE_REPOSITORY}/${IMAGE_NAME}:${IMAGE_VERSION}
7+
8+
docker build -t ${IMAGE} client-java-contrib/
9+
10+
docker push ${IMAGE}

0 commit comments

Comments
 (0)