Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeking advice for generating openapi spec for CRD #13

Closed
tamalsaha opened this issue Oct 18, 2017 · 9 comments
Closed

Seeking advice for generating openapi spec for CRD #13

tamalsaha opened this issue Oct 18, 2017 · 9 comments
Assignees

Comments

@tamalsaha
Copy link
Member

@mbohlool , is there an example on how to use this generator to generate/serve open api spec for CRDs from GO types?

From kubernetes-client/java#96, I am looking for ways to generate non-Go client for CRDs.

Any help is much appreciated!

@mbohlool
Copy link
Contributor

There are two parts in OpenAPI generation and handling:

1- Generating a model from a go type. This should be possible with this library. Look at my answer to this stackoverflow thread.

2- Creating the OpenAPI spec from the model generated in 1. You need to also add routing and call the builder package. For CRD those operation are standard k8s CRUD operations. I would like to see we add support for CRD OpenAPI spec to kubernetes then you can get the spec from kubernetes. But you can also provide the CRUD operation by hand or copy one of the other API object spec and plug in your model in it.

3- When you got your OpenAPI spec then you should be able to generate a client using https://github.com/kubernetes-client/gen. After generation you may need to copy the useful utilities from the language repo. For example for python this is the repo.

@tamalsaha
Copy link
Member Author

Thanks a lot @mbohlool ! I will give it a try and see how it goes.

@tamalsaha
Copy link
Member Author

tamalsaha commented Oct 22, 2017

@mbohlool , I was able to generate openapi spec from appscode/voyager project.

generated spec: https://github.com/appscode/voyager/blob/master/apis/voyager/v1beta1/openapi_generated.go

Generator command: https://github.com/appscode/voyager/blob/0370feff31c1012c3e94a52bb4218136ca6e4a42/hack/codegen.sh#L51

I am not sure what is the next step? How do I serve this spec so that swagger generators can access them?

@mbohlool
Copy link
Contributor

mbohlool commented Oct 24, 2017

This generates only models. You need to provide a route list to pkg/builder of kube-openapi repo to get an spec.Swagger that is a final spec. You can serve that directly yourself or you can use kube-openapi/pkg/handler to do that (handler have a connivance method that accept parameters for both builder and handler, then build and handle the spec. main kubernetes repo call that method, so you can check that for an example of how to serve OpenAPI spec).

Basically you only did step 1 in my previous comment and you need to proceed to step 2 and 3.

@sttts
Copy link
Contributor

sttts commented Nov 10, 2017

/xref kubernetes/kubernetes#55472

@mbohlool mbohlool self-assigned this Dec 12, 2017
@erictune
Copy link
Member

xref kubernetes/code-generator#28

@jessesuen
Copy link
Contributor

In case others can benefit from this, I wasn't able to get this to work using only kube-openapi. Instead I constructed the go-openapi spec.Swagger directly and fed it the models which were generated by kube-openapi. The solution can be seen here:
https://github.com/argoproj/argo/blob/master/hack/gen-openapi-spec/main.go

which generates this:
https://github.com/argoproj/argo/blob/master/api/openapi-spec/swagger.json

@tamalsaha
Copy link
Member Author

@jessesuen your swagger.json is missing the api call information. To generate client that information will be needed.

@tamalsaha
Copy link
Member Author

I have figured out how to get this to work. Here is my working demo: https://github.com/tamalsaha/kube-openapi-generator

zoidyzoidzoid added a commit to zoidyzoidzoid/shipper that referenced this issue Dec 4, 2019
Some of the modern Kubernetes clients use clients generated from a
swagger.json

kubernetes-client/javascript uses kubernetes-client/gen which uses
OpenApiTools/openapi-generator and kubernetes/kubernetes/api/openapi-spec/swagger.json

There are open/closed/rotten/ignored issues all over the place about
making these easier to use with CRDs.

Wrongly closed issue on kubernetes-client/javascript: kubernetes-client/javascript#341
Open issue on kubernetes-client/gen: kubernetes-client/gen#121

It seems like kubernetes/kube-openapi is one of the repos working on
solving the main part of this problem, generating the Go code to make
it easy to generate a swagger.json

I found the following Argo code via the following issue:

kubernetes/kube-openapi#13

https://github.com/argoproj/argo/blob/master/hack/gen-openapi-spec/main.go
https://github.com/argoproj/argo/blob/master/pkg/apis/workflow/v1alpha1/openapi_generated.go#L14:6
https://github.com/kubernetes/kube-openapi/blob/master/cmd/openapi-gen/openapi-gen.go

So I'm working on adding similar stuff to Shipper.

gen-openapi-spec uses the generated openapi_generated.go module that is
generated via the openapi-gen command.

The following commit adds the code to generate openapi_generated.go

Signed-off-by: William Stewart <zoidbergwill@gmail.com>
sttts pushed a commit to sttts/kube-openapi that referenced this issue Nov 5, 2020
…tes#13

Fixes kubernetes#13

Added golangci and go report card badges

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
sttts pushed a commit to sttts/kube-openapi that referenced this issue Nov 5, 2020
Completed test for ServeError with nil Error, as discussed in kubernetes#13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants