|
| 1 | +# Guide to generate Java codes from CustomResourceDefinition |
| 2 | + |
| 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 | + |
| 8 | + |
| 9 | +### Steps |
| 10 | + |
| 11 | + |
| 12 | +##### Fetch openapi spec from the cluster |
| 13 | + |
| 14 | +Run the following command to fetch openapi spec from your existing cluster above 1.15+ version (with CRD already deployed): |
| 15 | + |
| 16 | +```bash |
| 17 | +kubectl get --raw="/openapi/v2" > /tmp/swagger # or any other file-path |
| 18 | +``` |
| 19 | + |
| 20 | +Note that if you can't find a 1.15+ cluster anywhere, consider run one locally by [kind](https://github.com/bsycorp/kind) then |
| 21 | +don't forget to deploy your CRDs into the local-run kubernetes cluster. |
| 22 | + |
| 23 | + |
| 24 | +##### Generate Java model from the downloaded openapi spec |
| 25 | + |
| 26 | +Run the following command and there will be a java sources generated under `/tmp/java`. |
| 27 | + |
| 28 | +``` |
| 29 | +docker run -i --rm yue9944882/java-model-gen < /tmp/swagger | tar -xzf - -C /tmp/ |
| 30 | +``` |
| 31 | + |
| 32 | +By default, the package-name of generated codes will be `io.kubernetes.client`. If you want to override the package-name, consider |
| 33 | +use `-p <package_name>` flag. |
| 34 | + |
| 35 | +``` |
| 36 | +docker run -i --rm yue9944882/java-model-gen -p com.example < /tmp/swagger | tar -xzf - -C /tmp/ |
| 37 | +``` |
| 38 | + |
| 39 | + |
| 40 | +##### Best practice to manipulate Java models? |
| 41 | + |
| 42 | +It's recommended to use [CustomObjectApi](https://github.com/kubernetes-client/java/blob/master/kubernetes/src/main/java/io/kubernetes/client/apis/CustomObjectsApi.java) |
| 43 | +to read/write the extended resources from/to the cluster. |
| 44 | + |
| 45 | +### Hand-on exercise |
| 46 | + |
| 47 | + |
| 48 | +Now we make use of the example CRD provided by offical documents to generate a piece of java models: |
| 49 | + |
| 50 | +1. Deploy CRD into the cluster |
| 51 | + |
| 52 | +```bash |
| 53 | +# Create the example CRD |
| 54 | +kubectl create -f - |
| 55 | +apiVersion: apiextensions.k8s.io/v1beta1 |
| 56 | +kind: CustomResourceDefinition |
| 57 | +metadata: |
| 58 | + # name must match the spec fields below, and be in the form: <plural>.<group> |
| 59 | + name: crontabs.stable.example.com |
| 60 | +spec: |
| 61 | + # group name to use for REST API: /apis/<group>/<version> |
| 62 | + group: stable.example.com |
| 63 | + # list of versions supported by this CustomResourceDefinition |
| 64 | + versions: |
| 65 | + - name: v1 |
| 66 | + # Each version can be enabled/disabled by Served flag. |
| 67 | + served: true |
| 68 | + # One and only one version must be marked as the storage version. |
| 69 | + storage: true |
| 70 | + # either Namespaced or Cluster |
| 71 | + scope: Namespaced |
| 72 | + names: |
| 73 | + # plural name to be used in the URL: /apis/<group>/<version>/<plural> |
| 74 | + plural: crontabs |
| 75 | + # singular name to be used as an alias on the CLI and for display |
| 76 | + singular: crontab |
| 77 | + # kind is normally the CamelCased singular type. Your resource manifests use this. |
| 78 | + kind: CronTab |
| 79 | + # shortNames allow shorter string to match your resource on the CLI |
| 80 | + shortNames: |
| 81 | + - ct |
| 82 | + preserveUnknownFields: false |
| 83 | + validation: |
| 84 | + openAPIV3Schema: |
| 85 | + type: object |
| 86 | + properties: |
| 87 | + spec: |
| 88 | + type: object |
| 89 | + properties: |
| 90 | + cronSpec: |
| 91 | + type: string |
| 92 | + image: |
| 93 | + type: string |
| 94 | + replicas: |
| 95 | + type: integer |
| 96 | +``` |
| 97 | + |
| 98 | +2. Fetch openapi spec from server and dump it to /tmp/swagger |
| 99 | + |
| 100 | +```bash |
| 101 | +kubectl get --raw="/openapi/v2" > /tmp/swagger |
| 102 | +``` |
| 103 | + |
| 104 | +3. Generate models under `/tmp/java` directory |
| 105 | + |
| 106 | +```bash |
| 107 | +docker run -i --rm yue9944882/java-model-gen -p com.example < /tmp/swagger | tar -xzf - -C /tmp/ |
| 108 | +``` |
| 109 | + |
| 110 | +after generation you will see bunch of generated model sources under the `/tmp/java`: |
| 111 | + |
| 112 | +```bash |
| 113 | +ls -R /tmp/java/ | grep ComExample |
| 114 | +ComExampleStableV1CronTab.java |
| 115 | +ComExampleStableV1CronTabList.java |
| 116 | +ComExampleStableV1CronTabSpec.java |
| 117 | +``` |
| 118 | + |
| 119 | +you're now free to request the models from master apiserver now: |
| 120 | + |
| 121 | +```java |
| 122 | +// request extended models |
| 123 | +ComExampleStableV1CronTab crontab = new ComExampleStableV1CronTab(); |
| 124 | +CustomObjectsApi customObjectsApi = new CustomObjectsApi(apiClient); |
| 125 | +customObjectsApi.createNamespacedCustomObject( |
| 126 | + "com.example.stable", |
| 127 | + "v1", |
| 128 | + namespace, |
| 129 | + "crontabs", |
| 130 | + crontab, |
| 131 | + "true" |
| 132 | +); |
| 133 | +``` |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments