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

Ignore specific markers in openapi generation from protos #576

Merged
merged 10 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/v0.40.5/allow_ignored_kube_markers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
changelog:
- type: NEW_FEATURE
issueLink: https://github.com/solo-io/gloo-mesh-enterprise/issues/18119
resolvesIssue: false
description: |
Allows the user to define one or more kube markers to ignore. This is useful when using protos that contain
unsupported kubebuilder decorators.
19 changes: 11 additions & 8 deletions codegen/collector/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ type OpenApiProtocExecutor struct {
// Whether to exclude kubebuilder markers and validations (such as PreserveUnknownFields, MinItems, default, and all CEL rules)
// Type and Required markers will be included regardless
DisableKubeMarkers bool

// when set, this list of substrings will be used to identify kubebuilder markers to ignore. When multiple are
// supplied, this will function as a logical OR i.e. any rule which contains a provided substring will be ignored
IgnoredKubeMarkerSubstrings []string
}

func (o *OpenApiProtocExecutor) Execute(protoFile string, toFile string, imports []string) error {
Expand All @@ -104,17 +108,16 @@ func (o *OpenApiProtocExecutor) Execute(protoFile string, toFile string, imports
_, fileName := filepath.Split(protoFile)
directoryName := fileName[0 : len(fileName)-len(filepath.Ext(fileName))]

baseArgument := "--openapi_out=yaml=true,single_file=false,include_description=true,multiline_description=true,proto_oneof=true,int_native=true"
baseArgument = fmt.Sprintf("%s,enum_as_int_or_string=%v", baseArgument, o.EnumAsIntOrString)
baseArgument = fmt.Sprintf("%s,additional_empty_schema=%v", baseArgument, strings.Join(o.MessagesWithEmptySchema, "+"))
baseArgument = fmt.Sprintf("%s,disable_kube_markers=%v", baseArgument, o.DisableKubeMarkers)
baseArgument = fmt.Sprintf("%s,ignored_kube_marker_substrings=%v", baseArgument, strings.Join(o.IgnoredKubeMarkerSubstrings, "+"))
Comment on lines +111 to +115
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to make this more readable, I'd group these options into a slice and use strings.Join().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah honestly I should have done that. I think this is better than what we have, so if we revisit this and add another argument we could improve this again.


// Create the directory
directoryPath := filepath.Join(o.OutputDir, directoryName)
_ = os.Mkdir(directoryPath, os.ModePerm)

cmd.Args = append(cmd.Args,
fmt.Sprintf("--openapi_out=yaml=true,single_file=false,include_description=true,multiline_description=true,enum_as_int_or_string=%v,proto_oneof=true,int_native=true,additional_empty_schema=%v,disable_kube_markers=%v:%s",
o.EnumAsIntOrString,
strings.Join(o.MessagesWithEmptySchema, "+"),
o.DisableKubeMarkers,
directoryPath),
)
cmd.Args = append(cmd.Args, fmt.Sprintf("%s:%s", baseArgument, directoryPath))

cmd.Args = append(cmd.Args,
"-o",
Expand Down
4 changes: 4 additions & 0 deletions codegen/proto/schemagen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type ValidationSchemaOptions struct {
// Type and Required markers will be included regardless
// Default: false
DisableKubeMarkers bool

// when set, this list of substrings will be used to identify kubebuilder markers to ignore. When multiple are
// supplied, this will function as a logical OR i.e. any rule which contains a provided substring will be ignored
IgnoredKubeMarkerSubstrings []string
}

// prevent k8s from validating proto.Any fields (since it's unstructured)
Expand Down
9 changes: 5 additions & 4 deletions codegen/proto/schemagen/protoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ func (p *protocGenerator) GetJSONSchemas(protoFiles []string, imports []string,

// The Executor used to compile protos
protocExecutor := &collector.OpenApiProtocExecutor{
OutputDir: tmpOutputDir,
EnumAsIntOrString: p.validationSchemaOptions.EnumAsIntOrString,
MessagesWithEmptySchema: p.validationSchemaOptions.MessagesWithEmptySchema,
DisableKubeMarkers: p.validationSchemaOptions.DisableKubeMarkers,
OutputDir: tmpOutputDir,
EnumAsIntOrString: p.validationSchemaOptions.EnumAsIntOrString,
MessagesWithEmptySchema: p.validationSchemaOptions.MessagesWithEmptySchema,
DisableKubeMarkers: p.validationSchemaOptions.DisableKubeMarkers,
IgnoredKubeMarkerSubstrings: p.validationSchemaOptions.IgnoredKubeMarkerSubstrings,
}

// 1. Generate the openApiSchemas for the project, writing them to a temp directory (schemaOutputDir)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ require (
github.com/solo-io/go-utils v0.21.4
github.com/solo-io/k8s-utils v0.0.1
github.com/solo-io/protoc-gen-ext v0.0.20
github.com/solo-io/protoc-gen-openapi v0.2.4
github.com/solo-io/protoc-gen-openapi v0.2.5
github.com/spf13/pflag v1.0.5
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20220921164117-439092de6870
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,8 @@ github.com/solo-io/k8s-utils v0.0.1 h1:e2alFsqTT7GU10d6cFDX2y+86J142DrsRwy5itvvZ
github.com/solo-io/k8s-utils v0.0.1/go.mod h1:53N9+9Gl2MwqIZJ7/ocA9gKvWt+6z7MPD2qKQix7oFE=
github.com/solo-io/protoc-gen-ext v0.0.20 h1:0cE+DvIp7G97/xlETL3didPQ1s5SHav5mkebljXk/Ws=
github.com/solo-io/protoc-gen-ext v0.0.20/go.mod h1:iGyCvmKmhJNXs5MgBcYFBF0om7LDnCVD2WwhOZGnqeA=
github.com/solo-io/protoc-gen-openapi v0.2.4 h1:9tqGhCAq83IRSzHhKDzpWnPlbPPORTM2izVxjLk0Ftw=
github.com/solo-io/protoc-gen-openapi v0.2.4/go.mod h1:osEjRl1miHqlq4Wl/8SEqHFoyydptPL1EzEdM9c4vfE=
github.com/solo-io/protoc-gen-openapi v0.2.5 h1:l8YKsVks6JDFRzA9liYZIqauqpYRxHXnmHi4TjTIRf4=
github.com/solo-io/protoc-gen-openapi v0.2.5/go.mod h1:osEjRl1miHqlq4Wl/8SEqHFoyydptPL1EzEdM9c4vfE=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
Expand Down
Loading