-
Notifications
You must be signed in to change notification settings - Fork 315
Tool for generating k8s types wrapping Istio types #37
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
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: If they are not already assigned, you can assign the PR to them by writing The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
6a639a5
to
8bb7ad5
Compare
Sorry for the dumb questions, but I'm a bit confused by what this PR is for exactly. What is the exact input of the tool? A proto package or a Go package? And what's the output, and what is that used for? |
Hey @geeknoid, I provided a README with the PR. The tool is used to generate Kubernetes types representing the Custom Resource Definitions used by Istio. This tool simply generates a wrapper around this Istio type, e.g. type Policy struct {
v1.TypeMeta `json:",inline"`
// +optional
v1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
// Spec defines the implementation of this definition.
// +optional
Spec v1alpha1.Policy `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`
} Where The generated code can then be run through other Kubernetes code generators to produce Kubernetes clients, informers, listers, etc., making it easier for folks to work with the Istio resources within Kubernetes. Here are examples of some of the generated files: authentication/v1alpha1/types.go HTH |
Thanks, I saw the README, but it just mentions an "input package", which given the context could mean a Go package or a proto package. The work I was expecting in this space was a protoc plugin that takes as input an Istio .proto file and produces as output an OpenAPI definition representing the proto. From this OpenAPI definition, we could then generate all the downstream k8s artifacts. How would you contrast your approach vs. what I describe? Is your approach preferable overall? |
Sorry... it works off a go package and the go files emitted by protoc. Any types which need to have k8s counterparts are annotated (comment tags) appropriately. Those comments should also include tags to drive the other generators (e.g. client-gen, openapi-gen, etc.) I can't say what the benefits are of this approach over the protoc plugin approach. I was only focused on getting k8s style apis generated and was not looking at OpenAPI definitions for the raw types. That said, there is a k8s code generator for openapi as well ( The k8s code generation framework was easier for me to wrap my head around, as I've never used protoc. Both approaches use the .proto files as the source (comment tags are copied over to the generated go files, then used by the other code generators). One difference was using the package comments in HTH |
Here are the diffs for containing the annotations used to drive all the code generation for k8s, istio/api@c2154e2. Focus on the changes to the .proto files and the new |
Couple more questions:
It would be good to turn on OpenAPI generation on istio/api. We need those files for other things downstream. Now that I understand better, I'll try and get to the full review later today. Thanks. |
There are a few things to consider:
- We've ran into a few serialization related issues between protos and
K8s API machinery, particularly around oneof serialization. We need to make
sure that these work, tests here would help. This might be a concern for
OpenAPI spec generation as well.
- The annotations themselves will carry over to documentation, I
believe. They need to be hid.
It looks like this approach would work for versioning: When we have
multiple API Groups, then we will need to create multiple proto packages.
As long as the root package names of the generated/top-level code can
accommodate that, it should work.
The one main downside is, we will loose the opportunity to integrate with
protoc-gen-validate, to produce more detailed OpenAPI Spec.
…On Thu, Jan 24, 2019 at 7:43 AM Rob Cernich ***@***.***> wrote:
Here are the diffs for containing the annotations used to drive all the
code generation for k8s, ***@***.***
<istio/api@c2154e2>.
Focus on the changes to the .proto files and the new doc.go files. The
.pb.go files are just duplicates coming out of protoc.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#37 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AQR8I_g2NSVXANt6SKGKQsWS3pxhqkSWks5vGdSxgaJpZM4aLUUJ>
.
|
I can modify the tool such that generator tags are above the type/message doc section. The k8s code generation tools scan comments above the type comments. That said, I'll need to verify that those comments are copied over by protoc. Another option may be to modify the doc plugin to filter comment lines with go-style tags. There should not be any issues with serialization, but there was some manual work required to get https://github.com/rcernich/istio-api/blob/istio-8772/mixer/v1/config/client/zz_custom.deepcopy.go I did attempt to constrain this to individual fields, but that seemed a bit more difficult. That said, I can revisit that approach to constrain marshal/unmarshal to specific fields. (I bailed as it was simpler to just run the whole type through. This is certainly something that can be optimized.) I also had to rework the proto imports to make them compatible with the k8s go-to-protobuf generator. This required adding It sounds like I need to update the readme in the api project to account for all these details. Summarizing: To generate k8s api types from istio types:
It seems like this may be a lot of manual effort, but other than the |
One additional note, there may be some issues with types that are mapped to multiple k8s types, e.g. namespaced vs clustered resources. These would require two sets of k8s tags to drive downstream code generation (e.g. |
Thanks for all the clarifying answers. I've got one more. I still don't understand why some of the special comments are in doc.go and some are in the .proto file. Wouldn't it be possible to put everything in the .proto file? The protoc compiler carries all the comments forward into the generated go files. I prefer this approach as it makes the .proto the source of truth for our API definitions, as opposed to being only a partial source of truth. As for hiding the comments from the generated docs, please see if you can make that work with the tools as-is. Otherwise, I can modify the doc gen tool to support skipping blocks within comments. Finally, it'd be great to have some tests checked in alongside this tool. Something that exercises the core features, including serialization. Thanks. |
Testing...sure, I'll look at adding some tests. |
I don't think we need (or maybe even want) the flexibility to split the API like you describe. I'd rather force people to split up the package in the first place. If you move the content of doc.go as an "unattached" comment in a proto file, then I think the protoc compiler will just carry everything as-is into the go file, also unattached to anything. So you end up with semantically the same thing you have now, except that the source of truth is the proto. |
I can do that. It will mean the |
Updated with tests. I removed the package flag and instead use the base of the group (i.e. either group or group of group.some.name). I added support for adding tags for specific generated types (e.g. I still have to update the PR for the api project using the new generator. This will move the generator comments to the comment section above the type comments and will include the proper tags for generating the clientsets for cluster resources. We'll see how that goes. |
I'm going through the code now. |
Sorry about that, I clicked the wrong button |
Currently, protoc isn't copying over the detached comments for the types, which means the generator tags need to be in the message comments. I'll keep digging to see if there's a workaround. (I just got builds working in api, so I haven't done much investigating up to this point.) |
It looks like protobuf has changed pretty significantly in the last few months and there are many diffs in the generated .pb.go files. The protoc image is using master for golang/protobuf and gogo/protobuf and I'm wondering if these should be using a tag or ref when building the image. Opinions? |
I believe we're using a tagged version in the master branch. We should do
the same for istio/api. Following 1.1 release, we can update the tag #.
I think this is it:
https://github.com/istio/api/blob/825044c7e15f6723d558b7b878855670663c2e1e/tools/protoc/Dockerfile#L8
…On Tue, Jan 29, 2019 at 7:33 AM Rob Cernich ***@***.***> wrote:
It looks like protobuf has changed pretty significantly in the last few
months and there are many diffs in the generated .pb.go files. The protoc
image is using master for golang/protobuf and gogo/protobuf and I'm
wondering if these should be using a tag or ref when building the image.
Opinions?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#37 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AQR8I57eGyYL004zjGENa52d_SB1-LXYks5vIGnNgaJpZM4aLUUJ>
.
|
It's actually this that's the problem. I was able to narrow down to specific commit ids that seem to correlate with the last build of the image. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code looks good in general. The only issue is the copyright injection in the output.
Thanks.
cmd/kubetype-gen/main.go
Outdated
arguments := args.Default() | ||
|
||
arguments.GeneratedByCommentTemplate = "// Code generated by kubetype-gen. DO NOT EDIT." | ||
arguments.GoHeaderFilePath = filepath.Join(args.DefaultSourceTree(), "istio.io/tools/cmd/kubetype-gen/boilerplate.go.txt") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe it's correct to put the Istio copyright on the output of the tool. This would be like putting a copyright from a compiler vendor into produced binaries. The copyright to the material is actually with the author of the original content. I suggest just going with something like:
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: authentication/v1alpha1/policy.proto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do that. I was just following the convention of the other code generators. I just about have the api PR polished up, which should provide a good example of the output of the tool.
Thanks for taking the time to review!
Signed-off-by: rcernich <rcernich@redhat.com>
Signed-off-by: rcernich <rcernich@redhat.com>
This should be good to go now. Please refer to the individual commits in istio/api#764 to see it in action. Changes to isio/api source: istio/api@1e4498c Generated k8s code: istio/api@152cda4 I still looking at cleaning up the Makefile in istio/api. That said, this change needs to be committed first, so I can get the proper info in the Gopkg.lock file. |
Thanks. Looks good. |
Signed-off-by: rcernich rcernich@redhat.com