-
Notifications
You must be signed in to change notification settings - Fork 140
generate policies using go generate #3160
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
Closed
Closed
Changes from all commits
Commits
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
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,70 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
| "os" | ||
| "text/template" | ||
| ) | ||
|
|
||
| //go:generate go run gen_policies.go | ||
|
|
||
| // Define policy types | ||
| var policies = []struct { | ||
| Name string | ||
| HasSingleRef bool // true if it uses a single TargetRef, false if it uses TargetRefs | ||
| }{ | ||
| {"ClientSettingsPolicy", true}, | ||
| {"ObservabilityPolicy", false}, | ||
| {"UpstreamSettingsPolicy", false}, | ||
| } | ||
|
|
||
| // Template for generating policy methods | ||
| const tmpl = `// Code generated by gen_policies.go; DO NOT EDIT. | ||
|
|
||
| package v1alpha1 | ||
|
|
||
| import "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
|
|
||
| {{- range . }} | ||
|
|
||
| func (p *{{ .Name }}) GetTargetRefs() []v1alpha2.LocalPolicyTargetReference { | ||
| {{- if .HasSingleRef }} | ||
| return []v1alpha2.LocalPolicyTargetReference{p.Spec.TargetRef} | ||
| {{- else }} | ||
| return p.Spec.TargetRefs | ||
| {{- end }} | ||
| } | ||
|
|
||
| func (p *{{ .Name }}) GetPolicyStatus() v1alpha2.PolicyStatus { | ||
| return p.Status | ||
| } | ||
|
|
||
| func (p *{{ .Name }}) SetPolicyStatus(status v1alpha2.PolicyStatus) { | ||
| p.Status = status | ||
| } | ||
|
|
||
| {{- end }} | ||
| ` | ||
|
|
||
| func main() { | ||
| // Ensure the output directory exists | ||
| outputDir := "../apis/v1alpha1" | ||
| if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { | ||
| log.Fatalf("failed to create directory: %v", err) | ||
| } | ||
|
|
||
| // Open the output file | ||
| file, err := os.Create("../policy_methods.go") | ||
| if err != nil { | ||
| log.Fatalf("failed to create file: %v", err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| // Create a template and execute it with the policy list | ||
| t := template.Must(template.New("policy").Parse(tmpl)) | ||
| if err := t.Execute(file, policies); err != nil { | ||
| log.Fatalf("failed to execute template: %v", err) | ||
| } | ||
|
|
||
| log.Printf("Writing PolicyMethods to %s... Done\n", "policy_methods.go") | ||
| } | ||
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,59 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
| "os" | ||
| "text/template" | ||
| ) | ||
|
|
||
| //go:generate go run gen_policies.go | ||
|
|
||
| // Define policy types | ||
| var policies = []string{ | ||
| "ObservabilityPolicy", | ||
| } | ||
|
|
||
| // Template for generating policy methods | ||
| const tmpl = `// Code generated by gen_policies.go; DO NOT EDIT. | ||
|
|
||
| package v1alpha2 | ||
|
|
||
| import "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
|
|
||
| {{ range . }} | ||
| func (p *{{ . }}) GetTargetRefs() []v1alpha2.LocalPolicyTargetReference { | ||
| return p.Spec.TargetRefs | ||
| } | ||
|
|
||
| func (p *{{ . }}) GetPolicyStatus() v1alpha2.PolicyStatus { | ||
| return p.Status | ||
| } | ||
|
|
||
| func (p *{{ . }}) SetPolicyStatus(status v1alpha2.PolicyStatus) { | ||
| p.Status = status | ||
| } | ||
| {{ end }} | ||
| ` | ||
|
|
||
| func main() { | ||
| // Ensure the output directory exists | ||
| outputDir := "../apis/v1alpha2" | ||
| if err := os.MkdirAll(outputDir, os.ModePerm); err != nil { | ||
| log.Fatalf("failed to create directory: %v", err) | ||
| } | ||
|
|
||
| // Open the output file | ||
| file, err := os.Create("../policy_methods.go") | ||
| if err != nil { | ||
| log.Fatalf("failed to create file: %v", err) | ||
| } | ||
| defer file.Close() | ||
|
|
||
| // Create a template and execute it with the policy list | ||
| t := template.Must(template.New("policy").Parse(tmpl)) | ||
| if err := t.Execute(file, policies); err != nil { | ||
| log.Fatalf("failed to execute template: %v", err) | ||
| } | ||
|
|
||
| log.Printf("Writing PolicyMethods to %s... Done\n", "policy_methods.go") | ||
| } |
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.
Let's deduplicate the common template and main function that we have in both v1alpha1 and v1alpha2 into a common file, maybe in
apis/top-level. Then we can just include the specific policy lists in each api version directory.The package version at the top of the template could also be provided as a templated value.