You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
8
6
and [KubernetesListObject](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/common/KubernetesListObject.java) interfaces.
9
7
8
+
### Example Commands For remote CRD manifests
10
9
11
-
### Steps
12
-
13
-
##### Download and checkout generator scripts
14
-
15
-
Download the scripts for code generation then checkout to the correct directory:
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
-
39
29
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
47
31
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.
74
36
75
37
```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
0 commit comments