根据CRD的yaml文件生成java的models类
首先这里生成的类对应kubernetes-client/java
参考:https://github.com/kubernetes-client/java/blob/master/docs/generate-model-from-third-party-resources.md?spm=a2c6h.12873639.article-detail.14.54981476uqaym8&file=generate-model-from-third-party-resources.md
两种方式
1.通过 Github Action 远程生成
具体可参考https://blog.csdn.net/wsdlll/article/details/125385113
本人未尝试成功,有童鞋实现后帮忙分享下
2.在本地环境中生成
-
拉取代码生成器的镜像
docker pull ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 -
运行代码生成器的镜像文件
基本上,代码生成器容器会本地 docker 守护进程上自动配置一个 kubernetes 集群并将您的 CRD 应用到集群上来工作。在验证所有 CRD 都已正确安装后,会下载 OpenAPI 模式并生成一个最小的 Java 项目,其中包含为您的 CRD 生成的类模型。
要使代码生成器容器工作,您需要将
docker.sock和相应的主机路径挂载到容器,以便它可以连接到 docker 服务并将生成的项目保存到您的主机路径。还有一些配置项可以调整代码生成行为:-u: <CRD's download URL or file path, use it multiple times to read multiple CRDs> -n: <the target CRD group name, which is in the reverse order of ".spec.group"> -p: <output package name for the generated java classes> -o: <output path of the generated project>注意
-n 此处填写的是CRD group name,倒序目录,例如group name为 kubeovn.io,则此处填写io.kubeovn
示例:
为如下yaml(kube-ovn网络插件对应的ips crd)生成对应的java类模型
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: ips.kubeovn.io
spec:
group: kubeovn.io
versions:
- name: v1
served: true
storage: true
additionalPrinterColumns:
- name: V4IP
type: string
jsonPath: .spec.v4IpAddress
- name: V6IP
type: string
jsonPath: .spec.v6IpAddress
- name: Mac
type: string
jsonPath: .spec.macAddress
- name: Node
type: string
jsonPath: .spec.nodeName
- name: Subnet
type: string
jsonPath: .spec.subnet
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
podName:
type: string
namespace:
type: string
subnet:
type: string
attachSubnets:
type: array
items:
type: string
nodeName:
type: string
ipAddress:
type: string
v4IpAddress:
type: string
v6IpAddress:
type: string
attachIps:
type: array
items:
type: string
macAddress:
type: string
attachMacs:
type: array
items:
type: string
containerID:
type: string
podType:
type: string
scope: Cluster
names:
plural: ips
singular: ip
kind: IP
shortNames:
- ip
-
首先,确保 CRD yaml 存在于你的本地主机路径中,在以下示例中,我们手动将清单下载到 directory
/tmp/crds///这里拷贝至 /tmp/ctds [root@xxx crds]# pwd /tmp/crds [root@xxx crds]# ls -lrht total 48K -rw-r--r-- 1 root root 1.3K Jan 9 17:19 crontab-crd.yaml -rw-r--r-- 1 root root 31K Jan 11 11:26 crd.yaml -rw-r--r-- 1 root root 5.9K Jan 11 12:47 subnet.yaml -rw-r--r-- 1 root root 1.8K Jan 11 14:26 ips.yaml -
执行如下命令生成
LOCAL_MANIFEST_FILE=/tmp/crds/ips.yaml mkdir -p /tmp/java && cd /tmp/java docker run \ --rm \ -v "$LOCAL_MANIFEST_FILE":"$LOCAL_MANIFEST_FILE" \ -v /var/run/docker.sock:/var/run/docker.sock \ -v "$(pwd)":"$(pwd)" \ -ti \ --network host \ ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 \ /generate.sh \ -u $LOCAL_MANIFEST_FILE \ -n io.kubeovn \ #ips的group name为kubeovn.io,所以这里设置的值为io.kubeovn -p io.kubeovn \ -o "$(pwd)"3.执行步骤2后,会去生成java类
docker run \ > --rm \ > -v "$LOCAL_MANIFEST_FILE":"$LOCAL_MANIFEST_FILE" \ > -v /var/run/docker.sock:/var/run/docker.sock \ > -v "$(pwd)":"$(pwd)" \ > -ti \ > --network host \ > ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 \ > /generate.sh \ > -u $LOCAL_MANIFEST_FILE \ > -n io.kubeovn \ #ips的group name为kubeovn.io,所以这里设置的值为io.kubeovn -p io.kubeovn \ -o "$(pwd)" -p io.kubeovn \ -o "$(pwd)"Creating cluster "kind" ... ✓ Ensuring node image (kindest/node:v1.21.1) ✓ Preparing nodes ✓ Writing configuration ✓ Starting control-plane ️ ✓ Installing CNI ✓ Installing StorageClass Set kubectl context to "kind-kind" You can now use your cluster with: kubectl cluster-info --context kind-kind Have a nice day! customresourcedefinition.apiextensions.k8s.io/ips.kubeovn.io created Verifying CRD installation. ....省略 ) [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [INFO] Environment variable JAVA_POST_PROCESS_FILE not defined so the Java code may not be properly formatted. To define it, try 'export JAVA_POST_PROCESS_FILE="/usr/local/bin/clang-format -i"' (Linux/Mac) [INFO] NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI). [INFO] Processing operation readIP [INFO] Processing operation replaceIP [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [INFO] Processing operation deleteIP [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [INFO] Processing operation patchIP [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json-patch+json) [INFO] Processing operation listIP [INFO] Processing operation createIP [WARNING] Multiple schemas found in the OAS 'content' section, returning only the first one (application/json) [INFO] Processing operation deleteCollectionIP [INFO] 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/] [INFO] 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/] [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ObjectMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [WARNING] #/components/schemas/v1.ListMeta is not defined [INFO] writing file /output_dir/src/main/java/io/kubeovn/models/V1IP.java [INFO] Skipped /output_dir/docs/V1IP.md (Skipped by modelDocs options supplied by user.) [INFO] writing file /output_dir/src/main/java/io/kubeovn/models/V1IPList.java [INFO] Skipped /output_dir/docs/V1IPList.md (Skipped by modelDocs options supplied by user.) [INFO] writing file /output_dir/src/main/java/io/kubeovn/models/V1IPSpec.java [INFO] Skipped /output_dir/docs/V1IPSpec.md (Skipped by modelDocs options supplied by user.) [INFO] Skipping generation of APIs. [INFO] 'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/] [INFO] writing file /output_dir/pom.xml [INFO] writing file /output_dir/README.md [INFO] writing file /output_dir/build.gradle [INFO] writing file /output_dir/build.sbt [INFO] writing file /output_dir/settings.gradle [INFO] writing file /output_dir/gradle.properties [INFO] writing file /output_dir/src/main/AndroidManifest.xml [INFO] writing file /output_dir/.travis.yml [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ApiClient.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ServerConfiguration.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ServerVariable.java [INFO] writing file /output_dir/api/openapi.yaml [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/StringUtil.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/auth/HttpBasicAuth.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/auth/HttpBearerAuth.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/auth/ApiKeyAuth.java [INFO] writing file /output_dir/gradlew [INFO] writing file /output_dir/gradlew.bat [INFO] writing file /output_dir/gradle/wrapper/gradle-wrapper.properties [INFO] writing file /output_dir/gradle/wrapper/gradle-wrapper.jar [INFO] writing file /output_dir/git_push.sh [INFO] writing file /output_dir/.gitignore [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ApiException.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/Configuration.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/Pair.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/auth/Authentication.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ApiCallback.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ApiResponse.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/JSON.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ProgressRequestBody.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/ProgressResponseBody.java [INFO] writing file /output_dir/src/main/java/io/kubernetes/client/openapi/GzipRequestInterceptor.java [INFO] writing file /output_dir/.openapi-generator-ignore [INFO] writing file /output_dir/.openapi-generator/VERSION [INFO] writing file /output_dir/.openapi-generator/FILES [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 17.814 s [INFO] Finished at: 2023-01-11T06:38:17Z [INFO] ------------------------------------------------------------------------ ---Done. ---Done. [root@xxx java]# 如上代表成功4.查看生成的model类
如下可以看到在指定的位置生成的类
[root@xxx models]# ls -R /tmp/java/ | grep V1IP V1IP.java V1IPList.java V1IPSpec.java




