-
Notifications
You must be signed in to change notification settings - Fork 578
Add Protobuf generation to the codegen utility #1385
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
Open
JoelSpeed
wants to merge
8
commits into
openshift:master
Choose a base branch
from
JoelSpeed:codegen-go-to-protobuf
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d9c4b6f
Bootstrap initial go-to-protobuf generator in codegen
JoelSpeed bbc48c1
Add gogoproto to vendor
JoelSpeed 84295f0
Implement protobuf generator within codegen
JoelSpeed 47d1a91
Add verification of generated protobuf files
JoelSpeed f38c6db
Add the ability to skip protobuf generation for certain versions
JoelSpeed a716ea1
Enable codegen protobuf on legacy APIs
JoelSpeed 53cd745
Update verify protobuf scripts
JoelSpeed fff23c1
Update generated files
JoelSpeed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| swaggerdocs: | ||
| commentPolicy: Warn | ||
| protobuf: | ||
| disabled: false | ||
| disabledVersions: | ||
| - "1.0" | ||
| - pre012 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,4 @@ | ||
| schemapatch: | ||
| protobuf: | ||
| disabled: false | ||
| disabledVersions: | ||
| - v1alpha1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| swaggerdocs: | ||
| commentPolicy: Warn | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| swaggerdocs: | ||
| commentPolicy: Warn | ||
| protobuf: | ||
| disabled: false |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| schemapatch: | ||
| swaggerdocs: | ||
| commentPolicy: Warn | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| swaggerdocs: | ||
| commentPolicy: Warn | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| protobuf: | ||
| disabled: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| //go:build tools | ||
| // +build tools | ||
|
|
||
| package tools | ||
|
|
||
| import ( | ||
| _ "github.com/gogo/protobuf/gogoproto" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/openshift/api/tools/codegen/pkg/generation" | ||
| "github.com/openshift/api/tools/codegen/pkg/protobuf" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| goToProtobufHeaderFilePath string | ||
| ) | ||
|
|
||
| // goToProtobufCmd represents the go-to-protobuf command | ||
| var goToProtobufCmd = &cobra.Command{ | ||
| Use: "go-to-protobuf", | ||
| Short: "go-to-protobuf generates protofbuf definitions for API types", | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| genCtx, err := generation.NewContext(generation.Options{ | ||
| BaseDir: baseDir, | ||
| APIGroupVersions: apiGroupVersions, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("could not build generation context: %w", err) | ||
| } | ||
|
|
||
| gen := newGoToProtobufGenerator() | ||
|
|
||
| return executeGenerators(genCtx, gen) | ||
| }, | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.AddCommand(goToProtobufCmd) | ||
|
|
||
| rootCmd.PersistentFlags().StringVar(&goToProtobufHeaderFilePath, "go-to-protobuf:header-file-path", "", "Path to file containing boilerplate header text. The string YEAR will be replaced with the current 4-digit year. When omitted, no header is added to the generated files.") | ||
| } | ||
|
|
||
| // newGoToProtobufGenerator builds a new go-to-protobuf generator. | ||
| func newGoToProtobufGenerator() generation.Generator { | ||
| return protobuf.NewGenerator(protobuf.Options{ | ||
| HeaderFilePath: goToProtobufHeaderFilePath, | ||
| Verify: verify, | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package protobuf | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/openshift/api/tools/codegen/pkg/generation" | ||
| "k8s.io/apimachinery/pkg/util/sets" | ||
| "k8s.io/klog/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| // DefaultOutputFileBaseName is the default output file base name for the generated protobuf functions. | ||
| DefaultOutputFileBaseName = "generated.pb" | ||
| ) | ||
|
|
||
| // Options contains the configuration required for the protobuf generator. | ||
| type Options struct { | ||
| // Disabled indicates whether the deepcopy generator is enabled or not. | ||
| // This defaults to true as the protobuf generator is disabled by default. | ||
| Disabled *bool | ||
|
|
||
| // DisabledVersions allows you to explicitly disable the generation of protobuf for | ||
| // specific versions of an API. | ||
| // This is a list of version names. | ||
| // When omitted, no versions are disabled. | ||
| DisabledVersions []string | ||
|
|
||
| // HeaderFilePath is the path to the file containing the boilerplate header text. | ||
| // When omitted, no header is added to the generated files. | ||
| HeaderFilePath string | ||
|
|
||
| // Verify determines whether the generator should verify the content instead | ||
| // of updating the generated file. | ||
| Verify bool | ||
| } | ||
|
|
||
| // generator implements the generation.Generator interface. | ||
| // It is designed to generate deepcopy function for a particular API group. | ||
| type generator struct { | ||
| disabled bool | ||
| disabledVersions sets.String | ||
| headerFilePath string | ||
| verify bool | ||
| } | ||
|
|
||
| // NewGenerator builds a new protobuf generator. | ||
| func NewGenerator(opts Options) generation.Generator { | ||
| disabled := true | ||
| if opts.Disabled != nil { | ||
| disabled = *opts.Disabled | ||
| } | ||
|
|
||
| return &generator{ | ||
| disabled: disabled, | ||
| disabledVersions: sets.NewString(opts.DisabledVersions...), | ||
| headerFilePath: opts.HeaderFilePath, | ||
| verify: opts.Verify, | ||
| } | ||
| } | ||
|
|
||
| // ApplyConfig creates returns a new generator based on the configuration passed. | ||
| // If the deepcopy configuration is empty, the existing generation is returned. | ||
| func (g *generator) ApplyConfig(config *generation.Config) generation.Generator { | ||
| if config == nil || config.Protobuf == nil { | ||
| return g | ||
| } | ||
|
|
||
| return NewGenerator(Options{ | ||
| Disabled: config.Protobuf.Disabled, | ||
| DisabledVersions: config.Protobuf.DisabledVersions, | ||
| HeaderFilePath: config.Protobuf.HeaderFilePath, | ||
| Verify: g.verify, | ||
| }) | ||
| } | ||
|
|
||
| // Name returns the name of the generator. | ||
| func (g *generator) Name() string { | ||
| return "go-to-protobuf" | ||
| } | ||
|
|
||
| // GenGroup runs the go-to-protobuf generator against the given group context. | ||
| func (g *generator) GenGroup(groupCtx generation.APIGroupContext) ([]generation.Result, error) { | ||
| if g.disabled { | ||
| return nil, nil | ||
| } | ||
|
|
||
| // If there is no header file, create an empty file and pass that through. | ||
| headerFilePath := g.headerFilePath | ||
| if headerFilePath == "" { | ||
| tmpFile, err := os.CreateTemp("", "protobuf-header-*.txt") | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create temporary file: %w", err) | ||
| } | ||
| tmpFile.Close() | ||
|
|
||
| defer os.Remove(tmpFile.Name()) | ||
|
|
||
| headerFilePath = tmpFile.Name() | ||
| } | ||
|
|
||
| for _, version := range groupCtx.Versions { | ||
| if g.disabledVersions.Has(version.Name) { | ||
| klog.V(1).Infof("Skipping generation of protobuf functions for %s/%s", groupCtx.Name, version.Name) | ||
| continue | ||
| } | ||
|
|
||
| action := "Generating" | ||
| if g.verify { | ||
| action = "Verifying" | ||
| } | ||
|
|
||
| klog.V(1).Infof("%s protobuf functions for for %s/%s", action, groupCtx.Name, version.Name) | ||
|
|
||
| if err := generateProtobufFunctions(version.Path, version.PackagePath, headerFilePath, g.verify); err != nil { | ||
| return nil, fmt.Errorf("could not generate protobuf functions for %s/%s: %w", groupCtx.Name, version.Name, err) | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.