8

Developing CRDs for Kubernetes, using VScode as an IDE. Want to provide autocompletion and Intellisense in IDE.

It needs a JSON schema to do so. I have a huge number of CRDs to support. I want to do it in an easy way to convert CRDs to JSON schema.

venkat7668
  • 2,657
  • 1
  • 22
  • 26
  • You could, in theory, extract the Open API schemas (AFAIK, a superset of JSON schemas) from a custom resource definition (`spec.versions[].schema.openAPIV3Schema`). https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation https://github.com/redhat-developer/vscode-yaml#associating-a-schema-in-the-yaml-file – ashu Feb 12 '22 at 10:36
  • There's also kubeval for CR validations, but it won't allow autocompletion. https://www.kubeval.com/#crds – ashu Feb 12 '22 at 10:38
  • Despite the fact that you were asking about a way to convert CRDs to json schema, I believe that answer from @nicovak covers the original intention for the question. Please consider accepting it as an answer to your question. – Ruslan Shupoval Apr 29 '23 at 18:13

2 Answers2

2

You can export the swagger definition (including your CRDs) of your Kubernetes server and then generate the json schema from the swagger export.

Create a proxy to your API server and export the swagger

kubectl proxy --port=8080
curl localhost:8080/openapi/v2 > k8s-swagger.json

Using openapi2jsonschema generate the json schemas

openapi2jsonschema -o "schemas" --kubernetes --stand-alone k8s-swagger.json
Lukas Eichler
  • 5,689
  • 1
  • 24
  • 43
2

You can have CRD IntelliSense thanks to vscode-kubernetes-tools extension. It will fetch CRD from the active cluster. Here is the relevant merged pull request for feature details.

nicovak
  • 116
  • 2
  • 4