-
Notifications
You must be signed in to change notification settings - Fork 4k
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
VPA: Add automated flag generator for all components #7599
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
68132b0
VPA: Add automated flag generator for all components
omerap12 863d49f
Address issues and add merge script
omerap12 4743dfb
fix misspelling
omerap12 b7ac55b
Add BASH_SOURCE
omerap12 1611b35
sub shell to avoid cd -
omerap12 e227e20
Remove the temporary file after merging
omerap12 06558a4
sub shell
omerap12 ce75c91
Add final message
omerap12 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 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,257 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"io" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
"strings" | ||
"text/template" | ||
"time" | ||
) | ||
|
||
type flagInfo struct { | ||
Name string | ||
DefaultValue string | ||
Type string | ||
Description string | ||
SourceFile string | ||
} | ||
|
||
type templateData struct { | ||
Flags []flagInfo | ||
Timestamp string | ||
} | ||
|
||
var componentTemplates = map[string]string{ | ||
"recommender": `# What are the parameters to VPA recommender? | ||
This document is auto-generated from the flag definitions in the VPA recommender code. | ||
Last updated: {{ .Timestamp }} | ||
|
||
| Flag | Type | Default | Description | | ||
|------|------|---------|-------------| | ||
{{- range .Flags }} | ||
| --{{ .Name }} | {{ .Type }} | {{ .DefaultValue }} | {{ .Description }} | | ||
{{- end }} | ||
`, | ||
"updater": `# What are the parameters to VPA updater? | ||
This document is auto-generated from the flag definitions in the VPA updater code. | ||
Last updated: {{ .Timestamp }} | ||
|
||
| Flag | Type | Default | Description | | ||
|------|------|---------|-------------| | ||
{{- range .Flags }} | ||
| --{{ .Name }} | {{ .Type }} | {{ .DefaultValue }} | {{ .Description }} | | ||
{{- end }} | ||
`, | ||
"admission": `# What are the parameters to VPA admission controller? | ||
This document is auto-generated from the flag definitions in the VPA admission controller code. | ||
Last updated: {{ .Timestamp }} | ||
|
||
| Flag | Type | Default | Description | | ||
|------|------|---------|-------------| | ||
{{- range .Flags }} | ||
| --{{ .Name }} | {{ .Type }} | {{ .DefaultValue }} | {{ .Description }} | | ||
{{- end }} | ||
`, | ||
} | ||
|
||
func main() { | ||
if len(os.Args) != 3 { | ||
fmt.Println("Generates markdown documentation for VPA recommender flags.") | ||
fmt.Println("usage: vpa-recommender-generate-flags <source-dir> <output-file>") | ||
fmt.Println(" <source-dir> - Path to the directory containing the Go source files") | ||
fmt.Println(" <output-file> - Path to the output markdown file") | ||
os.Exit(1) | ||
} | ||
|
||
sourceDir := os.Args[1] | ||
outputFile := os.Args[2] | ||
|
||
flags, err := collectFlags(sourceDir) | ||
if err != nil { | ||
log.Fatalf("Error collecting flags: %v", err) | ||
} | ||
|
||
sort.Slice(flags, func(i, j int) bool { | ||
return flags[i].Name < flags[j].Name | ||
}) | ||
|
||
err = generateDocs(flags, outputFile) | ||
if err != nil { | ||
log.Fatalf("Error generating documentation: %v", err) | ||
} | ||
|
||
fmt.Println("Successfully generated documentation in", outputFile) | ||
} | ||
|
||
func extractFlagFromCall(call *ast.CallExpr, sourcePath string) *flagInfo { | ||
if len(call.Args) < 3 { | ||
return nil | ||
} | ||
|
||
// Extract flag name | ||
nameArg, ok := call.Args[0].(*ast.BasicLit) | ||
if !ok { | ||
return nil | ||
} | ||
name := strings.Trim(nameArg.Value, "\"") | ||
|
||
// Extract default value | ||
var defaultValue string | ||
switch v := call.Args[1].(type) { | ||
case *ast.BasicLit: | ||
defaultValue = strings.Trim(v.Value, "\"") | ||
case *ast.Ident: | ||
defaultValue = v.Name | ||
default: | ||
defaultValue = fmt.Sprintf("%v", v) | ||
} | ||
|
||
// Extract description | ||
descArg, ok := call.Args[2].(*ast.BasicLit) | ||
if !ok { | ||
return nil | ||
} | ||
description := strings.Trim(descArg.Value, "\"") | ||
|
||
return &flagInfo{ | ||
Name: name, | ||
DefaultValue: defaultValue, | ||
Type: call.Fun.(*ast.SelectorExpr).Sel.Name, | ||
Description: description, | ||
SourceFile: sourcePath, | ||
} | ||
} | ||
|
||
func collectFlags(sourceDir string) ([]flagInfo, error) { | ||
var flags []flagInfo | ||
|
||
err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Skip non-Go files | ||
if !strings.HasSuffix(path, ".go") || info.IsDir() { | ||
return nil | ||
} | ||
|
||
fset := token.NewFileSet() | ||
file, err := parser.ParseFile(fset, path, nil, parser.ParseComments) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error parsing file %s: %v", path, err) | ||
} | ||
|
||
fileFlags := extractFlags(file, getRelativePath(sourceDir, path)) | ||
flags = append(flags, fileFlags...) | ||
|
||
return nil | ||
}) | ||
|
||
return flags, err | ||
} | ||
|
||
func extractFlags(file *ast.File, sourcePath string) []flagInfo { | ||
var flags []flagInfo | ||
|
||
ast.Inspect(file, func(n ast.Node) bool { | ||
// Look for flag.String(), flag.Bool(), flag.Float64(), etc. | ||
if call, ok := n.(*ast.CallExpr); ok { | ||
if sel, ok := call.Fun.(*ast.SelectorExpr); ok { | ||
if ident, ok := sel.X.(*ast.Ident); ok { | ||
if ident.Name == "flag" { | ||
if flag := extractFlagFromCall(call, sourcePath); flag != nil { | ||
flags = append(flags, *flag) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
return flags | ||
} | ||
|
||
func generateDocs(flags []flagInfo, outputFile string) error { | ||
component := determineComponent(outputFile) | ||
markdownTemplate, ok := componentTemplates[component] | ||
if !ok { | ||
return fmt.Errorf("couldn't find template for %s ", component) | ||
} | ||
tmpl, err := template.New("flags").Parse(markdownTemplate) | ||
if err != nil { | ||
return fmt.Errorf("error parsing template: %v", err) | ||
} | ||
|
||
data := templateData{ | ||
Flags: flags, | ||
Timestamp: time.Now().UTC().Format("2006-01-02 15:04:05 UTC"), | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := tmpl.Execute(&buf, data); err != nil { | ||
return fmt.Errorf("error executing template: %v", err) | ||
} | ||
|
||
f, err := os.Create(outputFile) | ||
if err != nil { | ||
return fmt.Errorf("error creating output file: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
_, err = io.Copy(f, &buf) | ||
return err | ||
} | ||
|
||
func getRelativePath(baseDir, fullPath string) string { | ||
rel, err := filepath.Rel(baseDir, fullPath) | ||
if err != nil { | ||
return fullPath | ||
} | ||
return rel | ||
} | ||
|
||
// We determine the component by the path of the output file. | ||
// if the path contains "recommender", we generate recommender flags. | ||
// if the path contains "updater", we generate updater flags. | ||
// if the path contains "admission", we generate admission flags. | ||
// Otherwise, we generate recommender flags. | ||
|
||
func determineComponent(outputPath string) string { | ||
if strings.Contains(outputPath, "updater") { | ||
return "updater" | ||
} | ||
if strings.Contains(outputPath, "admission") { | ||
return "admission" | ||
} | ||
if strings.Contains(outputPath, "recommender") { | ||
return "recommender" | ||
} | ||
fmt.Println("Couldn't find component. default is recommender") | ||
return "recommender" | ||
} |
This file contains 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 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 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
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.
Would it make sense to make the name of the Makefile job the same across all components?
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.
yeah it does! lol I feel dumb