-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Generate apis types from existed crd definition yaml #3082
Comments
Hi @ArenaSu, It should be possible. Mainly with the tool, you will need to call create API and generate only the controller. However, we need to do some manual changes, for now, see that:
Therefore, would be great to do (IMHO): Create a doc in the reference (https://book.kubebuilder.io/reference/reference.html) such as Working with external/third-party apis
Also, help to address the issue #1999 is welcome Would you like to contribute with? |
/label help-wanted |
@jmrodri: The label(s) In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/cc @jmrodri |
Adding few pointers here, in case it helps:
This dependency cycle in a project is highly not suggested since go cannot differentiate between the two. In order to use 2 versions of the same package in a project, you would have to use 2 different Meanwhile, coming to the best practices in Kubernetes world - it would be helpful to understand your use case. Having multiple GVKs in a same project can definitely be possible, though only one version is eventually stored in etcd server on cluster. In your case, if |
Oh thank you @varshaprasad96, @ArenaSu maybe nit trying to create controllers for Kinds that are created by other projects. I'd suggest you do not do that. And instead to do the steps described on my previous comment: #3082 (comment) |
The Scenario is below:
@camilamacedo86 @varshaprasad96 It's a very common scenario i think. What i want to know is how to generate apis types from crd definition yamls and i think it can solve the problem indeed (as CRD Java Model Generate action do). And i'm sorry that i do not find the method from your comments. Do i miss the point ? |
@ArenaSu I'm not personally aware of any tool that generates go code from CRDs. The usual option is generally unmarshalling the yaml and generating the struct out of it (https://pkg.go.dev/sigs.k8s.io/[email protected]#Unmarshal) to handle the objects further. |
@ArenaSu since you are using CRDs from a Go package that already exists with type definitions. While you may not be able to import both I am still curious if there is a better way to handle a scenario like this rather than having to re-implement the type definitions. I would assume that there aren't incompatible changes between the CRDs since they are both the same major version, but there may be new fields added to them from If your operator can already tell which cluster requires which version of the CRD to use you could have certain logic to populate the fields that only exist in the corresponding version of the CRD. This way you would only need to import and use the definitions from keda |
@everettraven @camilamacedo86 I get it, thanks a lot. I do use runtime.RawExtension and unstructured.Unstructured to make multi-version crds compatible and handle them for different clusters respectively over not only keda but also many other crds. And i will copy multi-version crd definition code to my project to solve the problem. Um it's a "strenuous" work. |
@everettraven @camilamacedo86 Hi, i am trying to develop a tool to generate api types code from CRD yaml.But i have no experience before.And i am reading the controller-tools source code to find the way how to create a standard generator. I would be very grateful if you give me some ideas or suggestions to generate code from apiextensions CustomResourceDefinition as you are experts in this area. Maybe some useful libs or the specialized way to do it? |
@ArenaSu I've had the experience of developing custom generators using controller-tool as a library and hence got the urge to answer the question on that :) So adding some inputs here, in case it helps! Note: The major utilities which c-t provides us with is the ability to define markers in the format we want, parse them with any custom logic and scaffold out relevant bits of code (the output maybe a yaml, a go file or anything). Step 1:
Step 2: Step 3: Also not to complicate more, but just suggesting on one of the methods on how to parse CRD/yaml effectively (and dynamically - when we don't know the field names) - is to convert the yaml into AST (https://pkg.go.dev/go/ast), decode the same and create api types. This is more involved process. |
Thank you very much.It do help a lot.I will dig into the method what you list and try to develop the tool. |
What do you want to happen?
How to generate apis types from existed crd definition yaml ? For example i have a crd definition yaml, and i want to generate golang apis types, is there a way to do it ?
The Scenario is below:
My project (generated by kubebuilder) import openfunction v0.7.0 which import keda(github.com/kedacore/keda/v2) v2.4.0 .Meanwhile my project import another inner project which import keda(github.com/kedacore/keda/v2) v2.8.1. Then it conflicts and error occurred when exec go mod tidy. I want to generate keda v2.4.0 api types from v2.4.0 crds and generate keda v2.8.1 api types from v2.8.1 crds. Then move the api types to different directories and import them inner.
And i know kubernetes-client Java have a action named "CRD Java Model Generate", it can generate apis types from crd definition yaml.
Extra Labels
No response
The text was updated successfully, but these errors were encountered: