kubernetes-client / java

Official Java client library for kubernetes
http://kubernetes.io/
Apache License 2.0
3.59k stars 1.92k forks source link

question: how to use crd-model-gen image to generate Java classes from CRD YAMLs with `x-kubernetes-validations`? #2769

Open davidxia opened 1 year ago

davidxia commented 1 year ago

Hi, I'm using ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 to generate Java classes from K8s CRD YAMLs. My YAML has x-kubernetes-validations. Currently I'm getting the following error.

  docker run \
    --rm \
    -v "$LOCAL_MANIFEST_FILE":"$LOCAL_MANIFEST_FILE" \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v "$(pwd)":"$(pwd)" \
    --network host \
    ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 \
    /generate.sh \
    -u "$LOCAL_MANIFEST_FILE" \
    -n com.example \
    -p com.example.foo \
    --validate=false \
    -o "$(pwd)/target/generated-sources"

error: error validating "/home/dxia/src/com.example/foo/foo-api/foo.yaml": error validating data: ValidationError(CustomResourceDefinition.spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.resources.properties.storage): unknown field "x-kubernetes-validations" in io.k8s.apiextensions-apiserver.pkg.apis.apiextensions.v1.JSONSchemaProps; if you choose to ignore these errors, turn validation off with --validate=false

I tried adding --validate=false to the docker run args, but it looks like the executable in the image only supports the following args based on this new error.

[INFO] --- exec-maven-plugin:3.0.0:exec (run-generate-java-script) @ workbench-api ---
find: ‘target/generated-sources/src/main/java/com/example/foo/api/models’: No such file or directory
/generate.sh: illegal option -- -
Usage: generate Java model classes from CRDs
 -n: the prefix of the target CRD's api group to generate.
 -p: the base package name of the generated java project. 
 -o: output directory of the generated java project. 
 -u: url location of the YAML manifest to install CRDs to a Kubernetes cluster. 
[ERROR] Command execution failed.

my questions

  1. Is crd-model-gen image meant to be public or internal to this repo only?
  2. If it's meant to be public, any way to support x-kubernetes-validations or to disable this validation?
  3. Any plans to support generating classes from CRD YAMLs with x-kubernetes-validations?
yue9944882 commented 1 year ago

Is crd-model-gen image meant to be public or internal to this repo only?

is it a public open image

If it's meant to be public, any way to support x-kubernetes-validation or to disable this validation?

looks like there's a typo in that property, it should be x-kubernetes-validations rather than singular form in your yaml. can you try that again?

Any plans to support generating classes from CRD YAMLs with x-kubernetes-validation?

at the moment, it's not in our roadmap. but we're definitely open to receive PR to add this piece. thanks!

davidxia commented 1 year ago

looks like there's a typo in that property

Oh I accidentally typed it wrong (updated previous messages), but I used the plural in my YAML and the error message shows the plural also doesn't work.

k8s-triage-robot commented 9 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

dsyer commented 9 months ago

/remove-lifecycle stale

This is still definitely an issue.

k8s-triage-robot commented 6 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

dsyer commented 6 months ago

/remove-lifecycle stale

This is still definitely an issue.

k8s-triage-robot commented 3 months ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

dsyer commented 3 months ago

/remove-lifecycle stale

No change afaik

code-weirdo commented 3 months ago

For future reference, I managed to find a workaround to get it working by launching a bash terminal instead of the generation script, then patching /generate.sh to include a --validate=false flag to the kubectl apply command.

$ docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v "$(pwd)":"$(pwd)" -it --network host ghcr.io/kubernetes-client/java/crd-model-gen:v1.0.6 bash
$ sed -i 's|kubectl apply -f "$url"|kubectl apply -f "$url" --validate=false|g' /generate.sh
$ /generate.sh ...

Hopefully this helps someone else that runs into this...

k8s-triage-robot commented 3 days ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

dsyer commented 3 days ago

/remove-lifecycle stale