How to generate OpenAPI spec file using kubebuilder

815 Views Asked by At

I am generating Kubernetes CRD (Custom Resource Definition) using kubebuilder. Along with CRD, I also need to document the REST endpoints by creating OpenAPI v3 Spec (OAS) file. Is there a way to get this done using kubebuilder? Also does kubebuilder allow us to add sample Request/Response payload details in the .go file so that the generated OAS file is more user friendly?

One option I found is to use curl :/openapi/v3, but maintaining the spec file manually is not sustainable. I want to use kubebuilder generated .go spec file as one source of truth. Any suggestion?

2

There are 2 best solutions below

5
Hemanth Kumar On

Seems to be there is no intended way to generate openapi files. As per this Git Link comment by DirectXMan12 :

KubeBuilder is not intended to generate go openapi files, since they're not useful for CRDs (they're basically only useful for aggregated API servers, and even then you can just bundle in the JSON/YAML form and parse on init).

You can also refer to this Git linkcomment and doc for further information .

Edit : Kubebuilder is generating only CRD and by using controller gen can generate an open API but kubebuilder is still not intended to generate open api. You can use other third party tools like mentioned in this document by Vyom Srivastava.

0
Nikita On

If you want to generate the code for OpenAPI spec (model) you probably should take a look at https://github.com/kubernetes/kube-openapi/ package. There is a cmd to generate Go-source files to handle OpenAPI schema / validation.